Search in sources :

Example 1 with IInjectorProvider

use of org.eclipse.xtext.testing.IInjectorProvider 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 IInjectorProvider

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

the class InjectionExtension method postProcessTestInstance.

/**
 * @since 2.24
 */
@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {
    IInjectorProvider injectorProvider = getOrCreateInjectorProvider(context);
    if (injectorProvider != null) {
        setupRegistry(injectorProvider, context);
        Injector injector = injectorProvider.getInjector();
        if (injector != null) {
            injector.injectMembers(testInstance);
        }
    }
}
Also used : IInjectorProvider(org.eclipse.xtext.testing.IInjectorProvider) Injector(com.google.inject.Injector)

Example 3 with IInjectorProvider

use of org.eclipse.xtext.testing.IInjectorProvider 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)

Example 4 with IInjectorProvider

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

the class AbstractScenarioRunner method process.

protected void process(String data) throws Exception {
    IInjectorProvider delegate = getOrCreateInjectorProvider().getDelegate();
    if (delegate instanceof IRegistryConfigurator) {
        IRegistryConfigurator registryConfigurator = (IRegistryConfigurator) delegate;
        registryConfigurator.setupRegistry();
        try {
            ScenarioProcessor processor = delegate.getInjector().getInstance(processorClass);
            String preProcessed = processor.preProcess(data);
            if (preProcessed == null) {
                throw new AssumptionViolatedException("Input is filtered by the pre processing step: " + data);
            }
            doProcess(preProcessed, processor);
        } finally {
            registryConfigurator.restoreRegistry();
        }
    }
}
Also used : ScenarioProcessor(org.eclipse.xtext.testing.smoketest.ScenarioProcessor) IInjectorProvider(org.eclipse.xtext.testing.IInjectorProvider) AssumptionViolatedException(org.junit.AssumptionViolatedException) IRegistryConfigurator(org.eclipse.xtext.testing.IRegistryConfigurator)

Example 5 with IInjectorProvider

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

the class AbstractParallelScenarioRunner method process.

@Override
protected void process(String data) throws Exception {
    IInjectorProvider delegate = getOrCreateInjectorProvider().getDelegate();
    ScenarioProcessor processor = delegate.getInjector().getInstance(getProcessorClass());
    String preProcessed = processor.preProcess(data);
    if (preProcessed == null) {
        throw new AssumptionViolatedException("Input is filtered by the pre processing step: " + data);
    }
    doProcess(preProcessed, processor);
}
Also used : ScenarioProcessor(org.eclipse.xtext.testing.smoketest.ScenarioProcessor) IInjectorProvider(org.eclipse.xtext.testing.IInjectorProvider) AssumptionViolatedException(org.junit.AssumptionViolatedException)

Aggregations

IInjectorProvider (org.eclipse.xtext.testing.IInjectorProvider)9 IRegistryConfigurator (org.eclipse.xtext.testing.IRegistryConfigurator)3 AssumptionViolatedException (org.junit.AssumptionViolatedException)3 Injector (com.google.inject.Injector)2 InjectWith (org.eclipse.xtext.testing.InjectWith)2 ScenarioProcessor (org.eclipse.xtext.testing.smoketest.ScenarioProcessor)2 Exceptions.throwUncheckedException (org.eclipse.xtext.util.Exceptions.throwUncheckedException)2 Statement (org.junit.runners.model.Statement)2 Provider (com.google.inject.Provider)1 ExtensionConfigurationException (org.junit.jupiter.api.extension.ExtensionConfigurationException)1 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)1 FrameworkMethod (org.junit.runners.model.FrameworkMethod)1