You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by fp...@apache.org on 2020/10/21 15:08:41 UTC
[karaf-winegrower] branch master updated: Enable to skip service
injection in junit5 extension to let another extension provide it
This is an automated email from the ASF dual-hosted git repository.
fpapon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf-winegrower.git
The following commit(s) were added to refs/heads/master by this push:
new 612ffbc Enable to skip service injection in junit5 extension to let another extension provide it
new 6742b32 Merge pull request #16 from rmannibucau/rmannibucau/ensure-you-can-skip-service-injection
612ffbc is described below
commit 612ffbcdbb85b2ab5199d95d406ccdbb00acbf8c
Author: Romain Manni-Bucau <rm...@gmail.com>
AuthorDate: Wed Oct 21 09:23:16 2020 +0200
Enable to skip service injection in junit5 extension to let another extension provide it
---
.../extension/testing/junit5/NotAnOSGiService.java | 28 ++++++++++++++++++++++
.../testing/junit5/internal/BaseInjection.java | 9 +++++--
2 files changed, 35 insertions(+), 2 deletions(-)
diff --git a/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/NotAnOSGiService.java b/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/NotAnOSGiService.java
new file mode 100644
index 0000000..8617539
--- /dev/null
+++ b/winegrower-extension/winegrower-testing/winegrower-testing-junit5/src/main/java/org/apache/winegrower/extension/testing/junit5/NotAnOSGiService.java
@@ -0,0 +1,28 @@
+/**
+ * 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;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+/**
+ * Mark a parameter as not being injected from Winegrower extension but another one.
+ */
+@Target(PARAMETER)
+@Retention(RUNTIME)
+public @interface NotAnOSGiService {
+}
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 cd014b0..b083a02 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
@@ -16,6 +16,7 @@ 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;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
@@ -23,6 +24,8 @@ import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
+import java.lang.reflect.Parameter;
+
public abstract class BaseInjection implements TestInstancePostProcessor, ParameterResolver {
@Override
public void postProcessTestInstance(final Object o, final ExtensionContext context) {
@@ -33,8 +36,10 @@ public abstract class BaseInjection implements TestInstancePostProcessor, Parame
@Override
public boolean supportsParameter(final ParameterContext parameterContext, final ExtensionContext context)
throws ParameterResolutionException {
- final Class<?> type = parameterContext.getParameter().getType();
- return type == Ripener.class || type == OSGiServices.class || store(context).get(Ripener.class, Ripener.class).getServices().findService(type).isPresent();
+ final Parameter parameter = parameterContext.getParameter();
+ final Class<?> type = parameter.getType();
+ return !parameter.isAnnotationPresent(NotAnOSGiService.class) &&
+ (type == Ripener.class || type == OSGiServices.class || store(context).get(Ripener.class, Ripener.class).getServices().findService(type).isPresent());
}
@Override