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:20:48 UTC

[sling-org-apache-sling-testing-osgi-mock] annotated tag org.apache.sling.testing.osgi-mock-2.0.0 created (now b46b27e)

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

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


      at b46b27e  (tag)
 tagging 0b7c7605c99f2077820d034543a3fd4b04c52a37 (commit)
      by Stefan Seifert
      on Thu Dec 10 21:53:28 2015 +0000

- Log -----------------------------------------------------------------
org.apache.sling.testing.osgi-mock-2.0.0
-----------------------------------------------------------------------

This annotated tag includes the following new commits:

     new e5ba840  SLING-4042 Donate sling-mock, jcr-mock, osgi-mock implementation
     new e1ee5ad  svn:ignore
     new 8d20817  SLING-4042 move all mock projects to mocks/ subdirectory
     new ad838b2  SLING-4042 add missing package-info files
     new 740687d  SLING-4042 make sure dependency injection is continued even if not all injections succeeded, and report root cause of invocation target exceptions
     new 7e5bc2d  SLING-4042 add README files
     new 21a5152  add/update SCM urls
     new 74be83a  [maven-release-plugin] prepare release org.apache.sling.testing.osgi-mock-1.0.0
     new d794a37  [maven-release-plugin] prepare for next development iteration
     new 0a80e0c  SLING-4090 Define package versions for Sling Mock, JCR Mock, OSGi Mock
     new 59f74f6  SLING-4142 MockOsgi: Activate/Deactivate method does not support different signatures
     new a822c76  SLING-4162 Introduce "OsgiContext" junit rule for OSGi and OsgiContextImpl
     new b30dd56  SLING-4162 fix javadoc
     new c40b2dd  SLING-4163 OSGi Mock: Reference bind/unbind method picking order
     new abe8b21  fix some javadoc copy&paste errors and formatting
     new 7c2b519  SLING-4166 OSGi Mock: Support for "modified" SCR lifecycle method
     new 4d131cd  SLING-4166 update package version
     new 907c7df  SLING-4165 OSGi Mock: Fail-fast when calling methods requiring SCR metadata and this is not present
     new 5538cda  SLING-4201 MockOsgi.activate()/deactivate()/modified() should fail fast if method is declared but can't be found
     new 50112aa  SLING-4202 OSGi Mock: Fail-fast when mandatory references cannot be injected
     new 70f5447  fix some javadoc errors/warnings
     new d7f336c  [maven-release-plugin] prepare release org.apache.sling.testing.osgi-mock-1.1.0
     new 5022c27  [maven-release-plugin] prepare for next development iteration
     new b910746  rename package-private class ReflectinServiceUtil to OsgiServiceUtil because it does not any longer use reflection to do it's job (no functional change)
     new 68de5d4  SLING-4435 OSGi Mock: Cache reading of OSGi SCR Metadata files
     new dc9866e  SLING-4439 add support for unregistering services and prepare dynamic service reference handling
     new e3e8bb1  SLING-4439 implement dynamic service registration
     new e6baa2c  SLING-4439 implement dynamic service registration
     new a980473  SLING-4439 implement dynamic service registration
     new e828e3d  SLING-4439 implement dynamic service registration - support string array properties in osgi metadata as well - merge properties from osgi metadata with those specified on activation/registration call - support "old-style" scr component metadata file as well (and test this case)
     new 5afcfc6  [maven-release-plugin] prepare release org.apache.sling.testing.osgi-mock-1.2.0
     new cea3da7  [maven-release-plugin] prepare for next development iteration
     new c389c24  SLING-4472 MockBundleContent.getProperty should return null
     new 4900b9b  Add missing licence headers and ignores for rat
     new 174e654  SLING-4670 : SCR metadata can't be read if components element is missing
     new c1ab86f  SLING-4698 - Set parent.relativePath to empty for all modules
     new ad91a5b  SLING-4718 Support OSGi component name different from implementation class
     new dd63020  SLING-4719 osgi-mock: Add Mock EventAdmin implementation
     new aede0b4  SLING-4720 Add support for "Long" and "Boolean" property types in SCR metadata
     new bd78f8a  SLING-4725 osgi-mock: Make compatible to OSGi 5/6
     new 9a4208d  SLING-4470 introduce separate "logging-mock" project for default log configuration in mock tests, remove compile-dependency on slf4j-simple
     new 61fec88  update dependencies for release
     new 20100ff  [maven-release-plugin] prepare release org.apache.sling.testing.osgi-mock-1.3.0
     new 1e80946  [maven-release-plugin] prepare for next development iteration
     new 62541d8  update to next development snapshot unit released artifacts are in maven central
     new c5653dd  SLING-4756 - ServiceListener notifications are not filtered
     new cd84d7c  SLING-4756 - ServiceListener notifications are not filtered
     new 88790a8  SLING-4756 - ServiceListener notifications are not filtered
     new 3704cb2  update to released mock versions
     new 0f222f7  SLING-4769 Osgi Mock: Add support for bundle headers
     new 5644c49  SLING-4756 add support for filtering via felix.framework FilterImpl implementation
     new f4513f8  SLING-4771 Osgi Mock: Allow to set mock bundle symbolic name
     new 9b26d4c  OsgiServiceUtil: clarify exception message when no matching references were found
     new 7da1c22  MockBundleTest - cast once in the @BeforeMethod instead of every test
     new 4d21da0  SLING-4780 - Implement MockBundle.getLastModified
     new 9248ced  SLING-4781 - Implement MockBundle.getEntryPaths
     new ed82c56  SLING-4781 - Implement MockBundle.getEntryPaths
     new 456e603  SLING-4813 - Support CGLIB enhanced classes for Mockito based service mocks
     new 8d9a404  [maven-release-plugin] prepare release org.apache.sling.testing.osgi-mock-1.4.0
     new 1e43ffc  [maven-release-plugin] prepare for next development iteration
     new 2cd752f  Update to Sling Parent 23
     new 5e5797b  Testing: remove superflous sling.java.version=6 as it's the default now
     new 384b81a  Update rat exclude rules
     new a69eb1b  @no-jira : test failed with a timeout on jenkins - tring with 2sec timeout
     new 64dc2d0  SLING-4845 - MockBundleContext is not thread-safe
     new 9260a58  SLING-4845 - MockBundleContext is not thread-safe
     new e283546  set parent version to 24 and add empty relativePath where missing
     new e996493  remove unused LOG variable
     new 45d1dbc  SLING-4901 osgi-mock: Add support for ComponentContext.getUsingBundle()
     new ec78cea  [maven-release-plugin] prepare release org.apache.sling.testing.osgi-mock-1.5.0
     new b693d17  [maven-release-plugin] prepare for next development iteration
     new 1e64d07  cosmetic: beautify error messages when setup/teardown fails
     new 2b257a4  SLING-5083 osgi-mock: Eliminate compile dependency to org.apache.felix.scr.annotations
     new ee4eeb2  SLING-5089 mock-osgi: Deactivate all services on shutdown
     new 3747fc7  SLING-5089 deactivate services in reverse order
     new 571dcdb  SLING-5090 osgi-mock: Support ServiceFactory
     new 29e4a32  SLING-5088 properly shutdown executor service in MockEventAdmin
     new a80c2f8  SLING-5088 deprecate activate/deactivate methods without bundleContext parameter because there is a small risk for memory leaks
     new 3e81888  cosmetic: fix javadoc errors
     new 07870d2  Update the main reactor to parent 25
     new 5f24bfb  [maven-release-plugin] prepare release org.apache.sling.testing.osgi-mock-1.6.0
     new b52bba4  [maven-release-plugin] prepare for next development iteration
     new 5bdecdf  SLING-5143 osgi-mock: MockBundleContext is not thread-safe when using iterators
     new 3c17662  SLING-5198 osgi-mock: Add basic ConfigurationAdmin support
     new 4aa8c55  [maven-release-plugin] prepare release org.apache.sling.testing.osgi-mock-1.7.0
     new d38d8c1  [maven-release-plugin] prepare for next development iteration
     new 505f211  SLING-5312 osgi-mock: Eliminate runtime-depenency to Felix SCR annotations
     new 9618e26  SLING-5323 Update to OSGi R6
     new 3f5a172  SLING-5324 osgi-mock: Support OSGi R6 field-based reference bindings
     new bf51720  SLING-5325 osgi-mock: Support target filtering on DS references
     new 5e0ac1d  SLING-5323 no need to update to JDK 1.7, we stick with JDK 1.6.
     new eb580c3  SLING-5327 refactor and unify lifecycle method handling (and fix missing argument variants for modified methods - should support the same as the activate method)
     new db27b68  SLING-5327 osgi-mock: Support OSGi R6 Component propert types for configuration
     new b29a918  SLING-5327 inline the required classes from org.apache.felix.scr
     new 5d2ecfe  SLING-5372 osgi-mock: MockBundleContext.getServices(Class,String) throws NPE when no services found
     new 4e489a6  [maven-release-plugin] prepare release org.apache.sling.testing.osgi-mock-2.0.0
     new 0b7c760  [maven-release-plugin] copy for tag org.apache.sling.testing.osgi-mock-2.0.0

The 97 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


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

[sling-org-apache-sling-testing-osgi-mock] 04/10: SLING-5323 no need to update to JDK 1.7, we stick with JDK 1.6.

Posted by ro...@apache.org.
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-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit 5e0ac1d148c38bc77f04b689d0bfb20e15d3a873
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Tue Nov 24 06:33:26 2015 +0000

    SLING-5323 no need to update to JDK 1.7, we stick with JDK 1.6.
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1716042 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                                              | 5 -----
 src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java       | 4 ++--
 .../java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java     | 4 ++--
 3 files changed, 4 insertions(+), 9 deletions(-)

diff --git a/pom.xml b/pom.xml
index 085b1ad..7b7b28a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -40,11 +40,6 @@
         <url>http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock</url>
     </scm>
     
