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

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

commit 50112aaddc782b1710a018d177e455007f3532fa
Author: Stefan Seifert <[email protected]>
AuthorDate: Wed Nov 26 22:17:39 2014 +0000

    SLING-4202 OSGi Mock: Fail-fast when mandatory references cannot be injected
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/osgi-mock@1641954 
13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/sling/testing/mock/osgi/MockOsgi.java   |  2 +-
 .../testing/mock/osgi/ReflectionServiceUtil.java   | 35 +++++++++-------------
 .../mock/osgi/context/OsgiContextImplTest.java     | 10 +++++++
 3 files changed, 25 insertions(+), 22 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 d6e1b71..82448e4 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
@@ -108,7 +108,7 @@ public final class MockOsgi {
      * is thrown.
      * @param target Service instance
      * @param bundleContext Bundle context from which services are fetched to 
inject.
-     * @return true if all dependencies could be injected
+     * @return true if all dependencies could be injected, false if the 
service has no dependencies.
      */
     public static boolean injectServices(Object target, BundleContext 
bundleContext) {
         return ReflectionServiceUtil.injectServices(target, bundleContext);
diff --git 
a/src/main/java/org/apache/sling/testing/mock/osgi/ReflectionServiceUtil.java 
b/src/main/java/org/apache/sling/testing/mock/osgi/ReflectionServiceUtil.java
index 4056ce4..77d5e08 100644
--- 
a/src/main/java/org/apache/sling/testing/mock/osgi/ReflectionServiceUtil.java
+++ 
b/src/main/java/org/apache/sling/testing/mock/osgi/ReflectionServiceUtil.java
@@ -34,8 +34,6 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 
 /**
@@ -43,8 +41,6 @@ import org.w3c.dom.Document;
  */
 final class ReflectionServiceUtil {
 
-    private static final Logger log = 
LoggerFactory.getLogger(ReflectionServiceUtil.class);
-
     private ReflectionServiceUtil() {
         // static methods only
     }
@@ -261,7 +257,7 @@ final class ReflectionServiceUtil {
      * multiple references.
      * @param target Service instance
      * @param bundleContext Bundle context from which services are fetched to 
inject.
-     * @return true if all dependencies could be injected
+     * @return true if all dependencies could be injected, false if the 
service has no dependencies.
      */
     public static boolean injectServices(Object target, BundleContext 
bundleContext) {
 
@@ -273,17 +269,18 @@ final class ReflectionServiceUtil {
             throw new NoScrMetadataException(targetClass);
         }
         List<Reference> references = 
OsgiMetadataUtil.getReferences(targetClass, metadata);
+        if (references.isEmpty()) {
+            return false;
+        }
 
         // try to inject services
-        boolean allInjected = true;
         for (Reference reference : references) {
-            boolean injectSuccess = injectServiceReference(reference, target, 
bundleContext);
-            allInjected = allInjected && injectSuccess;
+            injectServiceReference(reference, target, bundleContext);
         }
-        return allInjected;
+        return true;
     }
 
-    private static boolean injectServiceReference(Reference reference, Object 
target, BundleContext bundleContext) {
+    private static void injectServiceReference(Reference reference, Object 
target, BundleContext bundleContext) {
         Class<?> targetClass = target.getClass();
 
         // get reference type
@@ -302,18 +299,14 @@ final class ReflectionServiceUtil {
             boolean isOptional = (reference.getCardinality() == 
ReferenceCardinality.OPTIONAL_UNARY || reference
                     .getCardinality() == 
ReferenceCardinality.OPTIONAL_MULTIPLE);
             if (!isOptional) {
-                log.warn("Unable to inject mandatory reference '{}' for class 
{}", reference.getName(),
-                        targetClass.getName());
+                throw new RuntimeException("Unable to inject mandatory 
reference '" + reference.getName() + "' for class " + targetClass.getName());
             }
-            return isOptional;
         }
 
         // multiple references found? check if reference is not multiple
         if (matchingServices.size() > 1
                 && (reference.getCardinality() == 
ReferenceCardinality.MANDATORY_UNARY || reference.getCardinality() == 
ReferenceCardinality.OPTIONAL_UNARY)) {
-            log.warn("Multiple matches found for unary reference '{}' for 
class {}", reference.getName(),
-                    targetClass.getName());
-            return false;
+            throw new RuntimeException("Multiple matches found for unary 
reference '" + reference.getName() + "' for class "+ targetClass.getName());
         }
 
         // try to invoke bind method
@@ -326,7 +319,7 @@ final class ReflectionServiceUtil {
                 for (ServiceInfo matchingService : matchingServices) {
                     invokeMethod(target, bindMethod, new Object[] { 
matchingService.getServiceReference() });
                 }
-                return true;
+                return;
             }
             
             // 2. assignable from service instance
@@ -341,7 +334,7 @@ final class ReflectionServiceUtil {
                 for (ServiceInfo matchingService : matchingServices) {
                     invokeMethod(target, bindMethod, new Object[] { 
matchingService.getServiceInstance() });
                 }
-                return true;
+                return;
             }
             
             // 3. assignable from service instance plus map
@@ -350,12 +343,12 @@ final class ReflectionServiceUtil {
                 for (ServiceInfo matchingService : matchingServices) {
                     invokeMethod(target, bindMethod, new Object[] { 
matchingService.getServiceInstance(), matchingService.getServiceConfig() });
                 }
-                return true;
+                return;
             }
         }
 
-        log.warn("Bind method not found for reference '{}' for class {}", 
reference.getName(), targetClass.getName());
-        return false;
+        throw new RuntimeException("Bind method with name " + bindMethodName + 
" not found "
+                + "for reference '" + reference.getName() + "' for class {}" + 
 targetClass.getName());
     }
 
     private static List<ServiceInfo> getMatchingServices(Class<?> type, 
BundleContext bundleContext) {
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 19c5a5b..35eecbb 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
@@ -21,6 +21,7 @@ package org.apache.sling.testing.mock.osgi.context;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
+import static org.mockito.Mockito.mock;
 
 import java.util.HashMap;
 import java.util.HashSet;
@@ -29,6 +30,8 @@ import java.util.Set;
 
 import org.apache.sling.testing.mock.osgi.NoScrMetadataException;
 import org.apache.sling.testing.mock.osgi.ReflectionServiceUtilTest;
+import 
org.apache.sling.testing.mock.osgi.ReflectionServiceUtilTest.ServiceInterface1;
+import 
org.apache.sling.testing.mock.osgi.ReflectionServiceUtilTest.ServiceInterface2;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -92,6 +95,13 @@ public class OsgiContextImplTest {
 
     @Test
     public void testRegisterInjectActivate() {
+        context.registerService(ServiceInterface1.class, 
mock(ServiceInterface1.class));
+        context.registerService(ServiceInterface2.class, 
mock(ServiceInterface2.class));
+        context.registerInjectActivateService(new 
ReflectionServiceUtilTest.Service3());
+    }
+
+    @Test(expected=RuntimeException.class)
+    public void testRegisterInjectActivate_RefrenceMissing() {
         context.registerInjectActivateService(new 
ReflectionServiceUtilTest.Service3());
     }
 

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to