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;
}
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);
}
}
}
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;
}
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();
}
}
}
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);
}
Aggregations