incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1065993 - in /sling/whiteboard/bdelacretaz/junit: extension/src/main/java/org/apache/sling/extensions/junit/ extension/src/main/java/org/apache/sling/extensions/junit/impl/ testbundle/ testbundle/src/main/java/org/apache/sling/extensions/j...
Date Tue, 01 Feb 2011 11:14:23 GMT
Author: bdelacretaz
Date: Tue Feb  1 11:14:22 2011
New Revision: 1065993

URL: http://svn.apache.org/viewvc?rev=1065993&view=rev
Log:
SLING-1963 - TestReference annotation support

Added:
    sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/AutoDetect.java
  (with props)
    sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/TestObjectProcessor.java
  (with props)
    sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/TestReference.java
  (with props)
    sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/impl/AnnotationsProcessor.java
  (with props)
    sling/whiteboard/bdelacretaz/junit/testbundle/src/main/java/org/apache/sling/extensions/junit/testbundle/tests/OsgiAwareTest.java
  (with props)
Modified:
    sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/Activator.java
    sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/SlingAnnotationsTestRunner.java
    sling/whiteboard/bdelacretaz/junit/testbundle/pom.xml
    sling/whiteboard/bdelacretaz/junit/testbundle/src/main/java/org/apache/sling/extensions/junit/testbundle/tests/JUnit4Test.java

Modified: sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/Activator.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/Activator.java?rev=1065993&r1=1065992&r2=1065993&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/Activator.java
(original)
+++ sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/Activator.java
Tue Feb  1 11:14:22 2011
@@ -22,10 +22,10 @@ import org.osgi.framework.BundleContext;
 public class Activator implements BundleActivator {
 
     public void start(BundleContext context) throws Exception {
-        SlingAnnotationsTestRunner.bundleContext = context;
+        SlingAnnotationsTestRunner.setBundleContext(context);
     }
 
     public void stop(BundleContext context) throws Exception {
-        SlingAnnotationsTestRunner.bundleContext = null;
+        SlingAnnotationsTestRunner.setBundleContext(null);
     }
 }

Added: sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/AutoDetect.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/AutoDetect.java?rev=1065993&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/AutoDetect.java
(added)
+++ sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/AutoDetect.java
Tue Feb  1 11:14:22 2011
@@ -0,0 +1,28 @@
+/*
+ * 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.extensions.junit;
+
+/**
+ * Placeholder class for default value of annotation 
+ * properties with "auto detect" capabilities. As done
+ * in the Felix SCR plugin.
+ */
+public class AutoDetect {
+    private AutoDetect() {
+        // disallows instancing this class
+    }
+}

Propchange: sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/AutoDetect.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/AutoDetect.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/SlingAnnotationsTestRunner.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/SlingAnnotationsTestRunner.java?rev=1065993&r1=1065992&r2=1065993&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/SlingAnnotationsTestRunner.java
(original)
+++ sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/SlingAnnotationsTestRunner.java
Tue Feb  1 11:14:22 2011
@@ -19,24 +19,43 @@ package org.apache.sling.extensions.juni
 import org.junit.runners.BlockJUnit4ClassRunner;
 import org.junit.runners.model.InitializationError;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+/** TestRunner which uses a TestObjectProcessor to 
+ *  handle annotations in test classes.
+ *  A test that has RunWith=SlingAnnotationsTestRunner can
+ *  use @TestReference, for example, to access OSGi services.
+ */
 public class SlingAnnotationsTestRunner extends BlockJUnit4ClassRunner {
-    private final Logger log = LoggerFactory.getLogger(getClass());
-    
-    // TODO better way to inject this?
-    static BundleContext bundleContext;
+    private static final Logger log = LoggerFactory.getLogger(SlingAnnotationsTestRunner.class);
+
+    private static BundleContext bundleContext;
+    private static TestObjectProcessor testObjectProcessor;  
     
     public SlingAnnotationsTestRunner(Class<?> clazz) throws InitializationError {
         super(clazz);
     }
     
+    static void setBundleContext(BundleContext ctx) {
+        bundleContext = ctx;
+        testObjectProcessor = null;
+    }
+    
     @Override
     protected Object createTest() throws Exception {
-        final Object result = super.createTest();
-        log.info("TODO handle annotations for {}, BundleContext={}", 
-                result.getClass().getName(), bundleContext);
-        return super.createTest();
+        if(testObjectProcessor == null && bundleContext != null) {
+            final ServiceReference ref = bundleContext.getServiceReference(TestObjectProcessor.class.getName());
+            if(ref != null) {
+                testObjectProcessor = (TestObjectProcessor)bundleContext.getService(ref);
+            }
+            log.info("Got TestObjectProcessor {}", testObjectProcessor);
+        }
+
+        if(testObjectProcessor == null) {
+            throw new IllegalStateException("No TestObjectProcessor service available");
+        }
+        return testObjectProcessor.process(super.createTest());
     }
 }

