You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2015/07/23 23:57:09 UTC

svn commit: r1692452 - in /sling/trunk/testing/mocks/osgi-mock/src: main/java/org/apache/sling/testing/mock/osgi/ test/java/org/apache/sling/testing/mock/osgi/

Author: sseifert
Date: Thu Jul 23 21:57:09 2015
New Revision: 1692452

URL: http://svn.apache.org/r1692452
Log:
SLING-4901 osgi-mock: Add support for ComponentContext.getUsingBundle()

Added:
    sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ComponentContextBuilder.java   (with props)
Modified:
    sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockComponentContext.java
    sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java
    sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java
    sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockComponentContextTest.java

Added: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ComponentContextBuilder.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ComponentContextBuilder.java?rev=1692452&view=auto
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ComponentContextBuilder.java (added)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ComponentContextBuilder.java Thu Jul 23 21:57:09 2015
@@ -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);
+    }
+    
+}

Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ComponentContextBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ComponentContextBuilder.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Thu Jul 23 21:57:09 2015
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/ComponentContextBuilder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockComponentContext.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockComponentContext.java?rev=1692452&r1=1692451&r2=1692452&view=diff
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockComponentContext.java (original)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockComponentContext.java Thu Jul 23 21:57:09 2015
@@ -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 Co
 
     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 Co
         // 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();
     }
 

Modified: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java?rev=1692452&r1=1692451&r2=1692452&view=diff
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java (original)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java Thu Jul 23 21:57:09 2015
@@ -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
      */

Modified: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java?rev=1692452&r1=1692451&r2=1692452&view=diff
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java (original)
+++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/package-info.java Thu Jul 23 21:57:09 2015
@@ -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;

Modified: sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockComponentContextTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockComponentContextTest.java?rev=1692452&r1=1692451&r2=1692452&view=diff
==============================================================================
--- sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockComponentContextTest.java (original)
+++ sling/trunk/testing/mocks/osgi-mock/src/test/java/org/apache/sling/testing/mock/osgi/MockComponentContextTest.java Thu Jul 23 21:57:09 2015
@@ -20,13 +20,16 @@ package org.apache.sling.testing.mock.os
 
 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());
+    }
+
 }