Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUREFIRE-1935] Upgrade to JUnit Platform 1.8, start Launcher via LauncherSession #389

Merged
merged 1 commit into from Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls use throw new SurefireReflectionException( e.grtLocalMessage(), e );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SurefireReflectionException only has one constructor, the one taking a Throwable cause.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@papegaaij
ok, no problem.

}
}
}

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;
}
}