diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformStreamCorruptionIT.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformStreamCorruptionIT.java new file mode 100644 index 0000000000..00a556c744 --- /dev/null +++ b/surefire-its/src/test/java/org/apache/maven/surefire/its/JUnitPlatformStreamCorruptionIT.java @@ -0,0 +1,56 @@ +package org.apache.maven.surefire.its; + +/* + * 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. + */ + +import org.apache.maven.it.VerificationException; +import org.apache.maven.surefire.its.fixture.OutputValidator; +import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +import static org.apache.maven.surefire.its.fixture.HelperAssertions.assumeJavaVersion; +import static org.fest.assertions.Assertions.assertThat; +import static org.hamcrest.CoreMatchers.startsWith; + +public class JUnitPlatformStreamCorruptionIT + extends SurefireJUnit4IntegrationTestCase +{ + @Before + public void setUp() + { + assumeJavaVersion( 1.8d ); + } + + @Test + public void warningIsNotEmitted() throws VerificationException + { + OutputValidator validator = unpack( "/surefire-1614-stream-corruption" ) + .executeTest() + .verifyErrorFree( 1 ); + + List lines = validator.loadLogLines( + startsWith( "[WARNING] Corrupted STDOUT by directly writing to native stream in forked JVM" ) ); + + assertThat( lines ) + .isEmpty(); + } +} diff --git a/surefire-its/src/test/resources/surefire-1614-stream-corruption/pom.xml b/surefire-its/src/test/resources/surefire-1614-stream-corruption/pom.xml new file mode 100644 index 0000000000..25726cb30c --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1614-stream-corruption/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + org.apache.maven.plugins.surefire + junit-platform-1.0.0 + 1.0 + [SUREFIRE-1614] JUnit 5: Vintage Output Stream Corruption + + + UTF-8 + 1.8 + 1.8 + + + + + org.junit.vintage + junit-vintage-engine + 5.3.2 + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.version} + + + + + diff --git a/surefire-its/src/test/resources/surefire-1614-stream-corruption/src/test/java/com/example/demo/CustomRunner.java b/surefire-its/src/test/resources/surefire-1614-stream-corruption/src/test/java/com/example/demo/CustomRunner.java new file mode 100644 index 0000000000..cdfa6727a8 --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1614-stream-corruption/src/test/java/com/example/demo/CustomRunner.java @@ -0,0 +1,22 @@ +package com.example.demo; + +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.model.InitializationError; +import org.junit.runners.model.TestClass; + +public class CustomRunner + extends BlockJUnit4ClassRunner +{ + + public CustomRunner( Class klass ) throws InitializationError + { + super( klass ); + } + + @Override + protected TestClass createTestClass( Class testClass ) + { + System.out.println( "Creating test class" ); + return super.createTestClass( testClass ); + } +} diff --git a/surefire-its/src/test/resources/surefire-1614-stream-corruption/src/test/java/com/example/demo/SurefireStreamCorruptionTest.java b/surefire-its/src/test/resources/surefire-1614-stream-corruption/src/test/java/com/example/demo/SurefireStreamCorruptionTest.java new file mode 100644 index 0000000000..6d877adf65 --- /dev/null +++ b/surefire-its/src/test/resources/surefire-1614-stream-corruption/src/test/java/com/example/demo/SurefireStreamCorruptionTest.java @@ -0,0 +1,13 @@ +package com.example.demo; + +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith( CustomRunner.class ) +public class SurefireStreamCorruptionTest +{ + @Test + public void contextLoads() + { + } +} diff --git a/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProvider.java b/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProvider.java index 177adef2cb..fef72e600e 100644 --- a/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProvider.java +++ b/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProvider.java @@ -27,6 +27,8 @@ import static java.util.stream.Collectors.toList; import static org.apache.maven.surefire.booter.ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP; import static org.apache.maven.surefire.booter.ProviderParameterNames.TESTNG_GROUPS_PROP; +import static org.apache.maven.surefire.report.ConsoleOutputCapture.startCapture; +import static org.apache.maven.surefire.util.TestsToRun.fromClass; import static org.junit.platform.commons.util.StringUtils.isBlank; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request; @@ -44,7 +46,6 @@ import org.apache.maven.surefire.providerapi.AbstractProvider; import org.apache.maven.surefire.providerapi.ProviderParameters; -import org.apache.maven.surefire.report.ConsoleOutputCapture; import org.apache.maven.surefire.report.ConsoleOutputReceiver; import org.apache.maven.surefire.report.ReporterException; import org.apache.maven.surefire.report.ReporterFactory; @@ -104,22 +105,35 @@ public Iterable> getSuites() public RunResult invoke( Object forkTestSet ) throws TestSetFailedException, ReporterException { - if ( forkTestSet instanceof TestsToRun ) - { - return invokeAllTests( (TestsToRun) forkTestSet ); - } - else if ( forkTestSet instanceof Class ) - { - return invokeAllTests( TestsToRun.fromClass( (Class) forkTestSet ) ); - } - else if ( forkTestSet == null ) + ReporterFactory reporterFactory = parameters.getReporterFactory(); + final RunResult runResult; + try { - return invokeAllTests( scanClasspath() ); + RunListener runListener = reporterFactory.createReporter(); + startCapture( ( ConsoleOutputReceiver ) runListener ); + if ( forkTestSet instanceof TestsToRun ) + { + invokeAllTests( (TestsToRun) forkTestSet, runListener ); + } + else if ( forkTestSet instanceof Class ) + { + invokeAllTests( fromClass( ( Class ) forkTestSet ), runListener ); + } + else if ( forkTestSet == null ) + { + invokeAllTests( scanClasspath(), runListener ); + } + else + { + throw new IllegalArgumentException( + "Unexpected value of forkTestSet: " + forkTestSet ); + } } - else + finally { - throw new IllegalArgumentException( "Unexpected value of forkTestSet: " + forkTestSet ); + runResult = reporterFactory.close(); } + return runResult; } private TestsToRun scanClasspath() @@ -130,22 +144,10 @@ private TestsToRun scanClasspath() return parameters.getRunOrderCalculator().orderTestClasses( scannedClasses ); } - private RunResult invokeAllTests( TestsToRun testsToRun ) + private void invokeAllTests( TestsToRun testsToRun, RunListener runListener ) { - RunResult runResult; - ReporterFactory reporterFactory = parameters.getReporterFactory(); - try - { - RunListener runListener = reporterFactory.createReporter(); - ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) runListener ); - LauncherDiscoveryRequest discoveryRequest = buildLauncherDiscoveryRequest( testsToRun ); - launcher.execute( discoveryRequest, new RunListenerAdapter( runListener ) ); - } - finally - { - runResult = reporterFactory.close(); - } - return runResult; + LauncherDiscoveryRequest discoveryRequest = buildLauncherDiscoveryRequest( testsToRun ); + launcher.execute( discoveryRequest, new RunListenerAdapter( runListener ) ); } private LauncherDiscoveryRequest buildLauncherDiscoveryRequest( TestsToRun testsToRun )