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

jsedding pushed a commit to branch org.apache.sling.testing.osgi.unit
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit cbe6816cea60cf471849567a1b36b62ee3db40f1
Author: Julian Sedding <[email protected]>
AuthorDate: Mon Jan 8 15:07:45 2024 +0100

    add support for JUnit5 TestTemplates (i.e. RepeatedTest and 
ParameterizedTest)
---
 README.md                                          | 10 ++++++
 .../sling/testing/osgi/unit/OSGiSupportImpl.java   | 20 +++++++-----
 .../testing/osgi/unit/impl/OSGiSupportTest.java    | 38 ++++++++++++++++++++++
 3 files changed, 59 insertions(+), 9 deletions(-)

diff --git a/README.md b/README.md
index 025cd4e3..2d2f63ef 100644
--- a/README.md
+++ b/README.md
@@ -40,3 +40,13 @@ class SampleTest {
     } 
 }
 ```
+
+## Known Limitations
+
+Due to the fact that the test-class and test-instance are processed outside 
the OSGi environment,
+`org.junit.jupiter.api.extension.ParameterResolver` implementations returning 
objects that are **not
+exported by the system-bundle** in a running OSGi framework, cannot currently 
be injected.
+
+Known injections that are currently impossible due to this limitation are
+- `org.junit.jupiter.api.TestInfo`
+- `org.junit.jupiter.api.RepetitionInfo`
diff --git 
a/src/main/java/org/apache/sling/testing/osgi/unit/OSGiSupportImpl.java 
b/src/main/java/org/apache/sling/testing/osgi/unit/OSGiSupportImpl.java
index 2ad9b35f..eb93186d 100644
--- a/src/main/java/org/apache/sling/testing/osgi/unit/OSGiSupportImpl.java
+++ b/src/main/java/org/apache/sling/testing/osgi/unit/OSGiSupportImpl.java
@@ -27,7 +27,6 @@ import biz.aQute.resolve.ResolverLogger;
 import org.apache.sling.testing.osgi.unit.impl.BundleUtil;
 import org.apache.sling.testing.osgi.unit.impl.OSGiUnitConfig;
 import org.apache.sling.testing.osgi.unit.impl.OSGiUtil;
-import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
 import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
@@ -73,9 +72,7 @@ import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.ServiceLoader;
-import java.util.Set;
 import java.util.concurrent.CountDownLatch;
-import java.util.function.BinaryOperator;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
@@ -85,11 +82,6 @@ import static java.util.function.Predicate.not;
 import static 
org.apache.sling.testing.osgi.unit.impl.BundleUtil.collectJarFilesFromClassPath;
 import static org.osgi.framework.Constants.FRAMEWORK_STORAGE;
 
-// TODO - When running the bundled test in the OSGi framework, it imports 
org.apache.sling.testing.osgi.unit
-//        because it uses the OSGiSupport annotation. However, in that 
context, the annotation is no longer
-//        needed, because the test method is invoked via 
InvocationInterceptor#interceptTestMethod.
-//        If we can weave the class before bundling, and strip its use of the 
OSGiSupport annotation,
-//        then we could avoid having to install this bundle and all its 
transitive dependencies, mostly aQute.
 public class OSGiSupportImpl implements BeforeTestExecutionCallback, 
AfterTestExecutionCallback, InvocationInterceptor, ParameterResolver {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(OSGiSupportImpl.class);
@@ -98,6 +90,17 @@ public class OSGiSupportImpl implements 
BeforeTestExecutionCallback, AfterTestEx
 
     @Override
     public void interceptTestMethod(Invocation<Void> invocation, 
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext 
extensionContext) throws Throwable {
+        invokeWithinContextOfOSGiFramework(invocationContext, 
extensionContext);
+        invocation.skip();
+    }
+
+    @Override
+    public void interceptTestTemplateMethod(Invocation<Void> invocation, 
ReflectiveInvocationContext<Method> invocationContext, ExtensionContext 
extensionContext) throws Throwable {
+        invokeWithinContextOfOSGiFramework(invocationContext, 
extensionContext);
+        invocation.skip();
+    }
+
+    private static void 
invokeWithinContextOfOSGiFramework(ReflectiveInvocationContext<Method> 
invocationContext, ExtensionContext extensionContext) throws 
ClassNotFoundException, NoSuchMethodException {
         final ExtensionContext.Store store = getStore(extensionContext);
         final Bundle bundle = store.get(Bundle.class, Bundle.class);
         final Method method = invocationContext.getExecutable();
@@ -124,7 +127,6 @@ public class OSGiSupportImpl implements 
BeforeTestExecutionCallback, AfterTestEx
 
         final Object instanceInOSGi = 
ReflectionSupport.newInstance(targetClassInOsgi);
         ReflectionSupport.invokeMethod(methodInOsgi, instanceInOSGi, 
arguments);
-        invocation.skip();
     }
 
     @Override
diff --git 
a/src/test/java/org/apache/sling/testing/osgi/unit/impl/OSGiSupportTest.java 
b/src/test/java/org/apache/sling/testing/osgi/unit/impl/OSGiSupportTest.java
index eb1980bf..74585bb7 100644
--- a/src/test/java/org/apache/sling/testing/osgi/unit/impl/OSGiSupportTest.java
+++ b/src/test/java/org/apache/sling/testing/osgi/unit/impl/OSGiSupportTest.java
@@ -21,8 +21,14 @@ package org.apache.sling.testing.osgi.unit.impl;
 import org.apache.sling.testing.osgi.unit.OSGiSupport;
 import org.apache.sling.testing.osgi.unit.Service;
 import org.jetbrains.annotations.NotNull;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.RepeatedTest;
+import org.junit.jupiter.api.RepetitionInfo;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
 import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
@@ -187,6 +193,38 @@ class OSGiSupportTest {
         assertNotNull(condition);
     }
 
+    @RepeatedTest(2)
+    void repeatedTestSupport(Framework framework, @Service Condition 
condition) {
+        assertNotNull(framework);
+        assertNotNull(condition);
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"foo", "bar"})
+    void parameterizedTestSupport(String fooOrBar, Framework framework, 
@Service Condition condition) {
+        assertThat(fooOrBar).matches(List.of("foo", "bar")::contains);
+        assertNotNull(framework);
+        assertNotNull(condition);
+    }
+
+    @Disabled("The RepetitionInfo class of the resolved object is different 
from the RepetitionInfo class within OSGi ")
+    @RepeatedTest(2)
+    void repetitionInfoSupport(Framework framework, @Service Condition 
condition, RepetitionInfo repetitionInfo) {
+        assertNotNull(framework);
+        assertNotNull(condition);
+        assertThat(repetitionInfo.getCurrentRepetition()).isBetween(1, 
repetitionInfo.getTotalRepetitions());
+    }
+
+    @Disabled("The TestInfo class of the resolved object is different from the 
TestInfo class within OSGi ")
+    @Test
+    void testInfoSupport(Framework framework, @Service Condition condition, 
TestInfo testInfo) {
+        assertNotNull(framework);
+        assertNotNull(condition);
+        assertThat(testInfo.getDisplayName()).isEqualTo("testInfoSupport");
+    }
+
+
+
     @NotNull
     private static List<String> getSymbolicNames(Bundle... bundles) {
         return Stream.of(bundles)

Reply via email to