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:14 UTC

[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

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) {