Added: sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/TestObjectProcessor.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/TestObjectProcessor.java?rev=1065993&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/TestObjectProcessor.java
(added)
+++ sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/TestObjectProcessor.java
Tue Feb  1 11:14:22 2011
@@ -0,0 +1,22 @@
+/*
+ * 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.extensions.junit;
+
+/** Process test objects, to handle annotations, etc. */
+public interface TestObjectProcessor {
+    public Object process(Object testObject) throws Exception;
+}

Propchange: sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/TestObjectProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/TestObjectProcessor.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/TestReference.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/TestReference.java?rev=1065993&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/TestReference.java
(added)
+++ sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/TestReference.java
Tue Feb  1 11:14:22 2011
@@ -0,0 +1,45 @@
+/*
+ * 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.extensions.junit;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/** Annotation used to inject services in test classes. Similar
+ *  to the Felix @Reference annotation, but we need RetentionPolicy.RUNTIME
+ *  so we cannot use that one. 
+ */
+@Target( { ElementType.FIELD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface TestReference {
+    /**
+     * The local name of the reference.
+     * Default value is the name of the field to
+     * which the annotation applies.
+     */
+    String name() default "";
+
+    /**
+     * The name of the service interface. This name is used by the Service
+     * Component Runtime to access the service on behalf of the component. 
+     * The default value for is the type of the field to which
+     * the annotation applies.
+     */
+    Class<?> referenceInterface() default AutoDetect.class;
+}

Propchange: sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/TestReference.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/TestReference.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/impl/AnnotationsProcessor.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/impl/AnnotationsProcessor.java?rev=1065993&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/impl/AnnotationsProcessor.java
(added)
+++ sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/impl/AnnotationsProcessor.java
Tue Feb  1 11:14:22 2011
@@ -0,0 +1,86 @@
+/*
+ * 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.extensions.junit.impl;
+
+import java.lang.reflect.Field;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.extensions.junit.TestObjectProcessor;
+import org.apache.sling.extensions.junit.TestReference;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Processor for annotations in test classes */
+@Component
+@Service
+public class AnnotationsProcessor implements TestObjectProcessor {
+    private Logger log = LoggerFactory.getLogger(getClass());
+    private BundleContext bundleContext;
+    
+    protected void activate(ComponentContext ctx) {
+        bundleContext = ctx.getBundleContext();
+    }
+    
+    protected void deactivate(ComponentContext ctx) {
+        bundleContext = null;
+    }
+    
+    /** Process annotations on the test object */
+    public Object process(Object testObject) throws Exception {
+        for(Field f : testObject.getClass().getDeclaredFields()) {
+            if(f.isAnnotationPresent(TestReference.class)) {
+                processTestReference(testObject, f);
+            }
+        }
+        return testObject;
+    }
+    
+    /** Process the TestReference annotation to inject services into fields */
+    private void processTestReference(Object testObject, Field f) throws Exception {
+        final Class<?> serviceType = f.getType();
+        final Object service = getService(serviceType);
+        if(service != null) {
+            f.setAccessible(true);
+            f.set(testObject, service);
+            log.debug("Injected service {} into field {}", 
+                    serviceType.getName(), f.getName());
+        } else {
+            log.warn("Service {} not found for field {}", 
+                    serviceType.getName(), f.getName());
+        }
+    }
+    
+    private Object getService(Class<?> c) {
+        Object result = null;
+        if(bundleContext != null) {
+            // BundleContext is not a service, but can be injected
+            if(c.equals(BundleContext.class)) {
+                result = bundleContext;
+            } else {
+                ServiceReference ref = bundleContext.getServiceReference(c.getName());
+                if(ref != null) {
+                    result = bundleContext.getService(ref);
+                }
+            }
+        }
+        return result;
+    }
+}

Propchange: sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/impl/AnnotationsProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/bdelacretaz/junit/extension/src/main/java/org/apache/sling/extensions/junit/impl/AnnotationsProcessor.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/whiteboard/bdelacretaz/junit/testbundle/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/testbundle/pom.xml?rev=1065993&r1=1065992&r2=1065993&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/testbundle/pom.xml (original)
+++ sling/whiteboard/bdelacretaz/junit/testbundle/pom.xml Tue Feb  1 11:14:22 2011
@@ -75,6 +75,12 @@
             <artifactId>org.osgi.compendium</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+            <version>1.4.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
             <version>1.5.11</version>

Modified: sling/whiteboard/bdelacretaz/junit/testbundle/src/main/java/org/apache/sling/extensions/junit/testbundle/tests/JUnit4Test.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/testbundle/src/main/java/org/apache/sling/extensions/junit/testbundle/tests/JUnit4Test.java?rev=1065993&r1=1065992&r2=1065993&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/testbundle/src/main/java/org/apache/sling/extensions/junit/testbundle/tests/JUnit4Test.java
(original)
+++ sling/whiteboard/bdelacretaz/junit/testbundle/src/main/java/org/apache/sling/extensions/junit/testbundle/tests/JUnit4Test.java
Tue Feb  1 11:14:22 2011
@@ -22,11 +22,8 @@ import static org.junit.Assert.fail;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.apache.sling.extensions.junit.SlingAnnotationsTestRunner;
 
 /** Example test using the JUnit4 annotations */
-@RunWith(SlingAnnotationsTestRunner.class)
 public class JUnit4Test {
     private String title;
     

Added: sling/whiteboard/bdelacretaz/junit/testbundle/src/main/java/org/apache/sling/extensions/junit/testbundle/tests/OsgiAwareTest.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/testbundle/src/main/java/org/apache/sling/extensions/junit/testbundle/tests/OsgiAwareTest.java?rev=1065993&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/testbundle/src/main/java/org/apache/sling/extensions/junit/testbundle/tests/OsgiAwareTest.java
(added)
+++ sling/whiteboard/bdelacretaz/junit/testbundle/src/main/java/org/apache/sling/extensions/junit/testbundle/tests/OsgiAwareTest.java
Tue Feb  1 11:14:22 2011
@@ -0,0 +1,51 @@
+/*
+ * 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.extensions.junit.testbundle.tests;
+
+import static org.junit.Assert.assertNotNull;
+
+import org.apache.sling.extensions.junit.SlingAnnotationsTestRunner;
+import org.apache.sling.extensions.junit.TestReference;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.cm.ConfigurationAdmin;
+
+/** Test OSGi services injection */
+@RunWith(SlingAnnotationsTestRunner.class)
+public class OsgiAwareTest {
+    
+    @TestReference
+    private ConfigurationAdmin configAdmin;
+
+    @TestReference
+    private BundleContext bundleContext;
+    
+    @Test
+    public void testConfigAdmin() {
+        assertNotNull(
+                "Expecting ConfigurationAdmin to be injected by Sling test runner", 
+                configAdmin);
+    }
+    
+    @Test
+    public void testBundleContext() {
+        assertNotNull(
+                "Expecting BundleContext to be injected by Sling test runner", 
+                bundleContext);
+    }
+}

Propchange: sling/whiteboard/bdelacretaz/junit/testbundle/src/main/java/org/apache/sling/extensions/junit/testbundle/tests/OsgiAwareTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/bdelacretaz/junit/testbundle/src/main/java/org/apache/sling/extensions/junit/testbundle/tests/OsgiAwareTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message