Skip to content

Commit

Permalink
testing JUnit 5 equivalent of XpectRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
trancexpress committed May 24, 2024
1 parent e27f2f2 commit 0995b3f
Show file tree
Hide file tree
Showing 14 changed files with 603 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.xpect.XpectFile;
import org.eclipse.xpect.registry.ILanguageInfo;
import org.eclipse.xpect.runner.XpectRunner;
import org.eclipse.xpect.runner.XpectTestGlobalState;

import com.google.inject.Injector;

Expand Down Expand Up @@ -67,8 +68,8 @@ protected static ResourceSet cloneResourceSet(ResourceSet rs) {
// need delegation or nothing because of "java" protocol
// result.setResourceFactoryRegistry(rs.getResourceFactoryRegistry());
result.setURIConverter(rs.getURIConverter());
if (XpectRunner.testClassloader != null) {
result.setClasspathURIContext(XpectRunner.testClassloader);
if (XpectTestGlobalState.INSTANCE.testClass() != null) {
result.setClasspathURIContext(XpectTestGlobalState.INSTANCE.testClass().getClassLoader());
result.setClasspathUriResolver(new ClassloaderClasspathUriResolver());
} else if (rs instanceof XtextResourceSet) {
XtextResourceSet xrs = (XtextResourceSet) rs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.xpect.XpectFile;
import org.eclipse.xpect.XpectJavaModel;
import org.eclipse.xpect.runner.XpectRunner;
import org.eclipse.xpect.runner.XpectTestGlobalState;
import org.eclipse.xpect.ui.internal.XpectActivator;

import com.google.inject.Injector;
Expand All @@ -40,8 +41,8 @@ public static XpectFile loadFile(IFile file) {
Injector injector = XpectActivator.getInstance().getInjector(XpectActivator.ORG_ECLIPSE_XPECT_XPECT);
XtextResourceSet rs = new XtextResourceSet();
IJavaProject javaProject = JavaCore.create(file.getProject());
if (XpectRunner.testClassloader != null) {
rs.setClasspathURIContext(XpectRunner.testClassloader);
if (XpectTestGlobalState.INSTANCE.testClass() != null) {
rs.setClasspathURIContext(XpectTestGlobalState.INSTANCE.testClass().getClassLoader());
rs.setClasspathUriResolver(new ClassloaderClasspathUriResolver());
} else if (javaProject != null && javaProject.exists()) {
rs.setClasspathURIContext(javaProject);
Expand Down
2 changes: 2 additions & 0 deletions org.eclipse.xpect/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ Require-Bundle: org.eclipse.xtext,
org.antlr.runtime;bundle-version="[3.2.0,3.2.1)",
org.apache.log4j;bundle-version="1.2.24",
org.junit;bundle-version="4.11.0";visibility:=reexport,
junit-jupiter-api;bundle-version="5.9.1";visibility:=reexport,
org.eclipse.xtext.common.types;visibility:=reexport,
org.apache.log4j;bundle-version="1.2.0";visibility:=reexport,
org.objectweb.asm;bundle-version="[9.5.0,10.0.0)";resolution:=optional
Bundle-RequiredExecutionEnvironment: JavaSE-11
Export-Package: org.eclipse.xpect,
org.eclipse.xpect.dynamic,
org.eclipse.xpect.expectation,
org.eclipse.xpect.expectation.impl;
x-friends:="org.eclipse.xpect.xtext.lib,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.eclipse.xpect.dynamic;

import java.util.stream.Stream;

import org.eclipse.xpect.XpectImport;
import org.eclipse.xpect.runner.TestTitleProvider;
import org.junit.jupiter.api.DynamicContainer;
import org.junit.jupiter.api.TestFactory;

@XpectImport(TestTitleProvider.class)
public interface IXpectDynamicTestFactory {

@TestFactory
default Stream<DynamicContainer> generateTests() {
return XpectDynamicTestFactory.xpectTests(getClass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.eclipse.xpect.dynamic;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.xpect.XjmTestMethod;
import org.eclipse.xpect.runner.ValidatingSetup;
import org.eclipse.xpect.runner.XpectTestGlobalState;
import org.eclipse.xpect.setup.ThisTestObject;
import org.eclipse.xpect.state.Creates;
import org.eclipse.xpect.state.StateContainer;
import org.junit.jupiter.api.DynamicTest;

import com.google.common.base.Preconditions;

public class XpectDynamicTest {

private final String className;
private XjmTestMethod method;
private final StateContainer state;

public XpectDynamicTest(StateContainer state, XjmTestMethod method) {
Preconditions.checkNotNull(method);
this.className = XpectTestGlobalState.INSTANCE.testClass().getName();
this.method = method;
this.state = state;
}

@Creates
public XpectDynamicTest create() {
return this;
}

public XjmTestMethod getMethod() {
return method;
}

public StateContainer getState() {
return state;
}

public DynamicTest test() {
String testName = getName();
return DynamicTest.dynamicTest(testName, () -> runInternal());
}

public String getName() {
String testName = formatDisplayName(method.getName(), className);
return testName;
}

protected void runInternal() throws Throwable {
Object test = state.get(Object.class, ThisTestObject.class).get();
try {
method.getJavaMethod().invoke(test);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}

private static String formatDisplayName(String name, String className) {
return String.format("%s(%s)", name, className);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package org.eclipse.xpect.dynamic;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.emf.common.util.URI;
import org.eclipse.xpect.XjmMethod;
import org.eclipse.xpect.XjmTestMethod;
import org.eclipse.xpect.XpectFile;
import org.eclipse.xpect.XpectInvocation;
import org.eclipse.xpect.XpectJavaModel;
import org.eclipse.xpect.runner.IXpectURIProvider;
import org.eclipse.xpect.runner.TestExecutor;
import org.eclipse.xpect.runner.ValidatingSetup;
import org.eclipse.xpect.setup.ThisRootTestClass;
import org.eclipse.xpect.state.Creates;
import org.eclipse.xpect.state.StateContainer;
import org.junit.jupiter.api.DynamicContainer;
import org.junit.jupiter.api.DynamicTest;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

public class XpectDynamicTestCase {
private List<DynamicTest> children;
private final StateContainer state;
private final XpectFile xpectFile;

public XpectDynamicTestCase(StateContainer state, XpectFile file) {
this.xpectFile = file;
this.state = state;
}

@Creates
public XpectDynamicTestCase create() {
return this;
}

/**
* <p>
* To run code before an Xpect test begins, extend this class and annotate the extending class with:
* {@code @org.eclipse.xpect.XpectReplace(org.eclipse.xpect.dynamic.XpectDynamicTestCase)}
* </p>
* <p>
* The extended class must then be included in the values of the annotation of the test case:
* {@code @org.eclipse.xpect.XpectImport({...class})}
* </p>
*/
public void setUp() throws Throwable {
// nothing to set up
}

/**
* <p>
* To run code after an Xpect test finishes, extend this class and annotate the extending class with:
* {@code @org.eclipse.xpect.XpectReplace(org.eclipse.xpect.dynamic.XpectDynamicTestCase)}
* </p>
* <p>
* The extended class must then be included in the values of the annotation of the test case:
* {@code @org.eclipse.xpect.XpectImport({...class})}
* </p>
*/
public void tearDown() throws Throwable {
// nothing to tear down
}

public DynamicContainer dynamicContainer() {
return DynamicContainer.dynamicContainer(getName(), getChildren());
}

protected List<DynamicTest> createChildren() {
List<DynamicTest> children = Lists.newArrayList();
if (xpectFile != null) {
/*
* With JUnit 4 runners, we can do setup validation before children run and state clean-up after children are done.
* With JUnit 5 we cannot.
* So the first test does setup validation and the last test does clean-up.
* Meaning their execution times will likely increase notably, when compared to the JUnit 4 reported time.
* 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.
* XXX: does JUnit 5 offer anything for dynamic tests, to improve this situation?
* As of writing this code, @BeforeEach and @AfterEach are only called before and after the factory method runs.
* We have a factory method with every URL we must run an Xpect test for, as those URLs are known only via an annotation.
*/
AtomicBoolean setupValidated = new AtomicBoolean(false);
AtomicBoolean setupValidationFailed = new AtomicBoolean(false);
AtomicInteger finishedChildren = new AtomicInteger(0);
XpectJavaModel xjm = xpectFile.getJavaModel();
XjmTestMethod[] methods = xjm.getMethods().values().stream().filter(m -> m instanceof XjmTestMethod).toArray(XjmTestMethod[]::new);
XpectInvocation[] invocations = Iterables.toArray(xpectFile.getInvocations(), XpectInvocation.class);
int childrenCount = methods.length + invocations.length;
for (XjmTestMethod method : methods) {
DynamicTest test = createDynamicTest(method);
children.add(wrapTest(test, childrenCount, setupValidated, setupValidationFailed, finishedChildren));
}
for (XpectInvocation inv : invocations) {
DynamicTest test = createDynamicTest(inv);
children.add(wrapTest(test, childrenCount, setupValidated, setupValidationFailed, finishedChildren));
}
}
return children;
}

protected DynamicTest createDynamicTest(XjmTestMethod method) {
StateContainer childState = TestExecutor.createState(state, TestExecutor.createTestConfiguration(method));
return childState.get(XpectDynamicTest.class).get().test();
}

protected DynamicTest createDynamicTest(XpectInvocation invocation) {
StateContainer childState = TestExecutor.createState(state, TestExecutor.createXpectConfiguration(invocation));
DynamicTest test = childState.get(XpectInvocationDynamicTest.class).get().test();
return test;
}

protected DynamicTest wrapTest(DynamicTest test, final int childrenCount, AtomicBoolean validatedSetup, AtomicBoolean setupValidationFailed, AtomicInteger finishedChildren) {
return DynamicTest.dynamicTest(test.getDisplayName(), () -> {
try {
if (!validatedSetup.getAndSet(true)) {
// first test is running, validate setup
try {
state.get(ValidatingSetup.class).get().validate();
} catch (Throwable t) {
setupValidationFailed.set(true);
throw t;
}
}
if (setupValidationFailed.get()) {
throw new AssertionError("Setup validation failed");
}
try {
setUp();
test.getExecutable().execute();
} finally {
tearDown();
}
} finally {
int finished = finishedChildren.incrementAndGet();
if (finished >= childrenCount) {
// last test is done, do clean-up
state.invalidate();
}
}
});
}

protected List<DynamicTest> getChildren() {
if (children == null)
children = createChildren();
return children;
}

public Class<?> getJavaTestClass() {
return state.get(Class.class, ThisRootTestClass.class).get();
}

public IXpectURIProvider getURIProvider() {
return state.get(IXpectURIProvider.class).get();
}

public StateContainer getState() {
return state;
}

public URI getUri() {
return xpectFile.eResource().getURI();
}

public XpectFile getXpectFile() {
return xpectFile;
}

public String getName() {
IXpectURIProvider uriProvider = getURIProvider();
URI uri = getUri();
URI deresolved = uriProvider.deresolveToProject(uri);
String pathInProject = deresolved.trimSegments(1).toString();
String fileName = deresolved.lastSegment();
return fileName + ": " + pathInProject;
}
}
Loading

0 comments on commit 0995b3f

Please sign in to comment.