You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2011/09/12 14:23:42 UTC

svn commit: r1169708 - in /sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit: annotations/SlingAnnotationsTestRunner.java impl/AnnotationsProcessor.java

Author: bdelacretaz
Date: Mon Sep 12 12:23:42 2011
New Revision: 1169708

URL: http://svn.apache.org/viewvc?rev=1169708&view=rev
Log:
SLING-2219 - use dynamic reference to AnnotationsProcessor

Modified:
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/annotations/SlingAnnotationsTestRunner.java
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/AnnotationsProcessor.java

Modified: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/annotations/SlingAnnotationsTestRunner.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/annotations/SlingAnnotationsTestRunner.java?rev=1169708&r1=1169707&r2=1169708&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/annotations/SlingAnnotationsTestRunner.java (original)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/annotations/SlingAnnotationsTestRunner.java Mon Sep 12 12:23:42 2011
@@ -33,8 +33,6 @@ import org.slf4j.LoggerFactory;
 public class SlingAnnotationsTestRunner extends BlockJUnit4ClassRunner {
     private static final Logger log = LoggerFactory.getLogger(SlingAnnotationsTestRunner.class);
 
-    private static TestObjectProcessor testObjectProcessor;  
-    
     public SlingAnnotationsTestRunner(Class<?> clazz) throws InitializationError {
         super(clazz);
     }
@@ -42,19 +40,15 @@ public class SlingAnnotationsTestRunner 
     @Override
     protected Object createTest() throws Exception {
         final BundleContext ctx = Activator.getBundleContext();
-        if(testObjectProcessor == null && ctx != null) {
-            final ServiceReference ref = ctx.getServiceReference(TestObjectProcessor.class.getName());
-            if(ref != null) {
-                testObjectProcessor = (TestObjectProcessor)ctx.getService(ref);
-            }
-            log.info("Got TestObjectProcessor {}", testObjectProcessor);
-        }
+        final ServiceReference ref = ctx.getServiceReference(TestObjectProcessor.class.getName());
+        final TestObjectProcessor top = ref == null ? null : (TestObjectProcessor)ctx.getService(ref);
 
-        if(testObjectProcessor == null) {
+        if(top == null) {
             log.info("No TestObjectProcessor service available, annotations will not be processed");
             return super.createTest();
         } else { 
-            return testObjectProcessor.process(super.createTest());
+            log.debug("Using TestObjectProcessor {}", top);
+            return top.process(super.createTest());
         }
     }
 }

Modified: sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/AnnotationsProcessor.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/AnnotationsProcessor.java?rev=1169708&r1=1169707&r2=1169708&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/AnnotationsProcessor.java (original)
+++ sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/impl/AnnotationsProcessor.java Mon Sep 12 12:23:42 2011
@@ -29,7 +29,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /** Processor for annotations in test classes */
-@Component
+@Component(immediate=true)
 @Service
 public class AnnotationsProcessor implements TestObjectProcessor {
     private Logger log = LoggerFactory.getLogger(getClass());
@@ -40,14 +40,17 @@ public class AnnotationsProcessor implem
         if(bundleContext == null) {
             throw new IllegalArgumentException("Null BundleContext in activate()");
         }
+        log.debug("{} activated, BundleContext={}", this, bundleContext);
     }
     
     protected void deactivate(ComponentContext ctx) {
         bundleContext = null;
+        log.debug("{} deactivated", this);
     }
     
     /** Process annotations on the test object */
     public Object process(Object testObject) throws Exception {
+        log.debug("processing {}", testObject);
         for(Field f : testObject.getClass().getDeclaredFields()) {
             if(f.isAnnotationPresent(TestReference.class)) {
                 processTestReference(testObject, f);
@@ -59,7 +62,9 @@ public class AnnotationsProcessor implem
     /** Process the TestReference annotation to inject services into fields */
     private void processTestReference(Object testObject, Field f) throws Exception {
         if(bundleContext == null) {
-            throw new IllegalArgumentException("Null BundleContext in processTestReference(), not activated?");
+            final String msg = "Null BundleContext in processTestReference(), not activated?";
+            log.error(msg);
+            throw new IllegalArgumentException(msg);
         }
         
         final Class<?> serviceType = f.getType();