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 1 commit
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, Collection<WrappedReportEntry> testEntries) {
return new TreePrinter(getConsoleLogger(), classEntries, testEntries);
}

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

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

import java.util.*;

import static java.util.Collections.singletonList;
import static java.util.Collections.synchronizedMap;

abstract class ConsoleTreeReporterBase extends ConsoleReporter {

protected static final Map<String, Set<String>> reportEntriesName = synchronizedMap(new HashMap<>());
protected static final Map<String, List<WrappedReportEntry>> reportEntriesMap = synchronizedMap(new HashMap<>());
protected static final Map<String, Collection<WrappedReportEntry>> testSetStatsMap = synchronizedMap(new HashMap<>());
protected static final int $ = 36;

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

@Override
public void testSetStarting(TestSetReportEntry report) {
if (hasNested(report)) {
reportEntriesName.putIfAbsent(getRootSourceName(report), new HashSet<>());
reportEntriesName.computeIfPresent(getRootSourceName(report),
(k, v) -> {
v.add(getRootSourceName(report));
v.add(report.getSourceName());
return v;
});
}
}

@Override
public void testSetCompleted(WrappedReportEntry report, TestSetStats testSetStats, List<String> testResults) {
if (hasNested(testSetStats)) {
testSetStatsMap.putIfAbsent(getRootSourceName(report), new ArrayList<>(testSetStats.getReportEntries()));
reportEntriesMap.putIfAbsent(getRootSourceName(report), new ArrayList<>());
}
if (reportEntriesName.containsKey(getRootSourceName(report))) {
reportEntriesMap.computeIfPresent(getRootSourceName(report), (k, v) -> {
v.add(report);
return v;
});

if (reportEntriesMap.get(getRootSourceName(report)).size() == reportEntriesName.get(getRootSourceName(report)).size()) {
reportEntriesMap.get(getRootSourceName(report)).sort(Comparator.comparing(WrappedReportEntry::getSourceName));
getTreePrinter(reportEntriesMap.get(getRootSourceName(report)), testSetStatsMap.get(getRootSourceName(report)))
.printTests();
clean(getRootSourceName(report));
}
} else {
getTreePrinter(singletonList(report), testSetStats.getReportEntries()).printTests();
}
}

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

protected void clean(String rootSourceName) {
reportEntriesName.remove(rootSourceName);
reportEntriesMap.remove(rootSourceName);
}

protected String getRootSourceName(ReportEntry reportEntry) {
return reportEntry.getSourceName().split("\\$")[0];
fabriciorby marked this conversation as resolved.
Show resolved Hide resolved
}

protected boolean hasNested(TestSetStats testSetStats) {
return testSetStats.getReportEntries()
.stream()
.anyMatch(this::hasNested);
}

protected boolean hasNested(ReportEntry reportEntry) {
return reportEntry.getSourceName()
.chars()
.filter(c -> c == $)
.count() > 0;
}

}
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, Collection<WrappedReportEntry> testEntries) {
return new TreePrinter(getConsoleLogger(), classEntries, testEntries, Theme.UNICODE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.apache.maven.surefire.shared.utils.logging.MessageBuilder;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.LongStream;
Expand All @@ -41,42 +42,42 @@
public class TreePrinter {

private final ConsoleLogger consoleLogger;
private final WrappedReportEntry classResult;
private final List<WrappedReportEntry> classResults;
private final List<String> sourceNames;
private final Set<String> distinctSourceName;
private final TestSetStats testSetStats;
private final Collection<WrappedReportEntry> 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, Collection<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, Collection<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 +86,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 +158,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 +185,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
15 changes: 10 additions & 5 deletions src/test/java/NestedExampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public class NestedExampleTest {

@Test
@DisplayName("Should pass")
void test() {
void test() throws InterruptedException {
Thread.sleep(100);
}

@Nested
Expand All @@ -16,7 +17,8 @@ class InnerTest {

@Test
@DisplayName("Inner test should pass")
void test() {
void test() throws InterruptedException {
Thread.sleep(200);
}

@Nested
Expand All @@ -25,7 +27,8 @@ class InnerInnerTest {

@Test
@DisplayName("Inner Inner Test should pass")
void test() {
void test() throws InterruptedException {
Thread.sleep(300);
}

@Nested
Expand All @@ -34,7 +37,8 @@ class InnerInnerInnerTest {

@Test
@DisplayName("Inner Inner Inner Test should pass")
void test() {
void test() throws InterruptedException {
Thread.sleep(400);
}

}
Expand All @@ -45,7 +49,8 @@ void test() {

@Test
@DisplayName("Should pass2")
void test2() {
void test2() throws InterruptedException {
Thread.sleep(500);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.apache.maven.surefire.api.report.SimpleReportEntry;
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.PlexusContainerException;
import org.codehaus.plexus.logging.Logger;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -13,21 +15,35 @@
class ConsoleTreeReporterTest {

//Test for NestedExampleTest

Utf8RecodingDeferredFileOutputStream stdout = new Utf8RecodingDeferredFileOutputStream("stdout");
Utf8RecodingDeferredFileOutputStream stderr = new Utf8RecodingDeferredFileOutputStream("stderr");

static DefaultPlexusContainer container;
static Logger logger;

@BeforeAll
static void setupContainer() throws PlexusContainerException {
container = new DefaultPlexusContainer();
logger = container.getLogger();
}

@Test
void testSetStarting() {
//Runs 4 times for this class
SimpleReportEntry simpleReportEntry1 = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest", "Nested Sample", null, null);
SimpleReportEntry simpleReportEntry2 = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest$InnerTest", "Inner Test", null, null);
SimpleReportEntry simpleReportEntry3 = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest$InnerTest$InnerInnerTest", "Inner Inner Test", null, null);
SimpleReportEntry simpleReportEntry4 = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest$InnerTest$InnerInnerTest$InnerInnerInnerTest", "Inner Inner Inner Test", null, null);

ConsoleTreeReporterAscii consoleTreeReporter = new ConsoleTreeReporterAscii(new PluginConsoleLogger(logger), false, false);
consoleTreeReporter.testSetStarting(simpleReportEntry1);
consoleTreeReporter.testSetStarting(simpleReportEntry2);
consoleTreeReporter.testSetStarting(simpleReportEntry3);
consoleTreeReporter.testSetStarting(simpleReportEntry4);
}

@Test
void testSetCompleted() throws PlexusContainerException {
void testSetCompleted() {

//TestStarting parameters
SimpleReportEntry simpleReportEntry1 = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest", "Nested Sample", null, null);
Expand Down Expand Up @@ -56,19 +72,18 @@ void testSetCompleted() throws PlexusContainerException {
testSetStats.testSucceeded(wrappedReportEntry4);
testSetStats.testSucceeded(wrappedReportEntry5);

SimpleReportEntry classEntry = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "source", "sourceTest", "name", "nameTest");
WrappedReportEntry classReportEntry = new WrappedReportEntry(classEntry, ReportEntryType.SUCCESS, 112, stdout, stderr);
TestSetStats testSetStatsForClass = new TestSetStats(false, true);

DefaultPlexusContainer container = new DefaultPlexusContainer();
container.getLogger();

ConsoleTreeReporter consoleTreeReporter = new ConsoleTreeReporter(new PluginConsoleLogger(container.getLogger()), false, false);
consoleTreeReporter.testSetStarting(classReportEntry);
ConsoleTreeReporterAscii consoleTreeReporter = new ConsoleTreeReporterAscii(new PluginConsoleLogger(logger), false, false);
consoleTreeReporter.testSetStarting(simpleReportEntry1);
consoleTreeReporter.testSetStarting(simpleReportEntry2);
consoleTreeReporter.testSetStarting(simpleReportEntry3);
consoleTreeReporter.testSetStarting(simpleReportEntry4);
consoleTreeReporter.testSetCompleted(classReportEntry, testSetStats, null);
consoleTreeReporter.testSetCompleted(wrappedReportEntry5, testSetStats, null);
consoleTreeReporter.testSetCompleted(wrappedReportEntry4, testSetStatsForClass, null);
consoleTreeReporter.testSetCompleted(wrappedReportEntry3, testSetStatsForClass, null);
consoleTreeReporter.testSetCompleted(wrappedReportEntry2, testSetStatsForClass, null);

//TODO see how to unit test this
}

Expand Down