Search in sources :

Example 1 with InjectWith

use of org.eclipse.xtext.testing.InjectWith in project xtext-core by eclipse.

the class InjectionExtension method getOrCreateInjectorProvider.

/**
 * 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) {
    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) {
            try {
                injectorProvider = klass.getDeclaredConstructor().newInstance();
                injectorProviderClassCache.put(klass, injectorProvider);
            } catch (Exception e) {
                throwUncheckedException(e);
            }
        }
        return injectorProvider;
    } else {
        Optional<ExtensionContext> parentContext = context.getParent().filter(p -> p.getTestClass().isPresent());
        if (parentContext.isPresent()) {
            return getOrCreateInjectorProvider(parentContext.get());
        }
    }
    return null;
}
Also used : InjectWith(org.eclipse.xtext.testing.InjectWith) IInjectorProvider(org.eclipse.xtext.testing.IInjectorProvider) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Exceptions.throwUncheckedException(org.eclipse.xtext.util.Exceptions.throwUncheckedException) ExtensionConfigurationException(org.junit.jupiter.api.extension.ExtensionConfigurationException)

Example 2 with InjectWith

use of org.eclipse.xtext.testing.InjectWith in project xtext-core by eclipse.

the class InjectorProviders method getOrCreateInjectorProvider.

public static IInjectorProvider getOrCreateInjectorProvider(TestClass testClass) {
    InjectWith injectWith = testClass.getJavaClass().getAnnotation(InjectWith.class);
    if (injectWith != null) {
        Class<? extends IInjectorProvider> klass = injectWith.value();
        IInjectorProvider injectorProvider = injectorProviderClassCache.get(klass);
        if (injectorProvider == null) {
            try {
                injectorProvider = klass.getDeclaredConstructor().newInstance();
                injectorProviderClassCache.put(klass, injectorProvider);
            } catch (Exception e) {
                throwUncheckedException(e);
            }
        }
        return injectorProvider;
    }
    return null;
}
Also used : InjectWith(org.eclipse.xtext.testing.InjectWith) IInjectorProvider(org.eclipse.xtext.testing.IInjectorProvider) Exceptions.throwUncheckedException(org.eclipse.xtext.util.Exceptions.throwUncheckedException)

Aggregations

IInjectorProvider (org.eclipse.xtext.testing.IInjectorProvider)2 InjectWith (org.eclipse.xtext.testing.InjectWith)2 Exceptions.throwUncheckedException (org.eclipse.xtext.util.Exceptions.throwUncheckedException)2 ExtensionConfigurationException (org.junit.jupiter.api.extension.ExtensionConfigurationException)1 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)1