You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:19:39 UTC

[sling-org-apache-sling-testing-osgi-mock] 10/12: SLING-4901 osgi-mock: Add support for ComponentContext.getUsingBundle()

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.testing.osgi-mock-1.5.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit 45d1dbcd4771b887ac1ba78d22337a0726f93935
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Thu Jul 23 21:57:09 2015 +0000

    SLING-4901 osgi-mock: Add support for ComponentContext.getUsingBundle()
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1692452 13f79535-47bb-0310-9956-ffa450edef68
---
 .../testing/mock/osgi/ComponentContextBuilder.java | 72 ++++++++++++++++++++++
 .../testing/mock/osgi/MockComponentContext.java    | 22 +++----
 .../apache/sling/testing/mock/osgi/MockOsgi.java   | 17 +++--
 .../sling/testing/mock/osgi/package-info.java      |  2 +-
 .../mock/osgi/MockComponentContextTest.java        | 14 +++++
 5 files changed, 109 insertions(+), 18 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/ComponentContextBuilder.java b/src/main/java/org/apache/sling/testing/mock/osgi/ComponentContextBuilder.java
new file mode 100644
index 0000000..2e8916a
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/ComponentContextBuilder.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.testing.mock.osgi;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.component.ComponentContext;
+
+/**
+ * Builds a mocked {@link ComponentContext}.
+ */
+public final class ComponentContextBuilder {
+
+    private BundleContext bundleContext;
+    private Dictionary<String, Object> properties;
+    private Bundle usingBundle;
+    
+    ComponentContextBuilder() {
+        // constructor package-scope only
+    }
+    
+    public ComponentContextBuilder bundleContext(BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+        return this;
+    }
+    
+    public ComponentContextBuilder properties(Dictionary<String, Object> properties) {
+        this.properties = properties;
+        return this;
+    }
+    
+    public ComponentContextBuilder properties(Map<String, Object> properties) {
+        this.properties = MapUtil.toDictionary(properties);
+        return this;
+    }
+        
+    public ComponentContextBuilder usingBundle(Bundle usingBundle) {
+        this.usingBundle = usingBundle;
+        return this;
+    }
+    
+    public ComponentContext build() {
+        if (bundleContext == null) {
+            bundleContext = MockOsgi.newBundleContext();
+        }
+        if (properties == null) {
+            properties = new Hashtable<String, Object>();
+        }
+        return new MockComponentContext((MockBundleContext)bundleContext, properties, usingBundle);
+    }
+    
+}
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockComponentContext.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockComponentContext.java
index 40e57a9..78154a5 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockComponentContext.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockComponentContext.java
@@ -19,7 +19,6 @@
 package org.apache.sling.testing.mock.osgi;
 
 import java.util.Dictionary;
-import java.util.Hashtable;
 
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -34,14 +33,13 @@ class MockComponentContext implements ComponentContext {
 
     private final MockBundleContext bundleContext;
     private final Dictionary<String, Object> properties;
+    private final Bundle usingBundle;
 
-    public MockComponentContext(final MockBundleContext mockBundleContext) {
-        this(mockBundleContext, null);
-    }
-
-    public MockComponentContext(final MockBundleContext mockBundleContext, final Dictionary<String, Object> properties) {
+    public MockComponentContext(final MockBundleContext mockBundleContext, 
+            final Dictionary<String, Object> properties, final Bundle usingBundle) {
         this.bundleContext = mockBundleContext;
-        this.properties = properties != null ? properties : new Hashtable<String, Object>();
+        this.properties = properties;
+        this.usingBundle = usingBundle;
     }
 
     @Override
@@ -69,19 +67,19 @@ class MockComponentContext implements ComponentContext {
         // allow calling, but ignore
     }
 
-    // --- unsupported operations ---
     @Override
-    public ComponentInstance getComponentInstance() {
-        throw new UnsupportedOperationException();
+    public Bundle getUsingBundle() {
+        return usingBundle;
     }
 
+    // --- unsupported operations ---
     @Override
-    public ServiceReference getServiceReference() {
+    public ComponentInstance getComponentInstance() {
         throw new UnsupportedOperationException();
     }
 
     @Override
-    public Bundle getUsingBundle() {
+    public ServiceReference getServiceReference() {
         throw new UnsupportedOperationException();
     }
 
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java
index dfba00e..8d74aac 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java
@@ -60,7 +60,7 @@ public final class MockOsgi {
      * @return Mocked {@link ComponentContext} instance
      */
     public static ComponentContext newComponentContext() {
-        return new MockComponentContext((MockBundleContext) newBundleContext());
+        return componentContext().build();
     }
 
     /**
@@ -68,7 +68,7 @@ public final class MockOsgi {
      * @return Mocked {@link ComponentContext} instance
      */
     public static ComponentContext newComponentContext(Dictionary<String, Object> properties) {
-        return newComponentContext(newBundleContext(), properties);
+        return componentContext().properties(properties).build();
     }
 
     /**
@@ -76,7 +76,7 @@ public final class MockOsgi {
      * @return Mocked {@link ComponentContext} instance
      */
     public static ComponentContext newComponentContext(Map<String, Object> properties) {
-        return newComponentContext(toDictionary(properties));
+        return componentContext().properties(properties).build();
     }
 
     /**
@@ -86,7 +86,7 @@ public final class MockOsgi {
      */
     public static ComponentContext newComponentContext(BundleContext bundleContext,
             Dictionary<String, Object> properties) {
-        return new MockComponentContext((MockBundleContext) bundleContext, properties);
+        return componentContext().bundleContext(bundleContext).properties(properties).build();
     }
 
     /**
@@ -95,10 +95,17 @@ public final class MockOsgi {
      * @return Mocked {@link ComponentContext} instance
      */
     public static ComponentContext newComponentContext(BundleContext bundleContext, Map<String, Object> properties) {
-        return newComponentContext(bundleContext, toDictionary(properties));
+        return componentContext().bundleContext(bundleContext).properties(properties).build();
     }
 
     /**
+     * @return {@link ComponentContextBuilder} to build a mocked {@link ComponentContext}
+     */
+    public static ComponentContextBuilder componentContext() {
+        return new ComponentContextBuilder();
+    }
+    
+    /**
      * @param loggerContext Context class for logging
      * @return Mocked {@link LogService} instance
      */
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java b/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java
index f5cd3c6..dbaa66f 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java
@@ -19,5 +19,5 @@
 /**
  * Mock implementation of selected OSGi APIs.
  */
-@aQute.bnd.annotation.Version("2.2")
+@aQute.bnd.annotation.Version("2.3")
 package org.apache.sling.testing.mock.osgi;
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/MockComponentContextTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/MockComponentContextTest.java
index 18f7ba4..b4497eb 100644
--- a/src/test/java/org/apache/sling/testing/mock/osgi/MockComponentContextTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/MockComponentContextTest.java
@@ -20,13 +20,16 @@ package org.apache.sling.testing.mock.osgi;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
+import static org.mockito.Mockito.mock;
 
 import java.util.Dictionary;
 import java.util.Hashtable;
 
 import org.junit.Before;
 import org.junit.Test;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentContext;
 
@@ -81,4 +84,15 @@ public class MockComponentContextTest {
         underTest.disableComponent("myComponent");
     }
 
+    @Test
+    public void testGetUsingBundle() {
+        // test context without using bundle
+        assertNull(underTest.getUsingBundle());
+        
+        // test context with using bundle
+        Bundle usingBundle = mock(Bundle.class);
+        ComponentContext contextWithUsingBundle = MockOsgi.componentContext().usingBundle(usingBundle).build();
+        assertSame(usingBundle, contextWithUsingBundle.getUsingBundle());
+    }
+
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.