mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-16 08:48:55 +00:00
[xtext][junit] Reduce the number of injectors in the tests
Instead of the map of Class<Test> -> InjectProvider a mapping of Class<InjectorProvider> -> InjectorProvider is used.
This commit is contained in:
parent
79768f3ba4
commit
94029a0173
2 changed files with 75 additions and 24 deletions
|
@ -7,24 +7,18 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.xtext.junit4;
|
||||
|
||||
import static org.eclipse.xtext.util.Exceptions.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.xtext.junit4.internal.InjectorProviders;
|
||||
import org.junit.runners.BlockJUnit4ClassRunner;
|
||||
import org.junit.runners.model.FrameworkMethod;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
/**
|
||||
* @author Michael Clay - Initial contribution and API
|
||||
*/
|
||||
public class XtextRunner extends BlockJUnit4ClassRunner {
|
||||
private static Map<Class<?>, IInjectorProvider> injectorProviderClassCache = Maps.newHashMap();
|
||||
|
||||
public XtextRunner(Class<?> klass) throws InitializationError {
|
||||
super(klass);
|
||||
}
|
||||
|
@ -64,29 +58,15 @@ public class XtextRunner extends BlockJUnit4ClassRunner {
|
|||
}
|
||||
|
||||
protected IInjectorProvider getOrCreateInjectorProvider() {
|
||||
IInjectorProvider injectorProvider = getInjectorProvider();
|
||||
if (injectorProvider == null) {
|
||||
injectorProvider = createInjectorProvider();
|
||||
injectorProviderClassCache.put(getTestClass().getJavaClass(), injectorProvider);
|
||||
}
|
||||
return injectorProvider;
|
||||
return InjectorProviders.getOrCreateInjectorProvider(getTestClass());
|
||||
}
|
||||
|
||||
protected IInjectorProvider getInjectorProvider() {
|
||||
return injectorProviderClassCache.get(getTestClass().getJavaClass());
|
||||
return InjectorProviders.getInjectorProvider(getTestClass());
|
||||
}
|
||||
|
||||
protected IInjectorProvider createInjectorProvider() {
|
||||
IInjectorProvider injectorProvider = null;
|
||||
InjectWith injectWith = getTestClass().getJavaClass().getAnnotation(InjectWith.class);
|
||||
if (injectWith != null) {
|
||||
try {
|
||||
injectorProvider = injectWith.value().newInstance();
|
||||
} catch (Exception e) {
|
||||
throwUncheckedException(e);
|
||||
}
|
||||
}
|
||||
return injectorProvider;
|
||||
return InjectorProviders.createInjectorProvider(getTestClass());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2013 itemis AG (http://www.itemis.eu) and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*******************************************************************************/
|
||||
package org.eclipse.xtext.junit4.internal;
|
||||
|
||||
import static org.eclipse.xtext.util.Exceptions.*;
|
||||
|
||||
import org.eclipse.xtext.junit4.IInjectorProvider;
|
||||
import org.eclipse.xtext.junit4.InjectWith;
|
||||
import org.junit.runners.model.TestClass;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import com.google.common.collect.ClassToInstanceMap;
|
||||
import com.google.common.collect.MutableClassToInstanceMap;
|
||||
|
||||
/**
|
||||
* Maintains a map of injector provider types to their instance.
|
||||
*
|
||||
* Clients will usually use {@link #getOrCreateInjectorProvider(TestClass)} to obtain
|
||||
* an valid injector provider in the context of a given {@link TestClass}.
|
||||
*
|
||||
* @author Sebastian Zarnekow - Initial contribution and API
|
||||
*/
|
||||
@Beta
|
||||
public class InjectorProviders {
|
||||
|
||||
private static ClassToInstanceMap<IInjectorProvider> injectorProviderClassCache = MutableClassToInstanceMap.create();
|
||||
|
||||
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.newInstance();
|
||||
injectorProviderClassCache.put(klass, injectorProvider);
|
||||
} catch (Exception e) {
|
||||
throwUncheckedException(e);
|
||||
}
|
||||
}
|
||||
return injectorProvider;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IInjectorProvider getInjectorProvider(TestClass testClass) {
|
||||
InjectWith injectWith = testClass.getJavaClass().getAnnotation(InjectWith.class);
|
||||
if (injectWith != null) {
|
||||
return injectorProviderClassCache.get(injectWith.value());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IInjectorProvider createInjectorProvider(TestClass testClass) {
|
||||
InjectWith injectWith = testClass.getJavaClass().getAnnotation(InjectWith.class);
|
||||
if (injectWith != null) {
|
||||
try {
|
||||
return injectWith.value().newInstance();
|
||||
} catch (Exception e) {
|
||||
throwUncheckedException(e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue