jsedding commented on a change in pull request #5:
URL: 
https://github.com/apache/sling-org-apache-sling-junit-core/pull/5#discussion_r528755102



##########
File path: 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;

Review comment:
       Why the package move? Shouldn't we instead move it to 
`org.apache.sling.junit.impl`, so it stays an implementation detail despite the 
class being made public? Or am I missing something and `ServiceGetter` should 
be used by client code?

##########
File path: src/main/java/org/apache/sling/junit/annotations/TestReference.java
##########
@@ -39,6 +38,12 @@
      */
     String name() default "";
 
+    /**
+     * LDAP-style filter for targeting a specific reference implementation
+     *
+     * @return the local name of the reference ("" by default)
+     */
+    String filter() default "";

Review comment:
       Could we name this property `target`, so it's in line with the 
`@Reference` annotations?

##########
File path: 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 {

Review comment:
       I'm not sure I am terribly fond of shipping Sling JUnit Core with test 
classes. At the very least we should make sure these are not added to the API 
(I think they aren't, but didn't verify). I would prefer if the test classes 
coould be moved to `src/test/**` and instead be built into an ad-hoc tinybundle 
for testing with PaxExam.

##########
File path: src/main/java/org/apache/sling/junit/impl/AnnotationsProcessor.java
##########
@@ -64,25 +67,32 @@ private void processTestReference(Object testObject, Field 
f) throws Exception {
             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) {

Review comment:
       Why don't you replace this entire method with 
`ServiceGetter.create(bundleContext, c, filter).getService()`? It looks to me 
like that already handles all the edge cases. I.e. inject `BundleContext` and 
`null` target filter expression.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to