Skip to content

Commit 95a2dbc

Browse files
committed
testing JUnit 5 equivalent of XpectRunner
1 parent e27f2f2 commit 95a2dbc

File tree

14 files changed

+611
-12
lines changed

14 files changed

+611
-12
lines changed

org.eclipse.xpect.ui/src/org/eclipse/xpect/ui/util/XpectFileAccess.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.eclipse.xpect.XpectFile;
3131
import org.eclipse.xpect.registry.ILanguageInfo;
3232
import org.eclipse.xpect.runner.XpectRunner;
33+
import org.eclipse.xpect.runner.XpectTestGlobalState;
3334

3435
import com.google.inject.Injector;
3536

@@ -67,8 +68,8 @@ protected static ResourceSet cloneResourceSet(ResourceSet rs) {
6768
// need delegation or nothing because of "java" protocol
6869
// result.setResourceFactoryRegistry(rs.getResourceFactoryRegistry());
6970
result.setURIConverter(rs.getURIConverter());
70-
if (XpectRunner.testClassloader != null) {
71-
result.setClasspathURIContext(XpectRunner.testClassloader);
71+
if (XpectTestGlobalState.INSTANCE.testClass() != null) {
72+
result.setClasspathURIContext(XpectTestGlobalState.INSTANCE.testClass().getClassLoader());
7273
result.setClasspathUriResolver(new ClassloaderClasspathUriResolver());
7374
} else if (rs instanceof XtextResourceSet) {
7475
XtextResourceSet xrs = (XtextResourceSet) rs;

org.eclipse.xpect.ui/src/org/eclipse/xpect/ui/util/XpectUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.eclipse.xpect.XpectFile;
2929
import org.eclipse.xpect.XpectJavaModel;
3030
import org.eclipse.xpect.runner.XpectRunner;
31+
import org.eclipse.xpect.runner.XpectTestGlobalState;
3132
import org.eclipse.xpect.ui.internal.XpectActivator;
3233

3334
import com.google.inject.Injector;
@@ -40,8 +41,8 @@ public static XpectFile loadFile(IFile file) {
4041
Injector injector = XpectActivator.getInstance().getInjector(XpectActivator.ORG_ECLIPSE_XPECT_XPECT);
4142
XtextResourceSet rs = new XtextResourceSet();
4243
IJavaProject javaProject = JavaCore.create(file.getProject());
43-
if (XpectRunner.testClassloader != null) {
44-
rs.setClasspathURIContext(XpectRunner.testClassloader);
44+
if (XpectTestGlobalState.INSTANCE.testClass() != null) {
45+
rs.setClasspathURIContext(XpectTestGlobalState.INSTANCE.testClass().getClassLoader());
4546
rs.setClasspathUriResolver(new ClassloaderClasspathUriResolver());
4647
} else if (javaProject != null && javaProject.exists()) {
4748
rs.setClasspathURIContext(javaProject);

org.eclipse.xpect/META-INF/MANIFEST.MF

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ Require-Bundle: org.eclipse.xtext,
1919
org.antlr.runtime;bundle-version="[3.2.0,3.2.1)",
2020
org.apache.log4j;bundle-version="1.2.24",
2121
org.junit;bundle-version="4.11.0";visibility:=reexport,
22+
junit-jupiter-api;bundle-version="5.9.1";visibility:=reexport,
2223
org.eclipse.xtext.common.types;visibility:=reexport,
2324
org.apache.log4j;bundle-version="1.2.0";visibility:=reexport,
2425
org.objectweb.asm;bundle-version="[9.5.0,10.0.0)";resolution:=optional
2526
Bundle-RequiredExecutionEnvironment: JavaSE-11
2627
Export-Package: org.eclipse.xpect,
28+
org.eclipse.xpect.dynamic,
2729
org.eclipse.xpect.expectation,
2830
org.eclipse.xpect.expectation.impl;
2931
x-friends:="org.eclipse.xpect.xtext.lib,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.eclipse.xpect.dynamic;
2+
3+
import java.util.stream.Stream;
4+
5+
import org.eclipse.xpect.XpectImport;
6+
import org.eclipse.xpect.runner.TestTitleProvider;
7+
import org.junit.jupiter.api.DynamicNode;
8+
import org.junit.jupiter.api.TestFactory;
9+
10+
@XpectImport(TestTitleProvider.class)
11+
public interface IXpectDynamicTestFactory {
12+
13+
@TestFactory
14+
default Stream<DynamicNode> tests() {
15+
return XpectDynamicTestFactory.xpectTests(getClass());
16+
}
17+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.eclipse.xpect.dynamic;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
5+
import org.eclipse.xpect.XjmTestMethod;
6+
import org.eclipse.xpect.runner.ValidatingSetup;
7+
import org.eclipse.xpect.runner.XpectTestGlobalState;
8+
import org.eclipse.xpect.setup.ThisTestObject;
9+
import org.eclipse.xpect.state.Creates;
10+
import org.eclipse.xpect.state.StateContainer;
11+
import org.junit.jupiter.api.DynamicTest;
12+
13+
import com.google.common.base.Preconditions;
14+
15+
public class XpectDynamicTest {
16+
17+
private final String className;
18+
private XjmTestMethod method;
19+
private final StateContainer state;
20+
21+
public XpectDynamicTest(StateContainer state, XjmTestMethod method) {
22+
Preconditions.checkNotNull(method);
23+
this.className = XpectTestGlobalState.INSTANCE.testClass().getName();
24+
this.method = method;
25+
this.state = state;
26+
}
27+
28+
@Creates
29+
public XpectDynamicTest create() {
30+
return this;
31+
}
32+
33+
public XjmTestMethod getMethod() {
34+
return method;
35+
}
36+
37+
public StateContainer getState() {
38+
return state;
39+
}
40+
41+
public DynamicTest test() {
42+
String testName = getName();
43+
return DynamicTest.dynamicTest(testName, () -> runInternal());
44+
}
45+
46+
public String getName() {
47+
String testName = formatDisplayName(method.getName(), className);
48+
return testName;
49+
}
50+
51+
protected void runInternal() throws Throwable {
52+
Object test = state.get(Object.class, ThisTestObject.class).get();
53+
try {
54+
method.getJavaMethod().invoke(test);
55+
} catch (InvocationTargetException e) {
56+
throw e.getCause();
57+
}
58+
}
59+
60+
private static String formatDisplayName(String name, String className) {
61+
return String.format("%s(%s)", name, className);
62+
}
63+
64+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package org.eclipse.xpect.dynamic;
2+
3+
import java.util.List;
4+
import java.util.concurrent.atomic.AtomicBoolean;
5+
import java.util.concurrent.atomic.AtomicInteger;
6+
7+
import org.eclipse.emf.common.util.URI;
8+
import org.eclipse.xpect.XjmTestMethod;
9+
import org.eclipse.xpect.XpectFile;
10+
import org.eclipse.xpect.XpectInvocation;
11+
import org.eclipse.xpect.XpectJavaModel;
12+
import org.eclipse.xpect.runner.IXpectURIProvider;
13+
import org.eclipse.xpect.runner.TestExecutor;
14+
import org.eclipse.xpect.runner.ValidatingSetup;
15+
import org.eclipse.xpect.setup.ThisRootTestClass;
16+
import org.eclipse.xpect.state.Creates;
17+
import org.eclipse.xpect.state.StateContainer;
18+
import org.junit.jupiter.api.DynamicTest;
19+
20+
import com.google.common.collect.Iterables;
21+
import com.google.common.collect.Lists;
22+
23+
public class XpectDynamicTestCase {
24+
private List<DynamicTest> children;
25+
private final StateContainer state;
26+
private final XpectFile xpectFile;
27+
28+
public XpectDynamicTestCase(StateContainer state, XpectFile file) {
29+
this.xpectFile = file;
30+
this.state = state;
31+
}
32+
33+
@Creates
34+
public XpectDynamicTestCase create() {
35+
return this;
36+
}
37+
38+
/**
39+
* <p>
40+
* To run code before an Xpect test begins, extend this class and annotate the extending class with:
41+
* {@code @org.eclipse.xpect.XpectReplace(org.eclipse.xpect.dynamic.XpectDynamicTestCase)}
42+
* </p>
43+
* <p>
44+
* The extended class must then be included in the values of the annotation of the test case:
45+
* {@code @org.eclipse.xpect.XpectImport({...class})}
46+
* </p>
47+
*/
48+
public void setUp() throws Throwable {
49+
// nothing to set up
50+
}
51+
52+
/**
53+
* <p>
54+
* To run code after an Xpect test finishes, extend this class and annotate the extending class with:
55+
* {@code @org.eclipse.xpect.XpectReplace(org.eclipse.xpect.dynamic.XpectDynamicTestCase)}
56+
* </p>
57+
* <p>
58+
* The extended class must then be included in the values of the annotation of the test case:
59+
* {@code @org.eclipse.xpect.XpectImport({...class})}
60+
* </p>
61+
*/
62+
public void tearDown() throws Throwable {
63+
// nothing to tear down
64+
}
65+
66+
protected List<DynamicTest> createChildren() {
67+
List<DynamicTest> tests = Lists.newArrayList();
68+
if (xpectFile != null) {
69+
/*
70+
* With JUnit 4 runners, we can do setup validation before children run and state clean-up after children are done.
71+
* With JUnit 5 we cannot.
72+
* So the first test does setup validation and the last test does clean-up.
73+
* Meaning their execution times will likely increase notably, when compared to the JUnit 4 reported time.
74+
* Alternatively we can add a first and last test, for setup validation resp. clean-up. This means extra test nodes in the result, as well as extra test results. We then also have the problem of running tests if the setup validation failed.
75+
* XXX: does JUnit 5 offer anything for dynamic tests, to improve this situation?
76+
* As of writing this code, @BeforeEach and @AfterEach are only called before and after the factory method runs.
77+
* We have a factory method with every URL we must run an Xpect test for, as those URLs are known only via an annotation.
78+
*/
79+
AtomicBoolean setupValidated = new AtomicBoolean(false);
80+
AtomicBoolean setupValidationFailed = new AtomicBoolean(false);
81+
AtomicInteger finishedChildren = new AtomicInteger(0);
82+
XpectJavaModel xjm = xpectFile.getJavaModel();
83+
XjmTestMethod[] methods = xjm.getMethods().values().stream().filter(m -> m instanceof XjmTestMethod).toArray(XjmTestMethod[]::new);
84+
XpectInvocation[] invocations = Iterables.toArray(xpectFile.getInvocations(), XpectInvocation.class);
85+
int childrenCount = methods.length + invocations.length;
86+
for (XjmTestMethod method : methods) {
87+
DynamicTest test = createDynamicTest(method);
88+
tests.add(wrapTest(test, childrenCount, setupValidated, setupValidationFailed, finishedChildren));
89+
}
90+
for (XpectInvocation inv : invocations) {
91+
DynamicTest test = createDynamicTest(inv);
92+
tests.add(wrapTest(test, childrenCount, setupValidated, setupValidationFailed, finishedChildren));
93+
}
94+
}
95+
return tests;
96+
}
97+
98+
protected DynamicTest createDynamicTest(XjmTestMethod method) {
99+
StateContainer childState = TestExecutor.createState(state, TestExecutor.createTestConfiguration(method));
100+
return childState.get(XpectDynamicTest.class).get().test();
101+
}
102+
103+
protected DynamicTest createDynamicTest(XpectInvocation invocation) {
104+
StateContainer childState = TestExecutor.createState(state, TestExecutor.createXpectConfiguration(invocation));
105+
DynamicTest test = childState.get(XpectInvocationDynamicTest.class).get().test();
106+
return test;
107+
}
108+
109+
protected DynamicTest wrapTest(DynamicTest test, final int childrenCount, AtomicBoolean validatedSetup, AtomicBoolean setupValidationFailed, AtomicInteger finishedChildren) {
110+
return DynamicTest.dynamicTest(test.getDisplayName(), () -> {
111+
try {
112+
if (!validatedSetup.getAndSet(true)) {
113+
// first test is running, validate setup
114+
try {
115+
state.get(ValidatingSetup.class).get().validate();
116+
} catch (Throwable t) {
117+
setupValidationFailed.set(true);
118+
throw t;
119+
}
120+
}
121+
if (setupValidationFailed.get()) {
122+
throw new AssertionError("Setup validation failed");
123+
}
124+
try {
125+
setUp();
126+
test.getExecutable().execute();
127+
} finally {
128+
tearDown();
129+
}
130+
} finally {
131+
int finished = finishedChildren.incrementAndGet();
132+
if (finished >= childrenCount) {
133+
// last test is done, do clean-up
134+
state.invalidate();
135+
}
136+
}
137+
});
138+
}
139+
140+
protected List<DynamicTest> getChildren() {
141+
if (children == null)
142+
children = createChildren();
143+
return children;
144+
}
145+
146+
public Class<?> getJavaTestClass() {
147+
return state.get(Class.class, ThisRootTestClass.class).get();
148+
}
149+
150+
public IXpectURIProvider getURIProvider() {
151+
return state.get(IXpectURIProvider.class).get();
152+
}
153+
154+
public StateContainer getState() {
155+
return state;
156+
}
157+
158+
public URI getUri() {
159+
return xpectFile.eResource().getURI();
160+
}
161+
162+
public XpectFile getXpectFile() {
163+
return xpectFile;
164+
}
165+
166+
public String getName() {
167+
IXpectURIProvider uriProvider = getURIProvider();
168+
URI uri = getUri();
169+
URI deresolved = uriProvider.deresolveToProject(uri);
170+
String pathInProject = deresolved.trimSegments(1).toString();
171+
String fileName = deresolved.lastSegment();
172+
return fileName + ": " + pathInProject;
173+
}
174+
}

0 commit comments

Comments
 (0)