Skip to content
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

Fixes 22# - Add elapsed time to all Nested Classes #23

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.fabriciorby</groupId>
<artifactId>maven-surefire-junit5-tree-reporter</artifactId>
<version>1.0.1</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>maven-surefire-junit5-tree-reporter</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.apache.maven.plugin.surefire.report.ConsoleTreeReporter;
import org.apache.maven.plugin.surefire.report.ConsoleTreeReporterAscii;
import org.apache.maven.plugin.surefire.report.TestSetStats;
import org.apache.maven.plugin.surefire.report.WrappedReportEntry;
import org.apache.maven.surefire.extensions.StatelessTestsetInfoConsoleReportEventListener;
Expand All @@ -35,7 +35,7 @@ public class JUnit5StatelessTestsetInfoTreeReporter extends JUnit5StatelessTests
@Override
public StatelessTestsetInfoConsoleReportEventListener<WrappedReportEntry, TestSetStats> createListener(
ConsoleLogger logger) {
return new ConsoleTreeReporter(logger, isUsePhrasedClassNameInRunning(),
return new ConsoleTreeReporterAscii(logger, isUsePhrasedClassNameInRunning(),
isUsePhrasedClassNameInTestCaseSummary());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,25 @@
*/

import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.apache.maven.surefire.api.report.TestSetReportEntry;

import java.util.Collection;
import java.util.List;

/**
* Tree view class for console reporters.
*
* @author <a href="mailto:[email protected]">Fabrício Yamamoto</a>
*/
public class ConsoleTreeReporter extends ConsoleReporter {
public class ConsoleTreeReporterAscii extends ConsoleTreeReporterBase {

public ConsoleTreeReporter(ConsoleLogger logger, boolean usePhrasedClassNameInRunning,
boolean usePhrasedClassNameInTestCaseSummary) {
public ConsoleTreeReporterAscii(ConsoleLogger logger, boolean usePhrasedClassNameInRunning,
boolean usePhrasedClassNameInTestCaseSummary) {
super(logger, usePhrasedClassNameInRunning, usePhrasedClassNameInTestCaseSummary);
}

@Override
public void testSetStarting(TestSetReportEntry report) {
}

@Override
public void testSetCompleted(WrappedReportEntry report, TestSetStats testSetStats, List<String> testResults) {
new TreePrinter(getConsoleLogger(), report, testSetStats)
.printTests();
TreePrinter getTreePrinter(List<WrappedReportEntry> classEntries, List<WrappedReportEntry> testEntries) {
return new TreePrinter(getConsoleLogger(), classEntries, testEntries);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.apache.maven.plugin.surefire.report;

import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.apache.maven.surefire.api.report.TestSetReportEntry;

import java.util.List;

abstract class ConsoleTreeReporterBase extends ConsoleReporter {

public ConsoleTreeReporterBase(ConsoleLogger logger, boolean usePhrasedClassNameInRunning,
boolean usePhrasedClassNameInTestCaseSummary) {
super(logger, usePhrasedClassNameInRunning, usePhrasedClassNameInTestCaseSummary);
}

abstract TreePrinter getTreePrinter(List<WrappedReportEntry> classEntries, List<WrappedReportEntry> testEntries);

@Override
public void testSetStarting(TestSetReportEntry report) {
new TestReportHandler(report)
.prepare();
}

@Override
public void testSetCompleted(WrappedReportEntry report, TestSetStats testSetStats, List<String> testResults) {
new TestReportHandler(report, testSetStats)
.print(this::getTreePrinter);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@

import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;

import java.util.Collection;
import java.util.List;

/**
* Tree view class for console reporters using UNICODE theme.
*
* @author <a href="mailto:[email protected]">Fabrício Yamamoto</a>
*/
public class ConsoleTreeReporterUnicode extends ConsoleTreeReporter {
public class ConsoleTreeReporterUnicode extends ConsoleTreeReporterBase {

public ConsoleTreeReporterUnicode(ConsoleLogger logger, boolean usePhrasedClassNameInRunning,
boolean usePhrasedClassNameInTestCaseSummary) {
super(logger, usePhrasedClassNameInRunning, usePhrasedClassNameInTestCaseSummary);
}

@Override
public void testSetCompleted(WrappedReportEntry report, TestSetStats testSetStats, List<String> testResults) {
new TreePrinter(getConsoleLogger(), report, testSetStats, Theme.UNICODE)
.printTests();
TreePrinter getTreePrinter(List<WrappedReportEntry> classEntries, List<WrappedReportEntry> testEntries) {
return new TreePrinter(getConsoleLogger(), classEntries, testEntries, Theme.UNICODE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package org.apache.maven.plugin.surefire.report;

import org.apache.maven.surefire.api.report.ReportEntry;

import java.util.*;
import java.util.function.BiFunction;

import static java.util.Collections.*;

public class TestReportHandler {

protected static final Map<String, Set<String>> classNames = synchronizedMap(new HashMap<>());
protected static final Map<String, List<WrappedReportEntry>> classEntries = synchronizedMap(new HashMap<>());
protected static final Map<String, List<WrappedReportEntry>> testEntries = synchronizedMap(new HashMap<>());
protected static final int $ = 36;

private final ReportEntry report;
private final TestSetStats testSetStats;
private final String sourceRootName;

public TestReportHandler(ReportEntry report, TestSetStats testSetStats) {
this.report = report;
this.testSetStats = testSetStats;
this.sourceRootName = getSourceRootName();
}

public TestReportHandler(ReportEntry report) {
this(report, null);
}

public void prepare() {
if (hasNestedTests()) {
markClassNamesForNestedTests();
}
}

public void print(BiFunction<List<WrappedReportEntry>,List<WrappedReportEntry>, TreePrinter> getTreePrinter) {
if (isMarkedAsNestedTest()) {
prepareEntriesForNestedTests();
if (isNestedTestReadyToPrint()) {
printNestedTests(getTreePrinter);
}
} else {
printTests(getTreePrinter);
}
}

private boolean isMarkedAsNestedTest() {
return classNames.containsKey(sourceRootName);
}

private void prepareClassEntriesForNestedTest() {
classEntries.putIfAbsent(sourceRootName, new ArrayList<>());
classEntries.computeIfPresent(sourceRootName, addToCollection((WrappedReportEntry) report));
}

private List<WrappedReportEntry> getClassEntryList() {
return classEntries.get(sourceRootName);
}

private void markClassNamesForNestedTests() {
classNames.putIfAbsent(sourceRootName, new HashSet<>(singleton(sourceRootName)));
classNames.computeIfPresent(sourceRootName, addToCollection(report.getSourceName()));
}

private Set<String> getClassNameList() {
return classNames.get(sourceRootName);
}

private void prepareTestEntriesForNestedTest() {
testEntries.putIfAbsent(sourceRootName, new ArrayList<>(testSetStats.getReportEntries()));
}

private List<WrappedReportEntry> getTestEntryList() {
return testEntries.get(sourceRootName);
}

private void cleanEntries() {
classNames.remove(sourceRootName);
classEntries.remove(sourceRootName);
testEntries.remove(sourceRootName);
}

private void sortClassEntryList() {
getClassEntryList()
.sort(Comparator.comparing(ReportEntry::getSourceName));
}

private void prepareEntriesForNestedTests() {
if (hasNestedTests()) {
prepareTestEntriesForNestedTest();
}
prepareClassEntriesForNestedTest();
}

private boolean isNestedTestReadyToPrint() {
return getClassEntryList().size() == getClassNameList().size();
}

private void printNestedTests(BiFunction<List<WrappedReportEntry>,List<WrappedReportEntry>, TreePrinter> getTreePrinter) {
sortClassEntryList();
getTreePrinter
.apply(getClassEntryList(), getTestEntryList())
.printTests();
cleanEntries();
}

private void printTests(BiFunction<List<WrappedReportEntry>,List<WrappedReportEntry>, TreePrinter> getTreePrinter) {
getTreePrinter
.apply(singletonList((WrappedReportEntry) report), new ArrayList<>(testSetStats.getReportEntries()))
.printTests();
}

private <J, K, V extends Collection<K>> BiFunction<J, V, V> addToCollection(K obj) {
return (k, v) -> {
v.add(obj);
return v;
};
}

private String getSourceRootName() {
return report.getSourceName().split("\\$", -1)[0];
}

private boolean hasNestedTests() {
if (this.testSetStats == null) {
return hasNestedTests(report);
} else {
return hasNestedTests(testSetStats);
}
}

private boolean hasNestedTests(TestSetStats testSetStats) {
return testSetStats.getReportEntries()
.stream()
.anyMatch(this::hasNestedTests);
}

private boolean hasNestedTests(ReportEntry reportEntry) {
return reportEntry.getSourceName()
.chars()
.filter(c -> c == $)
.count() > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,42 @@
public class TreePrinter {

private final ConsoleLogger consoleLogger;
private final WrappedReportEntry classResult;
private final List<WrappedReportEntry> classResults;
private final List<WrappedReportEntry> testSetStats;
private final List<String> sourceNames;
private final Set<String> distinctSourceName;
private final TestSetStats testSetStats;
private final Theme theme;
private static final int $ = 36;

public TreePrinter(ConsoleLogger consoleLogger, WrappedReportEntry classResult, TestSetStats testSetStats, Theme theme) {
public TreePrinter(ConsoleLogger consoleLogger, List<WrappedReportEntry> classResults, List<WrappedReportEntry> testSetStats, Theme theme) {
this.consoleLogger = consoleLogger;
this.classResult = classResult;
this.sourceNames = getSourceNames(testSetStats);
this.distinctSourceName = getDistinctSourceNames(testSetStats);
this.classResults = classResults;
this.testSetStats = testSetStats;
this.sourceNames = getSourceNames();
this.distinctSourceName = getDistinctSourceNames();
this.theme = theme;
}

public TreePrinter(ConsoleLogger consoleLogger, WrappedReportEntry classResult, TestSetStats testSetStats) {
this(consoleLogger, classResult, testSetStats, Theme.ASCII);
public TreePrinter(ConsoleLogger consoleLogger, List<WrappedReportEntry> classResults, List<WrappedReportEntry> testSetStats) {
this(consoleLogger, classResults, testSetStats, Theme.ASCII);
}

private List<String> getSourceNames(TestSetStats testSetStats) {
return testSetStats.getReportEntries()
private List<String> getSourceNames() {
return testSetStats
.stream()
.map(WrappedReportEntry::getSourceName)
.collect(toList());
}

private Set<String> getDistinctSourceNames(TestSetStats testSetStats) {
return testSetStats.getReportEntries()
private Set<String> getDistinctSourceNames() {
return testSetStats
.stream()
.map(WrappedReportEntry::getSourceName)
.collect(toSet());
}

public void printTests() {
testSetStats.getReportEntries()
testSetStats
.stream()
.map(TestPrinter::new)
.forEach(TestPrinter::printTest);
Expand All @@ -85,7 +85,7 @@ public void printTests() {
private class TestPrinter {

private final WrappedReportEntry testResult;
private final long treeLength;
private final int treeLength;

public TestPrinter(WrappedReportEntry testResult) {
this.testResult = testResult;
Expand Down Expand Up @@ -157,10 +157,10 @@ private void printClass() {
} else {
builder.a(theme.entry());
}

concatenateWithTestGroup(builder, testResult, !isBlank(testResult.getReportNameWithGroup()));
if (treeLength == 0L) {
builder.a(" - " + classResult.elapsedTimeAsString() + "s");
}
builder.a(" - " + classResults.get(treeLength).elapsedTimeAsString() + "s");

println(builder.toString());
}

Expand All @@ -184,8 +184,8 @@ private MessageBuilder getTestPrefix() {
return builder;
}

private long getTreeLength() {
return testResult.getSourceName()
private int getTreeLength() {
return (int) testResult.getSourceName()
.chars()
.filter(c -> c == $)
.count();
Expand Down
Loading