Skip to content

Commit

Permalink
Added testplan to all listener methods and introduced composed listen…
Browse files Browse the repository at this point in the history
…er in the registry
  • Loading branch information
bechte committed Oct 30, 2015
1 parent 2faf053 commit 5b99f73
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 59 deletions.
Expand Up @@ -39,22 +39,22 @@ public void testPlanExecutionStarted(TestPlan testPlan) {
}

@Override
public void testPlanExecutionPaused() {
public void testPlanExecutionPaused(TestPlan testPlan) {
out.println("Test execution paused.");
}

@Override
public void testPlanExecutionRestarted() {
public void testPlanExecutionRestarted(TestPlan testPlan) {
out.println("Test execution continued.");
}

@Override
public void testPlanExecutionStopped() {
public void testPlanExecutionStopped(TestPlan testPlan) {
out.println("Test execution canceled.");
}

@Override
public void testPlanExecutionFinished() {
public void testPlanExecutionFinished(TestPlan testPlan) {
}

@Override
Expand Down
Expand Up @@ -49,23 +49,23 @@ public void testPlanExecutionStarted(TestPlan testPlan) {
}

@Override
public void testPlanExecutionPaused() {
public void testPlanExecutionPaused(TestPlan testPlan) {
this.timePaused = System.currentTimeMillis();
}

@Override
public void testPlanExecutionRestarted() {
public void testPlanExecutionRestarted(TestPlan testPlan) {
this.timeStarted += System.currentTimeMillis() - this.timePaused;
this.timePaused = 0;
}

@Override
public void testPlanExecutionStopped() {
public void testPlanExecutionStopped(TestPlan testPlan) {
reportSummary("Test run stopped");
}

@Override
public void testPlanExecutionFinished() {
public void testPlanExecutionFinished(TestPlan testPlan) {
reportSummary("Test run finished");
}

Expand Down
Expand Up @@ -18,20 +18,20 @@
public interface TestExecutionListener {

default void dynamicTestFound(TestDescriptor testDescriptor) {
};
}

default void testStarted(TestDescriptor testDescriptor) {
};
}

default void testSkipped(TestDescriptor testDescriptor, Throwable t) {
};
}

default void testAborted(TestDescriptor testDescriptor, Throwable t) {
};
}

default void testFailed(TestDescriptor testDescriptor, Throwable t) {
};
}

default void testSucceeded(TestDescriptor testDescriptor) {
};
}
}
27 changes: 12 additions & 15 deletions junit-launcher/src/main/java/org/junit/gen5/launcher/Launcher.java
Expand Up @@ -12,6 +12,8 @@

import static org.junit.gen5.launcher.TestEngineRegistry.lookupAllTestEngines;

import java.util.List;

import org.junit.gen5.engine.EngineExecutionContext;
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestEngine;
Expand All @@ -27,9 +29,8 @@ public class Launcher {

private final TestListenerRegistry listenerRegistry = new TestListenerRegistry();

public void registerTestPlanExecutionListeners(TestPlanExecutionListener... testListeners) {
listenerRegistry.registerTestPlanExecutionListeners(testListeners);
listenerRegistry.registerTestExecutionListeners(testListeners);
public void registerTestPlanExecutionListeners(TestExecutionListener... testListeners) {
listenerRegistry.registerListener(testListeners);
}

public TestPlan discover(TestPlanSpecification specification) {
Expand All @@ -43,22 +44,18 @@ public TestPlan discover(TestPlanSpecification specification) {
}

public void execute(TestPlanSpecification specification) {
TestPlan plan = discover(specification);
execute(plan);
execute(discover(specification));
}

private void execute(TestPlan testPlan) {
listenerRegistry.notifyTestPlanExecutionListeners(
testPlanExecutionListener -> testPlanExecutionListener.testPlanExecutionStarted(testPlan));

TestExecutionListener compositeListener = listenerRegistry.getCompositeTestExecutionListener();
public void execute(TestPlan testPlan) {
TestPlanExecutionListener testPlanExecutionListener = listenerRegistry.getCompositeTestPlanExecutionListener();
TestExecutionListener testExecutionListener = listenerRegistry.getCompositeTestExecutionListener();

testPlanExecutionListener.testPlanExecutionStarted(testPlan);
for (TestEngine testEngine : lookupAllTestEngines()) {
testEngine.execute(
new EngineExecutionContext(testPlan.getAllTestDescriptorsForTestEngine(testEngine), compositeListener));
List<TestDescriptor> testDescriptors = testPlan.getAllTestDescriptorsForTestEngine(testEngine);
testEngine.execute(new EngineExecutionContext(testDescriptors, testExecutionListener));
}

listenerRegistry.notifyTestPlanExecutionListeners(TestPlanExecutionListener::testPlanExecutionFinished);
testPlanExecutionListener.testPlanExecutionFinished(testPlan);
}

}
Expand Up @@ -10,8 +10,6 @@

package org.junit.gen5.launcher;

import static java.util.Arrays.*;

import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
Expand All @@ -26,24 +24,28 @@
*/
class TestListenerRegistry {

private final List<TestExecutionListener> testExecutionListeners = new LinkedList<>();

private final List<TestPlanExecutionListener> testPlanExecutionListeners = new LinkedList<>();
private final List<TestExecutionListener> testExecutionListeners = new LinkedList<>();

void registerTestExecutionListeners(TestExecutionListener... listeners) {
this.testExecutionListeners.addAll(asList(listeners));
void registerListener(TestExecutionListener... listeners) {
for (TestExecutionListener listener : listeners) {
this.testExecutionListeners.add(listener);
if (listener instanceof TestPlanExecutionListener) {
this.testPlanExecutionListeners.add((TestPlanExecutionListener) listener);
}
}
}

void registerTestPlanExecutionListeners(TestPlanExecutionListener... listeners) {
this.testPlanExecutionListeners.addAll(asList(listeners));
private void notifyTestPlanExecutionListeners(Consumer<TestPlanExecutionListener> consumer) {
this.testPlanExecutionListeners.forEach(consumer);
}

void notifyTestExecutionListeners(Consumer<TestExecutionListener> consumer) {
private void notifyTestExecutionListeners(Consumer<TestExecutionListener> consumer) {
this.testExecutionListeners.forEach(consumer);
}

void notifyTestPlanExecutionListeners(Consumer<TestPlanExecutionListener> consumer) {
this.testPlanExecutionListeners.forEach(consumer);
TestPlanExecutionListener getCompositeTestPlanExecutionListener() {
return new CompositeTestPlanExecutionListener();
}

TestExecutionListener getCompositeTestExecutionListener() {
Expand All @@ -54,35 +56,60 @@ private class CompositeTestExecutionListener implements TestExecutionListener {

@Override
public void dynamicTestFound(TestDescriptor testDescriptor) {
notifyTestExecutionListeners(
testExecutionListener -> testExecutionListener.dynamicTestFound(testDescriptor));
notifyTestExecutionListeners(listener -> listener.dynamicTestFound(testDescriptor));
}

@Override
public void testStarted(TestDescriptor testDescriptor) {
notifyTestExecutionListeners(testExecutionListener -> testExecutionListener.testStarted(testDescriptor));
notifyTestExecutionListeners(listener -> listener.testStarted(testDescriptor));
}

@Override
public void testSkipped(TestDescriptor testDescriptor, Throwable t) {
notifyTestExecutionListeners(testExecutionListener -> testExecutionListener.testSkipped(testDescriptor, t));
notifyTestExecutionListeners(listener -> listener.testSkipped(testDescriptor, t));
}

@Override
public void testAborted(TestDescriptor testDescriptor, Throwable t) {
notifyTestExecutionListeners(testExecutionListener -> testExecutionListener.testAborted(testDescriptor, t));
notifyTestExecutionListeners(listener -> listener.testAborted(testDescriptor, t));
}

@Override
public void testFailed(TestDescriptor testDescriptor, Throwable t) {
notifyTestExecutionListeners(testExecutionListener -> testExecutionListener.testFailed(testDescriptor, t));
notifyTestExecutionListeners(listener -> listener.testFailed(testDescriptor, t));
}

@Override
public void testSucceeded(TestDescriptor testDescriptor) {
notifyTestExecutionListeners(testExecutionListener -> testExecutionListener.testSucceeded(testDescriptor));
notifyTestExecutionListeners(listener -> listener.testSucceeded(testDescriptor));
}

}

}
private class CompositeTestPlanExecutionListener implements TestPlanExecutionListener {

@Override
public void testPlanExecutionStarted(TestPlan testPlan) {
notifyTestPlanExecutionListeners(listener -> listener.testPlanExecutionStarted(testPlan));
}

@Override
public void testPlanExecutionPaused(TestPlan testPlan) {
notifyTestPlanExecutionListeners(listener -> listener.testPlanExecutionPaused(testPlan));
}

@Override
public void testPlanExecutionRestarted(TestPlan testPlan) {
notifyTestPlanExecutionListeners(listener -> listener.testPlanExecutionRestarted(testPlan));
}

@Override
public void testPlanExecutionStopped(TestPlan testPlan) {
notifyTestPlanExecutionListeners(listener -> listener.testPlanExecutionStopped(testPlan));
}

@Override
public void testPlanExecutionFinished(TestPlan testPlan) {
notifyTestPlanExecutionListeners(listener -> listener.testPlanExecutionFinished(testPlan));
}
}
}
Expand Up @@ -20,18 +20,17 @@
public interface TestPlanExecutionListener extends TestExecutionListener {

default void testPlanExecutionStarted(TestPlan testPlan) {
};
}

default void testPlanExecutionPaused() {
};
default void testPlanExecutionPaused(TestPlan testPlan) {
}

default void testPlanExecutionRestarted() {
};
default void testPlanExecutionRestarted(TestPlan testPlan) {
}

default void testPlanExecutionStopped() {
};

default void testPlanExecutionFinished() {
};
default void testPlanExecutionStopped(TestPlan testPlan) {
}

default void testPlanExecutionFinished(TestPlan testPlan) {
}
}

0 comments on commit 5b99f73

Please sign in to comment.