Merge pull request #27 from rmannibucau/rmannibucau/KARAF-6900

[KARAF-6900] ensure junit5 extension can ignore parameter injection w…
diff --git a/winegrower-extension/winegrower-testing/winegrower-testing-junit5/pom.xml b/winegrower-extension/winegrower-testing/winegrower-testing-junit5/pom.xml
index b5ed698..93418ab 100644
--- a/winegrower-extension/winegrower-testing/winegrower-testing-junit5/pom.xml
+++ b/winegrower-extension/winegrower-testing/winegrower-testing-junit5/pom.xml
@@ -38,6 +38,12 @@
       <scope>provided</scope>
     </dependency>
     <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-engine</artifactId>
+      <version>${junit.version}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
       <groupId>org.apache.winegrower</groupId>
       <artifactId>winegrower-core</artifactId>
       <version>${project.version}</version>
diff --git a/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/MonoWinegrower.java b/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/MonoWinegrower.java
index e6ad164..9a3e6b8 100644
--- a/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/MonoWinegrower.java
+++ b/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/MonoWinegrower.java
@@ -21,6 +21,7 @@
 
 import org.apache.winegrower.Ripener;
 import org.apache.winegrower.extension.testing.junit5.internal.MonoWinegrowerExtension;
+import org.apache.winegrower.extension.testing.junit5.internal.engine.CaptureExtensionRegistry;
 import org.junit.jupiter.api.extension.ExtendWith;
 
 /**
@@ -32,6 +33,7 @@
  */
 @Target(TYPE)
 @Retention(RUNTIME)
+@ExtendWith(CaptureExtensionRegistry.class)
 @ExtendWith(MonoWinegrowerExtension.class)
 public @interface MonoWinegrower {
 }
diff --git a/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/Winegrower.java b/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/Winegrower.java
index fa676ef..1b7615a 100644
--- a/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/Winegrower.java
+++ b/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/Winegrower.java
@@ -22,6 +22,7 @@
 
 import org.apache.winegrower.api.LifecycleCallbacks;
 import org.apache.winegrower.extension.testing.junit5.internal.WinegrowerExtension;
+import org.apache.winegrower.extension.testing.junit5.internal.engine.CaptureExtensionRegistry;
 import org.apache.winegrower.scanner.manifest.ManifestContributor;
 import org.junit.jupiter.api.extension.ExtendWith;
 
@@ -30,6 +31,7 @@
  */
 @Target(TYPE)
 @Retention(RUNTIME)
+@ExtendWith(CaptureExtensionRegistry.class)
 @ExtendWith(WinegrowerExtension.class)
 public @interface Winegrower {
     String workDir() default "";
diff --git a/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/internal/BaseInjection.java b/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/internal/BaseInjection.java
index b083a02..36353fd 100644
--- a/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/internal/BaseInjection.java
+++ b/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/internal/BaseInjection.java
@@ -13,8 +13,6 @@
  */
 package org.apache.winegrower.extension.testing.junit5.internal;
 
-import static java.util.Optional.ofNullable;
-
 import org.apache.winegrower.Ripener;
 import org.apache.winegrower.extension.testing.junit5.NotAnOSGiService;
 import org.apache.winegrower.service.OSGiServices;
@@ -26,6 +24,8 @@
 
 import java.lang.reflect.Parameter;
 
+import static java.util.Optional.ofNullable;
+
 public abstract class BaseInjection implements TestInstancePostProcessor, ParameterResolver {
     @Override
     public void postProcessTestInstance(final Object o, final ExtensionContext context) {
@@ -36,6 +36,17 @@
     @Override
     public boolean supportsParameter(final ParameterContext parameterContext, final ExtensionContext context)
             throws ParameterResolutionException {
+        try { // first check if the parameter is handled by another extension
+            if (org.apache.winegrower.extension.testing.junit5.internal.engine.CaptureExtensionRegistry
+                    .get(ParameterResolver.class, context)
+                    .anyMatch(it -> it != BaseInjection.this && it.supportsParameter(parameterContext, context))) {
+                return false;
+            }
+        } catch (final Exception | Error e) {
+            // ignore, the engine is not the expected one, fallback on default behavior
+        }
+
+        // if not handled assume it is a service we know
         final Parameter parameter = parameterContext.getParameter();
         final Class<?> type = parameter.getType();
         return !parameter.isAnnotationPresent(NotAnOSGiService.class) &&
diff --git a/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/internal/engine/CaptureExtensionRegistry.java b/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/internal/engine/CaptureExtensionRegistry.java
new file mode 100644
index 0000000..f72dabc
--- /dev/null
+++ b/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/internal/engine/CaptureExtensionRegistry.java
@@ -0,0 +1,39 @@
+/**
+ * Licensed 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.winegrower.extension.testing.junit5.internal.engine;
+
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.engine.execution.BeforeEachMethodAdapter;
+import org.junit.jupiter.engine.extension.ExtensionRegistry;
+
+import java.util.stream.Stream;
+
+import static java.util.Optional.ofNullable;
+
+public class CaptureExtensionRegistry implements BeforeEachMethodAdapter {
+    public static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(CaptureExtensionRegistry.class);
+
+    @Override
+    public void invokeBeforeEachMethod(final ExtensionContext context, final ExtensionRegistry registry) {
+        context.getStore(NAMESPACE).put(ExtensionRegistry.class, registry);
+    }
+
+    public static <T extends Extension> Stream<T> get(final Class<T> type, final ExtensionContext context) {
+        return ofNullable(context.getStore(NAMESPACE)
+                .get(ExtensionRegistry.class, ExtensionRegistry.class))
+                .map(it -> it.stream(type))
+                .orElseGet(Stream::empty);
+    }
+}
diff --git a/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/test/java/org/apache/winegrower/extension/testing/junit5/internal/WinegrowerExtensionTest.java b/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/test/java/org/apache/winegrower/extension/testing/junit5/internal/WinegrowerExtensionTest.java
index 63c6404..7e69889 100644
--- a/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/test/java/org/apache/winegrower/extension/testing/junit5/internal/WinegrowerExtensionTest.java
+++ b/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/test/java/org/apache/winegrower/extension/testing/junit5/internal/WinegrowerExtensionTest.java
@@ -13,16 +13,22 @@
  */
 package org.apache.winegrower.extension.testing.junit5.internal;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-
 import org.apache.winegrower.Ripener;
 import org.apache.winegrower.api.InjectedService;
 import org.apache.winegrower.extension.testing.junit5.Winegrower;
 import org.apache.winegrower.service.OSGiServices;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.ParameterResolver;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 @Winegrower
+@ExtendWith(WinegrowerExtensionTest.CustomInjector.class)
 class WinegrowerExtensionTest {
     @InjectedService
     private Ripener ripener;
@@ -35,4 +41,21 @@
         assertEquals(ripener, this.ripener);
         assertNotNull(services);
     }
+
+    @Test
+    void notOSGiInjectionWithoutQualifier(final CustomInjector extension) {
+        assertNotNull(extension);
+    }
+
+    public static class CustomInjector implements ParameterResolver {
+        @Override
+        public boolean supportsParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext) throws ParameterResolutionException {
+            return CustomInjector.class == parameterContext.getParameter().getType();
+        }
+
+        @Override
+        public Object resolveParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext) throws ParameterResolutionException {
+            return this;
+        }
+    }
 }