From 3858baa3c3c5bfec03845209e153b3c086c4d93b Mon Sep 17 00:00:00 2001 From: NissMoony Date: Thu, 24 Aug 2023 10:00:49 +0800 Subject: [PATCH 1/2] [SUREFIRE-1934] Ability to disable system-out/system-err for successfuly passed tests. Added ability to disable system-out and system-err blocks. --- .../plugin/surefire/AbstractSurefireMojo.java | 27 ++++++++++++++++++- .../plugin/surefire/CommonReflector.java | 2 ++ .../surefire/StartupReportConfiguration.java | 9 +++++++ ...faultStatelessReportMojoConfiguration.java | 9 ++++++- .../extensions/SurefireStatelessReporter.java | 1 + .../junit5/JUnit5Xml30StatelessReporter.java | 1 + .../report/NullStatelessXmlReporter.java | 2 +- .../surefire/report/StatelessXmlReporter.java | 20 +++++++++++--- .../plugin/surefire/CommonReflectorTest.java | 1 + .../booterclient/ForkStarterTest.java | 2 ++ .../TestSetMockReporterFactory.java | 1 + .../extensions/StatelessReporterTest.java | 4 +-- .../report/DefaultReporterFactoryTest.java | 3 +++ .../report/StatelessXmlReporterTest.java | 5 +++- .../api/booter/ProviderParameterNames.java | 2 ++ .../StatelessReportMojoConfiguration.java | 10 ++++++- .../junitcore/JUnitCoreParameters.java | 12 ++++++++- .../junitcore/JUnitCoreParametersTest.java | 1 + .../surefire/junitcore/JUnitCoreTester.java | 1 + 19 files changed, 102 insertions(+), 11 deletions(-) diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java index 7cbe8bbb2b..4ca3084d0b 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java @@ -679,6 +679,16 @@ public abstract class AbstractSurefireMojo extends AbstractMojo implements Suref @Parameter(property = "enableAssertions", defaultValue = "true") private boolean enableAssertions; + /** + * Flag for including/excluding system-out and system-err elements in xml reports. + *
+ * True by default. + * + * @since 3.0.0 + */ + @Parameter(property = "enableOutputElements", defaultValue = "true") + private boolean enableOutputElements; + /** * The current build session instance. */ @@ -1476,6 +1486,9 @@ private void convertJunitCoreParameters() throws MojoExecutionException { Double.toString(getParallelTestsTimeoutForcedInSeconds())); getProperties() .setProperty(ProviderParameterNames.PARALLEL_OPTIMIZE_PROP, Boolean.toString(isParallelOptimized())); + getProperties() + .setProperty( + ProviderParameterNames.ENABLE_OUTPUT_ELEMENTS_PROP, Boolean.toString(isEnableOutputElements())); String message = "parallel='" + usedParallel + '\'' + ", perCoreThreadCount=" + getPerCoreThreadCount() @@ -1484,7 +1497,8 @@ private void convertJunitCoreParameters() throws MojoExecutionException { + ", threadCountSuites=" + getThreadCountSuites() + ", threadCountClasses=" + getThreadCountClasses() + ", threadCountMethods=" + getThreadCountMethods() - + ", parallelOptimized=" + isParallelOptimized(); + + ", parallelOptimized=" + isParallelOptimized() + + ", enableOutputElements=" + isEnableOutputElements(); logDebugOrCliShowErrors(message); } @@ -1978,6 +1992,7 @@ private StartupReportConfiguration getStartupReportConfiguration(String configCh getReportSchemaLocation(), getEncoding(), isForking, + isEnableOutputElements(), xmlReporter, outReporter, testsetReporter); @@ -2518,6 +2533,7 @@ private String getConfigChecksum() { checksum.add(getTempDir()); checksum.add(useModulePath()); checksum.add(getEnableProcessChecker()); + checksum.add(isEnableOutputElements()); addPluginSpecificChecksumItems(checksum); return checksum.getSha1(); } @@ -3474,6 +3490,15 @@ public void setEnableAssertions(boolean enableAssertions) { this.enableAssertions = enableAssertions; } + public boolean isEnableOutputElements() { + return enableOutputElements; + } + + @SuppressWarnings("UnusedDeclaration") + public void setEnableOutputElements(boolean enableOutputElements) { + this.enableOutputElements = enableOutputElements; + } + public MavenSession getSession() { return session; } diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/CommonReflector.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/CommonReflector.java index 0d76ee8ce9..54d5746ba4 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/CommonReflector.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/CommonReflector.java @@ -86,6 +86,7 @@ private Object createStartupReportConfiguration(@Nonnull StartupReportConfigurat String.class, String.class, boolean.class, + boolean.class, statelessTestsetReporter, consoleOutputReporter, statelessTestsetInfoReporter); @@ -103,6 +104,7 @@ private Object createStartupReportConfiguration(@Nonnull StartupReportConfigurat reporterConfiguration.getXsdSchemaLocation(), reporterConfiguration.getEncoding().name(), reporterConfiguration.isForking(), + reporterConfiguration.isEnableOutputElements(), reporterConfiguration.getXmlReporter().clone(surefireClassLoader), reporterConfiguration.getConsoleOutputReporter().clone(surefireClassLoader), reporterConfiguration.getTestsetReporter().clone(surefireClassLoader) diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/StartupReportConfiguration.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/StartupReportConfiguration.java index d42e22cdac..d0a5a459c2 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/StartupReportConfiguration.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/StartupReportConfiguration.java @@ -85,6 +85,8 @@ public final class StartupReportConfiguration { private final boolean isForking; + private final boolean enableOutputElements; + private final SurefireStatelessReporter xmlReporter; private final SurefireConsoleOutputReporter consoleOutputReporter; @@ -108,6 +110,7 @@ public StartupReportConfiguration( String xsdSchemaLocation, String encoding, boolean isForking, + boolean enableOutputElements, SurefireStatelessReporter xmlReporter, SurefireConsoleOutputReporter consoleOutputReporter, SurefireStatelessTestsetInfoReporter testsetReporter) { @@ -127,6 +130,7 @@ public StartupReportConfiguration( String charset = trimToNull(encoding); this.encoding = charset == null ? UTF_8 : Charset.forName(charset); this.isForking = isForking; + this.enableOutputElements = enableOutputElements; this.xmlReporter = xmlReporter; this.consoleOutputReporter = consoleOutputReporter; this.testsetReporter = testsetReporter; @@ -177,6 +181,7 @@ public StatelessReportEventListener instantiat trimStackTrace, rerunFailingTestsCount, xsdSchemaLocation, + enableOutputElements, testClassMethodRunHistory); return xmlReporter.isDisable() ? null : xmlReporter.createListener(xmlReporterConfig); @@ -239,6 +244,10 @@ public boolean isForking() { return isForking; } + public boolean isEnableOutputElements() { + return enableOutputElements; + } + private File resolveReportsDirectory(Integer forkNumber) { return forkNumber == null ? reportsDirectory : replaceForkThreadsInPath(reportsDirectory, forkNumber); } diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/DefaultStatelessReportMojoConfiguration.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/DefaultStatelessReportMojoConfiguration.java index f134e24a21..3b1c99716d 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/DefaultStatelessReportMojoConfiguration.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/DefaultStatelessReportMojoConfiguration.java @@ -43,8 +43,15 @@ public DefaultStatelessReportMojoConfiguration( boolean trimStackTrace, int rerunFailingTestsCount, String xsdSchemaLocation, + boolean enableOutputElements, Map> testClassMethodRunHistory) { - super(reportsDirectory, reportNameSuffix, trimStackTrace, rerunFailingTestsCount, xsdSchemaLocation); + super( + reportsDirectory, + reportNameSuffix, + trimStackTrace, + rerunFailingTestsCount, + xsdSchemaLocation, + enableOutputElements); this.testClassMethodRunHistory = testClassMethodRunHistory; } diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireStatelessReporter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireStatelessReporter.java index 7e73432fb6..2b7eaf83bb 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireStatelessReporter.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireStatelessReporter.java @@ -64,6 +64,7 @@ public StatelessReportEventListener createList configuration.getRerunFailingTestsCount(), configuration.getTestClassMethodRunHistory(), configuration.getXsdSchemaLocation(), + configuration.isEnableOutputElements(), getVersion(), false, false, diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5Xml30StatelessReporter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5Xml30StatelessReporter.java index 91182c5650..980aa2ceea 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5Xml30StatelessReporter.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/junit5/JUnit5Xml30StatelessReporter.java @@ -103,6 +103,7 @@ public StatelessReportEventListener createList configuration.getRerunFailingTestsCount(), configuration.getTestClassMethodRunHistory(), configuration.getXsdSchemaLocation(), + configuration.isEnableOutputElements(), getVersion(), getUsePhrasedFileName(), getUsePhrasedTestSuiteClassName(), diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullStatelessXmlReporter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullStatelessXmlReporter.java index 73ec5a282e..2ce569f0ad 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullStatelessXmlReporter.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/NullStatelessXmlReporter.java @@ -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); + super(null, null, false, 0, null, null, true, null, false, false, false, false); } @Override diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java index 62299da4ea..306f14e83d 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporter.java @@ -101,6 +101,8 @@ public class StatelessXmlReporter implements StatelessReportEventListener> testClassMethodRunHistoryMap, String xsdSchemaLocation, + boolean enableOutputElements, String xsdVersion, boolean phrasedFileName, boolean phrasedSuiteName, @@ -133,6 +136,7 @@ public StatelessXmlReporter( this.rerunFailingTestsCount = rerunFailingTestsCount; this.testClassMethodRunHistoryMap = testClassMethodRunHistoryMap; this.xsdSchemaLocation = xsdSchemaLocation; + this.enableOutputElements = enableOutputElements; this.xsdVersion = xsdVersion; this.phrasedFileName = phrasedFileName; this.phrasedSuiteName = phrasedSuiteName; @@ -223,11 +227,14 @@ private void serializeTestClassWithoutRerun( ppw, methodEntry, trimStackTrace, + enableOutputElements, outputStream, methodEntry.getReportEntryType().getXmlTag(), false); } - createOutErrElements(fw, ppw, methodEntry, outputStream); + if (enableOutputElements) { + createOutErrElements(fw, ppw, methodEntry, outputStream); + } ppw.endElement(); } } @@ -258,10 +265,13 @@ private void serializeTestClassWithRerun( ppw, singleRunEntry, trimStackTrace, + enableOutputElements, outputStream, singleRunEntry.getReportEntryType().getXmlTag(), false); - createOutErrElements(fw, ppw, singleRunEntry, outputStream); + if (enableOutputElements) { + createOutErrElements(fw, ppw, singleRunEntry, outputStream); + } } else if (singleRunEntry.getReportEntryType() == SKIPPED) { // The version 3.1.0 should produce a new XSD schema with version 3.1.0, see SUREFIRE-1986, // and the XSD schema should add a new element "rerunSkipped" @@ -274,6 +284,7 @@ private void serializeTestClassWithRerun( ppw, singleRunEntry, trimStackTrace, + enableOutputElements, outputStream, singleRunEntry.getReportEntryType().getRerunXmlTag(), true); @@ -299,6 +310,7 @@ private void serializeTestClassWithRerun( ppw, singleRunEntry, trimStackTrace, + enableOutputElements, outputStream, singleRunEntry.getReportEntryType().getFlakyXmlTag(), true); @@ -313,6 +325,7 @@ private void serializeTestClassWithRerun( ppw, firstMethodEntry, trimStackTrace, + enableOutputElements, outputStream, firstMethodEntry.getReportEntryType().getXmlTag(), false); @@ -430,6 +443,7 @@ private static void getTestProblems( XMLWriter ppw, WrappedReportEntry report, boolean trimStackTrace, + boolean enableOutputElements, OutputStream fw, String testErrorType, boolean createOutErrElementsInside) @@ -470,7 +484,7 @@ private static void getTestProblems( } } - if (createOutErrElementsInside) { + if (enableOutputElements && createOutErrElementsInside) { createOutErrElements(outputStreamWriter, ppw, report, fw); } diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/CommonReflectorTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/CommonReflectorTest.java index 6c56e557d3..45d9b27dd3 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/CommonReflectorTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/CommonReflectorTest.java @@ -82,6 +82,7 @@ public void setup() { null, null, false, + true, xmlReporter, consoleOutputReporter, infoReporter); diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkStarterTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkStarterTest.java index 7419ff3f71..b10e919b78 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkStarterTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkStarterTest.java @@ -162,6 +162,7 @@ public void processShouldExitWithoutSayingGoodBye() throws Exception { null, null, true, + true, xmlReporter, outputReporter, statelessTestsetInfoReporter); @@ -247,6 +248,7 @@ public void processShouldWaitForAck() throws Exception { null, null, true, + true, xmlReporter, outputReporter, statelessTestsetInfoReporter); diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/TestSetMockReporterFactory.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/TestSetMockReporterFactory.java index 4ab9957395..c8dc5b236c 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/TestSetMockReporterFactory.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/TestSetMockReporterFactory.java @@ -66,6 +66,7 @@ private static StartupReportConfiguration defaultValue() { null, null, true, + true, new SurefireStatelessReporter(), new SurefireConsoleOutputReporter(), new SurefireStatelessTestsetInfoReporter()); diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/extensions/StatelessReporterTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/extensions/StatelessReporterTest.java index 41e6b250f2..e5f9393cc3 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/extensions/StatelessReporterTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/extensions/StatelessReporterTest.java @@ -66,7 +66,7 @@ public void shouldCreateConsoleListener() { String schema = "https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd"; Map> testClassMethodRunHistory = new HashMap<>(); DefaultStatelessReportMojoConfiguration config = new DefaultStatelessReportMojoConfiguration( - reportsDirectory, reportNameSuffix, true, 5, schema, testClassMethodRunHistory); + reportsDirectory, reportNameSuffix, true, 5, schema, true, testClassMethodRunHistory); SurefireStatelessReporter extension = new SurefireStatelessReporter(); assertThat(extension.getVersion()).isEqualTo("3.0"); @@ -141,7 +141,7 @@ public void shouldCreateJUnit5ConsoleListener() { String schema = "https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd"; Map> testClassMethodRunHistory = new HashMap<>(); DefaultStatelessReportMojoConfiguration config = new DefaultStatelessReportMojoConfiguration( - reportsDirectory, reportNameSuffix, true, 5, schema, testClassMethodRunHistory); + reportsDirectory, reportNameSuffix, true, 5, schema, true, testClassMethodRunHistory); JUnit5Xml30StatelessReporter extension = new JUnit5Xml30StatelessReporter(); assertThat(extension.getVersion()).isEqualTo("3.0"); diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/DefaultReporterFactoryTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/DefaultReporterFactoryTest.java index 9d4c9f924a..1197ab2937 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/DefaultReporterFactoryTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/DefaultReporterFactoryTest.java @@ -90,6 +90,7 @@ public void testMergeTestHistoryResult() throws Exception { null, null, false, + true, new SurefireStatelessReporter(), new SurefireConsoleOutputReporter(), new SurefireStatelessTestsetInfoReporter()); @@ -293,6 +294,7 @@ public void testLogger() { null, null, false, + true, new SurefireStatelessReporter(), new SurefireConsoleOutputReporter(), new SurefireStatelessTestsetInfoReporter()); @@ -357,6 +359,7 @@ public void testCreateReporterWithZeroStatistics() { null, null, false, + true, new SurefireStatelessReporter(), new SurefireConsoleOutputReporter(), new SurefireStatelessTestsetInfoReporter()); diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporterTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporterTest.java index d2e871f637..6a8a21f083 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporterTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/report/StatelessXmlReporterTest.java @@ -105,6 +105,7 @@ public void testFileNameWithoutSuffix() { 0, new ConcurrentHashMap>(), XSD, + true, "3.0", false, false, @@ -165,6 +166,7 @@ public void testAllFieldsSerialized() throws IOException { 0, new ConcurrentHashMap>(), XSD, + true, "3.0", false, false, @@ -267,6 +269,7 @@ public void testOutputRerunFlakyFailure() throws IOException { 1, new HashMap>(), XSD, + true, "3.0", false, false, @@ -370,7 +373,7 @@ public void testOutputRerunFlakyAssumption() throws IOException { rerunStats.testSucceeded(testTwoSecondError); StatelessXmlReporter reporter = new StatelessXmlReporter( - reportDir, null, false, 1, new HashMap<>(), XSD, "3.0", false, false, false, false); + reportDir, null, false, 1, new HashMap<>(), XSD, true, "3.0", false, false, false, false); WrappedReportEntry testSetReportEntry = new WrappedReportEntry( new SimpleReportEntry( diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/api/booter/ProviderParameterNames.java b/surefire-api/src/main/java/org/apache/maven/surefire/api/booter/ProviderParameterNames.java index d59d3de3e8..5a0712a16b 100644 --- a/surefire-api/src/main/java/org/apache/maven/surefire/api/booter/ProviderParameterNames.java +++ b/surefire-api/src/main/java/org/apache/maven/surefire/api/booter/ProviderParameterNames.java @@ -45,4 +45,6 @@ public class ProviderParameterNames { public static final String PARALLEL_TIMEOUTFORCED_PROP = "paralleltimeoutforced"; public static final String PARALLEL_OPTIMIZE_PROP = "paralleloptimization"; + + public static final String ENABLE_OUTPUT_ELEMENTS_PROP = "enableoutputelements"; } diff --git a/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/StatelessReportMojoConfiguration.java b/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/StatelessReportMojoConfiguration.java index f0424207a7..0e0df8342d 100644 --- a/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/StatelessReportMojoConfiguration.java +++ b/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/StatelessReportMojoConfiguration.java @@ -36,17 +36,21 @@ public class StatelessReportMojoConfiguration { private final String xsdSchemaLocation; + private final boolean enableOutputElements; + public StatelessReportMojoConfiguration( File reportsDirectory, String reportNameSuffix, boolean trimStackTrace, int rerunFailingTestsCount, - String xsdSchemaLocation) { + String xsdSchemaLocation, + boolean enableOutputElements) { this.reportsDirectory = reportsDirectory; this.reportNameSuffix = reportNameSuffix; this.trimStackTrace = trimStackTrace; this.rerunFailingTestsCount = rerunFailingTestsCount; this.xsdSchemaLocation = xsdSchemaLocation; + this.enableOutputElements = enableOutputElements; } public File getReportsDirectory() { @@ -68,4 +72,8 @@ public int getRerunFailingTestsCount() { public String getXsdSchemaLocation() { return xsdSchemaLocation; } + + public boolean isEnableOutputElements() { + return enableOutputElements; + } } diff --git a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreParameters.java b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreParameters.java index 600baf476d..7b8f60e76c 100644 --- a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreParameters.java +++ b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreParameters.java @@ -48,6 +48,8 @@ public final class JUnitCoreParameters { public static final String PARALLEL_OPTIMIZE_KEY = ProviderParameterNames.PARALLEL_OPTIMIZE_PROP; + public static final String ENABLE_OUTPUT_ELEMENTS_KEY = ProviderParameterNames.ENABLE_OUTPUT_ELEMENTS_PROP; + private final String parallel; private final boolean perCoreThreadCount; @@ -68,6 +70,8 @@ public final class JUnitCoreParameters { private final boolean parallelOptimization; + private final boolean enableOutputElements; + public JUnitCoreParameters(Map properties) { parallel = property(properties, PARALLEL_KEY, "none").toLowerCase(); perCoreThreadCount = property(properties, PERCORETHREADCOUNT_KEY, true); @@ -79,6 +83,7 @@ public JUnitCoreParameters(Map properties) { parallelTestsTimeoutInSeconds = Math.max(property(properties, PARALLEL_TIMEOUT_KEY, 0d), 0); parallelTestsTimeoutForcedInSeconds = Math.max(property(properties, PARALLEL_TIMEOUTFORCED_KEY, 0d), 0); parallelOptimization = property(properties, PARALLEL_OPTIMIZE_KEY, true); + enableOutputElements = property(properties, ENABLE_OUTPUT_ELEMENTS_KEY, true); } private static Collection lowerCase(String... elements) { @@ -164,13 +169,18 @@ public boolean isParallelOptimization() { return parallelOptimization; } + public boolean isEnableOutputElements() { + return enableOutputElements; + } + @Override public String toString() { return "parallel='" + parallel + '\'' + ", perCoreThreadCount=" + perCoreThreadCount + ", threadCount=" + threadCount + ", useUnlimitedThreads=" + useUnlimitedThreads + ", threadCountSuites=" + threadCountSuites + ", threadCountClasses=" + threadCountClasses + ", threadCountMethods=" + threadCountMethods - + ", parallelOptimization=" + parallelOptimization; + + ", parallelOptimization=" + parallelOptimization + + ", enableOutputElements=" + enableOutputElements; } private static boolean property(Map properties, String key, boolean fallback) { diff --git a/surefire-providers/surefire-junit47/src/test/java/org/apache/maven/surefire/junitcore/JUnitCoreParametersTest.java b/surefire-providers/surefire-junit47/src/test/java/org/apache/maven/surefire/junitcore/JUnitCoreParametersTest.java index 8dd9f55da5..4cea435f71 100644 --- a/surefire-providers/surefire-junit47/src/test/java/org/apache/maven/surefire/junitcore/JUnitCoreParametersTest.java +++ b/surefire-providers/surefire-junit47/src/test/java/org/apache/maven/surefire/junitcore/JUnitCoreParametersTest.java @@ -45,6 +45,7 @@ public void defaultParameters() { assertThat(newTestSetDefault().getParallelTestsTimeoutInSeconds(), is(0d)); assertThat(newTestSetDefault().getParallelTestsTimeoutForcedInSeconds(), is(0d)); assertTrue(newTestSetDefault().isParallelOptimization()); + assertTrue(newTestSetDefault().isEnableOutputElements()); } @Test diff --git a/surefire-providers/surefire-junit47/src/test/java/org/apache/maven/surefire/junitcore/JUnitCoreTester.java b/surefire-providers/surefire-junit47/src/test/java/org/apache/maven/surefire/junitcore/JUnitCoreTester.java index ef9c00c707..279a5c4051 100644 --- a/surefire-providers/surefire-junit47/src/test/java/org/apache/maven/surefire/junitcore/JUnitCoreTester.java +++ b/surefire-providers/surefire-junit47/src/test/java/org/apache/maven/surefire/junitcore/JUnitCoreTester.java @@ -109,6 +109,7 @@ private static StartupReportConfiguration defaultStartupReportConfiguration() { null, null, false, + true, new SurefireStatelessReporter(), new SurefireConsoleOutputReporter(), new SurefireStatelessTestsetInfoReporter()); From 5a10016045aea6625d0cd028f8dd088e588b2696 Mon Sep 17 00:00:00 2001 From: NissMoony Date: Thu, 24 Aug 2023 12:13:22 +0800 Subject: [PATCH 2/2] [SUREFIRE-1934] Ability to disable system-out/system-err for successfuly passed tests. Added it-tests. --- .../its/jiras/Surefire1934OutElementsIT.java | 54 ++++++++++++++++ .../pom.xml | 63 ++++++++++++++++++ .../disableOutElements/TestOutElements.java | 15 +++++ .../surefire-1934-enable-out-elements/pom.xml | 64 +++++++++++++++++++ .../enableOutElements/TestOutElements.java | 15 +++++ 5 files changed, 211 insertions(+) create mode 100644 surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1934OutElementsIT.java create mode 100644 surefire-its/src/test/resources/surefire-1934-disable-out-elements/pom.xml create mode 100644 surefire-its/src/test/resources/surefire-1934-disable-out-elements/src/test/java/disableOutElements/TestOutElements.java create mode 100644 surefire-its/src/test/resources/surefire-1934-enable-out-elements/pom.xml create mode 100644 surefire-its/src/test/resources/surefire-1934-enable-out-elements/src/test/java/enableOutElements/TestOutElements.java diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1934OutElementsIT.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1934OutElementsIT.java new file mode 100644 index 0000000000..4d2e7ea599 --- /dev/null +++ b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1934OutElementsIT.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.surefire.its.jiras; + +import org.apache.maven.surefire.its.fixture.OutputValidator; +import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase; +import org.apache.maven.surefire.its.fixture.TestFile; +import org.junit.Test; + +/** + * Test Surefire-1934. Enabling and disabling system-out and system-err blocks in plugin configuration. + * + * @author NissMoony + */ +public class Surefire1934OutElementsIT extends SurefireJUnit4IntegrationTestCase { + + @Test + public void testOutElementsDisabled() { + final OutputValidator outputValidator = + unpack("/surefire-1934-disable-out-elements").executeTest(); + final TestFile reportFile = + outputValidator.getSurefireReportsXmlFile("TEST-disableOutElements.TestOutElements.xml"); + + reportFile.assertNotContainsText(""); + reportFile.assertNotContainsText(""); + } + + @Test + public void testOutElementsEnabled() { + final OutputValidator outputValidator = + unpack("/surefire-1934-enable-out-elements").executeTest(); + final TestFile reportFile = + outputValidator.getSurefireReportsXmlFile("TEST-enableOutElements.TestOutElements.xml"); + + reportFile.assertContainsText(""); + reportFile.assertContainsText(""); + } +} diff --git a/surefire-its/src/test/resources/surefire-1934-disable-out-elements/pom.xml b/surefire-its/src/test/resources/surefire-1934-disable-out-elements/pom.xml new file mode 100644 index 0000000000..18bb130fda --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1934-disable-out-elements/pom.xml @@ -0,0 +1,63 @@ + + + + + 4.0.0 + + + org.apache.maven.surefire + it-parent + 1.0 + ../pom.xml + + + org.apache.maven.plugins.surefire + surefire-1934-disable-out-elements + 1.0 + http://maven.apache.org + + + + junit + junit + 4.0 + + + log4j + log4j + 1.2.16 + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.version} + + false + + + + + + diff --git a/surefire-its/src/test/resources/surefire-1934-disable-out-elements/src/test/java/disableOutElements/TestOutElements.java b/surefire-its/src/test/resources/surefire-1934-disable-out-elements/src/test/java/disableOutElements/TestOutElements.java new file mode 100644 index 0000000000..2d749ddc5a --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1934-disable-out-elements/src/test/java/disableOutElements/TestOutElements.java @@ -0,0 +1,15 @@ +package disableOutElements; + +import org.apache.log4j.Logger; +import org.junit.Test; + +public class TestOutElements { + + @Test + public void successfulTestWithLogs() { + Logger.getLogger(TestOutElements.class).info("Log output not expected in test report."); + System.out.println("System-out output should not be in the report."); + System.err.println("System-err output should not be in the report."); + } + +} diff --git a/surefire-its/src/test/resources/surefire-1934-enable-out-elements/pom.xml b/surefire-its/src/test/resources/surefire-1934-enable-out-elements/pom.xml new file mode 100644 index 0000000000..338c78eed8 --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1934-enable-out-elements/pom.xml @@ -0,0 +1,64 @@ + + + + + 4.0.0 + + + org.apache.maven.surefire + it-parent + 1.0 + ../pom.xml + + + org.apache.maven.plugins.surefire + surefire-1934-disable-out-elements + 1.0 + http://maven.apache.org + + + + junit + junit + 4.0 + + + log4j + log4j + 1.2.16 + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.version} + + + true + + + + + + diff --git a/surefire-its/src/test/resources/surefire-1934-enable-out-elements/src/test/java/enableOutElements/TestOutElements.java b/surefire-its/src/test/resources/surefire-1934-enable-out-elements/src/test/java/enableOutElements/TestOutElements.java new file mode 100644 index 0000000000..5d6bad1174 --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1934-enable-out-elements/src/test/java/enableOutElements/TestOutElements.java @@ -0,0 +1,15 @@ +package enableOutElements; + +import org.apache.log4j.Logger; +import org.junit.Test; + +public class TestOutElements { + + @Test + public void successfulTestWithLogs() { + Logger.getLogger(TestOutElements.class).info("Log output expected in test report."); + System.out.println("System-out output should be in the report."); + System.err.println("System-err output should be in the report."); + } + +}