-    <properties>
-        <!-- OSGi mocks 2.x only support Java 7, 1.x still supports Java 6 -->
-        <sling.java.version>7</sling.java.version>
-    </properties>
-
     <dependencies>
   
         <!-- This version of OSGi mocks is targeted at OSGi R6. osgi-mocks 1.x is compatible with older versions. -->
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 431ef27..cf2452a 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
@@ -232,9 +232,9 @@ public final class MockOsgi {
      * @return Configuration admin or null if not registered.
      */
     private static ConfigurationAdmin getConfigAdmin(BundleContext bundleContext) {
-        ServiceReference<ConfigurationAdmin> ref = bundleContext.getServiceReference(ConfigurationAdmin.class);
+        ServiceReference<?> ref = bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
         if (ref != null) {
-            return bundleContext.getService(ref);
+            return (ConfigurationAdmin)bundleContext.getService(ref);
         }
         return null;
     }
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
index ea304d5..3e22353 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
@@ -478,7 +478,7 @@ final class OsgiServiceUtil {
             field.setAccessible(true);
             Collection<Object> collection = (Collection<Object>)field.get(target);
             if (collection == null) {
-                collection = new ArrayList<>();
+                collection = new ArrayList<Object>();
             }
             if (item != null) {
                 collection.add(item);
@@ -500,7 +500,7 @@ final class OsgiServiceUtil {
             field.setAccessible(true);
             Collection<Object> collection = (Collection<Object>)field.get(target);
             if (collection == null) {
-                collection = new ArrayList<>();
+                collection = new ArrayList<Object>();
             }
             if (item != null) {
                 collection.remove(item);

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

[sling-org-apache-sling-testing-osgi-mock] 05/10: SLING-5327 refactor and unify lifecycle method handling (and fix missing argument variants for modified methods - should support the same as the activate method)

Posted by ro...@apache.org.
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-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit eb580c37ebd52ad0f540f7e9928d8e279eda7cd0
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Tue Nov 24 11:00:48 2015 +0000

    SLING-5327 refactor and unify lifecycle method handling (and fix missing argument variants for modified methods - should support the same as the activate method)
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1716103 13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/sling/testing/mock/osgi/MockOsgi.java   |   3 +-
 .../sling/testing/mock/osgi/OsgiServiceUtil.java   | 112 +++++++++++++--------
 2 files changed, 70 insertions(+), 45 deletions(-)

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 cf2452a..4834647 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
@@ -215,7 +215,8 @@ public final class MockOsgi {
      */
     public static boolean modified(Object target, BundleContext bundleContext, Map<String, Object> properties) {
         Map<String, Object> mergedProperties = propertiesMergeWithOsgiMetadata(target, getConfigAdmin(bundleContext), properties);
-        return OsgiServiceUtil.modified(target, bundleContext, mergedProperties);
+        ComponentContext componentContext = newComponentContext(bundleContext, mergedProperties);
+        return OsgiServiceUtil.modified(target, componentContext, mergedProperties);
     }
     
     /**
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
index 3e22353..e05a15c 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
@@ -80,7 +80,61 @@ final class OsgiServiceUtil {
         }
 
         // try to find matching activate/deactivate method and execute it
+        if (invokeLifecycleMethod(target, targetClass, methodName, !activate, 
+                componentContext, MapUtil.toMap(componentContext.getProperties()))) {
+            return true;
+        }
+        
+        if (fallbackDefaultName) {
+            return false;
+        }
         
+        throw new RuntimeException("No matching " + (activate ? "activation" : "deactivation") + " method with name '" + methodName + "' "
+                + " found in class " + targetClass.getName());
+    }
+
+    /**
+     * Simulate modification of configuration of OSGi service instance.
+     * @param target Service instance.
+     * @param properties Updated configuration
+     * @return true if modified method was called. False if it failed.
+     */
+    public static boolean modified(Object target, ComponentContext componentContext, Map<String,Object> properties) {
+        Class<?> targetClass = target.getClass();
+
+        // get method name for activation/deactivation from osgi metadata
+        OsgiMetadata metadata = OsgiMetadataUtil.getMetadata(targetClass);
+        if (metadata == null) {
+            throw new NoScrMetadataException(targetClass);
+        }
+        String methodName = metadata.getModifiedMethodName();
+        if (StringUtils.isEmpty(methodName)) {
+            return false;
+        }
+        
+        // try to find matching modified method and execute it
+        if (invokeLifecycleMethod(target, targetClass, methodName, false, componentContext, properties)) {
+            return true;
+        }
+
+        throw new RuntimeException("No matching modified method with name '" + methodName + "' "
+                + " found in class " + targetClass.getName());
+    }
+    
+    /**
+     * Invokes a lifecycle method (activation, deactivation or modified) with variable method arguments.
+     * @param target Target object
+     * @param targetClass Target object class
+     * @param methodName Method name
+     * @param allowIntegerArgument Allow int or Integer as arguments (only decactivate)
+     * @param componentContext Component context
+     * @param properties Component properties
+     * @return true if a method was found and invoked
+     */
+    private static boolean invokeLifecycleMethod(Object target, Class<?> targetClass, 
+            String methodName, boolean allowIntegerArgument,
+            ComponentContext componentContext, Map<String,Object> properties) {
+
         // 1. componentContext
         Method method = getMethod(targetClass, methodName, new Class<?>[] { ComponentContext.class });
         if (method != null) {
@@ -102,8 +156,11 @@ final class OsgiServiceUtil {
             return true;
         }
         
-        // 4. int (deactivation only)
-        if (!activate) {
+        // 4. Component property type (annotation lass)
+        // TODO: implement
+        
+        // 5. int (deactivation only)
+        if (allowIntegerArgument) {
             method = getMethod(targetClass, methodName, new Class<?>[] { int.class });
             if (method != null) {
                 invokeMethod(target, method, new Object[] { 0 });
@@ -111,8 +168,8 @@ final class OsgiServiceUtil {
             }
         }
         
-        // 5. Integer (deactivation only)
-        if (!activate) {
+        // 6. Integer (deactivation only)
+        if (allowIntegerArgument) {
             method = getMethod(targetClass, methodName, new Class<?>[] { Integer.class });
             if (method != null) {
                 invokeMethod(target, method, new Object[] { 0 });
@@ -120,9 +177,10 @@ final class OsgiServiceUtil {
             }
         }
         
-        // 6. mixed arguments of componentContext, bundleContext and map
-        Class<?>[] mixedArgsAllowed = activate ? new Class<?>[] { ComponentContext.class, BundleContext.class, Map.class }
-                : new Class<?>[] { ComponentContext.class, BundleContext.class, Map.class, int.class, Integer.class };
+        // 7. mixed arguments
+        Class<?>[] mixedArgsAllowed = allowIntegerArgument ?
+                new Class<?>[] { ComponentContext.class, BundleContext.class, Map.class, int.class, Integer.class }
+                : new Class<?>[] { ComponentContext.class, BundleContext.class, Map.class };
         method = getMethodWithAnyCombinationArgs(targetClass, methodName, mixedArgsAllowed);
         if (method != null) {
             Object[] args = new Object[method.getParameterTypes().length];
@@ -144,48 +202,14 @@ final class OsgiServiceUtil {
             return true;
         }
 
-        // 7. noargs
+        // 8. noargs
         method = getMethod(targetClass, methodName, new Class<?>[0]);
         if (method != null) {
             invokeMethod(target, method, new Object[0]);
             return true;
-        }
+        }        
         
-        if (fallbackDefaultName) {
-            return false;
-        }
-        throw new RuntimeException("No matching " + (activate ? "activation" : "deactivation") + " method with name '" + methodName + "' "
-                + " found in class " + targetClass.getName());
-    }
-
-    /**
-     * Simulate modification of configuration of OSGi service instance.
-     * @param target Service instance.
-     * @param properties Updated configuration
-     * @return true if modified method was called. False if it failed.
-     */
-    public static boolean modified(Object target, BundleContext bundleContext, Map<String,Object> properties) {
-        Class<?> targetClass = target.getClass();
-
-        // get method name for activation/deactivation from osgi metadata
-        OsgiMetadata metadata = OsgiMetadataUtil.getMetadata(targetClass);
-        if (metadata == null) {
-            throw new NoScrMetadataException(targetClass);
-        }
-        String methodName = metadata.getModifiedMethodName();
-        if (StringUtils.isEmpty(methodName)) {
-            return false;
-        }
-        
-        // try to find matching modified method and execute it
-        Method method = getMethod(targetClass, methodName, new Class<?>[] { Map.class });
-        if (method != null) {
-            invokeMethod(target, method, new Object[] { properties });
-            return true;
-        }
-        
-        throw new RuntimeException("No matching modified method with name '" + methodName + "' "
-                + " found in class " + targetClass.getName());
+        return false;
     }
 
     private static Method getMethod(Class clazz, String methodName, Class<?>[] types) {

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

[sling-org-apache-sling-testing-osgi-mock] 09/10: [maven-release-plugin] prepare release org.apache.sling.testing.osgi-mock-2.0.0

Posted by ro...@apache.org.
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-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit 4e489a61faf76e917e3e2bd48b17ad82ad6a24fd
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Thu Dec 10 21:49:22 2015 +0000

    [maven-release-plugin] prepare release org.apache.sling.testing.osgi-mock-2.0.0
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1719220 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/pom.xml b/pom.xml
index 575c118..f8f8527 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,16 +28,16 @@
     </parent>
 
     <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
-    <version>2.0.0-SNAPSHOT</version>
+    <version>2.0.0</version>
     <packaging>bundle</packaging>
 
     <name>Apache Sling Testing OSGi Mock</name>
     <description>Mock implementation of selected OSGi APIs.</description>
 
     <scm>
-        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock</connection>
-        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock</developerConnection>
-        <url>http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock</url>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/tags/org.apache.sling.testing.osgi-mock-2.0.0</connection>
+        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/tags/org.apache.sling.testing.osgi-mock-2.0.0</developerConnection>
+        <url>http://svn.apache.org/viewvc/sling/tags/org.apache.sling.testing.osgi-mock-2.0.0</url>
     </scm>
     
     <dependencies>

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

[sling-org-apache-sling-testing-osgi-mock] 01/10: SLING-5323 Update to OSGi R6

Posted by ro...@apache.org.
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-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit 9618e26a3b43964f05ce3382f4123b519a4ee757
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Mon Nov 23 20:32:53 2015 +0000

    SLING-5323 Update to OSGi R6
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1715946 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            | 50 +++++++------
 .../apache/sling/testing/mock/osgi/MapUtil.java    |  1 -
 .../sling/testing/mock/osgi/MatchAllFilter.java    |  6 ++
 .../apache/sling/testing/mock/osgi/MockBundle.java | 14 ++--
 .../sling/testing/mock/osgi/MockBundleContext.java | 47 ++++++------
 .../testing/mock/osgi/MockComponentContext.java    |  4 +-
 .../sling/testing/mock/osgi/MockConfiguration.java |  7 +-
 .../apache/sling/testing/mock/osgi/MockOsgi.java   | 84 +---------------------
 .../testing/mock/osgi/MockServiceReference.java    |  8 +--
 .../testing/mock/osgi/MockServiceRegistration.java | 19 ++---
 .../sling/testing/mock/osgi/OsgiServiceUtil.java   |  6 +-
 .../sling/testing/mock/osgi/package-info.java      |  2 +-
 .../testing/mock/osgi/MockBundleContextTest.java   | 20 +++---
 .../mock/osgi/MockComponentContextTest.java        |  2 +-
 .../testing/mock/osgi/OsgiServiceUtilTest.java     |  3 +-
 .../mock/osgi/context/OsgiContextImplTest.java     |  2 +-
 16 files changed, 109 insertions(+), 166 deletions(-)

diff --git a/pom.xml b/pom.xml
index 1f9613d..085b1ad 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
     </parent>
 
     <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
-    <version>1.7.1-SNAPSHOT</version>
+    <version>2.0.0-SNAPSHOT</version>
     <packaging>bundle</packaging>
 
     <name>Apache Sling Testing OSGi Mock</name>
@@ -39,17 +39,43 @@
         <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock</developerConnection>
         <url>http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock</url>
     </scm>
+    
+    <properties>
+        <!-- OSGi mocks 2.x only support Java 7, 1.x still supports Java 6 -->
+        <sling.java.version>7</sling.java.version>
+    </properties>
 
     <dependencies>
   
+        <!-- This version of OSGi mocks is targeted at OSGi R6. osgi-mocks 1.x is compatible with older versions. -->
         <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.core</artifactId>
+            <version>6.0.0</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.cm</artifactId>
+            <version>1.5.0</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.event</artifactId>
+            <version>1.3.1</version>
             <scope>compile</scope>
         </dependency>
         <dependency>
             <groupId>org.osgi</groupId>
-            <artifactId>org.osgi.compendium</artifactId>
+            <artifactId>org.osgi.service.log</artifactId>
+            <version>1.3.0</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.component</artifactId>
+            <version>1.3.0</version>
             <scope>compile</scope>
         </dependency>
     
@@ -136,24 +162,4 @@
         </plugins>
     </build>
     
-    <profiles>
-      <profile>
-        <id>osgi-6</id>
-        <dependencies>
-          <dependency>
-            <groupId>org.osgi</groupId>
-            <artifactId>org.osgi.core</artifactId>
-            <version>6.0.0</version>
-            <scope>provided</scope>
-          </dependency>
-          <dependency>
-            <groupId>org.osgi</groupId>
-            <artifactId>org.osgi.compendium</artifactId>
-            <version>5.0.0</version>
-            <scope>provided</scope>
-          </dependency>
-        </dependencies>
-      </profile>
-    </profiles>
-  
 </project>
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MapUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/MapUtil.java
index 45d7a31..e53442b 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MapUtil.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MapUtil.java
@@ -71,7 +71,6 @@ final class MapUtil {
      * @param properties Properties from unit test code or null if none where passed
      * @return Merged properties
      */
-    @SuppressWarnings("unchecked")
     public static Map<String, Object> propertiesMergeWithOsgiMetadata(Object target,
             ConfigurationAdmin configAdmin,
             Map<String, Object> properties) {
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java b/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java
index 0a7aaf3..3ef02df 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MatchAllFilter.java
@@ -19,6 +19,7 @@
 package org.apache.sling.testing.mock.osgi;
 
 import java.util.Dictionary;
+import java.util.Map;
 
 import org.osgi.framework.Filter;
 import org.osgi.framework.ServiceReference;
@@ -43,4 +44,9 @@ class MatchAllFilter implements Filter {
         return true;
     }
 
+    @Override
+    public boolean matches(Map<String, ?> map) {
+        return true;
+    }
+
 }
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java
index 1a7a7c1..1fc9483 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundle.java
@@ -186,7 +186,7 @@ public final class MockBundle implements Bundle {
     }
 
     @Override
-    public ServiceReference[] getRegisteredServices() {
+    public ServiceReference<?>[] getRegisteredServices() {
         throw new UnsupportedOperationException();
     }
 
@@ -201,7 +201,7 @@ public final class MockBundle implements Bundle {
     }
 
     @Override
-    public ServiceReference[] getServicesInUse() {
+    public ServiceReference<?>[] getServicesInUse() {
         throw new UnsupportedOperationException();
     }
 
@@ -250,27 +250,27 @@ public final class MockBundle implements Bundle {
         throw new UnsupportedOperationException();
     }
 
-    // this is part of org.osgi 4.2.0
+    @Override
     public Map<X509Certificate, List<X509Certificate>> getSignerCertificates(final int signersType) {
         throw new UnsupportedOperationException();
     }
 
-    // this is part of org.osgi 4.2.0
+    @Override
     public Version getVersion() {
         throw new UnsupportedOperationException();
     }
 
-    // this is part of org.osgi.core 6.0.0
+    @Override
     public int compareTo(Bundle o) {
         throw new UnsupportedOperationException();
     }
 
-    // this is part of org.osgi.core 6.0.0
+    @Override
     public <A> A adapt(Class<A> type) {
         throw new UnsupportedOperationException();
     }
 
-    // this is part of org.osgi.core 6.0.0
+    @Override
     public File getDataFile(String filename) {
         throw new UnsupportedOperationException();
     }
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
index 786fad7..6a659cb 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
@@ -47,6 +47,7 @@ import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceFactory;
 import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceObjects;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.cm.ConfigurationAdmin;
@@ -86,6 +87,7 @@ class MockBundleContext implements BundleContext {
         }
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     public ServiceRegistration registerService(final String clazz, final Object service, final Dictionary properties) {
         String[] clazzes;
@@ -97,8 +99,9 @@ class MockBundleContext implements BundleContext {
         return registerService(clazzes, service, properties);
     }
 
-    // this is part of org.osgi.core 6.0.0
-    public <S> ServiceRegistration registerService(Class<S> clazz, S service, Dictionary<String, ?> properties) {
+    @SuppressWarnings("unchecked")
+    @Override
+    public <S> ServiceRegistration<S> registerService(Class<S> clazz, S service, Dictionary<String, ?> properties) {
         return registerService(clazz.getName(), service, properties);
     }
 
@@ -171,6 +174,7 @@ class MockBundleContext implements BundleContext {
         }
     }
     
+    @SuppressWarnings("unchecked")
     @Override
     public ServiceReference getServiceReference(final String clazz) {
         try {
@@ -185,11 +189,13 @@ class MockBundleContext implements BundleContext {
         return null;
     }
 
-    // this is part of org.osgi.core 6.0.0
-    public ServiceReference getServiceReference(Class clazz) {
+    @SuppressWarnings("unchecked")
+    @Override
+    public <S> ServiceReference<S> getServiceReference(Class<S> clazz) {
         return getServiceReference(clazz.getName());
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     public ServiceReference[] getServiceReferences(final String clazz, final String filter) throws InvalidSyntaxException {
         Set<ServiceReference> result = new TreeSet<ServiceReference>();
@@ -205,11 +211,13 @@ class MockBundleContext implements BundleContext {
         }
     }
 
-    // this is part of org.osgi.core 6.0.0
-    public Collection<ServiceReference> getServiceReferences(Class clazz, String filter) throws InvalidSyntaxException {
-        return ImmutableList.<ServiceReference>copyOf(getServiceReferences(clazz.getName(), filter));
+    @SuppressWarnings("unchecked")
+    @Override
+    public <S> Collection<ServiceReference<S>> getServiceReferences(Class<S> clazz, String filter) throws InvalidSyntaxException {
+        return ImmutableList.<ServiceReference<S>>copyOf(getServiceReferences(clazz.getName(), filter));
     }
 
+    @SuppressWarnings("unchecked")
     @Override
     public ServiceReference[] getAllServiceReferences(final String clazz, final String filter) throws InvalidSyntaxException {
         // for now just do the same as getServiceReferences
@@ -217,8 +225,8 @@ class MockBundleContext implements BundleContext {
     }
 
     @Override
-    public Object getService(final ServiceReference serviceReference) {
-        return ((MockServiceReference)serviceReference).getService();
+    public <S> S getService(final ServiceReference<S> serviceReference) {
+        return ((MockServiceReference<S>)serviceReference).getService();
     }
 
     @Override
@@ -284,10 +292,11 @@ class MockBundleContext implements BundleContext {
         // accept method, but ignore it
     }
 
-    Object locateService(final String name, final ServiceReference reference) {
-        for (MockServiceRegistration serviceRegistration : this.registeredServices) {
+    @SuppressWarnings("unchecked")
+    <S> S locateService(final String name, final ServiceReference<S> reference) {
+        for (MockServiceRegistration<?> serviceRegistration : this.registeredServices) {
             if (serviceRegistration.getReference() == reference) {
-                return serviceRegistration.getService();
+                return (S)serviceRegistration.getService();
             }
         }
         return null;
@@ -308,7 +317,7 @@ class MockBundleContext implements BundleContext {
      * Deactivates all bundles registered in this mocked bundle context.
      */
     public void shutdown() {
-        for (MockServiceRegistration serviceRegistration : ImmutableList.copyOf(registeredServices).reverse()) {
+        for (MockServiceRegistration<?> serviceRegistration : ImmutableList.copyOf(registeredServices).reverse()) {
             try {
                 MockOsgi.deactivate(serviceRegistration.getService(), this, serviceRegistration.getProperties());
             }
@@ -339,21 +348,19 @@ class MockBundleContext implements BundleContext {
         throw new UnsupportedOperationException();
     }
 
-    // this is part of org.osgi.core 6.0.0
+    @Override
     public Bundle getBundle(String location) {
         throw new UnsupportedOperationException();
     }
 
-    // this is part of org.osgi.core 6.0.0
-    public <S> ServiceRegistration registerService(Class<S> clazz, ServiceFactory factory, Dictionary<String, ?> properties) {
+    @Override
+    public <S> ServiceRegistration<S> registerService(Class<S> clazz, ServiceFactory<S> factory, Dictionary<String, ?> properties) {
         throw new UnsupportedOperationException();
     }
 
-    // this is part of org.osgi.core 6.0.0
-    /* class org.osgi.framework.ServiceObjects does not exist in older OSGi versions
-    public ServiceObjects getServiceObjects(ServiceReference reference) {
+    @Override
+    public <S> ServiceObjects<S> getServiceObjects(ServiceReference<S> reference) {
         throw new UnsupportedOperationException();
     }
-    */
 
 }
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 78154a5..a00ec72 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
@@ -48,7 +48,7 @@ class MockComponentContext implements ComponentContext {
     }
 
     @Override
-    public Object locateService(final String name, final ServiceReference reference) {
+    public <S> S locateService(final String name, final ServiceReference<S> reference) {
         return this.bundleContext.locateService(name, reference);
     }
 
@@ -79,7 +79,7 @@ class MockComponentContext implements ComponentContext {
     }
 
     @Override
-    public ServiceReference getServiceReference() {
+    public ServiceReference<?> getServiceReference() {
         throw new UnsupportedOperationException();
     }
 
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockConfiguration.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockConfiguration.java
index 4959cb6..7bcac56 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockConfiguration.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockConfiguration.java
@@ -46,7 +46,7 @@ class MockConfiguration implements Configuration {
     }
 
     @Override
-    public Dictionary getProperties() {
+    public Dictionary<String, Object> getProperties() {
         // return copy of dictionary
         return new Hashtable<String,Object>(MapUtil.toMap(props));
     }
@@ -100,4 +100,9 @@ class MockConfiguration implements Configuration {
         throw new UnsupportedOperationException();
     }
 
+    @Override
+    public long getChangeCount() {
+        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 a189fda..431ef27 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
@@ -130,18 +130,6 @@ public final class MockOsgi {
     /**
      * Simulate activation of service instance. Invokes the @Activate annotated method.
      * @param target Service instance.
-     * @return true if activation method was called. False if no activate method is defined.
-     * @deprecated Please use {@link #activate(Object, BundleContext)}
-     *   and shutdown the bundle context after usage.
-     */
-    @Deprecated
-    public static boolean activate(Object target) {
-        return MockOsgi.activate(target, (Dictionary<String, Object>)null);
-    }
-
-    /**
-     * Simulate activation of service instance. Invokes the @Activate annotated method.
-     * @param target Service instance.
      * @param bundleContext Bundle context
      * @return true if activation method was called. False if no activate method is defined.
      */
@@ -152,34 +140,6 @@ public final class MockOsgi {
     /**
      * Simulate activation of service instance. Invokes the @Activate annotated method.
      * @param target Service instance.
-     * @param properties Properties
-     * @return true if activation method was called. False if no activate method is defined.
-     * @deprecated Please use {@link #activate(Object, BundleContext, Dictionary)}
-     *   and shutdown the bundle context after usage.
-     */
-    @Deprecated
-    public static boolean activate(Object target, Dictionary<String, Object> properties) {
-        Dictionary<String, Object> mergedProperties = propertiesMergeWithOsgiMetadata(target, null, properties);
-        ComponentContext componentContext = newComponentContext(mergedProperties);
-        return OsgiServiceUtil.activateDeactivate(target, componentContext, true);
-    }
-
-    /**
-     * Simulate activation of service instance. Invokes the @Activate annotated method.
-     * @param target Service instance.
-     * @param properties Properties
-     * @return true if activation method was called. False if no activate method is defined.
-     * @deprecated Please use {@link #activate(Object, BundleContext, Map)}
-     *   and shutdown the bundle context after usage.
-     */
-    @Deprecated
-    public static boolean activate(Object target, Map<String, Object> properties) {
-        return activate(target, toDictionary(properties));
-    }
-
-    /**
-     * Simulate activation of service instance. Invokes the @Activate annotated method.
-     * @param target Service instance.
      * @param bundleContext Bundle context
      * @param properties Properties
      * @return true if activation method was called. False if no activate method is defined.
@@ -204,18 +164,6 @@ public final class MockOsgi {
     /**
      * Simulate deactivation of service instance. Invokes the @Deactivate annotated method.
      * @param target Service instance.
-     * @return true if deactivation method was called. False if no deactivate method is defined.
-     * @deprecated Please use {@link #deactivate(Object, BundleContext)}
-     *   and shutdown the bundle context after usage.
-     */
-    @Deprecated
-    public static boolean deactivate(Object target) {
-        return MockOsgi.deactivate(target, (Dictionary<String, Object>)null);
-    }
-
-    /**
-     * Simulate deactivation of service instance. Invokes the @Deactivate annotated method.
-     * @param target Service instance.
      * @param bundleContext Bundle context.
      * @return true if deactivation method was called. False if no deactivate method is defined.
      */
@@ -226,34 +174,6 @@ public final class MockOsgi {
     /**
      * Simulate deactivation of service instance. Invokes the @Deactivate annotated method.
      * @param target Service instance.
-     * @param properties Properties
-     * @return true if deactivation method was called. False if no deactivate method is defined.
-     * @deprecated Please use {@link #deactivate(Object, BundleContext, Dictionary)}
-     *   and shutdown the bundle context after usage.
-     */
-    @Deprecated
-    public static boolean deactivate(Object target, Dictionary<String, Object> properties) {
-        Dictionary<String, Object> mergedProperties = propertiesMergeWithOsgiMetadata(target, null, properties);
-        ComponentContext componentContext = newComponentContext(mergedProperties);
-        return OsgiServiceUtil.activateDeactivate(target, componentContext, false);
-    }
-
-    /**
-     * Simulate deactivation of service instance. Invokes the @Deactivate annotated method.
-     * @param target Service instance.
-     * @param properties Properties
-     * @return true if deactivation method was called. False if no deactivate method is defined.
-     * @deprecated Please use {@link #deactivate(Object, BundleContext, Map)}
-     *   and shutdown the bundle context after usage.
-     */
-    @Deprecated
-    public static boolean deactivate(Object target, Map<String, Object> properties) {
-        return deactivate(target, toDictionary(properties));
-    }
-
-    /**
-     * Simulate deactivation of service instance. Invokes the @Deactivate annotated method.
-     * @param target Service instance.
      * @param bundleContext Bundle context
      * @param properties Properties
      * @return true if deactivation method was called. False if no deactivate method is defined.
@@ -312,9 +232,9 @@ public final class MockOsgi {
      * @return Configuration admin or null if not registered.
      */
     private static ConfigurationAdmin getConfigAdmin(BundleContext bundleContext) {
-        ServiceReference ref = bundleContext.getServiceReference(ConfigurationAdmin.class.getName());
+        ServiceReference<ConfigurationAdmin> ref = bundleContext.getServiceReference(ConfigurationAdmin.class);
         if (ref != null) {
-            return (ConfigurationAdmin)bundleContext.getService(ref);
+            return bundleContext.getService(ref);
         }
         return null;
     }
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceReference.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceReference.java
index c7568be..6af3a37 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceReference.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceReference.java
@@ -28,12 +28,12 @@ import org.osgi.framework.ServiceReference;
 /**
  * Mock {@link ServiceReference} implementation.
  */
-class MockServiceReference implements ServiceReference {
+class MockServiceReference<T> implements ServiceReference<T> {
 
     private final Bundle bundle;
-    private final MockServiceRegistration serviceRegistration;
+    private final MockServiceRegistration<T> serviceRegistration;
 
-    public MockServiceReference(final Bundle bundle, final MockServiceRegistration serviceRegistration) {
+    public MockServiceReference(final Bundle bundle, final MockServiceRegistration<T> serviceRegistration) {
         this.bundle = bundle;
         this.serviceRegistration = serviceRegistration;
     }
@@ -113,7 +113,7 @@ class MockServiceReference implements ServiceReference {
         }
     }
 
-    Object getService() {
+    T getService() {
         return this.serviceRegistration.getService();
     }
 
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java
index 8ef25a4..7141949 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java
@@ -38,24 +38,25 @@ import com.google.common.collect.ImmutableList;
 /**
  * Mock {@link ServiceRegistration} implementation.
  */
-class MockServiceRegistration implements ServiceRegistration, Comparable<MockServiceRegistration> {
+class MockServiceRegistration<T> implements ServiceRegistration<T>, Comparable<MockServiceRegistration<T>> {
 
     private static volatile long serviceCounter;
 
     private final Long serviceId;
     private final Set<String> clazzes;
-    private final Object service;
+    private final T service;
     private Dictionary<String, Object> properties;
-    private final ServiceReference serviceReference;
+    private final ServiceReference<T> serviceReference;
     private final MockBundleContext bundleContext;
 
-    public MockServiceRegistration(final Bundle bundle, final String[] clazzes, final Object service,
+    @SuppressWarnings("unchecked")
+    public MockServiceRegistration(final Bundle bundle, final String[] clazzes, final T service,
             final Dictionary<String, Object> properties, MockBundleContext bundleContext) {
         this.serviceId = ++serviceCounter;
         this.clazzes = new HashSet<String>(ImmutableList.copyOf(clazzes));
         
         if (service instanceof ServiceFactory) {
-            this.service = ((ServiceFactory)service).getService(bundleContext.getBundle(), this);
+            this.service = ((ServiceFactory<T>)service).getService(bundleContext.getBundle(), this);
         }
         else {
             this.service = service;
@@ -64,14 +65,14 @@ class MockServiceRegistration implements ServiceRegistration, Comparable<MockSer
         this.properties = properties != null ? properties : new Hashtable<String,Object>();
         this.properties.put(Constants.SERVICE_ID, this.serviceId);
         this.properties.put(Constants.OBJECTCLASS, clazzes);
-        this.serviceReference = new MockServiceReference(bundle, this);
+        this.serviceReference = new MockServiceReference<T>(bundle, this);
         this.bundleContext = bundleContext;
         
         readOsgiMetadata();
     }
 
     @Override
-    public ServiceReference getReference() {
+    public ServiceReference<T> getReference() {
         return this.serviceReference;
     }
 
@@ -100,7 +101,7 @@ class MockServiceRegistration implements ServiceRegistration, Comparable<MockSer
         return clazzes;
     }
 
-    Object getService() {
+    T getService() {
         return this.service;
     }
     
@@ -118,7 +119,7 @@ class MockServiceRegistration implements ServiceRegistration, Comparable<MockSer
     }
 
     @Override
-    public int compareTo(MockServiceRegistration obj) {
+    public int compareTo(MockServiceRegistration<T> obj) {
         return serviceId.compareTo(obj.serviceId);
     }
 
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
index 05a26bc..84ccbf8 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
@@ -53,7 +53,6 @@ final class OsgiServiceUtil {
      * @param componentContext Component context
      * @return true if activation/deactivation method was called. False if it failed.
      */
-    @SuppressWarnings("unchecked")
     public static boolean activateDeactivate(Object target, ComponentContext componentContext, boolean activate) {
         Class<?> targetClass = target.getClass();
 
@@ -390,7 +389,7 @@ final class OsgiServiceUtil {
         try {
             ServiceReference[] references = bundleContext.getServiceReferences(type.getName(), null);
             if (references != null) {
-                for (ServiceReference serviceReference : references) {
+                for (ServiceReference<?> serviceReference : references) {
                     Object serviceInstance = bundleContext.getService(serviceReference);
                     Map<String, Object> serviceConfig = new HashMap<String, Object>();
                     String[] keys = serviceReference.getPropertyKeys();
@@ -413,7 +412,7 @@ final class OsgiServiceUtil {
      * @return List of references
      */
     public static List<ReferenceInfo> getMatchingDynamicReferences(SortedSet<MockServiceRegistration> registeredServices,
-            MockServiceRegistration registration) {
+            MockServiceRegistration<?> registration) {
         List<ReferenceInfo> references = new ArrayList<ReferenceInfo>();
         for (MockServiceRegistration existingRegistration : registeredServices) {
             OsgiMetadata metadata = OsgiMetadataUtil.getMetadata(existingRegistration.getService().getClass());
@@ -444,6 +443,7 @@ final class OsgiServiceUtil {
             this.serviceReference = serviceReference;
         }
 
+        @SuppressWarnings("unchecked")
         public ServiceInfo(MockServiceRegistration registration) {
             this.serviceInstance = registration.getService();
             this.serviceConfig = MapUtil.toMap(registration.getProperties());
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 fe5f126..f15b6d0 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.4")
+@aQute.bnd.annotation.Version("3.0")
 package org.apache.sling.testing.mock.osgi;
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
index 092ab81..fb704f1 100644
--- a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
@@ -67,37 +67,37 @@ public class MockBundleContextTest {
         // prepare test services
         String clazz1 = String.class.getName();
         Object service1 = new Object();
-        Dictionary properties1 = getServiceProperties(null);
+        Dictionary<String, Object> properties1 = getServiceProperties(null);
         ServiceRegistration reg1 = bundleContext.registerService(clazz1, service1, properties1);
 
         String[] clazzes2 = new String[] { String.class.getName(), Integer.class.getName() };
         Object service2 = new Object();
-        Dictionary properties2 = getServiceProperties(null);
+        Dictionary<String, Object> properties2 = getServiceProperties(null);
         ServiceRegistration reg2 = bundleContext.registerService(clazzes2, service2, properties2);
 
         String clazz3 = Integer.class.getName();
         Object service3 = new Object();
-        Dictionary properties3 = getServiceProperties(100L);
+        Dictionary<String, Object> properties3 = getServiceProperties(100L);
         ServiceRegistration reg3 = bundleContext.registerService(clazz3, service3, properties3);
 
         // test get service references
-        ServiceReference refString = bundleContext.getServiceReference(String.class.getName());
+        ServiceReference<?> refString = bundleContext.getServiceReference(String.class.getName());
         assertSame(reg1.getReference(), refString);
 
-        ServiceReference refInteger = bundleContext.getServiceReference(Integer.class.getName());
+        ServiceReference<?> refInteger = bundleContext.getServiceReference(Integer.class.getName());
         assertSame(reg3.getReference(), refInteger);
 
-        ServiceReference[] refsString = bundleContext.getServiceReferences(String.class.getName(), null);
+        ServiceReference<?>[] refsString = bundleContext.getServiceReferences(String.class.getName(), null);
         assertEquals(2, refsString.length);
         assertSame(reg1.getReference(), refsString[0]);
         assertSame(reg2.getReference(), refsString[1]);
 
-        ServiceReference[] refsInteger = bundleContext.getServiceReferences(Integer.class.getName(), null);
+        ServiceReference<?>[] refsInteger = bundleContext.getServiceReferences(Integer.class.getName(), null);
         assertEquals(2, refsInteger.length);
         assertSame(reg3.getReference(), refsInteger[0]);
         assertSame(reg2.getReference(), refsInteger[1]);
 
-        ServiceReference[] allRefsString = bundleContext.getAllServiceReferences(String.class.getName(), null);
+        ServiceReference<?>[] allRefsString = bundleContext.getAllServiceReferences(String.class.getName(), null);
         assertArrayEquals(refsString, allRefsString);
 
         // test get services
@@ -116,7 +116,7 @@ public class MockBundleContextTest {
         // prepare test services
         String clazz1 = String.class.getName();
         Object service1 = new Object();
-        Dictionary properties1 = getServiceProperties(null);
+        Dictionary<String, Object> properties1 = getServiceProperties(null);
         ServiceRegistration reg1 = bundleContext.registerService(clazz1, service1, properties1);
         
         assertNotNull(bundleContext.getServiceReference(clazz1));
@@ -127,7 +127,7 @@ public class MockBundleContextTest {
     }
     
 
-    private Dictionary getServiceProperties(final Long serviceRanking) {
+    private Dictionary<String, Object> getServiceProperties(final Long serviceRanking) {
         Dictionary<String, Object> props = new Hashtable<String, Object>();
         if (serviceRanking != null) {
             props.put(Constants.SERVICE_RANKING, serviceRanking);
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 b4497eb..aab327e 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
@@ -71,7 +71,7 @@ public class MockComponentContextTest {
         String clazz = String.class.getName();
         Object service = new Object();
         underTest.getBundleContext().registerService(clazz, service, null);
-        ServiceReference ref = underTest.getBundleContext().getServiceReference(clazz);
+        ServiceReference<?> ref = underTest.getBundleContext().getServiceReference(clazz);
 
         // test locate service
         Object locatedService = underTest.locateService(null, ref);
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
index f8b9a73..170957f 100644
--- a/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
@@ -251,7 +251,6 @@ public class OsgiServiceUtilTest {
         private ComponentContext componentContext;
         private Map<String, Object> config;
 
-        @SuppressWarnings("unchecked")
         @Activate
         private void activate(ComponentContext ctx) {
             this.componentContext = ctx;
@@ -278,7 +277,7 @@ public class OsgiServiceUtilTest {
 
         public List<ServiceInterface2> getReferences2() {
             List<ServiceInterface2> services = new ArrayList<ServiceInterface2>();
-            for (ServiceReference serviceReference : references2) {
+            for (ServiceReference<?> serviceReference : references2) {
                 services.add((ServiceInterface2)componentContext.getBundleContext().getService(serviceReference));
             }
             return services;
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
index 60d5b0b..59990d5 100644
--- a/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
@@ -75,7 +75,7 @@ public class OsgiContextImplTest {
         Set<String> myService = new HashSet<String>();
         context.registerService(Set.class, myService, props);
 
-        ServiceReference serviceReference = context.bundleContext().getServiceReference(Set.class.getName());
+        ServiceReference<?> serviceReference = context.bundleContext().getServiceReference(Set.class.getName());
         Object serviceResult = context.bundleContext().getService(serviceReference);
         assertSame(myService, serviceResult);
         assertEquals("value1", serviceReference.getProperty("prop1"));

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

[sling-org-apache-sling-testing-osgi-mock] 08/10: SLING-5372 osgi-mock: MockBundleContext.getServices(Class, String) throws NPE when no services found

Posted by ro...@apache.org.
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-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit 5d2ecfeae6a3c9b5b6c7e22c2dcfd16da56cf6f3
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Thu Dec 10 21:39:16 2015 +0000

    SLING-5372 osgi-mock: MockBundleContext.getServices(Class,String) throws NPE when no services found
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1719219 13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/sling/testing/mock/osgi/MockBundleContext.java |  8 +++++++-
 .../sling/testing/mock/osgi/MockBundleContextTest.java    | 15 +++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
index 7ea264c..3c0ad53 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
@@ -218,7 +218,13 @@ class MockBundleContext implements BundleContext {
     @SuppressWarnings("unchecked")
     @Override
     public <S> Collection<ServiceReference<S>> getServiceReferences(Class<S> clazz, String filter) throws InvalidSyntaxException {
-        return ImmutableList.<ServiceReference<S>>copyOf(getServiceReferences(clazz.getName(), filter));
+        ServiceReference<S>[] result = getServiceReferences(clazz.getName(), filter);
+        if (result == null) {
+            return ImmutableList.<ServiceReference<S>>of();
+        }
+        else {
+            return ImmutableList.<ServiceReference<S>>copyOf(result);
+        }
     }
 
     @SuppressWarnings("unchecked")
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
index fb704f1..2b1cded 100644
--- a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
@@ -29,6 +29,7 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 
+import java.util.Collection;
 import java.util.Dictionary;
 import java.util.Hashtable;
 
@@ -92,6 +93,10 @@ public class MockBundleContextTest {
         assertSame(reg1.getReference(), refsString[0]);
         assertSame(reg2.getReference(), refsString[1]);
 
+        Collection<ServiceReference<String>> refColString = bundleContext.getServiceReferences(String.class, null);
+        assertEquals(2, refColString.size());
+        assertSame(reg1.getReference(), refColString.iterator().next());
+
         ServiceReference<?>[] refsInteger = bundleContext.getServiceReferences(Integer.class.getName(), null);
         assertEquals(2, refsInteger.length);
         assertSame(reg3.getReference(), refsInteger[0]);
@@ -112,6 +117,16 @@ public class MockBundleContextTest {
     }
     
     @Test
+    public void testNoServiceReferences() throws InvalidSyntaxException {
+        ServiceReference<?>[] refs = bundleContext.getServiceReferences(String.class.getName(), null);
+        assertNull(refs);
+
+        Collection<ServiceReference<String>> refCol = bundleContext.getServiceReferences(String.class, null);
+        assertNotNull(refCol);
+        assertTrue(refCol.isEmpty());
+    }
+    
+    @Test
     public void testServiceUnregistration() {
         // prepare test services
         String clazz1 = String.class.getName();

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

[sling-org-apache-sling-testing-osgi-mock] 06/10: SLING-5327 osgi-mock: Support OSGi R6 Component propert types for configuration

Posted by ro...@apache.org.
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-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit db27b68db6225804ed55f57989d0d7ff39100f61
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Tue Nov 24 12:03:58 2015 +0000

    SLING-5327 osgi-mock: Support OSGi R6 Component propert types for configuration
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1716114 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  6 ++
 .../sling/testing/mock/osgi/OsgiServiceUtil.java   | 56 +++++++++++++----
 .../OsgiServiceUtilActivateDeactivateTest.java     | 70 +++++++++++++++++++++-
 ....osgi.OsgiServiceUtilActivateDeactivateTest.xml |  3 +
 4 files changed, 122 insertions(+), 13 deletions(-)

diff --git a/pom.xml b/pom.xml
index 7b7b28a..adf4ceb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -87,6 +87,12 @@
             <version>5.0.0</version>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr</artifactId>
+            <version>2.0.2</version>
+            <scope>compile</scope>
+        </dependency>
 
         <dependency>
             <groupId>com.google.guava</groupId>
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
index e05a15c..3ba8ad4 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
@@ -18,19 +18,19 @@
  */
 package org.apache.sling.testing.mock.osgi;
 
+import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.SortedSet;
 
-import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.felix.scr.impl.helper.Annotations;
 import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.FieldCollectionType;
 import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.OsgiMetadata;
 import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.Reference;
@@ -157,7 +157,13 @@ final class OsgiServiceUtil {
         }
         
         // 4. Component property type (annotation lass)
-        // TODO: implement
+        method = getMethod(targetClass, methodName, new Class<?>[] { Annotation.class });
+        if (method != null) {
+            invokeMethod(target, method, new Object[] { Annotations.toObject(method.getParameterTypes()[0],
+                    MapUtil.toMap(componentContext.getProperties()), 
+                    componentContext.getBundleContext().getBundle(), false) });
+            return true;
+        }
         
         // 5. int (deactivation only)
         if (allowIntegerArgument) {
@@ -179,8 +185,8 @@ final class OsgiServiceUtil {
         
         // 7. mixed arguments
         Class<?>[] mixedArgsAllowed = allowIntegerArgument ?
-                new Class<?>[] { ComponentContext.class, BundleContext.class, Map.class, int.class, Integer.class }
-                : new Class<?>[] { ComponentContext.class, BundleContext.class, Map.class };
+                new Class<?>[] { ComponentContext.class, BundleContext.class, Map.class, Annotation.class, int.class, Integer.class }
+                : new Class<?>[] { ComponentContext.class, BundleContext.class, Map.class, Annotation.class };
         method = getMethodWithAnyCombinationArgs(targetClass, methodName, mixedArgsAllowed);
         if (method != null) {
             Object[] args = new Object[method.getParameterTypes().length];
@@ -194,6 +200,11 @@ final class OsgiServiceUtil {
                 else if (method.getParameterTypes()[i] == Map.class) {
                     args[i] = MapUtil.toMap(componentContext.getProperties());
                 }
+                else if (method.getParameterTypes()[i].isAnnotation()) {
+                    args[i] = Annotations.toObject(method.getParameterTypes()[i],
+                            MapUtil.toMap(componentContext.getProperties()), 
+                            componentContext.getBundleContext().getBundle(), false);
+                }
                 else if (method.getParameterTypes()[i] == int.class || method.getParameterTypes()[i] == Integer.class) {
                     args[i] = 0;
                 }
@@ -215,9 +226,18 @@ final class OsgiServiceUtil {
     private static Method getMethod(Class clazz, String methodName, Class<?>[] types) {
         Method[] methods = clazz.getDeclaredMethods();
         for (Method method : methods) {
-            if (StringUtils.equals(method.getName(), methodName)
-                    && Arrays.equals(method.getParameterTypes(), types)) {
-                return method;
+            if (StringUtils.equals(method.getName(), methodName) && method.getParameterTypes().length==types.length) {
+                boolean foundMismatch = false;
+                for (int i=0; i<types.length; i++) {
+                    if (!((method.getParameterTypes()[i]==types[i]) 
+                            || (types[i]==Annotation.class && method.getParameterTypes()[i].isAnnotation()))) {
+                        foundMismatch = true;
+                        break;
+                    }
+                }
+                if (!foundMismatch) {
+                    return method;
+                }
             }
         }
         // not found? check super classes
@@ -235,7 +255,7 @@ final class OsgiServiceUtil {
                 boolean foundMismatch = false;
                 for (int i=0; i<types.length; i++) {
                     if (!method.getParameterTypes()[i].isAssignableFrom(types[i])) {
-                        foundMismatch = false;
+                        foundMismatch = true;
                         break;
                     }
                 }
@@ -256,12 +276,24 @@ final class OsgiServiceUtil {
         Method[] methods = clazz.getDeclaredMethods();
         for (Method method : methods) {
             if (StringUtils.equals(method.getName(), methodName) && method.getParameterTypes().length > 1) {
+                boolean foundMismatch = false;
                 for (Class<?> parameterType : method.getParameterTypes()) {
-                    if (!ArrayUtils.contains(types,  parameterType)) {
-                        return null;
+                    boolean foundAnyMatch = false;
+                    for (int i=0; i<types.length; i++) {
+                        if ((parameterType==types[i]) 
+                                || (types[i]==Annotation.class && parameterType.isAnnotation())) {
+                            foundAnyMatch = true;
+                            break;
+                        }
                     }
+                    if (!foundAnyMatch) {
+                        foundMismatch = true;
+                        break;
+                    }
+                }
+                if (!foundMismatch) {
+                    return method;
                 }
-                return method;
             }
         }
         // not found? check super classes
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilActivateDeactivateTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilActivateDeactivateTest.java
index d848fe2..6974c01 100644
--- a/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilActivateDeactivateTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilActivateDeactivateTest.java
@@ -84,6 +84,7 @@ public class OsgiServiceUtilActivateDeactivateTest {
         
         assertTrue(MockOsgi.activate(service, bundleContext, map));
         assertTrue(service.isActivated());
+        assertEquals(map, ImmutableMap.copyOf(service.getMap()));
         
         assertTrue(MockOsgi.deactivate(service, bundleContext, map));
         assertFalse(service.isActivated());
@@ -113,6 +114,25 @@ public class OsgiServiceUtilActivateDeactivateTest {
         assertTrue(MockOsgi.deactivate(service, bundleContext, map));
         assertFalse(service.isActivated());
     }
+    
+    @Test
+    public void testService7() {
+        Service7 service = new Service7();
+        
+        assertTrue(MockOsgi.activate(service, bundleContext, map));
+        assertTrue(service.isActivated());
+        assertSame(bundleContext, service.getComponentContext().getBundleContext());
+        assertSame(bundleContext, service.getBundleContext());
+        assertEquals(map, ImmutableMap.copyOf(service.getMap()));
+        
+        assertTrue(MockOsgi.deactivate(service, bundleContext, map));
+        assertFalse(service.isActivated());
+    }
+    
+    
+    public @interface ServiceConfig {
+        String prop1();
+    }
 
     @Component
     public static class Service1 {
@@ -202,10 +222,12 @@ public class OsgiServiceUtilActivateDeactivateTest {
     public static class Service4 {
         
         private boolean activated;
+        private Map<String, Object> map;
 
         @Activate
-        private void activate() {
+        private void activate(ServiceConfig config) {
             this.activated = true;
+            map = ImmutableMap.<String, Object>of("prop1", config.prop1());
         }
 
         @Deactivate
@@ -217,6 +239,10 @@ public class OsgiServiceUtilActivateDeactivateTest {
             return activated;
         }
 
+        public Map<String, Object> getMap() {
+            return map;
+        }
+        
     }
 
     @Component
@@ -282,4 +308,46 @@ public class OsgiServiceUtilActivateDeactivateTest {
 
     }
 
+    @Component
+    public static class Service7 {
+        
+        private boolean activated;
+        private ComponentContext componentContext;
+        private BundleContext bundleContext;
+        private Map<String,Object> map;
+
+        @Activate
+        private void activate(ComponentContext componentContext, ServiceConfig config, BundleContext bundleContext) {
+            this.activated = true;
+            this.componentContext = componentContext;
+            this.bundleContext = bundleContext;
+            this.map = ImmutableMap.<String, Object>of("prop1", config.prop1());;
+        }
+
+        @Deactivate
+        private void deactivate() {
+            this.activated = false;
+            this.componentContext = null;
+            this.bundleContext = null;
+            this.map = null;
+        }
+        
+        public boolean isActivated() {
+            return activated;
+        }
+
+        public ComponentContext getComponentContext() {
+            return componentContext;
+        }
+
+        public BundleContext getBundleContext() {
+            return bundleContext;
+        }
+
+        public Map<String, Object> getMap() {
+            return map;
+        }
+
+    }
+
 }
diff --git a/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilActivateDeactivateTest.xml b/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilActivateDeactivateTest.xml
index 61b4bc6..a869506 100644
--- a/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilActivateDeactivateTest.xml
+++ b/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilActivateDeactivateTest.xml
@@ -36,4 +36,7 @@
   <scr:component name="org.apache.sling.testing.mock.osgi.OsgiServiceUtilActivateDeactivateTest$Service6" activate="activate" deactivate="deactivate">
     <implementation class="org.apache.sling.testing.mock.osgi.OsgiServiceUtilActivateDeactivateTest$Service6"/>
   </scr:component>
+  <scr:component name="org.apache.sling.testing.mock.osgi.OsgiServiceUtilActivateDeactivateTest$Service7" activate="activate" deactivate="deactivate">
+    <implementation class="org.apache.sling.testing.mock.osgi.OsgiServiceUtilActivateDeactivateTest$Service7"/>
+  </scr:component>
 </components>

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

[sling-org-apache-sling-testing-osgi-mock] 03/10: SLING-5325 osgi-mock: Support target filtering on DS references

Posted by ro...@apache.org.
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-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit bf51720f966b0a801ccb5bb87cf9284a6ad47447
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Tue Nov 24 00:36:18 2015 +0000

    SLING-5325 osgi-mock: Support target filtering on DS references
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1716001 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/testing/mock/osgi/MockBundleContext.java | 56 ++++++++++++----------
 .../sling/testing/mock/osgi/OsgiMetadataUtil.java  | 33 +++++++++++++
 .../sling/testing/mock/osgi/OsgiServiceUtil.java   | 43 ++++++++++-------
 ...ockBundleContextDynamicReferncesOsgiR6Test.java | 19 ++++++++
 .../testing/mock/osgi/OsgiServiceUtilTest.java     |  9 +++-
 ...sling.testing.mock.osgi.OsgiServiceUtilTest.xml |  1 +
 6 files changed, 115 insertions(+), 46 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
index 6a659cb..7ea264c 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
@@ -125,18 +125,20 @@ class MockBundleContext implements BundleContext {
         List<ReferenceInfo> affectedReferences = OsgiServiceUtil.getMatchingDynamicReferences(registeredServices, registration);
         for (ReferenceInfo referenceInfo : affectedReferences) {
             Reference reference = referenceInfo.getReference();
-            switch (reference.getCardinality()) {
-            case MANDATORY_UNARY:
-                throw new ReferenceViolationException("Mandatory unary reference of type " + reference.getInterfaceType() + " already fulfilled "
-                        + "for service " + reference.getServiceClass().getName() + ", registration of new service with this interface failed.");
-            case MANDATORY_MULTIPLE:
-            case OPTIONAL_MULTIPLE:
-            case OPTIONAL_UNARY:
-                OsgiServiceUtil.invokeBindMethod(reference, referenceInfo.getServiceRegistration().getService(),
-                        new ServiceInfo(registration));
-                break;
-            default:
-                throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
+            if (reference.matchesTargetFilter(registration.getReference())) {
+                switch (reference.getCardinality()) {
+                case MANDATORY_UNARY:
+                    throw new ReferenceViolationException("Mandatory unary reference of type " + reference.getInterfaceType() + " already fulfilled "
+                            + "for service " + reference.getServiceClass().getName() + ", registration of new service with this interface failed.");
+                case MANDATORY_MULTIPLE:
+                case OPTIONAL_MULTIPLE:
+                case OPTIONAL_UNARY:
+                    OsgiServiceUtil.invokeBindMethod(reference, referenceInfo.getServiceRegistration().getService(),
+                            new ServiceInfo(registration));
+                    break;
+                default:
+                    throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
+                }
             }
         }
     }
@@ -156,20 +158,22 @@ class MockBundleContext implements BundleContext {
         List<ReferenceInfo> affectedReferences = OsgiServiceUtil.getMatchingDynamicReferences(registeredServices, registration);
         for (ReferenceInfo referenceInfo : affectedReferences) {
             Reference reference = referenceInfo.getReference();
-            switch (reference.getCardinality()) {
-            case MANDATORY_UNARY:
-                throw new ReferenceViolationException("Reference of type " + reference.getInterfaceType() + " "
-                        + "for service " + reference.getServiceClass().getName() + " is mandatory unary, "
-                        + "unregistration of service with this interface failed.");
-            case MANDATORY_MULTIPLE:
-            case OPTIONAL_MULTIPLE:
-            case OPTIONAL_UNARY:
-                // it is currently not checked if for a MANDATORY_MULTIPLE reference the last reference is removed
-                OsgiServiceUtil.invokeUnbindMethod(reference, referenceInfo.getServiceRegistration().getService(),
-                        new ServiceInfo(registration));
-                break;
-            default:
-                throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
+            if (reference.matchesTargetFilter(registration.getReference())) {
+                switch (reference.getCardinality()) {
+                case MANDATORY_UNARY:
+                    throw new ReferenceViolationException("Reference of type " + reference.getInterfaceType() + " "
+                            + "for service " + reference.getServiceClass().getName() + " is mandatory unary, "
+                            + "unregistration of service with this interface failed.");
+                case MANDATORY_MULTIPLE:
+                case OPTIONAL_MULTIPLE:
+                case OPTIONAL_UNARY:
+                    // it is currently not checked if for a MANDATORY_MULTIPLE reference the last reference is removed
+                    OsgiServiceUtil.invokeUnbindMethod(reference, referenceInfo.getServiceRegistration().getService(),
+                            new ServiceInfo(registration));
+                    break;
+                default:
+                    throw new RuntimeException("Unepxected cardinality: " + reference.getCardinality());
+                }
             }
         }
     }
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
index 85db326..c3ad237 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
@@ -41,6 +41,10 @@ import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
 
 import org.apache.commons.lang3.StringUtils;
+import org.apache.felix.framework.FilterImpl;
+import org.osgi.framework.Filter;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -391,6 +395,8 @@ final class OsgiMetadataUtil {
         private final String unbind;
         private final String field;
         private final FieldCollectionType fieldCollectionType;
+        private final String target;
+        private final Filter targetFilter;
 
         private Reference(Class<?> clazz, Node node) {
             this.clazz = clazz;
@@ -403,6 +409,17 @@ final class OsgiMetadataUtil {
             this.unbind = getAttributeValue(node, "unbind");
             this.field = getAttributeValue(node, "field");
             this.fieldCollectionType = toFieldCollectionType(getAttributeValue(node, "field-collection-type"));
+            this.target = getAttributeValue(node, "target");
+            if (StringUtils.isNotEmpty(this.target)) {
+                try {
+                    this.targetFilter = new FilterImpl(this.target);
+                } catch (InvalidSyntaxException ex) {
+                    throw new RuntimeException("Invalid target filet in reference '" + this.name + "' of class " + clazz.getName(), ex);
+                }
+            }
+            else {
+                this.targetFilter = null;
+            }
         }
 
         public Class<?> getServiceClass() {
@@ -434,6 +451,11 @@ final class OsgiMetadataUtil {
                     || this.cardinality == ReferenceCardinality.MANDATORY_MULTIPLE;
         }
 
+        public boolean isCardinalityOptional() {
+            return this.cardinality == ReferenceCardinality.OPTIONAL_UNARY
+                    || this.cardinality == ReferenceCardinality.OPTIONAL_MULTIPLE;
+        }
+
         public ReferencePolicy getPolicy() {
             return policy;
         }
@@ -454,6 +476,17 @@ final class OsgiMetadataUtil {
             return this.field;
         }
         
+        public String getTarget() {
+            return this.target;
+        }
+        
+        public boolean matchesTargetFilter(ServiceReference<?> serviceReference) {
+            if (targetFilter == null) {
+                return true;
+            }
+            return targetFilter.match(serviceReference);
+        }
+        
         public FieldCollectionType getFieldCollectionType() {
             return this.fieldCollectionType;
         }
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
index 1f8eaa0..ea304d5 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
@@ -34,7 +34,6 @@ import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.FieldCollectionType;
 import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.OsgiMetadata;
 import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.Reference;
-import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.ReferenceCardinality;
 import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.ReferencePolicy;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.InvalidSyntaxException;
@@ -343,20 +342,21 @@ final class OsgiServiceUtil {
         Class<?> type = reference.getInterfaceTypeAsClass();
 
         // get matching service references
-        List<ServiceInfo> matchingServices = getMatchingServices(type, bundleContext);
+        List<ServiceInfo> matchingServices = getMatchingServices(type, bundleContext, reference.getTarget());
 
         // no references found? check if reference was optional
         if (matchingServices.isEmpty()) {
-            boolean isOptional = (reference.getCardinality() == ReferenceCardinality.OPTIONAL_UNARY || reference
-                    .getCardinality() == ReferenceCardinality.OPTIONAL_MULTIPLE);
-            if (!isOptional) {
+            if (!reference.isCardinalityOptional()) {
                 throw new ReferenceViolationException("Unable to inject mandatory reference '" + reference.getName() + "' for class " + targetClass.getName() + " : no matching services were found.");
             }
+            if (reference.isCardinalityMultiple()) {
+                // make sure at least empty array is set  
+                invokeBindUnbindMethod(reference, target, null, true);
+            }
         }
 
         // multiple references found? check if reference is not multiple
-        if (matchingServices.size() > 1
-                && (reference.getCardinality() == ReferenceCardinality.MANDATORY_UNARY || reference.getCardinality() == ReferenceCardinality.OPTIONAL_UNARY)) {
+        if (matchingServices.size() > 1 && !reference.isCardinalityMultiple()) {
             throw new ReferenceViolationException("Multiple matches found for unary reference '" + reference.getName() + "' for class "+ targetClass.getName());
         }
 
@@ -378,7 +378,7 @@ final class OsgiServiceUtil {
                     + "for reference '" + reference.getName() + "' for class " +  targetClass.getName());
         }
 
-        if (StringUtils.isNotEmpty(methodName)) {
+        if (StringUtils.isNotEmpty(methodName) && serviceInfo != null) {
             
             // 1. ServiceReference
             Method method = getMethod(targetClass, methodName, new Class<?>[] { ServiceReference.class });
@@ -414,9 +414,12 @@ final class OsgiServiceUtil {
                 switch (reference.getFieldCollectionType()) {
                     case SERVICE:
                     case REFERENCE:
-                        Object item = serviceInfo.getServiceInstance();
-                        if (reference.getFieldCollectionType() == FieldCollectionType.REFERENCE) {
-                            item = serviceInfo.getServiceReference();
+                        Object item = null;
+                        if (serviceInfo != null) {
+                            item = serviceInfo.getServiceInstance();
+                            if (reference.getFieldCollectionType() == FieldCollectionType.REFERENCE) {
+                                item = serviceInfo.getServiceReference();
+                            }
                         }
                         // 1. collection
                         Field field = getFieldWithAssignableType(targetClass, fieldName, Collection.class);
@@ -454,14 +457,14 @@ final class OsgiServiceUtil {
                 Class<?> interfaceType = reference.getInterfaceTypeAsClass();
                 Field field = getFieldWithAssignableType(targetClass, fieldName, interfaceType);
                 if (field != null) {
-                    setField(target, field, bind ? serviceInfo.getServiceInstance() : null);
+                    setField(target, field, bind && serviceInfo != null ? serviceInfo.getServiceInstance() : null);
                     return;
                 }
                 
                 // 2. ServiceReference
                 field = getField(targetClass, fieldName, ServiceReference.class);
                 if (field != null) {
-                    setField(target, field, bind ? serviceInfo.getServiceReference() : null);
+                    setField(target, field, bind && serviceInfo != null ? serviceInfo.getServiceReference() : null);
                     return;
                 }
             }
@@ -477,7 +480,9 @@ final class OsgiServiceUtil {
             if (collection == null) {
                 collection = new ArrayList<>();
             }
-            collection.add(item);
+            if (item != null) {
+                collection.add(item);
+            }
             field.set(target, collection);
             
         } catch (IllegalAccessException ex) {
@@ -495,9 +500,11 @@ final class OsgiServiceUtil {
             field.setAccessible(true);
             Collection<Object> collection = (Collection<Object>)field.get(target);
             if (collection == null) {
-                return;
+                collection = new ArrayList<>();
+            }
+            if (item != null) {
+                collection.remove(item);
             }
-            collection.remove(item);
             field.set(target, collection);
             
         } catch (IllegalAccessException ex) {
@@ -529,10 +536,10 @@ final class OsgiServiceUtil {
         invokeBindUnbindMethod(reference,  target, serviceInfo, false);
     }
     
-    private static List<ServiceInfo> getMatchingServices(Class<?> type, BundleContext bundleContext) {
+    private static List<ServiceInfo> getMatchingServices(Class<?> type, BundleContext bundleContext, String filter) {
         List<ServiceInfo> matchingServices = new ArrayList<ServiceInfo>();
         try {
-            ServiceReference[] references = bundleContext.getServiceReferences(type.getName(), null);
+            ServiceReference[] references = bundleContext.getServiceReferences(type.getName(), filter);
             if (references != null) {
                 for (ServiceReference<?> serviceReference : references) {
                     Object serviceInstance = bundleContext.getService(serviceReference);
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextDynamicReferncesOsgiR6Test.java b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextDynamicReferncesOsgiR6Test.java
index 0af80e9..b73bf5f 100644
--- a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextDynamicReferncesOsgiR6Test.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextDynamicReferncesOsgiR6Test.java
@@ -36,6 +36,7 @@ import org.mockito.runners.MockitoJUnitRunner;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
 
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 
 @RunWith(MockitoJUnitRunner.class)
@@ -138,6 +139,19 @@ public class MockBundleContextDynamicReferncesOsgiR6Test {
         assertDependencies2();
     }
     
+    @Test
+    public void testReferenceWithTargetFilter() {
+        assertDependencies3Filtered();
+        
+        bundleContext.registerService(ServiceInterface3.class.getName(), dependency3a, 
+                MapUtil.toDictionary(ImmutableMap.<String, Object>of("prop1", "abc")));
+
+        bundleContext.registerService(ServiceInterface3.class.getName(), dependency3b, 
+                MapUtil.toDictionary(ImmutableMap.<String, Object>of("prop1", "def")));
+        
+        assertDependencies3Filtered(dependency3a);
+    }
+    
     private void assertDependency1(ServiceInterface1 instance) {
         if (instance == null) {
             assertNull(service.getReference1());
@@ -166,4 +180,9 @@ public class MockBundleContextDynamicReferncesOsgiR6Test {
                 ImmutableSet.<ServiceSuperInterface3>copyOf(service.getReferences3()));
     }
     
+    private void assertDependencies3Filtered(ServiceSuperInterface3... instances) {
+        assertEquals(ImmutableSet.<ServiceSuperInterface3>copyOf(instances), 
+                ImmutableSet.<ServiceSuperInterface3>copyOf(service.getReferences3Filtered()));
+    }
+    
 }
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
index 9135bcf..8d086ad 100644
--- a/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
@@ -339,8 +339,9 @@ public class OsgiServiceUtilTest {
 
         private ServiceInterface1 reference1;
         private ServiceInterface1Optional reference1Optional;
-        private List<ServiceReference> references2 = new ArrayList<ServiceReference>();
-        private List<ServiceSuperInterface3> references3 = new ArrayList<ServiceSuperInterface3>();
+        private List<ServiceReference> references2;
+        private List<ServiceSuperInterface3> references3;
+        private List<ServiceSuperInterface3> references3Filtered;
 
         private ComponentContext componentContext;
         private Map<String, Object> config;
@@ -381,6 +382,10 @@ public class OsgiServiceUtilTest {
             return this.references3;
         }
 
+        public List<ServiceSuperInterface3> getReferences3Filtered() {
+            return this.references3Filtered;
+        }
+
         public ComponentContext getComponentContext() {
             return this.componentContext;
         }
diff --git a/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml b/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml
index 94602cc..d8d0095 100644
--- a/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml
+++ b/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml
@@ -50,6 +50,7 @@
     <reference name="reference1Optional" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface1Optional" cardinality="0..1" policy="dynamic" field="reference1Optional"/>
     <reference name="reference2" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface2" cardinality="1..n" policy="dynamic" field="references2" field-collection-type="reference"/>
     <reference name="reference3" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface3" cardinality="0..n" policy="dynamic" field="references3" field-collection-type="service"/>
+    <reference name="references3Filtered" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface3" cardinality="0..n" policy="dynamic" field="references3Filtered" field-collection-type="service" target="(prop1=abc)"/>
   </scr:component>
   <scr:component name="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service4_other_name">
     <implementation class="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service4"/>

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

[sling-org-apache-sling-testing-osgi-mock] 10/10: [maven-release-plugin] copy for tag org.apache.sling.testing.osgi-mock-2.0.0

Posted by ro...@apache.org.
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-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit 0b7c7605c99f2077820d034543a3fd4b04c52a37
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Thu Dec 10 21:53:28 2015 +0000

    [maven-release-plugin] copy for tag org.apache.sling.testing.osgi-mock-2.0.0
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/tags/org.apache.sling.testing.osgi-mock-2.0.0@1719221 13f79535-47bb-0310-9956-ffa450edef68

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

[sling-org-apache-sling-testing-osgi-mock] 07/10: SLING-5327 inline the required classes from org.apache.felix.scr

Posted by ro...@apache.org.
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-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit b29a918f1cffb489544dea11d8f92d1976112699
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Tue Nov 24 12:17:28 2015 +0000

    SLING-5327 inline the required classes from org.apache.felix.scr
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1716118 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index adf4ceb..575c118 100644
--- a/pom.xml
+++ b/pom.xml
@@ -87,11 +87,12 @@
             <version>5.0.0</version>
             <scope>compile</scope>
         </dependency>
+        <!-- The required classes o.a.f.scr.impl.helper.Annotations and o.a.f.scr.impl.helper.Coercions are inlined -->
         <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.scr</artifactId>
             <version>2.0.2</version>
-            <scope>compile</scope>
+            <scope>provided</scope>
         </dependency>
 
         <dependency>
@@ -141,6 +142,13 @@
                 <groupId>org.apache.felix</groupId>
                 <artifactId>maven-bundle-plugin</artifactId>
                 <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Embed-Dependency>
+                            org.apache.felix.scr;inline="org/apache/felix/scr/impl/helper/Annotations*|org/apache/felix/scr/impl/helper/Coercions*"
+                        </Embed-Dependency>
+                    </instructions>
+                </configuration>
             </plugin>
       
             <plugin>

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

[sling-org-apache-sling-testing-osgi-mock] 02/10: SLING-5324 osgi-mock: Support OSGi R6 field-based reference bindings

Posted by ro...@apache.org.
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-2.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git

commit 3f5a1729ed9dcc5b10affea05320b3f0f9d91612
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Mon Nov 23 23:45:50 2015 +0000

    SLING-5324 osgi-mock: Support OSGi R6 field-based reference bindings
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1715990 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/testing/mock/osgi/OsgiMetadataUtil.java  | 108 +++++++++++++
 .../sling/testing/mock/osgi/OsgiServiceUtil.java   | 173 +++++++++++++++++++--
 ...ockBundleContextDynamicReferncesOsgiR6Test.java | 169 ++++++++++++++++++++
 .../testing/mock/osgi/OsgiServiceUtilTest.java     |  56 +++++++
 ...sling.testing.mock.osgi.OsgiServiceUtilTest.xml |   8 +
 5 files changed, 500 insertions(+), 14 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
index 7c3db48..85db326 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
@@ -386,8 +386,11 @@ final class OsgiMetadataUtil {
         private final String interfaceType;
         private final ReferenceCardinality cardinality;
         private final ReferencePolicy policy;
+        private final ReferencePolicyOption policyOption;
         private final String bind;
         private final String unbind;
+        private final String field;
+        private final FieldCollectionType fieldCollectionType;
 
         private Reference(Class<?> clazz, Node node) {
             this.clazz = clazz;
@@ -395,8 +398,11 @@ final class OsgiMetadataUtil {
             this.interfaceType = getAttributeValue(node, "interface");
             this.cardinality = toCardinality(getAttributeValue(node, "cardinality"));
             this.policy = toPolicy(getAttributeValue(node, "policy"));
+            this.policyOption = toPolicyOption(getAttributeValue(node, "policy-option"));
             this.bind = getAttributeValue(node, "bind");
             this.unbind = getAttributeValue(node, "unbind");
+            this.field = getAttributeValue(node, "field");
+            this.fieldCollectionType = toFieldCollectionType(getAttributeValue(node, "field-collection-type"));
         }
 
         public Class<?> getServiceClass() {
@@ -411,14 +417,31 @@ final class OsgiMetadataUtil {
             return this.interfaceType;
         }
 
+        public Class getInterfaceTypeAsClass() {
+            try {
+                return Class.forName(getInterfaceType());
+            } catch (ClassNotFoundException e) {
+                throw new RuntimeException("Service reference type not found: " + getInterfaceType());
+            }
+        }
+
         public ReferenceCardinality getCardinality() {
             return this.cardinality;
         }
+        
+        public boolean isCardinalityMultiple() {
+            return this.cardinality == ReferenceCardinality.OPTIONAL_MULTIPLE
+                    || this.cardinality == ReferenceCardinality.MANDATORY_MULTIPLE;
+        }
 
         public ReferencePolicy getPolicy() {
             return policy;
         }
 
+        public ReferencePolicyOption getPolicyOption() {
+            return policyOption;
+        }
+
         public String getBind() {
             return this.bind;
         }
@@ -427,6 +450,14 @@ final class OsgiMetadataUtil {
             return this.unbind;
         }
 
+        public String getField() {
+            return this.field;
+        }
+        
+        public FieldCollectionType getFieldCollectionType() {
+            return this.fieldCollectionType;
+        }
+
         private static ReferenceCardinality toCardinality(String value) {
             for (ReferenceCardinality item : ReferenceCardinality.values()) {
                 if (StringUtils.equals(item.getCardinalityString(), value)) {
@@ -445,6 +476,24 @@ final class OsgiMetadataUtil {
             return ReferencePolicy.STATIC;
         }
 
+        private static ReferencePolicyOption toPolicyOption(String value) {
+            for (ReferencePolicyOption item : ReferencePolicyOption.values()) {
+                if (StringUtils.equalsIgnoreCase(item.name(), value)) {
+                    return item;
+                }
+            }
+            return ReferencePolicyOption.RELUCTANT;
+        }
+
+        private static FieldCollectionType toFieldCollectionType(String value) {
+            for (FieldCollectionType item : FieldCollectionType.values()) {
+                if (StringUtils.equalsIgnoreCase(item.name(), value)) {
+                    return item;
+                }
+            }
+            return null;
+        }
+
     }
 
 
@@ -513,4 +562,63 @@ final class OsgiMetadataUtil {
         DYNAMIC;
     }
 
+
+    /**
+     * Options for {@link Reference#policyOption()} property.
+     */
+    enum ReferencePolicyOption {
+
+        /**
+         * The reluctant policy option is the default policy option.
+         * When a new target service for a reference becomes available,
+         * references having the reluctant policy option for the static
+         * policy or the dynamic policy with a unary cardinality will
+         * ignore the new target service. References having the dynamic
+         * policy with a multiple cardinality will bind the new
+         * target service
+         */
+        RELUCTANT,
+
+        /**
+         * When a new target service for a reference becomes available,
+         * references having the greedy policy option will bind the new
+         * target service.
+         */
+        GREEDY;
+    }
+
+    /**
+     * Options for {@link Reference#policyOption()} property.
+     */
+    enum FieldCollectionType {
+
+        /**
+         * The bound service object. This is the default field collection type.
+         */
+        SERVICE,
+
+        /**
+         * A Service Reference for the bound service.
+         */
+        REFERENCE,
+
+        /**
+         * A Component Service Objects for the bound service.
+         */
+        SERVICEOBJECTS,
+
+        /**
+         * An unmodifiable Map containing the service properties of the bound service.
+         * This Map must implement Comparable.
+         */
+        PROPERTIES,
+
+        /**
+         * An unmodifiable Map.Entry whose key is an unmodifiable Map containing the 
+         * service properties of the bound service, as above, and whose value is the 
+         * bound service object. This Map.Entry must implement Comparable.
+         */
+        TUPLE;
+    }
+
 }
diff --git a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
index 84ccbf8..1f8eaa0 100644
--- a/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
+++ b/src/main/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtil.java
@@ -18,10 +18,12 @@
  */
 package org.apache.sling.testing.mock.osgi;
 
+import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -29,6 +31,7 @@ import java.util.SortedSet;
 
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.FieldCollectionType;
 import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.OsgiMetadata;
 import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.Reference;
 import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.ReferenceCardinality;
@@ -262,6 +265,49 @@ final class OsgiServiceUtil {
         }
     }
 
+    private static Field getField(Class clazz, String fieldName, Class<?> type) {
+        Field[] fields = clazz.getDeclaredFields();
+        for (Field field : fields) {
+            if (StringUtils.equals(field.getName(), fieldName) && field.getType().equals(type)) {
+                return field;
+            }
+        }
+        // not found? check super classes
+        Class<?> superClass = clazz.getSuperclass();
+        if (superClass != null && superClass != Object.class) {
+            return getField(superClass, fieldName, type);
+        }
+        return null;
+    }
+    
+    private static Field getFieldWithAssignableType(Class clazz, String fieldName, Class<?> type) {
+        Field[] fields = clazz.getDeclaredFields();
+        for (Field field : fields) {
+            if (StringUtils.equals(field.getName(), fieldName) && field.getType().isAssignableFrom(type)) {
+                return field;
+            }
+        }
+        // not found? check super classes
+        Class<?> superClass = clazz.getSuperclass();
+        if (superClass != null && superClass != Object.class) {
+            return getFieldWithAssignableType(superClass, fieldName, type);
+        }
+        return null;
+    }
+    
+    private static void setField(Object target, Field field, Object value) {
+        try {
+            field.setAccessible(true);
+            field.set(target, value);
+        } catch (IllegalAccessException ex) {
+            throw new RuntimeException("Unable to set field '" + field.getName() + "' for class "
+                    + target.getClass().getName(), ex);
+        } catch (IllegalArgumentException ex) {
+            throw new RuntimeException("Unable to set field '" + field.getName() + "' for class "
+                    + target.getClass().getName(), ex);
+        }
+    }
+    
     /**
      * Simulate OSGi service dependency injection. Injects direct references and
      * multiple references.
@@ -294,12 +340,7 @@ final class OsgiServiceUtil {
         Class<?> targetClass = target.getClass();
 
         // get reference type
-        Class<?> type;
-        try {
-            type = Class.forName(reference.getInterfaceType());
-        } catch (ClassNotFoundException ex) {
-            throw new RuntimeException("Unable to instantiate reference type: " + reference.getInterfaceType(), ex);
-        }
+        Class<?> type = reference.getInterfaceTypeAsClass();
 
         // get matching service references
         List<ServiceInfo> matchingServices = getMatchingServices(type, bundleContext);
@@ -330,6 +371,13 @@ final class OsgiServiceUtil {
 
         // try to invoke bind method
         String methodName = bind ? reference.getBind() : reference.getUnbind();
+        String fieldName = reference.getField();
+        
+        if (StringUtils.isEmpty(methodName) && StringUtils.isEmpty(fieldName)) {
+            throw new RuntimeException("No bind/unbind method name or file name defined "
+                    + "for reference '" + reference.getName() + "' for class " +  targetClass.getName());
+        }
+
         if (StringUtils.isNotEmpty(methodName)) {
             
             // 1. ServiceReference
@@ -340,12 +388,7 @@ final class OsgiServiceUtil {
             }
             
             // 2. assignable from service instance
-            Class<?> interfaceType;
-            try {
-                interfaceType = Class.forName(reference.getInterfaceType());
-            } catch (ClassNotFoundException e) {
-                throw new RuntimeException("Service reference type not found: " + reference.getInterfaceType());
-            }
+            Class<?> interfaceType = reference.getInterfaceTypeAsClass();
             method = getMethodWithAssignableTypes(targetClass, methodName, new Class<?>[] { interfaceType });
             if (method != null) {
                 invokeMethod(target, method, new Object[] { serviceInfo.getServiceInstance() });
@@ -358,10 +401,112 @@ final class OsgiServiceUtil {
                 invokeMethod(target, method, new Object[] { serviceInfo.getServiceInstance(), serviceInfo.getServiceConfig() });
                 return;
             }
+        
+            throw new RuntimeException((bind ? "Bind" : "Unbind") + " method with name " + methodName + " not found "
+                    + "for reference '" + reference.getName() + "' for class " +  targetClass.getName());
+        }
+        
+        // in OSGi declarative services 1.3 there are no bind/unbind methods - modify the field directly
+        else if (StringUtils.isNotEmpty(fieldName)) {
+            
+            // check for field with list/collection reference
+            if (reference.isCardinalityMultiple()) {
+                switch (reference.getFieldCollectionType()) {
+                    case SERVICE:
+                    case REFERENCE:
+                        Object item = serviceInfo.getServiceInstance();
+                        if (reference.getFieldCollectionType() == FieldCollectionType.REFERENCE) {
+                            item = serviceInfo.getServiceReference();
+                        }
+                        // 1. collection
+                        Field field = getFieldWithAssignableType(targetClass, fieldName, Collection.class);
+                        if (field != null) {
+                            if (bind) {
+                                addToCollection(target, field, item);
+                            }
+                            else {
+                                removeFromCollection(target, field, item);
+                            }
+                            return;
+                        }
+                        
+                        // 2. list
+                        field = getField(targetClass, fieldName, List.class);
+                        if (field != null) {
+                            if (bind) {
+                                addToCollection(target, field, item);
+                            }
+                            else {
+                                removeFromCollection(target, field, item);
+                            }
+                            return;
+                        }
+                        break;
+                    default:
+                        throw new RuntimeException("Field collection type '" + reference.getFieldCollectionType() + "' not supported "
+                                + "for reference '" + reference.getName() + "' for class " +  targetClass.getName());
+                }
+            }
+            
+            // check for single field reference
+            else {
+                // 1. assignable from service instance
+                Class<?> interfaceType = reference.getInterfaceTypeAsClass();
+                Field field = getFieldWithAssignableType(targetClass, fieldName, interfaceType);
+                if (field != null) {
+                    setField(target, field, bind ? serviceInfo.getServiceInstance() : null);
+                    return;
+                }
+                
+                // 2. ServiceReference
+                field = getField(targetClass, fieldName, ServiceReference.class);
+                if (field != null) {
+                    setField(target, field, bind ? serviceInfo.getServiceReference() : null);
+                    return;
+                }
+            }
         }
 
-        throw new RuntimeException((bind ? "Bind" : "Unbind") + " method with name " + methodName + " not found "
-                + "for reference '" + reference.getName() + "' for class " +  targetClass.getName());
+    }
+    
+    @SuppressWarnings("unchecked")
+    private static void addToCollection(Object target, Field field, Object item) {
+        try {
+            field.setAccessible(true);
+            Collection<Object> collection = (Collection<Object>)field.get(target);
+            if (collection == null) {
+                collection = new ArrayList<>();
+            }
+            collection.add(item);
+            field.set(target, collection);
+            
+        } catch (IllegalAccessException ex) {
+            throw new RuntimeException("Unable to set field '" + field.getName() + "' for class "
+                    + target.getClass().getName(), ex);
+        } catch (IllegalArgumentException ex) {
+            throw new RuntimeException("Unable to set field '" + field.getName() + "' for class "
+                    + target.getClass().getName(), ex);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    private static void removeFromCollection(Object target, Field field, Object item) {
+        try {
+            field.setAccessible(true);
+            Collection<Object> collection = (Collection<Object>)field.get(target);
+            if (collection == null) {
+                return;
+            }
+            collection.remove(item);
+            field.set(target, collection);
+            
+        } catch (IllegalAccessException ex) {
+            throw new RuntimeException("Unable to set field '" + field.getName() + "' for class "
+                    + target.getClass().getName(), ex);
+        } catch (IllegalArgumentException ex) {
+            throw new RuntimeException("Unable to set field '" + field.getName() + "' for class "
+                    + target.getClass().getName(), ex);
+        }
     }
 
     /**
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextDynamicReferncesOsgiR6Test.java b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextDynamicReferncesOsgiR6Test.java
new file mode 100644
index 0000000..0af80e9
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextDynamicReferncesOsgiR6Test.java
@@ -0,0 +1,169 @@
+/*
+ * 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 static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+import org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.Service3OsgiR6;
+import org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface1;
+import org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface1Optional;
+import org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface2;
+import org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface3;
+import org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceSuperInterface3;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+
+import com.google.common.collect.ImmutableSet;
+
+@RunWith(MockitoJUnitRunner.class)
+public class MockBundleContextDynamicReferncesOsgiR6Test {
+
+    private BundleContext bundleContext;
+    private Service3OsgiR6 service;
+    private ServiceRegistration reg1a;
+    private ServiceRegistration reg2a;
+    
+    @Mock
+    private ServiceInterface1 dependency1a;
+    @Mock
+    private ServiceInterface1 dependency1b;
+    @Mock
+    private ServiceInterface1Optional dependency1aOptional;
+    @Mock
+    private ServiceInterface1Optional dependency1bOptional;
+    @Mock
+    private ServiceInterface2 dependency2a;
+    @Mock
+    private ServiceInterface2 dependency2b;
+    @Mock
+    private ServiceSuperInterface3 dependency3a;
+    @Mock
+    private ServiceSuperInterface3 dependency3b;
+
+    @Before
+    public void setUp() {
+        bundleContext = MockOsgi.newBundleContext();
+        
+        // setup service instance with only minimum mandatory references
+        reg1a = bundleContext.registerService(ServiceInterface1.class.getName(), dependency1a, null);
+        reg2a = bundleContext.registerService(ServiceInterface2.class.getName(), dependency2a, null);
+        
+        service = new Service3OsgiR6();
+        MockOsgi.injectServices(service, bundleContext);
+        MockOsgi.activate(service, bundleContext);
+        bundleContext.registerService(Service3OsgiR6.class.getName(), service, null);
+        
+        assertDependency1(dependency1a);
+        assertDependency1Optional(null);
+        assertDependencies2(dependency2a);
+        assertDependencies3();
+    }
+
+    @Test
+    public void testAddRemoveOptionalUnaryService() {
+        ServiceRegistration reg1aOptional = bundleContext.registerService(ServiceInterface1Optional.class.getName(), dependency1aOptional, null);
+        assertDependency1Optional(dependency1aOptional);
+        
+        reg1aOptional.unregister();
+        assertDependency1Optional(null);
+    }
+    
+    public void testAddOptionalUnaryService_TooMany() {
+        bundleContext.registerService(ServiceInterface1Optional.class.getName(), dependency1aOptional, null);
+        assertDependency1Optional(dependency1aOptional);
+        
+        // in real OSGi this should fail - but this is not covered by the current implementation. so test the real implementation here.
+        bundleContext.registerService(ServiceInterface1Optional.class.getName(), dependency1bOptional, null);
+        assertDependency1Optional(dependency1bOptional);
+    }
+    
+    @Test(expected = ReferenceViolationException.class)
+    public void testAddMandatoryUnaryService_TooMany() {
+        bundleContext.registerService(ServiceInterface1.class.getName(), dependency1b, null);
+    }
+    
+    @Test(expected = ReferenceViolationException.class)
+    public void testRemoveMandatoryUnaryService_TooMany() {
+        reg1a.unregister();
+    }
+    
+    @Test
+    public void testAddRemoveOptionalMultipleService() {
+        ServiceRegistration reg3a = bundleContext.registerService(ServiceInterface3.class.getName(), dependency3a, null);
+        assertDependencies3(dependency3a);
+
+        ServiceRegistration reg3b = bundleContext.registerService(ServiceInterface3.class.getName(), dependency3b, null);
+        assertDependencies3(dependency3a, dependency3b);
+
+        reg3a.unregister();
+        assertDependencies3(dependency3b);
+        
+        reg3b.unregister();
+        assertDependencies3();
+    }
+    
+    @Test
+    public void testAddRemoveMandatoryMultipleService() {
+        ServiceRegistration reg2b = bundleContext.registerService(ServiceInterface2.class.getName(), dependency2b, null);
+        assertDependencies2(dependency2a, dependency2b);
+
+        reg2b.unregister();
+        assertDependencies2(dependency2a);
+        
+        // in real OSGi this should fail - but this is not covered by the current implementation. so test the real implementation here.
+        reg2a.unregister();
+        assertDependencies2();
+    }
+    
+    private void assertDependency1(ServiceInterface1 instance) {
+        if (instance == null) {
+            assertNull(service.getReference1());
+        }
+        else {
+            assertSame(instance, service.getReference1());
+        }
+    }
+    
+    private void assertDependency1Optional(ServiceInterface1Optional instance) {
+        if (instance == null) {
+            assertNull(service.getReference1Optional());
+        }
+        else {
+            assertSame(instance, service.getReference1Optional());
+        }
+    }
+    
+    private void assertDependencies2(ServiceInterface2... instances) {
+        assertEquals(ImmutableSet.<ServiceInterface2>copyOf(instances), 
+                ImmutableSet.<ServiceInterface2>copyOf(service.getReferences2()));
+    }
+    
+    private void assertDependencies3(ServiceSuperInterface3... instances) {
+        assertEquals(ImmutableSet.<ServiceSuperInterface3>copyOf(instances), 
+                ImmutableSet.<ServiceSuperInterface3>copyOf(service.getReferences3()));
+    }
+    
+}
diff --git a/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java b/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
index 170957f..9135bcf 100644
--- a/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/osgi/OsgiServiceUtilTest.java
@@ -335,6 +335,62 @@ public class OsgiServiceUtilTest {
 
     }
 
+    public static class Service3OsgiR6 {
+
+        private ServiceInterface1 reference1;
+        private ServiceInterface1Optional reference1Optional;
+        private List<ServiceReference> references2 = new ArrayList<ServiceReference>();
+        private List<ServiceSuperInterface3> references3 = new ArrayList<ServiceSuperInterface3>();
+
+        private ComponentContext componentContext;
+        private Map<String, Object> config;
+
+        @Activate
+        private void activate(ComponentContext ctx) {
+            this.componentContext = ctx;
+            this.config = MapUtil.toMap(ctx.getProperties());
+        }
+
+        @Deactivate
+        private void deactivate(ComponentContext ctx) {
+            this.componentContext = null;
+        }
+
+        @Modified
+        private void modified(Map<String,Object> newConfig) {
+            this.config = newConfig;
+        }
+
+        public ServiceInterface1 getReference1() {
+            return this.reference1;
+        }
+
+        public ServiceInterface1Optional getReference1Optional() {
+            return this.reference1Optional;
+        }
+
+        public List<ServiceInterface2> getReferences2() {
+            List<ServiceInterface2> services = new ArrayList<ServiceInterface2>();
+            for (ServiceReference<?> serviceReference : references2) {
+                services.add((ServiceInterface2)componentContext.getBundleContext().getService(serviceReference));
+            }
+            return services;
+        }
+
+        public List<ServiceSuperInterface3> getReferences3() {
+            return this.references3;
+        }
+
+        public ComponentContext getComponentContext() {
+            return this.componentContext;
+        }
+
+        public Map<String, Object> getConfig() {
+            return config;
+        }
+
+    }
+
     @Component
     @Reference(referenceInterface = ServiceInterface1.class, name = "customName", bind = "customBind", unbind = "customUnbind")
     public static class Service4 {
diff --git a/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml b/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml
index a9d3623..94602cc 100644
--- a/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml
+++ b/src/test/resources/OSGI-INF/org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.xml
@@ -43,6 +43,14 @@
     <reference name="reference2" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface2" cardinality="1..n" policy="dynamic" bind="bindReference2" unbind="unbindReference2"/>
     <reference name="reference3" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface3" cardinality="0..n" policy="dynamic" bind="bindReference3" unbind="unbindReference3"/>
   </scr:component>
+  <scr:component name="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service3OsgiR6" activate="activate" deactivate="deactivate" modified="modified">
+    <implementation class="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service3OsgiR6"/>
+    <property name="service.pid" value="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service3OsgiR6"/>
+    <reference name="reference1" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface1" cardinality="1..1" policy="dynamic" field="reference1"/>
+    <reference name="reference1Optional" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface1Optional" cardinality="0..1" policy="dynamic" field="reference1Optional"/>
+    <reference name="reference2" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface2" cardinality="1..n" policy="dynamic" field="references2" field-collection-type="reference"/>
+    <reference name="reference3" interface="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$ServiceInterface3" cardinality="0..n" policy="dynamic" field="references3" field-collection-type="service"/>
+  </scr:component>
   <scr:component name="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service4_other_name">
     <implementation class="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service4"/>
     <property name="service.pid" value="org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest$Service4"/>

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