Skip to content

Commit

Permalink
Consolidate Tracker and EventRecorder in JUnit 5 engine tests
Browse files Browse the repository at this point in the history
Issue: #81
  • Loading branch information
marcphilipp committed Jan 13, 2016
1 parent be5c300 commit 3a0605c
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 286 deletions.
Expand Up @@ -10,8 +10,6 @@

package org.junit.gen5.engine;

import java.util.Arrays;

import org.junit.gen5.engine.TestExecutionResult.Status;

/**
Expand Down Expand Up @@ -105,36 +103,4 @@ public interface EngineExecutionListener {
*/
void executionFinished(TestDescriptor testDescriptor, TestExecutionResult testExecutionResult);

/**
* Combine several {@code listeners} into a single one so that each one is receiving all events.
*
* @param listeners the individual listeners to combine
* @return the combined listener
*/
static EngineExecutionListener combine(EngineExecutionListener... listeners) {
return new EngineExecutionListener() {

@Override
public void dynamicTestRegistered(TestDescriptor testDescriptor) {
Arrays.stream(listeners).forEach(listener -> listener.dynamicTestRegistered(testDescriptor));
}

@Override
public void executionSkipped(TestDescriptor testDescriptor, String reason) {
Arrays.stream(listeners).forEach(listener -> listener.executionSkipped(testDescriptor, reason));
}

@Override
public void executionStarted(TestDescriptor testDescriptor) {
Arrays.stream(listeners).forEach(listener -> listener.executionStarted(testDescriptor));
}

@Override
public void executionFinished(TestDescriptor testDescriptor, TestExecutionResult testExecutionResult) {
Arrays.stream(listeners).forEach(
listener -> listener.executionFinished(testDescriptor, testExecutionResult));

}
};
}
}
Expand Up @@ -10,8 +10,19 @@

package org.junit.gen5.engine;

import static java.util.function.Predicate.isEqual;
import static java.util.stream.Collectors.toList;
import static org.junit.gen5.commons.util.FunctionUtils.where;
import static org.junit.gen5.engine.ExecutionEvent.*;
import static org.junit.gen5.engine.ExecutionEvent.Type.*;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Predicate;
import java.util.stream.Stream;

import org.junit.gen5.engine.ExecutionEvent.Type;
import org.junit.gen5.engine.TestExecutionResult.Status;

