Skip to content

Commit

Permalink
[SUREFIRE-1935] Upgrade to JUnit Platform 1.8, start Launcher via Lau…
Browse files Browse the repository at this point in the history
…ncherSession
  • Loading branch information
papegaaij committed Sep 12, 2021
1 parent 670dbe4 commit f308889
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 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,24 @@ public JUnitPlatformProvider( ProviderParameters parameters )
@Override
public Iterable<Class<?>> getSuites()
{
return scanClasspath();
try
{
return scanClasspath();
}
finally
{
if ( launcher instanceof AutoCloseable )
{
try
{
( (AutoCloseable) launcher ).close();
}
catch ( Exception e )
{
throw new SurefireReflectionException( e );
}
}
}
}

@Override
Expand Down Expand Up @@ -138,6 +156,17 @@ else if ( forkTestSet == null )
finally
{
runResult = reporterFactory.close();
if ( launcher instanceof AutoCloseable )
{
try
{
( (AutoCloseable) launcher ).close();
}
catch ( Exception e )
{
throw new SurefireReflectionException( e );
}
}
}
return runResult;
}
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,26 @@ 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();
}
}
}

0 comments on commit f308889

Please sign in to comment.