Skip to content

Commit

Permalink
Polish DummyTestEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Dec 8, 2015
1 parent e4c6400 commit 5eb4642
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.gen5.api.Assertions.*;
import static org.junit.gen5.engine.DummyTestEngine.TestResult.*;
import static org.junit.gen5.launcher.LauncherFactory.createLauncher;

import java.io.PrintWriter;
Expand All @@ -34,8 +33,8 @@ public void executeWithoutExitCode() throws Exception {
options.setRunAllTests(true);

DummyTestEngine dummyTestEngine = new DummyTestEngine();
dummyTestEngine.addTest("succeedingTest", SUCCESS, noOp());
dummyTestEngine.addTest("failingTest", FAILURE, noOp());
dummyTestEngine.addTest("succeedingTest", success());
dummyTestEngine.addTest("failingTest", () -> fail("should fail"));

ExecuteTestsTask task = new ExecuteTestsTask(options, () -> createLauncher(dummyTestEngine));
int exitCode = task.execute(new PrintWriter(stringWriter));
Expand All @@ -51,8 +50,8 @@ public void executeWithExitCode() throws Exception {
options.setExitCodeEnabled(true);

DummyTestEngine dummyTestEngine = new DummyTestEngine();
dummyTestEngine.addTest("succeedingTest", SUCCESS, noOp());
dummyTestEngine.addTest("failingTest", FAILURE, noOp());
dummyTestEngine.addTest("succeedingTest", success());
dummyTestEngine.addTest("failingTest", () -> fail("should fail"));

ExecuteTestsTask task = new ExecuteTestsTask(options, () -> createLauncher(dummyTestEngine));
int exitCode = task.execute(new PrintWriter(new StringWriter()));
Expand All @@ -69,7 +68,7 @@ public void executeWithCustomClassLoader() throws Exception {

ClassLoader oldClassLoader = ReflectionUtils.getDefaultClassLoader();
DummyTestEngine dummyTestEngine = new DummyTestEngine();
dummyTestEngine.addTest("failingTest", FAILURE, () -> {
dummyTestEngine.addTest("failingTest", () -> {
assertSame(oldClassLoader, ReflectionUtils.getDefaultClassLoader(), "should fail");
});

Expand All @@ -88,7 +87,7 @@ public void executeWithHiddenDetails() throws Exception {
options.setHideDetails(true);

DummyTestEngine dummyTestEngine = new DummyTestEngine();
dummyTestEngine.addTest("failingTest", FAILURE, noOp());
dummyTestEngine.addTest("failingTest", () -> fail("should fail"));

ExecuteTestsTask task = new ExecuteTestsTask(options, () -> createLauncher(dummyTestEngine));
task.execute(new PrintWriter(stringWriter));
Expand All @@ -100,7 +99,7 @@ public void executeWithHiddenDetails() throws Exception {
// @formatter:on
}

private static Runnable noOp() {
private static Runnable success() {
return () -> {
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

public class DummyTestDescriptor extends AbstractTestDescriptor {

private final String displayName;
public static final String ENGINE_ID = "dummy";

private final String displayName;

public DummyTestDescriptor(String displayName) {
super(DummyTestDescriptor.ENGINE_ID + ":" + displayName);
super(ENGINE_ID + ":" + displayName);
this.displayName = displayName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,28 @@

import java.util.LinkedHashMap;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.function.BiConsumer;

public final class DummyTestEngine implements TestEngine {

private final LinkedHashMap<String, Callable<TestResult>> children = new LinkedHashMap<>();
private final LinkedHashMap<String, Runnable> children = new LinkedHashMap<>();

@Override
public String getId() {
return ENGINE_ID;
}

public void addTest(String uniqueName, TestResult result, Runnable runnable) {
children.put(uniqueName, () -> {
runnable.run();
return result;
});
public void addTest(String uniqueName, Runnable runnable) {
children.put(uniqueName, runnable);
}

@Override
public TestDescriptor discoverTests(TestPlanSpecification specification) {
DummyTestDescriptor root = new DummyTestDescriptor("root");
// @formatter:off
Set<DummyTestDescriptor> children = this.children.keySet().stream()
.map(DummyTestDescriptor::new)
.collect(toSet());
// @formatter:on
DummyTestDescriptor root = new DummyTestDescriptor("root");
children.forEach(root::addChild);
return root;
}
Expand All @@ -50,33 +45,15 @@ public TestDescriptor discoverTests(TestPlanSpecification specification) {
public void execute(ExecutionRequest request) {
EngineExecutionListener listener = request.getEngineExecutionListener();
for (TestDescriptor childDescriptor : request.getRootTestDescriptor().getChildren()) {
Callable<TestResult> callable = children.get(childDescriptor.getDisplayName());
Runnable runnable = children.get(childDescriptor.getDisplayName());
listener.testStarted(childDescriptor);
try {
TestResult testResult = callable.call();
testResult.accept(listener, childDescriptor);
runnable.run();
listener.testSucceeded(childDescriptor);
}
catch (Throwable t) {
listener.testFailed(childDescriptor, t);
}
}
}

public enum TestResult implements BiConsumer<EngineExecutionListener, TestDescriptor> {
SUCCESS {

@Override
public void accept(EngineExecutionListener listener, TestDescriptor descriptor) {
listener.testSucceeded(descriptor);
}
},

FAILURE {

@Override
public void accept(EngineExecutionListener listener, TestDescriptor descriptor) {
listener.testFailed(descriptor, new Exception("failure"));
}
};
}
}

0 comments on commit 5eb4642

Please sign in to comment.