/**
* {@link EngineExecutionListener} that records all events and makes them available to tests.
Expand Down Expand Up @@ -53,6 +64,72 @@ public List<ExecutionEvent> getExecutionEvents() {
return executionEvents;
}

public Stream<ExecutionEvent> eventStream() {
return getExecutionEvents().stream();
}

public long getTestSkippedCount() {
return testEventsByType(SKIPPED).count();
}

public long getTestStartedCount() {
return testEventsByType(STARTED).count();
}

public long getTestFinishedCount() {
return testEventsByType(FINISHED).count();
}

public long getTestSuccessfulCount() {
return getTestFinishedCount(Status.SUCCESSFUL);
}

public long getTestAbortedCount() {
return getTestFinishedCount(Status.ABORTED);
}

public long getTestFailedCount() {
return getTestFinishedCount(Status.FAILED);
}

public long getContainerSkippedCount() {
return containerEventsByType(SKIPPED).count();
}

public long getContainerStartedCount() {
return containerEventsByType(STARTED).count();
}

public long getContainerFinishedCount() {
return containerEventsByType(FINISHED).count();
}

public List<ExecutionEvent> getFailedTestFinishedEvents() {
return testFinishedEvents(Status.FAILED).collect(toList());
}

private long getTestFinishedCount(Status status) {
return testFinishedEvents(status).count();
}

private Stream<ExecutionEvent> testFinishedEvents(Status status) {
return testEventsByType(FINISHED).filter(
byPayload(TestExecutionResult.class, where(TestExecutionResult::getStatus, isEqual(status))));
}

private Stream<ExecutionEvent> testEventsByType(Type type) {
return eventsByTypeAndTestDescriptor(type, TestDescriptor::isTest);
}

private Stream<ExecutionEvent> containerEventsByType(Type type) {
return eventsByTypeAndTestDescriptor(type, TestDescriptor::isContainer);
}

private Stream<ExecutionEvent> eventsByTypeAndTestDescriptor(Type type,
Predicate<? super TestDescriptor> predicate) {
return eventStream().filter(byType(type).and(byTestDescriptor(predicate)));
}

private void addEvent(ExecutionEvent event) {
executionEvents.add(event);
}
Expand Down

This file was deleted.

Expand Up @@ -14,12 +14,10 @@

import org.junit.gen5.api.BeforeEach;
import org.junit.gen5.engine.EngineDescriptor;
import org.junit.gen5.engine.EngineExecutionListener;
import org.junit.gen5.engine.ExecutionEventRecordingEngineExecutionListener;
import org.junit.gen5.engine.ExecutionRequest;
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestPlanSpecification;
import org.junit.gen5.engine.TrackingEngineExecutionListener;

/**
* Abstract base class for tests involving the {@link JUnit5TestEngine}.
Expand All @@ -28,27 +26,21 @@
*/
abstract class AbstractJUnit5TestEngineTests {

protected final JUnit5TestEngine engine = new JUnit5TestEngine();

protected TrackingEngineExecutionListener tracker;
protected ExecutionEventRecordingEngineExecutionListener eventRecorder;

private EngineExecutionListener listener;
private final JUnit5TestEngine engine = new JUnit5TestEngine();

@BeforeEach
void initListeners() {
tracker = new TrackingEngineExecutionListener();
eventRecorder = new ExecutionEventRecordingEngineExecutionListener();
listener = EngineExecutionListener.combine(tracker, eventRecorder);
}

protected void executeTestsForClass(Class<?> testClass) {
executeTests(build(forClass(testClass)));
protected ExecutionEventRecordingEngineExecutionListener executeTestsForClass(Class<?> testClass) {
return executeTests(build(forClass(testClass)));
}

protected void executeTests(TestPlanSpecification spec) {
protected ExecutionEventRecordingEngineExecutionListener executeTests(TestPlanSpecification spec) {
TestDescriptor testDescriptor = discoverTests(spec);
engine.execute(new ExecutionRequest(testDescriptor, listener));
ExecutionEventRecordingEngineExecutionListener eventRecorder = new ExecutionEventRecordingEngineExecutionListener();
engine.execute(new ExecutionRequest(testDescriptor, eventRecorder));
return eventRecorder;
}

protected EngineDescriptor discoverTests(TestPlanSpecification spec) {
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.junit.gen5.api.extension.ExtendWith;
import org.junit.gen5.api.extension.ExtensionRegistrar;
import org.junit.gen5.api.extension.ExtensionRegistry;
import org.junit.gen5.engine.ExecutionEventRecordingEngineExecutionListener;
import org.junit.gen5.engine.TestPlanSpecification;

/**
Expand All @@ -40,10 +41,10 @@ public class ClassLevelCallbackTests extends AbstractJUnit5TestEngineTests {
public void beforeAllAndAfterAllCallbacks() {
TestPlanSpecification spec = build(forClass(InstancePerMethodTestCase.class));

executeTests(spec);
ExecutionEventRecordingEngineExecutionListener eventRecorder = executeTests(spec);

assertEquals(1, tracker.testStartedCount.get(), "# tests started");
assertEquals(1, tracker.testSucceededCount.get(), "# tests succeeded");
assertEquals(1L, eventRecorder.getTestStartedCount(), "# tests started");
assertEquals(1L, eventRecorder.getTestSuccessfulCount(), "# tests succeeded");

// @formatter:off
assertEquals(asList(
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.junit.gen5.api.extension.ExtendWith;
import org.junit.gen5.api.extension.TestExecutionCondition;
import org.junit.gen5.api.extension.TestExtensionContext;
import org.junit.gen5.engine.ExecutionEventRecordingEngineExecutionListener;
import org.junit.gen5.engine.TestPlanSpecification;

/**
Expand Down Expand Up @@ -56,22 +57,22 @@ public void tearDown() {
@Test
public void executeTestsWithDisabledTestClass() {
TestPlanSpecification spec = build(forClass(DisabledTestClassTestCase.class));
executeTests(spec);
ExecutionEventRecordingEngineExecutionListener eventRecorder = executeTests(spec);

assertEquals(1, tracker.containerSkippedCount.get(), "# container skipped");
assertEquals(0, tracker.testStartedCount.get(), "# tests started");
assertEquals(1L, eventRecorder.getContainerSkippedCount(), "# container skipped");
assertEquals(0L, eventRecorder.getTestStartedCount(), "# tests started");
}

@Test
public void executeTestsWithDisabledTestMethods() {
TestPlanSpecification spec = build(forClass(DisabledTestMethodsTestCase.class));
executeTests(spec);
ExecutionEventRecordingEngineExecutionListener eventRecorder = executeTests(spec);

assertEquals(2, tracker.testStartedCount.get(), "# tests started");
assertEquals(2, tracker.testSucceededCount.get(), "# tests succeeded");
assertEquals(3, tracker.testSkippedCount.get(), "# tests skipped");
assertEquals(0, tracker.testAbortedCount.get(), "# tests aborted");
assertEquals(0, tracker.testFailedCount.get(), "# tests failed");
assertEquals(2L, eventRecorder.getTestStartedCount(), "# tests started");
assertEquals(2L, eventRecorder.getTestSuccessfulCount(), "# tests succeeded");
assertEquals(3L, eventRecorder.getTestSkippedCount(), "# tests skipped");
assertEquals(0L, eventRecorder.getTestAbortedCount(), "# tests aborted");
assertEquals(0L, eventRecorder.getTestFailedCount(), "# tests failed");
}

// -------------------------------------------------------------------
Expand Down

0 comments on commit 3a0605c

Please sign in to comment.