mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-15 08:18:55 +00:00
Make @InjectWith meta-annotation capable (fix #1789)
Use the JUnit Platform annotation retrieval to also look at super interfaces, composed interfaces etc. of the test class. JUnit Platform 1.y.z corresponds to JUnit Jupiter 5.y.z, that's why the platform range [1,2) should fit well to the existing Jupiter range [5,6) in the manifest. Signed-off-by: Michael Keppler <Michael.Keppler@gmx.de>
This commit is contained in:
parent
7c8f2ae545
commit
8d0d06c34f
4 changed files with 93 additions and 8 deletions
|
@ -29,7 +29,8 @@ Require-Bundle: org.eclipse.xtext;visibility:=reexport,
|
|||
Import-Package: org.apache.log4j;version="1.2.15",
|
||||
org.apache.log4j.spi;version="1.2.15",
|
||||
org.junit.jupiter.api;version="[5.0.0,6.0.0)";resolution:=optional,
|
||||
org.junit.jupiter.api.extension;version="[5.0.0,6.0.0)";resolution:=optional
|
||||
org.junit.jupiter.api.extension;version="[5.0.0,6.0.0)";resolution:=optional,
|
||||
org.junit.platform.commons.support;version="[1.0.0,2.0.0)";resolution:=optional
|
||||
Bundle-ActivationPolicy: lazy
|
||||
Automatic-Module-Name: org.eclipse.xtext.testing
|
||||
Eclipse-SourceReferences: eclipseSourceReferences
|
||||
|
|
|
@ -21,6 +21,7 @@ import org.junit.jupiter.api.extension.BeforeEachCallback;
|
|||
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
|
||||
import org.junit.platform.commons.support.AnnotationSupport;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource;
|
||||
|
||||
|
@ -38,7 +39,7 @@ import com.google.inject.Injector;
|
|||
* It takes care about JUnit5 {@link Nested nested} test classes. They are inner classes that might
|
||||
* be annotated as well and must be handled for injection.
|
||||
* </p>
|
||||
*
|
||||
*
|
||||
* @author Karsten Thoms - Initial contribution and API
|
||||
* @since 2.14
|
||||
*/
|
||||
|
@ -52,7 +53,7 @@ public class InjectionExtension implements BeforeEachCallback, AfterEachCallback
|
|||
* </p>
|
||||
*/
|
||||
private static ClassToInstanceMap<IInjectorProvider> injectorProviderClassCache = MutableClassToInstanceMap.create();
|
||||
|
||||
|
||||
/**
|
||||
* @since 2.24
|
||||
*/
|
||||
|
@ -64,7 +65,7 @@ public class InjectionExtension implements BeforeEachCallback, AfterEachCallback
|
|||
this.resetter = resetter;
|
||||
setup();
|
||||
}
|
||||
|
||||
|
||||
public void setup() {
|
||||
if (!didSetup) {
|
||||
didSetup = true;
|
||||
|
@ -140,15 +141,16 @@ public class InjectionExtension implements BeforeEachCallback, AfterEachCallback
|
|||
/**
|
||||
* Returns the {@link IInjectorProvider injector provider} for the given context. Tries to find an {@link InjectWith}
|
||||
* annotation on the required test classes along the {@link ExtensionContext#getParent() hierarchy} of contexts.
|
||||
*
|
||||
*
|
||||
* If the injector provider can be found, it will be reflectively instantiated and cached. Only one instance of any
|
||||
* given injector provider will be created.
|
||||
*
|
||||
*
|
||||
* @since 2.24
|
||||
*/
|
||||
protected static IInjectorProvider getOrCreateInjectorProvider(ExtensionContext context) {
|
||||
InjectWith injectWith = context.getRequiredTestClass().getAnnotation(InjectWith.class);
|
||||
if (injectWith != null) {
|
||||
Optional<InjectWith> injectWithOpt = AnnotationSupport.findAnnotation(context.getRequiredTestClass(), InjectWith.class);
|
||||
if (injectWithOpt.isPresent()) {
|
||||
InjectWith injectWith = injectWithOpt.get();
|
||||
Class<? extends IInjectorProvider> klass = injectWith.value();
|
||||
IInjectorProvider injectorProvider = injectorProviderClassCache.get(klass);
|
||||
if (injectorProvider == null) {
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2021 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.testing.tests.junit5;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.eclipse.xtext.testing.IInjectorProvider;
|
||||
import org.eclipse.xtext.testing.InjectWith;
|
||||
import org.eclipse.xtext.testing.extensions.InjectionExtension;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
/**
|
||||
* Test for {@link InjectionExtension} meta annotation/composed annotation
|
||||
* compatibility.
|
||||
*
|
||||
* @author Michael Keppler - Initial contribution and API
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE})
|
||||
@InjectWith(ComposedInject.MyInjectorProvider.class)
|
||||
@ExtendWith(InjectionExtension.class)
|
||||
public @interface ComposedInject {
|
||||
|
||||
public static final String INJECTED = "injected";
|
||||
|
||||
public static class MyInjectorProvider implements IInjectorProvider {
|
||||
|
||||
@Override
|
||||
public Injector getInjector() {
|
||||
return Guice.createInjector(binder -> binder.bind(String.class).toInstance(INJECTED));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2021 itemis AG (http://www.itemis.eu) and others.
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0.
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.testing.tests.junit5;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.eclipse.xtext.testing.extensions.InjectionExtension;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* Test for {@link InjectionExtension} meta annotation/composed annotation
|
||||
* compatibility. It uses a composed annotation instead of the original
|
||||
* {@code InjectWith} annotation.
|
||||
*
|
||||
* @author Michael Keppler - Initial contribution and API
|
||||
*/
|
||||
@ComposedInject
|
||||
public class ComposedInjectAnnotationTest {
|
||||
@Inject
|
||||
String toBeInjected = "";
|
||||
|
||||
@Test
|
||||
public void didUseComposedInjectAnnotation() {
|
||||
assertEquals(ComposedInject.INJECTED, toBeInjected);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue