summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Benz2011-07-19 03:26:59 (EDT)
committer Sebastian Zarnekow2011-07-22 09:38:36 (EDT)
commit0284e3ed4e5ed2ce9f13a847ba9fc38a5b807934 (patch)
treef55a467ba3e95e9816a69cf5d572fb25492057c0
parent819f478725af44c2f4f75f9e4869be9694d39296 (diff)
downloadorg.eclipse.xtext-0284e3ed4e5ed2ce9f13a847ba9fc38a5b807934.zip
org.eclipse.xtext-0284e3ed4e5ed2ce9f13a847ba9fc38a5b807934.tar.gz
org.eclipse.xtext-0284e3ed4e5ed2ce9f13a847ba9fc38a5b807934.tar.bz2
Fixed: XtextRunner creates an injector before saving the global registries, which causes the registries to be overwritten when generated standalone setups are used.
Signed-off-by: Sebastian Zarnekow <Sebastian.Zarnekow@itemis.de>
-rw-r--r--plugins/org.eclipse.xtext.junit4/META-INF/MANIFEST.MF3
-rw-r--r--plugins/org.eclipse.xtext.junit4/src/org/eclipse/xtext/junit4/XtextRunner.java24
-rw-r--r--plugins/org.eclipse.xtext.junit4/src/org/eclipse/xtext/junit4/XtextRunnerTest.java85
3 files changed, 103 insertions, 9 deletions
diff --git a/plugins/org.eclipse.xtext.junit4/META-INF/MANIFEST.MF b/plugins/org.eclipse.xtext.junit4/META-INF/MANIFEST.MF
index f27c15f..bd7ad81 100644
--- a/plugins/org.eclipse.xtext.junit4/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.xtext.junit4/META-INF/MANIFEST.MF
@@ -23,4 +23,5 @@ Import-Package: com.google.common.base,
org.junit;version="4.5.0";resolution:=optional,
org.junit.runners;version="4.5.0";resolution:=optional,
org.junit.runners.model;version="4.5.0";resolution:=optional
-Require-Bundle: org.eclipse.xtext;bundle-version="2.0.1"
+Require-Bundle: org.eclipse.xtext;bundle-version="2.0.1",
+ org.junit4;bundle-version="4.8.1";resolution:=optional
diff --git a/plugins/org.eclipse.xtext.junit4/src/org/eclipse/xtext/junit4/XtextRunner.java b/plugins/org.eclipse.xtext.junit4/src/org/eclipse/xtext/junit4/XtextRunner.java
index fd23927..985ea95 100644
--- a/plugins/org.eclipse.xtext.junit4/src/org/eclipse/xtext/junit4/XtextRunner.java
+++ b/plugins/org.eclipse.xtext.junit4/src/org/eclipse/xtext/junit4/XtextRunner.java
@@ -7,6 +7,8 @@
*******************************************************************************/
package org.eclipse.xtext.junit4;
+import static org.eclipse.xtext.util.Exceptions.*;
+
import java.util.Map;
import org.junit.runners.BlockJUnit4ClassRunner;
@@ -41,13 +43,14 @@ public class XtextRunner extends BlockJUnit4ClassRunner {
@Override
protected Statement methodBlock(FrameworkMethod method) {
- final Statement methodBlock = super.methodBlock(method);
- if (getInjectorProvider() instanceof IRegistryConfigurator) {
+ IInjectorProvider injectorProvider = getOrCreateInjectorProvider();
+ if (injectorProvider instanceof IRegistryConfigurator) {
+ final IRegistryConfigurator registryConfigurator = (IRegistryConfigurator) injectorProvider;
+ registryConfigurator.setupRegistry();
+ final Statement methodBlock = super.methodBlock(method);
return new Statement() {
@Override
public void evaluate() throws Throwable {
- IRegistryConfigurator registryConfigurator = (IRegistryConfigurator) getInjectorProvider();
- registryConfigurator.setupRegistry();
try {
methodBlock.evaluate();
} finally {
@@ -55,11 +58,12 @@ public class XtextRunner extends BlockJUnit4ClassRunner {
}
}
};
+ }else{
+ return super.methodBlock(method);
}
- return methodBlock;
}
- protected IInjectorProvider getOrCreateInjectorProvider() throws Exception {
+ protected IInjectorProvider getOrCreateInjectorProvider() {
IInjectorProvider injectorProvider = getInjectorProvider();
if (injectorProvider == null) {
injectorProvider = createInjectorProvider();
@@ -72,11 +76,15 @@ public class XtextRunner extends BlockJUnit4ClassRunner {
return injectorProviderClassCache.get(getTestClass().getJavaClass());
}
- protected IInjectorProvider createInjectorProvider() throws Exception {
+ protected IInjectorProvider createInjectorProvider() {
IInjectorProvider injectorProvider = null;
InjectWith injectWith = getTestClass().getJavaClass().getAnnotation(InjectWith.class);
if (injectWith != null) {
- injectorProvider = injectWith.value().newInstance();
+ try {
+ injectorProvider = injectWith.value().newInstance();
+ } catch (Exception e) {
+ throwUncheckedException(e);
+ }
}
return injectorProvider;
}
diff --git a/plugins/org.eclipse.xtext.junit4/src/org/eclipse/xtext/junit4/XtextRunnerTest.java b/plugins/org.eclipse.xtext.junit4/src/org/eclipse/xtext/junit4/XtextRunnerTest.java
new file mode 100644
index 0000000..43d6f13
--- /dev/null
+++ b/plugins/org.eclipse.xtext.junit4/src/org/eclipse/xtext/junit4/XtextRunnerTest.java
@@ -0,0 +1,85 @@
+/*******************************************************************************
+ * Copyright (c) 2011 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;
+
+import static org.junit.Assert.*;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.google.inject.Binder;
+import com.google.inject.Guice;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.Module;
+
+/**
+ * @author Sebastian Benz - Initial contribution and API
+ */
+@InjectWith(XtextRunnerTest.MyInjectorProvider.class)
+@RunWith(XtextRunner.class)
+public class XtextRunnerTest {
+
+ private static boolean injectorCreated = false;
+ private static boolean registrySaved = false;
+ private static boolean registryRestored = false;
+
+ @Inject
+ private Boolean fieldsInjected = false;
+
+ public static class MyInjectorProvider implements IRegistryConfigurator, IInjectorProvider {
+
+
+ public Injector getInjector() {
+ injectorCreated = true;
+
+ assertTrue(registrySaved);
+
+ return Guice.createInjector(new Module(){
+
+ public void configure(Binder binder) {
+ binder.bind(Boolean.class).toInstance(Boolean.TRUE);
+ }
+
+ });
+ }
+
+ public void setupRegistry() {
+ registrySaved = true;
+ assertFalse(injectorCreated);
+ }
+
+ public void restoreRegistry() {
+ assertTrue(registrySaved);
+ registryRestored = true;
+ }
+
+ }
+
+
+
+ @Before
+ public void beforeShouldBeExecutedAfterTheRegistriesAreInitialized(){
+ assertTrue(registrySaved);
+ assertTrue(injectorCreated);
+ assertTrue(fieldsInjected);
+ }
+
+ @Test
+ public void shouldSaveRegistriesBeforeCreatingAnInjector() {
+ // tests are performed in MyInjectorProvider
+ }
+
+ @After
+ public void afterShouldBeExecutedBeforeTheRegistriesAreRestored(){
+ assertFalse(registryRestored);
+ }
+
+}