From b69bc8315a65b60f0b987b1edd33862747ff2bbc Mon Sep 17 00:00:00 2001 From: NissMoony Date: Thu, 24 Aug 2023 10:00:49 +0800 Subject: [PATCH] [SUREFIRE-1934] Ability to disable system-out/system-err for successfully passed tests Co-authored-by: Michael Osipov This closes #670 --- .../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 | 8 ++- .../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 ++- .../jiras/Surefire1934OutErrElementsIT.java | 54 ++++++++++++++ .../pom.xml | 69 ++++++++++++++++++ .../TestOutErrElements.java | 15 ++++ .../pom.xml | 70 +++++++++++++++++++ .../TestOutErrElements.java | 15 ++++ .../junitcore/JUnitCoreParameters.java | 12 +++- .../junitcore/JUnitCoreParametersTest.java | 1 + .../surefire/junitcore/JUnitCoreTester.java | 1 + 24 files changed, 315 insertions(+), 9 deletions(-) create mode 100644 surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1934OutErrElementsIT.java create mode 100644 surefire-its/src/test/resources/surefire-1934-disable-out-err-elements/pom.xml create mode 100644 surefire-its/src/test/resources/surefire-1934-disable-out-err-elements/src/test/java/disableOutErrElements/TestOutErrElements.java create mode 100644 surefire-its/src/test/resources/surefire-1934-enable-out-err-elements/pom.xml create mode 100644 surefire-its/src/test/resources/surefire-1934-enable-out-err-elements/src/test/java/enableOutErrElements/TestOutErrElements.java 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 664121d04b..0383a49fa8 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 @@ -690,6 +690,15 @@ public abstract class AbstractSurefireMojo extends AbstractMojo implements Suref @Parameter(property = "enableAssertions", defaultValue = "true") private boolean enableAssertions; + /** + * Flag for including/excluding {@code } and {@code } elements for + * successfully passed tests in XML reports. + * Note that the default value may change to {@code false} is a future version. + * @since 3.3.1 + */ + @Parameter(property = "enableOutErrElements", defaultValue = "true") + private boolean enableOutErrElements; + /** * The current build session instance. */ @@ -1474,6 +1483,10 @@ private void convertJunitCoreParameters() throws MojoExecutionException { Double.toString(getParallelTestsTimeoutForcedInSeconds())); getProperties() .setProperty(ProviderParameterNames.PARALLEL_OPTIMIZE_PROP, Boolean.toString(isParallelOptimized())); + getProperties() + .setProperty( + ProviderParameterNames.ENABLE_OUT_ERR_ELEMENTS_PROP, + Boolean.toString(isEnableOutErrElements())); String message = "parallel='" + usedParallel + '\'' + ", perCoreThreadCount=" + getPerCoreThreadCount() @@ -1482,7 +1495,8 @@ private void convertJunitCoreParameters() throws MojoExecutionException { + ", threadCountSuites=" + getThreadCountSuites() + ", threadCountClasses=" + getThreadCountClasses() + ", threadCountMethods=" + getThreadCountMethods() - + ", parallelOptimized=" + isParallelOptimized(); + + ", parallelOptimized=" + isParallelOptimized() + + ", enableOutErrElements=" + isEnableOutErrElements(); logDebugOrCliShowErrors(message); } @@ -1976,6 +1990,7 @@ private StartupReportConfiguration getStartupReportConfiguration(String configCh getReportSchemaLocation(), getEncoding(), isForking, + isEnableOutErrElements(), xmlReporter, outReporter, testsetReporter); @@ -2516,6 +2531,7 @@ private String getConfigChecksum() { checksum.add(getTempDir()); checksum.add(useModulePath()); checksum.add(getEnableProcessChecker()); + checksum.add(isEnableOutErrElements()); addPluginSpecificChecksumItems(checksum); return checksum.getSha1(); } @@ -3472,6 +3488,15 @@ public void setEnableAssertions(boolean enableAssertions) { this.enableAssertions = enableAssertions; } + public boolean isEnableOutErrElements() { + return enableOutErrElements; + } + + @SuppressWarnings("UnusedDeclaration") + public void setEnableOutErrElements(boolean enableOutErrElements) { + this.enableOutErrElements = enableOutErrElements; + } + 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..4aaa34c631 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.isEnableOutErrElements(), 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..b4994869ed 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 enableOutErrElements; + private final SurefireStatelessReporter xmlReporter; private final SurefireConsoleOutputReporter consoleOutputReporter; @@ -108,6 +110,7 @@ public StartupReportConfiguration( String xsdSchemaLocation, String encoding, boolean isForking, + boolean enableOutErrElements, 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.enableOutErrElements = enableOutErrElements; this.xmlReporter = xmlReporter; this.consoleOutputReporter = consoleOutputReporter; this.testsetReporter = testsetReporter; @@ -177,6 +181,7 @@ public StatelessReportEventListener instantiat trimStackTrace, rerunFailingTestsCount, xsdSchemaLocation, + enableOutErrElements, testClassMethodRunHistory); return xmlReporter.isDisable() ? null : xmlReporter.createListener(xmlReporterConfig); @@ -239,6 +244,10 @@ public boolean isForking() { return isForking; } + public boolean isEnableOutErrElements() { + return enableOutErrElements; + } + 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..57cae2ad9d 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 enableOutErrElements, Map> testClassMethodRunHistory) { - super(reportsDirectory, reportNameSuffix, trimStackTrace, rerunFailingTestsCount, xsdSchemaLocation); + super( + reportsDirectory, + reportNameSuffix, + trimStackTrace, + rerunFailingTestsCount, + xsdSchemaLocation, + enableOutErrElements); 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 ebd7d0051f..ba898c10a0 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.isEnableOutErrElements(), 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 689a4caed8..e91342211c 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.isEnableOutErrElements(), 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 f5f2916c5e..e5e9c1023f 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 @@ -102,6 +102,8 @@ public class StatelessXmlReporter implements StatelessReportEventListener> testClassMethodRunHistoryMap, String xsdSchemaLocation, + boolean enableOutErrElements, String xsdVersion, boolean phrasedFileName, boolean phrasedSuiteName, @@ -134,6 +137,7 @@ public StatelessXmlReporter( this.rerunFailingTestsCount = rerunFailingTestsCount; this.testClassMethodRunHistoryMap = testClassMethodRunHistoryMap; this.xsdSchemaLocation = xsdSchemaLocation; + this.enableOutErrElements = enableOutErrElements; this.xsdVersion = xsdVersion; this.phrasedFileName = phrasedFileName; this.phrasedSuiteName = phrasedSuiteName; @@ -228,7 +232,9 @@ private void serializeTestClassWithoutRerun( methodEntry.getReportEntryType().getXmlTag(), false); } - createOutErrElements(fw, ppw, methodEntry, outputStream); + if (methodEntry.getReportEntryType() != SUCCESS || enableOutErrElements) { + createOutErrElements(fw, ppw, methodEntry, outputStream); + } ppw.endElement(); } } 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 e034e2f56e..8ee90ee2cd 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.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.1"); @@ -141,7 +141,7 @@ public void shouldCreateJUnit5ConsoleListener() { String schema = "https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.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.1"); 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 e767c30f16..081248f47c 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 @@ -85,6 +85,7 @@ public void testMergeTestHistoryResult() throws Exception { null, null, false, + true, new SurefireStatelessReporter(), new SurefireConsoleOutputReporter(), new SurefireStatelessTestsetInfoReporter()); @@ -288,6 +289,7 @@ public void testLogger() { null, null, false, + true, new SurefireStatelessReporter(), new SurefireConsoleOutputReporter(), new SurefireStatelessTestsetInfoReporter()); @@ -352,6 +354,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 f46bb20016..68a7818f2c 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.1", false, false, @@ -165,6 +166,7 @@ public void testAllFieldsSerialized() throws IOException { 0, new ConcurrentHashMap>(), XSD, + true, "3.0.1", false, false, @@ -267,6 +269,7 @@ public void testOutputRerunFlakyFailure() throws IOException { 1, new HashMap>(), XSD, + true, "3.0.1", 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.1", false, false, false, false); + reportDir, null, false, 1, new HashMap<>(), XSD, true, "3.0.1", 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..95da2d3bee 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_OUT_ERR_ELEMENTS_PROP = "enableouterrelements"; } 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..ec3716deb9 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 enableOutErrElements; + public StatelessReportMojoConfiguration( File reportsDirectory, String reportNameSuffix, boolean trimStackTrace, int rerunFailingTestsCount, - String xsdSchemaLocation) { + String xsdSchemaLocation, + boolean enableOutErrElements) { this.reportsDirectory = reportsDirectory; this.reportNameSuffix = reportNameSuffix; this.trimStackTrace = trimStackTrace; this.rerunFailingTestsCount = rerunFailingTestsCount; this.xsdSchemaLocation = xsdSchemaLocation; + this.enableOutErrElements = enableOutErrElements; } public File getReportsDirectory() { @@ -68,4 +72,8 @@ public int getRerunFailingTestsCount() { public String getXsdSchemaLocation() { return xsdSchemaLocation; } + + public boolean isEnableOutErrElements() { + return enableOutErrElements; + } } diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1934OutErrElementsIT.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1934OutErrElementsIT.java new file mode 100644 index 0000000000..1c955350df --- /dev/null +++ b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1934OutErrElementsIT.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 Surefire1934OutErrElementsIT extends SurefireJUnit4IntegrationTestCase { + + @Test + public void testOutErrElementsDisabled() { + final OutputValidator outputValidator = + unpack("/surefire-1934-disable-out-err-elements").executeTest(); + final TestFile reportFile = + outputValidator.getSurefireReportsXmlFile("TEST-disableOutErrElements.TestOutErrElements.xml"); + + reportFile.assertNotContainsText(""); + reportFile.assertNotContainsText(""); + } + + @Test + public void testOutErrElementsEnabled() { + final OutputValidator outputValidator = + unpack("/surefire-1934-enable-out-err-elements").executeTest(); + final TestFile reportFile = + outputValidator.getSurefireReportsXmlFile("TEST-enableOutErrElements.TestOutErrElements.xml"); + + reportFile.assertContainsText(""); + reportFile.assertContainsText(""); + } +} diff --git a/surefire-its/src/test/resources/surefire-1934-disable-out-err-elements/pom.xml b/surefire-its/src/test/resources/surefire-1934-disable-out-err-elements/pom.xml new file mode 100644 index 0000000000..4deef9036e --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1934-disable-out-err-elements/pom.xml @@ -0,0 +1,69 @@ + + + + + 4.0.0 + + + org.apache.maven.surefire + it-parent + 1.0 + ../pom.xml + + + org.apache.maven.plugins.surefire + surefire-1934-disable-out-err-elements + 1.0 + http://maven.apache.org + + + + junit + junit + 4.0 + + + org.slf4j + slf4j-api + 1.7.36 + + + org.slf4j + slf4j-simple + 1.7.36 + runtime + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.version} + + false + + + + + + diff --git a/surefire-its/src/test/resources/surefire-1934-disable-out-err-elements/src/test/java/disableOutErrElements/TestOutErrElements.java b/surefire-its/src/test/resources/surefire-1934-disable-out-err-elements/src/test/java/disableOutErrElements/TestOutErrElements.java new file mode 100644 index 0000000000..acfda567c1 --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1934-disable-out-err-elements/src/test/java/disableOutErrElements/TestOutErrElements.java @@ -0,0 +1,15 @@ +package disableOutElements; + +import org.junit.Test; +import org.slf4j.LoggerFactory; + +public class TestOutErrElements { + + @Test + public void successfulTestWithLogs() { + LoggerFactory.getLogger(TestOutErrElements.class).info("Log output not expected in test report."); + System.out.println("System-out output not expected in the report."); + System.err.println("System-err output not expected in the report."); + } + +} diff --git a/surefire-its/src/test/resources/surefire-1934-enable-out-err-elements/pom.xml b/surefire-its/src/test/resources/surefire-1934-enable-out-err-elements/pom.xml new file mode 100644 index 0000000000..7b0723b682 --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1934-enable-out-err-elements/pom.xml @@ -0,0 +1,70 @@ + + + + + 4.0.0 + + + org.apache.maven.surefire + it-parent + 1.0 + ../pom.xml + + + org.apache.maven.plugins.surefire + surefire-1934-disable-out-err-elements + 1.0 + http://maven.apache.org + + + + junit + junit + 4.0 + + + org.slf4j + slf4j-api + 1.7.36 + + + org.slf4j + slf4j-simple + 1.7.36 + runtime + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.version} + + + true + + + + + + diff --git a/surefire-its/src/test/resources/surefire-1934-enable-out-err-elements/src/test/java/enableOutErrElements/TestOutErrElements.java b/surefire-its/src/test/resources/surefire-1934-enable-out-err-elements/src/test/java/enableOutErrElements/TestOutErrElements.java new file mode 100644 index 0000000000..11b0b28737 --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1934-enable-out-err-elements/src/test/java/enableOutErrElements/TestOutErrElements.java @@ -0,0 +1,15 @@ +package enableOutElements; + +import org.junit.Test; +import org.slf4j.LoggerFactory; + +public class TestOutErrElements { + + @Test + public void successfulTestWithLogs() { + LoggerFactory.getLogger(TestOutErrElements.class).info("Log output expected in test report."); + System.out.println("System-out output expected in the report."); + System.err.println("System-err output expected in the report."); + } + +} 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..3e9beca45d 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_OUT_ERR_ELEMENTS_KEY = ProviderParameterNames.ENABLE_OUT_ERR_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 enableOutErrElements; + 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); + enableOutErrElements = property(properties, ENABLE_OUT_ERR_ELEMENTS_KEY, true); } private static Collection lowerCase(String... elements) { @@ -164,13 +169,18 @@ public boolean isParallelOptimization() { return parallelOptimization; } + public boolean isEnableOutErrElements() { + return enableOutErrElements; + } + @Override public String toString() { return "parallel='" + parallel + '\'' + ", perCoreThreadCount=" + perCoreThreadCount + ", threadCount=" + threadCount + ", useUnlimitedThreads=" + useUnlimitedThreads + ", threadCountSuites=" + threadCountSuites + ", threadCountClasses=" + threadCountClasses + ", threadCountMethods=" + threadCountMethods - + ", parallelOptimization=" + parallelOptimization; + + ", parallelOptimization=" + parallelOptimization + + ", enableOutErrElements=" + enableOutErrElements; } 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..c635ca0d50 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().isEnableOutErrElements()); } @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());