Skip to content

Commit

Permalink
[SUREFIRE-1360] Ability to disable properties for successfully passed…
Browse files Browse the repository at this point in the history
… tests

This closes #755
  • Loading branch information
michael-o committed Jul 7, 2024
1 parent c17b92b commit 6aaea8a
Show file tree
Hide file tree
Showing 25 changed files with 333 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,14 @@ public abstract class AbstractSurefireMojo extends AbstractMojo implements Suref
@Parameter(property = "enableOutErrElements", defaultValue = "true")
private boolean enableOutErrElements;

/**
* Flag for including/excluding {@code <properties />} element for successfully passed tests in XML reports.
*
* @since 3.3.1
*/
@Parameter(property = "enablePropertiesElement", defaultValue = "true")
private boolean enablePropertiesElement;

/**
* The current build session instance.
*/
Expand Down Expand Up @@ -1488,6 +1496,10 @@ private void convertJunitCoreParameters() throws MojoExecutionException {
.setProperty(
ProviderParameterNames.ENABLE_OUT_ERR_ELEMENTS_PROP,
Boolean.toString(isEnableOutErrElements()));
getProperties()
.setProperty(
ProviderParameterNames.ENABLE_PROPERTIES_ELEMENT_PROP,
Boolean.toString(isEnablePropertiesElement()));

String message = "parallel='" + usedParallel + '\''
+ ", perCoreThreadCount=" + getPerCoreThreadCount()
Expand All @@ -1497,7 +1509,8 @@ private void convertJunitCoreParameters() throws MojoExecutionException {
+ ", threadCountClasses=" + getThreadCountClasses()
+ ", threadCountMethods=" + getThreadCountMethods()
+ ", parallelOptimized=" + isParallelOptimized()
+ ", enableOutErrElements=" + isEnableOutErrElements();
+ ", enableOutErrElements=" + isEnableOutErrElements()
+ ", enablePropertiesElement=" + isEnablePropertiesElement();

logDebugOrCliShowErrors(message);
}
Expand Down Expand Up @@ -1992,6 +2005,7 @@ private StartupReportConfiguration getStartupReportConfiguration(String configCh
getEncoding(),
isForking,
isEnableOutErrElements(),
isEnablePropertiesElement(),
xmlReporter,
outReporter,
testsetReporter);
Expand Down Expand Up @@ -2533,6 +2547,7 @@ private String getConfigChecksum() {
checksum.add(useModulePath());
checksum.add(getEnableProcessChecker());
checksum.add(isEnableOutErrElements());
checksum.add(isEnablePropertiesElement());
addPluginSpecificChecksumItems(checksum);
return checksum.getSha1();
}
Expand Down Expand Up @@ -3498,6 +3513,15 @@ public void setEnableOutErrElements(boolean enableOutErrElements) {
this.enableOutErrElements = enableOutErrElements;
}

public boolean isEnablePropertiesElement() {
return enablePropertiesElement;
}

@SuppressWarnings("UnusedDeclaration")
public void setEnablePropertiesElement(boolean enablePropertiesElement) {
this.enablePropertiesElement = enablePropertiesElement;
}

public MavenSession getSession() {
return session;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private Object createStartupReportConfiguration(@Nonnull StartupReportConfigurat
String.class,
boolean.class,
boolean.class,
boolean.class,
statelessTestsetReporter,
consoleOutputReporter,
statelessTestsetInfoReporter);
Expand All @@ -105,6 +106,7 @@ private Object createStartupReportConfiguration(@Nonnull StartupReportConfigurat
reporterConfiguration.getEncoding().name(),
reporterConfiguration.isForking(),
reporterConfiguration.isEnableOutErrElements(),
reporterConfiguration.isEnablePropertiesElement(),
reporterConfiguration.getXmlReporter().clone(surefireClassLoader),
reporterConfiguration.getConsoleOutputReporter().clone(surefireClassLoader),
reporterConfiguration.getTestsetReporter().clone(surefireClassLoader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public final class StartupReportConfiguration {

private final boolean enableOutErrElements;

private final boolean enablePropertiesElement;

private final SurefireStatelessReporter xmlReporter;

private final SurefireConsoleOutputReporter consoleOutputReporter;
Expand All @@ -111,6 +113,7 @@ public StartupReportConfiguration(
String encoding,
boolean isForking,
boolean enableOutErrElements,
boolean enablePropertiesElement,
SurefireStatelessReporter xmlReporter,
SurefireConsoleOutputReporter consoleOutputReporter,
SurefireStatelessTestsetInfoReporter testsetReporter) {
Expand All @@ -131,6 +134,7 @@ public StartupReportConfiguration(
this.encoding = charset == null ? UTF_8 : Charset.forName(charset);
this.isForking = isForking;
this.enableOutErrElements = enableOutErrElements;
this.enablePropertiesElement = enablePropertiesElement;
this.xmlReporter = xmlReporter;
this.consoleOutputReporter = consoleOutputReporter;
this.testsetReporter = testsetReporter;
Expand Down Expand Up @@ -182,6 +186,7 @@ public StatelessReportEventListener<WrappedReportEntry, TestSetStats> instantiat
rerunFailingTestsCount,
xsdSchemaLocation,
enableOutErrElements,
enablePropertiesElement,
testClassMethodRunHistory);

return xmlReporter.isDisable() ? null : xmlReporter.createListener(xmlReporterConfig);
Expand Down Expand Up @@ -248,6 +253,10 @@ public boolean isEnableOutErrElements() {
return enableOutErrElements;
}

public boolean isEnablePropertiesElement() {
return enablePropertiesElement;
}

private File resolveReportsDirectory(Integer forkNumber) {
return forkNumber == null ? reportsDirectory : replaceForkThreadsInPath(reportsDirectory, forkNumber);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ public DefaultStatelessReportMojoConfiguration(
int rerunFailingTestsCount,
String xsdSchemaLocation,
boolean enableOutErrElements,
boolean enablePropertiesElement,
Map<String, Deque<WrappedReportEntry>> testClassMethodRunHistory) {
super(
reportsDirectory,
reportNameSuffix,
trimStackTrace,
rerunFailingTestsCount,
xsdSchemaLocation,
enableOutErrElements);
enableOutErrElements,
enablePropertiesElement);
this.testClassMethodRunHistory = testClassMethodRunHistory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public StatelessReportEventListener<WrappedReportEntry, TestSetStats> createList
false,
false,
false,
configuration.isEnableOutErrElements());
configuration.isEnableOutErrElements(),
configuration.isEnablePropertiesElement());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public StatelessReportEventListener<WrappedReportEntry, TestSetStats> createList
getUsePhrasedTestSuiteClassName(),
getUsePhrasedTestCaseClassName(),
getUsePhrasedTestCaseMethodName(),
configuration.isEnableOutErrElements());
configuration.isEnableOutErrElements(),
configuration.isEnablePropertiesElement());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class NullStatelessXmlReporter extends StatelessXmlReporter {
static final NullStatelessXmlReporter INSTANCE = new NullStatelessXmlReporter();

private NullStatelessXmlReporter() {
super(null, null, false, 0, null, null, null, false, false, false, false, true);
super(null, null, false, 0, null, null, null, false, false, false, false, true, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public class StatelessXmlReporter implements StatelessReportEventListener<Wrappe

private final boolean enableOutErrElements;

private final boolean enablePropertiesElement;

public StatelessXmlReporter(
File reportsDirectory,
String reportNameSuffix,
Expand All @@ -130,19 +132,21 @@ public StatelessXmlReporter(
boolean phrasedSuiteName,
boolean phrasedClassName,
boolean phrasedMethodName,
boolean enableOutErrElements) {
boolean enableOutErrElements,
boolean enablePropertiesElement) {
this.reportsDirectory = reportsDirectory;
this.reportNameSuffix = reportNameSuffix;
this.trimStackTrace = trimStackTrace;
this.rerunFailingTestsCount = rerunFailingTestsCount;
this.testClassMethodRunHistoryMap = testClassMethodRunHistoryMap;
this.xsdSchemaLocation = xsdSchemaLocation;
this.enableOutErrElements = enableOutErrElements;
this.xsdVersion = xsdVersion;
this.phrasedFileName = phrasedFileName;
this.phrasedSuiteName = phrasedSuiteName;
this.phrasedClassName = phrasedClassName;
this.phrasedMethodName = phrasedMethodName;
this.enableOutErrElements = enableOutErrElements;
this.enablePropertiesElement = enablePropertiesElement;
}

@Override
Expand All @@ -158,7 +162,27 @@ public void testSetCompleted(WrappedReportEntry testSetReportEntry, TestSetStats

createTestSuiteElement(ppw, testSetReportEntry, testSetStats); // TestSuite

showProperties(ppw, testSetReportEntry.getSystemProperties());
if (enablePropertiesElement) {
showProperties(ppw, testSetReportEntry.getSystemProperties());
} else {
boolean hasNonSuccess = false;
for (Map<String, List<WrappedReportEntry>> statistics : classMethodStatistics.values()) {
for (List<WrappedReportEntry> thisMethodRuns : statistics.values()) {
if (thisMethodRuns.stream()
.anyMatch(entry -> entry.getReportEntryType() != ReportEntryType.SUCCESS)) {
hasNonSuccess = true;
break;
}
}
if (hasNonSuccess) {
break;
}
}

if (hasNonSuccess) {
showProperties(ppw, testSetReportEntry.getSystemProperties());
}
}

for (Entry<String, Map<String, List<WrappedReportEntry>>> statistics : classMethodStatistics.entrySet()) {
for (Entry<String, List<WrappedReportEntry>> thisMethodRuns :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void setup() {
null,
false,
true,
true,
xmlReporter,
consoleOutputReporter,
infoReporter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public void processShouldExitWithoutSayingGoodBye() throws Exception {
null,
true,
true,
true,
xmlReporter,
outputReporter,
statelessTestsetInfoReporter);
Expand Down Expand Up @@ -249,6 +250,7 @@ public void processShouldWaitForAck() throws Exception {
null,
true,
true,
true,
xmlReporter,
outputReporter,
statelessTestsetInfoReporter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private static StartupReportConfiguration defaultValue() {
null,
true,
true,
true,
new SurefireStatelessReporter(),
new SurefireConsoleOutputReporter(),
new SurefireStatelessTestsetInfoReporter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void shouldCreateConsoleListener() {
String schema = "https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd";
Map<String, Deque<WrappedReportEntry>> testClassMethodRunHistory = new HashMap<>();
DefaultStatelessReportMojoConfiguration config = new DefaultStatelessReportMojoConfiguration(
reportsDirectory, reportNameSuffix, true, 5, schema, true, testClassMethodRunHistory);
reportsDirectory, reportNameSuffix, true, 5, schema, true, true, testClassMethodRunHistory);
SurefireStatelessReporter extension = new SurefireStatelessReporter();

assertThat(extension.getVersion()).isEqualTo("3.0.1");
Expand Down Expand Up @@ -141,7 +141,7 @@ public void shouldCreateJUnit5ConsoleListener() {
String schema = "https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd";
Map<String, Deque<WrappedReportEntry>> testClassMethodRunHistory = new HashMap<>();
DefaultStatelessReportMojoConfiguration config = new DefaultStatelessReportMojoConfiguration(
reportsDirectory, reportNameSuffix, true, 5, schema, true, testClassMethodRunHistory);
reportsDirectory, reportNameSuffix, true, 5, schema, true, true, testClassMethodRunHistory);
JUnit5Xml30StatelessReporter extension = new JUnit5Xml30StatelessReporter();

assertThat(extension.getVersion()).isEqualTo("3.0.1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void testMergeTestHistoryResult() throws Exception {
null,
false,
true,
true,
new SurefireStatelessReporter(),
new SurefireConsoleOutputReporter(),
new SurefireStatelessTestsetInfoReporter());
Expand Down Expand Up @@ -290,6 +291,7 @@ public void testLogger() {
null,
false,
true,
true,
new SurefireStatelessReporter(),
new SurefireConsoleOutputReporter(),
new SurefireStatelessTestsetInfoReporter());
Expand Down Expand Up @@ -355,6 +357,7 @@ public void testCreateReporterWithZeroStatistics() {
null,
false,
true,
true,
new SurefireStatelessReporter(),
new SurefireConsoleOutputReporter(),
new SurefireStatelessTestsetInfoReporter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public void testFileNameWithoutSuffix() {
false,
false,
false,
true,
true);
reporter.cleanTestHistoryMap();

Expand Down Expand Up @@ -171,6 +172,7 @@ public void testAllFieldsSerialized() throws IOException {
false,
false,
false,
true,
true);
reporter.testSetCompleted(testSetReportEntry, stats);

Expand Down Expand Up @@ -274,6 +276,7 @@ public void testOutputRerunFlakyFailure() throws IOException {
false,
false,
false,
true,
true);

reporter.testSetCompleted(testSetReportEntry, stats);
Expand Down Expand Up @@ -373,7 +376,7 @@ public void testOutputRerunFlakyAssumption() throws IOException {
rerunStats.testSucceeded(testTwoSecondError);

StatelessXmlReporter reporter = new StatelessXmlReporter(
reportDir, null, false, 1, new HashMap<>(), XSD, "3.0.1", false, false, false, false, true);
reportDir, null, false, 1, new HashMap<>(), XSD, "3.0.1", false, false, false, false, true, true);

WrappedReportEntry testSetReportEntry = new WrappedReportEntry(
new SimpleReportEntry(
Expand Down Expand Up @@ -537,7 +540,7 @@ public void testReporterHandlesATestWithoutMessageAndWithEmptyStackTrace() {
null);

StatelessXmlReporter reporter = new StatelessXmlReporter(
reportDir, null, false, 1, new HashMap<>(), XSD, "3.0.1", false, false, false, false, true);
reportDir, null, false, 1, new HashMap<>(), XSD, "3.0.1", false, false, false, false, true, true);

reporter.testSetCompleted(testReport, stats);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ public class ProviderParameterNames {
public static final String PARALLEL_OPTIMIZE_PROP = "paralleloptimization";

public static final String ENABLE_OUT_ERR_ELEMENTS_PROP = "enableouterrelements";

public static final String ENABLE_PROPERTIES_ELEMENT_PROP = "enablepropertieselement";
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,23 @@ public class StatelessReportMojoConfiguration {

private final boolean enableOutErrElements;

private final boolean enablePropertiesElement;

public StatelessReportMojoConfiguration(
File reportsDirectory,
String reportNameSuffix,
boolean trimStackTrace,
int rerunFailingTestsCount,
String xsdSchemaLocation,
boolean enableOutErrElements) {
boolean enableOutErrElements,
boolean enablePropertiesElement) {
this.reportsDirectory = reportsDirectory;
this.reportNameSuffix = reportNameSuffix;
this.trimStackTrace = trimStackTrace;
this.rerunFailingTestsCount = rerunFailingTestsCount;
this.xsdSchemaLocation = xsdSchemaLocation;
this.enableOutErrElements = enableOutErrElements;
this.enablePropertiesElement = enablePropertiesElement;
}

public File getReportsDirectory() {
Expand All @@ -76,4 +80,8 @@ public String getXsdSchemaLocation() {
public boolean isEnableOutErrElements() {
return enableOutErrElements;
}

public boolean isEnablePropertiesElement() {
return enablePropertiesElement;
}
}
Loading

0 comments on commit 6aaea8a

Please sign in to comment.