org.eclipse.xtext.testing.IInjectorProvider Java Examples

The following examples show how to use org.eclipse.xtext.testing.IInjectorProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XtextParametrizedRunner.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Implements behavior from: org.junit.runners.Parameterized$TestClassRunnerForParameters
 * org.eclipse.xtext.testing.XtextRunner
 */
@Override
public Object createTest() throws Exception {
	Object object;
	// Functionality of
	// org.junit.runners.Parameterized$TestClassRunnerForParameters
	if (fieldsAreAnnotated()) {
		object = createTestUsingFieldInjection();
	} else {
		object = createTestUsingConstructorInjection();
	}

	// Functionality of org.eclipse.xtext.testing.XtextRunner
	IInjectorProvider injectorProvider = getOrCreateInjectorProvider();
	if (injectorProvider != null) {
		Injector injector = injectorProvider.getInjector();
		if (injector != null)
			injector.injectMembers(object);
	}
	return object;
}
 
Example #2
Source File: AbstractScenarioRunner.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
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();
		}
	}
}
 
Example #3
Source File: InjectionExtension.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private static IInjectorProvider getOrCreateInjectorProvider(ExtensionContext context) {
	InjectWith injectWith = getInjectWith(context.getRequiredTestClass());
	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;
}
 
Example #4
Source File: InjectorProviders.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
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;
}
 
Example #5
Source File: AbstractInjectorExtension.java    From sarl with Apache License 2.0 6 votes vote down vote up
private static IInjectorProvider createInjectorProvider(ExtensionContext context) {
	InjectWith injectWith = context.getRequiredTestClass().getAnnotation(InjectWith.class);
	if (injectWith != null) {
		Class<? extends IInjectorProvider> klass = injectWith.value();
		try {
			// TODO this mode of creation is not efficient, but it ensures that the injector providers are really reset.
			final IInjectorProvider injectorProvider = klass.getDeclaredConstructor().newInstance();
			if (injectorProvider instanceof IRegistryConfigurator) {
				final IRegistryConfigurator registryConfigurator = (IRegistryConfigurator) injectorProvider;
				registryConfigurator.setupRegistry();
			}
			return injectorProvider;
		} catch (Exception e) {
			throwUncheckedException(e);
		}
	}
	throw new IllegalStateException("Expected valid @InjectWith annotation");
}
 
Example #6
Source File: XtextParametersRunnerFactory.java    From solidity-ide with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected Object createTest() throws Exception {
	Object object = getTestClass().getOnlyConstructor().newInstance(parameters.toArray());
	IInjectorProvider injectorProvider = getOrCreateInjectorProvider();
	if (injectorProvider != null) {
		Injector injector = injectorProvider.getInjector();
		if (injector != null)
			injector.injectMembers(object);
	}
	return object;
}
 
Example #7
Source File: AbstractParallelScenarioRunner.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected Statement childrenInvoker(final RunNotifier notifier) {
	return new Statement() {
		@Override
		public void evaluate() throws Throwable {
			WrappingInjectorProvider wrapped = getOrCreateInjectorProvider();
			wrapped.setupRegistry();
			try {
				prepareChildren(notifier);
			} finally {
				wrapped.restoreRegistry();
			}
			IInjectorProvider delegate = wrapped.getDelegate();
			if (delegate instanceof IRegistryConfigurator) {
				IRegistryConfigurator registryConfigurator = (IRegistryConfigurator) delegate;
				registryConfigurator.setupRegistry();
				try {
					runChildren(notifier);
				} finally {
					registryConfigurator.restoreRegistry();
				}
			} else {
				runChildren(notifier);
			}
		}
	};
}
 
Example #8
Source File: AbstractParallelScenarioRunner.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example #9
Source File: WrappingInjectorProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public WrappingInjectorProvider(IInjectorProvider delegate) {
	this.delegate = delegate;
	stateBeforeInjectorCreation = GlobalRegistries.makeCopyOfGlobalState();
	this.injector = createInjector();
	if (delegate instanceof IRegistryConfigurator) {
		((IRegistryConfigurator) delegate).setupRegistry();
	}
	registerFactory(injector);
	stateAfterInjectorCreation = GlobalRegistries.makeCopyOfGlobalState();
	if (delegate instanceof IRegistryConfigurator) {
		((IRegistryConfigurator) delegate).restoreRegistry();
	}
	stateBeforeInjectorCreation.restoreGlobalState();
}
 
Example #10
Source File: InjectorCache.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public static WrappingInjectorProvider wrap(IInjectorProvider delegate) {
	WrappingInjectorProvider result = cachedInjectors.get(delegate);
	if (result == null) {
		result = new WrappingInjectorProvider(delegate);
		cachedInjectors.put(delegate, result);
	}
	return result;
}
 
Example #11
Source File: InjectionExtension.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void beforeEach(ExtensionContext context) throws Exception {
	IInjectorProvider injectorProvider = getOrCreateInjectorProvider(context);
	if (injectorProvider instanceof IRegistryConfigurator) {
		final IRegistryConfigurator registryConfigurator = (IRegistryConfigurator) injectorProvider;
		registryConfigurator.setupRegistry();
	}
	if (injectorProvider != null) {
		Injector injector = injectorProvider.getInjector();
		if (injector != null) {
			Object testInstance = context.getRequiredTestInstance();
			injector.injectMembers(testInstance);
			try {
				TestInstances requiredTestInstances = context.getRequiredTestInstances();
				for (Object o : requiredTestInstances.getEnclosingInstances()) {
					injector.injectMembers(o);
				}
			} catch (NoSuchMethodError e) {
				if (!Modifier.isStatic(testInstance.getClass().getModifiers())) {
					if (testInstance.getClass().getDeclaringClass() != null) {
						throw new ExtensionConfigurationException("Injection of nested classes needs Junit5 >= 5.4", e);
					}
				}
				// OK, getRequiredTestInstances is not there in Junit5 < 5.4
			}
		}
	}
}
 
Example #12
Source File: InjectionExtension.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void afterEach(ExtensionContext context) throws Exception {
	IInjectorProvider injectorProvider = getOrCreateInjectorProvider(context);
	if (injectorProvider instanceof IRegistryConfigurator) {
		final IRegistryConfigurator registryConfigurator = (IRegistryConfigurator) injectorProvider;
		registryConfigurator.restoreRegistry();
	}
}
 
Example #13
Source File: InjectorProviders.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public static IInjectorProvider getInjectorProvider(TestClass testClass) {
	InjectWith injectWith = testClass.getJavaClass().getAnnotation(InjectWith.class);
	if (injectWith != null) {
		return injectorProviderClassCache.get(injectWith.value());
	}
	return null;
}
 
Example #14
Source File: InjectorProviders.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public static IInjectorProvider createInjectorProvider(TestClass testClass) {
	InjectWith injectWith = testClass.getJavaClass().getAnnotation(InjectWith.class);
	if (injectWith != null) {
		try {
			return injectWith.value().getDeclaredConstructor().newInstance();
		} catch (Exception e) {
			throwUncheckedException(e);
		}
	}
	return null;
}
 
Example #15
Source File: AbstractInjectorExtension.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Create the injector and inject the given instance.
 *
 * @param instance the instance to inject
 */
public void injectMembers(Object instance, ExtensionContext context) throws Exception {
	IInjectorProvider injectorProvider = createInjectorProvider(context);
	if (injectorProvider != null) {
		Injector injector = injectorProvider.getInjector();
		if (injector != null)
			injector.injectMembers(instance);
	}
}
 
Example #16
Source File: WrappingInjectorProvider.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public IInjectorProvider getDelegate() {
	return delegate;
}