-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide JUnit 5 equivalent of XpectRunner #343
Open
trancexpress
wants to merge
1
commit into
eclipse:master
Choose a base branch
from
trancexpress:XpectRunner_JUnit5
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+616
−12
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
org.eclipse.xpect/src/org/eclipse/xpect/dynamic/IXpectDynamicTestFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.DynamicNode; | ||
import org.junit.jupiter.api.TestFactory; | ||
|
||
@XpectImport(TestTitleProvider.class) | ||
public interface IXpectDynamicTestFactory { | ||
|
||
@TestFactory | ||
default Stream<DynamicNode> tests() { | ||
return XpectDynamicTestFactory.xpectTests(getClass()); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
org.eclipse.xpect/src/org/eclipse/xpect/dynamic/XpectDynamicTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
174 changes: 174 additions & 0 deletions
174
org.eclipse.xpect/src/org/eclipse/xpect/dynamic/XpectDynamicTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
package org.eclipse.xpect.dynamic; | ||
|
||
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.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.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 | ||
} | ||
|
||
protected List<DynamicTest> createChildren() { | ||
List<DynamicTest> tests = 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); | ||
tests.add(wrapTest(test, childrenCount, setupValidated, setupValidationFailed, finishedChildren)); | ||
} | ||
for (XpectInvocation inv : invocations) { | ||
DynamicTest test = createDynamicTest(inv); | ||
tests.add(wrapTest(test, childrenCount, setupValidated, setupValidationFailed, finishedChildren)); | ||
} | ||
} | ||
return tests; | ||
} | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use blocks for if |
||
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hint: all new classes miss file headers