Skip to content

Commit

Permalink
[SUREFIRE-1935] Use JUnit Platform 1.8 API to start Launcher via Laun…
Browse files Browse the repository at this point in the history
…cherSession
  • Loading branch information
papegaaij authored and Tibor17 committed Jan 20, 2022
1 parent 747c757 commit 97afea1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 15 deletions.
Expand Up @@ -25,10 +25,10 @@
import static java.util.Optional.of;
import static java.util.logging.Level.WARNING;
import static java.util.stream.Collectors.toList;
import static org.apache.maven.surefire.api.booter.ProviderParameterNames.TESTNG_GROUPS_PROP;
import static org.apache.maven.surefire.api.booter.ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP;
import static org.apache.maven.surefire.api.booter.ProviderParameterNames.INCLUDE_JUNIT5_ENGINES_PROP;
import static org.apache.maven.surefire.api.booter.ProviderParameterNames.EXCLUDE_JUNIT5_ENGINES_PROP;
import static org.apache.maven.surefire.api.booter.ProviderParameterNames.INCLUDE_JUNIT5_ENGINES_PROP;
import static org.apache.maven.surefire.api.booter.ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP;
import static org.apache.maven.surefire.api.booter.ProviderParameterNames.TESTNG_GROUPS_PROP;
import static org.apache.maven.surefire.api.report.ConsoleOutputCapture.startCapture;
import static org.apache.maven.surefire.api.util.TestsToRun.fromClass;
import static org.apache.maven.surefire.shared.utils.StringUtils.isBlank;
Expand Down Expand Up @@ -58,6 +58,7 @@
import org.apache.maven.surefire.api.testset.TestListResolver;
import org.apache.maven.surefire.api.testset.TestSetFailedException;
import org.apache.maven.surefire.api.util.ScanResult;
import org.apache.maven.surefire.api.util.SurefireReflectionException;
import org.apache.maven.surefire.api.util.TestsToRun;
import org.apache.maven.surefire.shared.utils.StringUtils;
import org.junit.platform.engine.DiscoverySelector;
Expand Down Expand Up @@ -104,7 +105,14 @@ public JUnitPlatformProvider( ProviderParameters parameters )
@Override
public Iterable<Class<?>> getSuites()
{
return scanClasspath();
try
{
return scanClasspath();
}
finally
{
closeLauncher();
}
}

@Override
Expand Down Expand Up @@ -153,22 +161,37 @@ private TestsToRun scanClasspath()
private void invokeAllTests( TestsToRun testsToRun, RunListener runListener )
{
RunListenerAdapter adapter = new RunListenerAdapter( runListener );
execute( testsToRun, adapter );
try
{
execute( testsToRun, adapter );
}
finally
{
closeLauncher();
}
// Rerun failing tests if requested
int count = parameters.getTestRequest().getRerunFailingTestsCount();
if ( count > 0 && adapter.hasFailingTests() )
{
for ( int i = 0; i < count; i++ )
{
// Replace the "discoveryRequest" so that it only specifies the failing tests
LauncherDiscoveryRequest discoveryRequest = buildLauncherDiscoveryRequestForRerunFailures( adapter );
// Reset adapter's recorded failures and invoke the failed tests again
adapter.reset();
launcher.execute( discoveryRequest, adapter );
// If no tests fail in the rerun, we're done
if ( !adapter.hasFailingTests() )
try
{
break;
// Replace the "discoveryRequest" so that it only specifies the failing tests
LauncherDiscoveryRequest discoveryRequest =
buildLauncherDiscoveryRequestForRerunFailures( adapter );
// Reset adapter's recorded failures and invoke the failed tests again
adapter.reset();
launcher.execute( discoveryRequest, adapter );
// If no tests fail in the rerun, we're done
if ( !adapter.hasFailingTests() )
{
break;
}
}
finally
{
closeLauncher();
}
}
}
Expand Down Expand Up @@ -202,6 +225,21 @@ private void execute( TestsToRun testsToRun, RunListenerAdapter adapter )
} );
}
}

private void closeLauncher()
{
if ( launcher instanceof AutoCloseable )
{
try
{
( (AutoCloseable) launcher ).close();
}
catch ( Exception e )
{
throw new SurefireReflectionException( e );
}
}
}

private LauncherDiscoveryRequest buildLauncherDiscoveryRequestForRerunFailures( RunListenerAdapter adapter )
{
Expand Down
Expand Up @@ -19,6 +19,7 @@
* under the License.
*/

import org.apache.maven.surefire.api.util.ReflectionUtils;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestExecutionListener;
Expand All @@ -29,8 +30,9 @@
* Launcher proxy which delays the most possible the initialization of the real JUnit
* Launcher in order to avoid stream/stdout corruption due to early logging.
*/
class LazyLauncher implements Launcher
class LazyLauncher implements Launcher, AutoCloseable
{
private AutoCloseable launcherSession;

private Launcher launcher;

Expand All @@ -57,8 +59,28 @@ private Launcher launcher()
{
if ( launcher == null )
{
launcher = LauncherFactory.create();
try
{
Class<?> sessionClass = Class.forName( "org.junit.platform.launcher.LauncherSession" );
launcherSession = ReflectionUtils.invokeGetter( LauncherFactory.class, null, "openSession" );
launcher = ReflectionUtils.invokeGetter( sessionClass, launcherSession, "getLauncher" );
}
catch ( ClassNotFoundException e )
{
launcher = LauncherFactory.create();
}
}
return launcher;
}

@Override
public void close() throws Exception
{
if ( launcherSession != null )
{
launcherSession.close();
launcherSession = null;
}
launcher = null;
}
}

0 comments on commit 97afea1

Please sign in to comment.