Skip to content

Commit

Permalink
Extracted summary creating listener from console runner for reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Nov 20, 2015
1 parent 39fab8b commit 48b0380
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 132 deletions.
Expand Up @@ -13,6 +13,7 @@
import static io.airlift.airline.SingleCommand.singleCommand;

import java.io.File;
import java.io.PrintWriter;
import java.util.List;
import java.util.Set;

Expand All @@ -29,6 +30,8 @@
import org.junit.gen5.engine.TestPlanSpecification;
import org.junit.gen5.engine.TestPlanSpecificationElement;
import org.junit.gen5.launcher.Launcher;
import org.junit.gen5.launcher.listeners.SummaryCreatingTestListener;
import org.junit.gen5.launcher.listeners.TestExecutionSummary;

/**
* @author Stefan Bechtold
Expand Down Expand Up @@ -87,14 +90,34 @@ private void run() {
// TODO Configure launcher?
Launcher launcher = new Launcher();

TestSummaryReportingTestListener testSummaryListener = new TestSummaryReportingTestListener(System.out);
TestExecutionSummary summary = new TestExecutionSummary();

registerListeners(launcher, summary);

TestPlanSpecification testPlanSpecification = createTestPlanSpecification();

launcher.execute(testPlanSpecification);

printSummaryToStandardOut(summary);

if (enableExitCode) {
long failedTests = summary.countFailedTests();
int exitCode = (int) Math.min(Integer.MAX_VALUE, failedTests);
System.exit(exitCode);
}
}

private void registerListeners(Launcher launcher, TestExecutionSummary summary) {
SummaryCreatingTestListener testSummaryListener = new SummaryCreatingTestListener(summary);
launcher.registerTestPlanExecutionListeners(
// @formatter:off
new ColoredPrintingTestListener(System.out, disableAnsiColors),
testSummaryListener
// @formatter:on
);
}

private TestPlanSpecification createTestPlanSpecification() {
TestPlanSpecification testPlanSpecification;
if (runAllTests) {
Set<File> rootDirectories = ReflectionUtils.getAllClasspathRootDirectories();
Expand All @@ -103,15 +126,13 @@ private void run() {
else {
testPlanSpecification = TestPlanSpecification.build(testPlanSpecificationElementsFromArguments());
}
return testPlanSpecification;
}

// TODO Provide means to allow manipulation of test plan?
launcher.execute(testPlanSpecification);

if (enableExitCode) {
long failedTests = testSummaryListener.getNumberOfFailedTests();
int exitCode = (int) Math.min(Integer.MAX_VALUE, failedTests);
System.exit(exitCode);
}
private void printSummaryToStandardOut(TestExecutionSummary summary) {
PrintWriter soutWriter = new PrintWriter(System.out);
summary.printOn(soutWriter);
soutWriter.close();
}

private List<TestPlanSpecificationElement> testPlanSpecificationElementsFromArguments() {
Expand Down

This file was deleted.

@@ -0,0 +1,92 @@
/*
* Copyright 2015 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.gen5.launcher.listeners;

import java.io.PrintStream;
import java.util.concurrent.atomic.AtomicLong;

import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestExecutionListener;
import org.junit.gen5.launcher.TestPlan;
import org.junit.gen5.launcher.TestPlanExecutionListener;

/**
* @author Stefan Bechtold
* @author Sam Brannen
* @since 5.0
*/
public class SummaryCreatingTestListener implements TestPlanExecutionListener, TestExecutionListener {

private final TestExecutionSummary summary;

public SummaryCreatingTestListener(TestExecutionSummary summary) {
this.summary = summary;
}

@Override
public void testPlanExecutionStarted(TestPlan testPlan) {
summary.testsFound.set(testPlan.countStaticTests());
summary.timeStarted = System.currentTimeMillis();
}

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

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

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

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

@Override
public void dynamicTestFound(TestDescriptor testDescriptor) {
summary.testsFound.incrementAndGet();
}

@Override
public void testStarted(TestDescriptor testDescriptor) {
summary.testsStarted.incrementAndGet();
}

@Override
public void testSkipped(TestDescriptor testDescriptor, Throwable t) {
summary.testsSkipped.addAndGet(testDescriptor.countStaticTests());
}

@Override
public void testAborted(TestDescriptor testDescriptor, Throwable t) {
summary.testsAborted.incrementAndGet();
}

@Override
public void testFailed(TestDescriptor testDescriptor, Throwable t) {
summary.testsFailed.incrementAndGet();
summary.addFailure(testDescriptor, t);
}

@Override
public void testSucceeded(TestDescriptor testDescriptor) {
summary.testsSucceeded.incrementAndGet();
}

}
@@ -0,0 +1,85 @@
/*
* Copyright 2015 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.gen5.launcher.listeners;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicLong;

import lombok.Value;

import org.junit.gen5.engine.TestDescriptor;

// TODO Give it a REAL interface and make it threadsafe
public class TestExecutionSummary {

final AtomicLong testsStarted = new AtomicLong();
final AtomicLong testsFound = new AtomicLong();
final AtomicLong testsSkipped = new AtomicLong();
final AtomicLong testsAborted = new AtomicLong();
final AtomicLong testsSucceeded = new AtomicLong();
final AtomicLong testsFailed = new AtomicLong();

long timeStarted;
long timePaused;
long timeFinished;

private String message;
private List<Failure> failures = new ArrayList<>();

void finishTestRun(String message) {
this.timeFinished = System.currentTimeMillis();
this.message = message;
}

public void printOn(PrintWriter writer) {

// @formatter:off
writer.println(String.format(
"%n%s after %d ms\n"
+ "[%10d tests found ]\n"
+ "[%10d tests started ]\n"
+ "[%10d tests skipped ]\n"
+ "[%10d tests aborted ]\n"
+ "[%10d tests successful]\n"
+ "[%10d tests failed ]\n",
this.message, (this.timeFinished - this.timeStarted), this.testsFound.get(), this.testsStarted.get(),
this.testsSkipped.get(), this.testsAborted.get(), this.testsSucceeded.get(), this.testsFailed.get()));
// @formatter:on

// if (countFailedTests() > 0) {
// writer.println("Test failures: ");
// }

writer.flush();
}

public long countFailedTests() {
return testsFailed.get();
}

public void addFailure(TestDescriptor testDescriptor, Throwable throwable) {
failures.add(new Failure(testDescriptor, throwable));
}

@Value
static class Failure {

final private TestDescriptor descriptor;
final private Throwable exception;
}
}

0 comments on commit 48b0380

Please sign in to comment.