You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cr...@apache.org on 2020/11/20 23:42:10 UTC

[sling-org-apache-sling-junit-core] branch SLING-9915-Support-for-SlingAnnotationsTestRunner updated (bbd02be -> c586af9)

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

cris pushed a change to branch SLING-9915-Support-for-SlingAnnotationsTestRunner
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-junit-core.git.


    from bbd02be  SLING-9915 Remove deprecated flags
     new 4945fd8  SLING-9915 Refactored ServiceGetter.java and made public such that it can be used by AnnotationsProcessor.java
     new b505ee8  SLING-9915 Added filter() method to TestReference interface
     new fa0560c  Added tests for TestReference(String filter)
     new 5b0d59b  Update AnnotationsProcessor::processTestReference to get the filter passed to the TestReference annotation, and call  getService(Class<?> c, String filter) which now uses ServiceGetter
     new c586af9  update package version to 1.2.0 as per bnd-baseline-maven-plugin recommendation

The 5 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.


Summary of changes:
 bnd.bnd                                            |  1 +
 .../sling/junit/{rules => }/ServiceGetter.java     |  4 +-
 .../apache/sling/junit/annotations/AutoDetect.java |  3 ++
 .../sling/junit/annotations/TestReference.java     |  6 +++
 .../sling/junit/annotations/package-info.java      |  2 +-
 .../sling/junit/impl/AnnotationsProcessor.java     | 36 ++++++++++------
 .../sling/junit/rules/ServerSideTeleporter.java    |  1 +
 .../java/org/apache/sling/junit/rules/Service.java |  1 +
 .../MyService.java}                                | 11 +++--
 .../sling/junit/tests/TestReferenceJTest.java      | 49 ++++++++++++++++++++++
 .../impl/MyCoolServiceForTesting.java}             | 37 ++++++++--------
 .../impl/MyLameServiceForTesting.java}             | 36 ++++++++--------
 12 files changed, 133 insertions(+), 54 deletions(-)
 rename src/main/java/org/apache/sling/junit/{rules => }/ServiceGetter.java (97%)
 copy src/main/java/org/apache/sling/junit/{TestObjectProcessor.java => tests/MyService.java} (75%)
 create mode 100644 src/main/java/org/apache/sling/junit/tests/TestReferenceJTest.java
 copy src/main/java/org/apache/sling/junit/{RendererSelector.java => tests/impl/MyCoolServiceForTesting.java} (56%)
 copy src/main/java/org/apache/sling/junit/{RendererSelector.java => tests/impl/MyLameServiceForTesting.java} (56%)


[sling-org-apache-sling-junit-core] 04/05: Update AnnotationsProcessor::processTestReference to get the filter passed to the TestReference annotation, and call getService(Class c, String filter) which now uses ServiceGetter

Posted by cr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cris pushed a commit to branch SLING-9915-Support-for-SlingAnnotationsTestRunner
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-junit-core.git

commit 5b0d59bdb89208f24c1bb570bbe64e7ffc07efba
Author: Cris Rockwell <cm...@umich.edu>
AuthorDate: Fri Nov 20 18:40:28 2020 -0500

    Update AnnotationsProcessor::processTestReference to get the filter passed to the TestReference annotation, and call  getService(Class<?> c, String filter) which now uses ServiceGetter
---
 .../sling/junit/impl/AnnotationsProcessor.java     | 36 ++++++++++++++--------
 1 file changed, 23 insertions(+), 13 deletions(-)

diff --git a/src/main/java/org/apache/sling/junit/impl/AnnotationsProcessor.java b/src/main/java/org/apache/sling/junit/impl/AnnotationsProcessor.java
index c4a4e92..aa347ac 100644
--- a/src/main/java/org/apache/sling/junit/impl/AnnotationsProcessor.java
+++ b/src/main/java/org/apache/sling/junit/impl/AnnotationsProcessor.java
@@ -16,10 +16,13 @@
  */
 package org.apache.sling.junit.impl;
 
+import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
+import java.util.Objects;
 
 import org.apache.sling.junit.TestObjectProcessor;
 import org.apache.sling.junit.annotations.TestReference;
+import org.apache.sling.junit.ServiceGetter;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentContext;
@@ -64,25 +67,32 @@ public class AnnotationsProcessor implements TestObjectProcessor {
             log.error(msg);
             throw new IllegalArgumentException(msg);
         }
-        
         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());
+        Annotation[] testReferences = f.getDeclaredAnnotations();
+        if(Objects.nonNull(testReferences) && testReferences.length != 0){
+            TestReference testReference = (TestReference) testReferences[0];
+            String filter = testReference.filter();
+            final Object service = getService(serviceType, filter);
+            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) {
+
+    private Object getService(Class<?> c, String filter) {
         Object result = null;
-        // BundleContext is not a service, but can be injected
         if(c.equals(BundleContext.class)) {
+            // BundleContext is not a service, but can be injected
             result = bundleContext;
+        } else if (Objects.nonNull(filter) && !filter.trim().isEmpty()){
+            // filter was used to target a specific service implementation
+            result = ServiceGetter.create(bundleContext, c, filter).getService();
         } else {
             ServiceReference ref = bundleContext.getServiceReference(c.getName());
             if(ref != null) {


[sling-org-apache-sling-junit-core] 01/05: SLING-9915 Refactored ServiceGetter.java and made public such that it can be used by AnnotationsProcessor.java

Posted by cr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cris pushed a commit to branch SLING-9915-Support-for-SlingAnnotationsTestRunner
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-junit-core.git

commit 4945fd89e2e9beb99f409bc2274838db514c810f
Author: Cris Rockwell <cm...@umich.edu>
AuthorDate: Fri Nov 20 18:28:14 2020 -0500

    SLING-9915 Refactored ServiceGetter.java and made public such that it can be used by AnnotationsProcessor.java
---
 src/main/java/org/apache/sling/junit/{rules => }/ServiceGetter.java  | 4 ++--
 src/main/java/org/apache/sling/junit/rules/ServerSideTeleporter.java | 1 +
 src/main/java/org/apache/sling/junit/rules/Service.java              | 1 +
 3 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/sling/junit/rules/ServiceGetter.java b/src/main/java/org/apache/sling/junit/ServiceGetter.java
similarity index 97%
rename from src/main/java/org/apache/sling/junit/rules/ServiceGetter.java
rename to src/main/java/org/apache/sling/junit/ServiceGetter.java
index d506db8..85a3851 100644
--- a/src/main/java/org/apache/sling/junit/rules/ServiceGetter.java
+++ b/src/main/java/org/apache/sling/junit/ServiceGetter.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.sling.junit.rules;
+package org.apache.sling.junit;
 
 import java.io.Closeable;
 
@@ -27,7 +27,7 @@ import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.util.tracker.ServiceTracker;
 
 /** Implements the logic used to get a service */
-class ServiceGetter<T> implements Closeable {
+public class ServiceGetter<T> implements Closeable {
 
     private final ServiceTracker tracker;
     private final BundleContext bundleContext;
diff --git a/src/main/java/org/apache/sling/junit/rules/ServerSideTeleporter.java b/src/main/java/org/apache/sling/junit/rules/ServerSideTeleporter.java
index 3de6538..dfd5964 100644
--- a/src/main/java/org/apache/sling/junit/rules/ServerSideTeleporter.java
+++ b/src/main/java/org/apache/sling/junit/rules/ServerSideTeleporter.java
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.sling.junit.Activator;
+import org.apache.sling.junit.ServiceGetter;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
diff --git a/src/main/java/org/apache/sling/junit/rules/Service.java b/src/main/java/org/apache/sling/junit/rules/Service.java
index ba84c18..4d8b598 100644
--- a/src/main/java/org/apache/sling/junit/rules/Service.java
+++ b/src/main/java/org/apache/sling/junit/rules/Service.java
@@ -18,6 +18,7 @@
 package org.apache.sling.junit.rules;
 
 import org.apache.sling.junit.Activator;
+import org.apache.sling.junit.ServiceGetter;
 import org.junit.rules.TestRule;
 import org.junit.runner.Description;
 import org.junit.runners.model.Statement;


[sling-org-apache-sling-junit-core] 03/05: Added tests for TestReference(String filter)

Posted by cr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cris pushed a commit to branch SLING-9915-Support-for-SlingAnnotationsTestRunner
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-junit-core.git

commit fa0560c56f41dc8c194e663bbdddf036fc6bbded
Author: Cris Rockwell <cm...@umich.edu>
AuthorDate: Fri Nov 20 18:32:59 2020 -0500

    Added tests for TestReference(String filter)
---
 bnd.bnd                                            |  1 +
 .../org/apache/sling/junit/tests/MyService.java    | 25 +++++++++++
 .../sling/junit/tests/TestReferenceJTest.java      | 49 ++++++++++++++++++++++
 .../junit/tests/impl/MyCoolServiceForTesting.java  | 42 +++++++++++++++++++
 .../junit/tests/impl/MyLameServiceForTesting.java  | 41 ++++++++++++++++++
 5 files changed, 158 insertions(+)

diff --git a/bnd.bnd b/bnd.bnd
index aea6f89..454a071 100644
--- a/bnd.bnd
+++ b/bnd.bnd
@@ -3,6 +3,7 @@ Export-Package: !org.junit.platform.*, \
                 junit.*;version=${junit.version}, \
                 org.junit.*;version=${junit.version}, \
                 org.hamcrest.*;version=${hamcrest.version};-split-package:=merge-first
+Sling-Test-Regexp: .*JTest
 Import-Package: org.junit.platform.*;resolution:=optional, \
                 *
 -includeresource: @org.jacoco.agent-*.jar!/org/jacoco/agent/rt/IAgent*
diff --git a/src/main/java/org/apache/sling/junit/tests/MyService.java b/src/main/java/org/apache/sling/junit/tests/MyService.java
new file mode 100644
index 0000000..fcbba0a
--- /dev/null
+++ b/src/main/java/org/apache/sling/junit/tests/MyService.java
@@ -0,0 +1,25 @@
+/*
+ * 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.junit.tests;
+
+public interface MyService {
+    /** @return Name of the Service which is used to discover the Service by the User **/
+    String getName();
+
+    /** @return Description of the Service  **/
+    String getDescription();
+}
diff --git a/src/main/java/org/apache/sling/junit/tests/TestReferenceJTest.java b/src/main/java/org/apache/sling/junit/tests/TestReferenceJTest.java
new file mode 100644
index 0000000..6899158
--- /dev/null
+++ b/src/main/java/org/apache/sling/junit/tests/TestReferenceJTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.junit.tests;
+
+import org.apache.sling.junit.annotations.SlingAnnotationsTestRunner;
+import org.apache.sling.junit.annotations.TestReference;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static junit.framework.TestCase.assertEquals;
+import static junit.framework.TestCase.assertNotNull;
+import static junit.framework.TestCase.assertNull;
+
+
+@RunWith(SlingAnnotationsTestRunner.class)
+public class TestReferenceJTest {
+
+    @TestReference(filter="(component.name=org.apache.sling.junit.tests.impl.MyCoolServiceForTesting)")
+    MyService myCoolService;
+
+    @TestReference(filter="(component.name=org.apache.sling.junit.tests.impl.MyLameServiceForTesting)")
+    MyService myLameService;
+
+    @TestReference(filter="(component.name=org.apache.sling.junit.tests.impl.MyNonExistingServiceForTesting)")
+    MyService myNullService;
+
+    @Test
+    public void exampleTestReference(){
+        assertNotNull(myCoolService);
+        assertNotNull(myLameService);
+        assertNull(myNullService);
+        assertEquals("Cool Service", myCoolService.getName());
+        assertEquals("Lame Service", myLameService.getName());
+    }
+}
diff --git a/src/main/java/org/apache/sling/junit/tests/impl/MyCoolServiceForTesting.java b/src/main/java/org/apache/sling/junit/tests/impl/MyCoolServiceForTesting.java
new file mode 100644
index 0000000..471ee64
--- /dev/null
+++ b/src/main/java/org/apache/sling/junit/tests/impl/MyCoolServiceForTesting.java
@@ -0,0 +1,42 @@
+/*
+ * 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.junit.tests.impl;
+
+import org.apache.sling.junit.tests.MyService;
+import org.osgi.service.component.annotations.Component;
+
+@Component(
+        service = MyService.class
+)
+public class MyCoolServiceForTesting implements MyService {
+
+    /**
+     * @return Name of the Service which is used to discover the Service by the User
+     **/
+    @Override
+    public String getName() {
+        return "Cool Service";
+    }
+
+    /**
+     * @return Description of the Service
+     **/
+    @Override
+    public String getDescription() {
+        return "My Cool Service is for testing @TestReference for in running JUnit tests within Sling";
+    }
+}
diff --git a/src/main/java/org/apache/sling/junit/tests/impl/MyLameServiceForTesting.java b/src/main/java/org/apache/sling/junit/tests/impl/MyLameServiceForTesting.java
new file mode 100644
index 0000000..7a473c8
--- /dev/null
+++ b/src/main/java/org/apache/sling/junit/tests/impl/MyLameServiceForTesting.java
@@ -0,0 +1,41 @@
+/*
+ * 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.junit.tests.impl;
+
+import org.apache.sling.junit.tests.MyService;
+import org.osgi.service.component.annotations.Component;
+
+@Component(
+        service = MyService.class
+)
+public class MyLameServiceForTesting implements MyService{
+    /**
+     * @return Name of the Service which is used to discover the Service by the User
+     **/
+    @Override
+    public String getName() {
+        return "Lame Service";
+    }
+
+    /**
+     * @return Description of the Service
+     **/
+    @Override
+    public String getDescription() {
+        return "My Lame Service is for testing @TestReference for in running JUnit tests within Sling";
+    }
+}


[sling-org-apache-sling-junit-core] 02/05: SLING-9915 Added filter() method to TestReference interface

Posted by cr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cris pushed a commit to branch SLING-9915-Support-for-SlingAnnotationsTestRunner
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-junit-core.git

commit b505ee89c5ad1656edce17b76c10640b83c54ae2
Author: Cris Rockwell <cm...@umich.edu>
AuthorDate: Fri Nov 20 18:31:57 2020 -0500

    SLING-9915 Added filter() method to TestReference interface
---
 src/main/java/org/apache/sling/junit/annotations/AutoDetect.java    | 3 +++
 src/main/java/org/apache/sling/junit/annotations/TestReference.java | 6 ++++++
 2 files changed, 9 insertions(+)

diff --git a/src/main/java/org/apache/sling/junit/annotations/AutoDetect.java b/src/main/java/org/apache/sling/junit/annotations/AutoDetect.java
index e501b3e..c26e885 100644
--- a/src/main/java/org/apache/sling/junit/annotations/AutoDetect.java
+++ b/src/main/java/org/apache/sling/junit/annotations/AutoDetect.java
@@ -25,4 +25,7 @@ public class AutoDetect {
     private AutoDetect() {
         // disallows instancing this class
     }
+    private AutoDetect(String filter) {
+        // disallows instancing this class
+    }
 }
diff --git a/src/main/java/org/apache/sling/junit/annotations/TestReference.java b/src/main/java/org/apache/sling/junit/annotations/TestReference.java
index 56791cb..c8db670 100644
--- a/src/main/java/org/apache/sling/junit/annotations/TestReference.java
+++ b/src/main/java/org/apache/sling/junit/annotations/TestReference.java
@@ -39,6 +39,12 @@ public @interface TestReference {
     String name() default "";
 
     /**
+     * LDAP-style filter for targeting a specific reference implementation
+     *
+     * @return the local name of the reference ("" by default)
+     */
+    String filter() 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.
      *


[sling-org-apache-sling-junit-core] 05/05: update package version to 1.2.0 as per bnd-baseline-maven-plugin recommendation

Posted by cr...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cris pushed a commit to branch SLING-9915-Support-for-SlingAnnotationsTestRunner
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-junit-core.git

commit c586af9f9e2665c277348aa9b1095a6b54971fdf
Author: Cris Rockwell <cm...@umich.edu>
AuthorDate: Fri Nov 20 18:41:47 2020 -0500

    update package version to 1.2.0 as per bnd-baseline-maven-plugin recommendation
---
 src/main/java/org/apache/sling/junit/annotations/package-info.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/sling/junit/annotations/package-info.java b/src/main/java/org/apache/sling/junit/annotations/package-info.java
index f265cce..c3874ab 100644
--- a/src/main/java/org/apache/sling/junit/annotations/package-info.java
+++ b/src/main/java/org/apache/sling/junit/annotations/package-info.java
@@ -16,7 +16,7 @@
  ~ specific language governing permissions and limitations
  ~ under the License.
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
-@Version("1.1.0")
+@Version("1.2.0")
 package org.apache.sling.junit.annotations;
 
 import org.osgi.annotation.versioning.Version;