Skip to content

Commit

Permalink
Allow all supported values of [parallel] option
Browse files Browse the repository at this point in the history
  • Loading branch information
sbabcoc committed Apr 20, 2022
1 parent 157d2d9 commit d30e429
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
Expand Up @@ -24,9 +24,7 @@

import java.util.Map;

import static java.lang.Integer.parseInt;
import static org.apache.maven.surefire.api.booter.ProviderParameterNames.PARALLEL_PROP;
import static org.apache.maven.surefire.api.booter.ProviderParameterNames.THREADCOUNT_PROP;
import static org.apache.maven.surefire.api.util.ReflectionUtils.invokeSetter;
import static org.apache.maven.surefire.api.util.ReflectionUtils.tryLoadClass;

Expand All @@ -37,33 +35,50 @@
*
* @since 3.0.0-M6
*/
public class TestNG740Configurator extends TestNG60Configurator
public class TestNG740Configurator
extends TestNG60Configurator
{
/**
* Convert and apply the value of the [parallel] setting.
* <p>
* <b>NOTE</b>: Since TestNG 7.4, the value of the {@code parallel} setting of the {@link XmlSuite} class has been
* specified via a <b>ParallelMode</b> enumeration. This method converts the [parallel] setting specified in the
* Surefire plugin configuration to its corresponding constant and applies this to the specified suite object.
*
* @param suite TestNG {@link XmlSuite} object
* @param options Surefire plugin configuration options
* @throws TestSetFailedException if unable to convert specified [parallel] setting
*/
@Override
public void configure( XmlSuite suite, Map<String, String> options )
protected void configureParallel( XmlSuite suite, Map<String, String> options )
throws TestSetFailedException
{
String threadCountAsString = options.get( THREADCOUNT_PROP );
int threadCount = threadCountAsString == null ? 1 : parseInt( threadCountAsString );
suite.setThreadCount( threadCount );

String parallel = options.get( PARALLEL_PROP );
// if [parallel] spec'd
if ( parallel != null )
{
if ( !"methods".equalsIgnoreCase( parallel ) && !"classes".equalsIgnoreCase( parallel ) )
// try to load the [ParallelMode] enumeration
Class enumClass = tryLoadClass( XmlSuite.class.getClassLoader(), "org.testng.xml.XmlSuite$ParallelMode" );
// if enumeration loaded
if ( enumClass != null )
{
throw new TestSetFailedException( "Unsupported TestNG parallel setting: "
+ parallel + " ( only METHODS or CLASSES supported )" );
try
{
// convert [parallel] option to corresponding constant
Enum<?> parallelEnum = Enum.valueOf( enumClass, parallel.toUpperCase() );
// set [XmlSuite] parallel mode to specified value
invokeSetter( suite, "setParallel", enumClass, parallelEnum );
}
catch ( IllegalArgumentException e )
{
throw new TestSetFailedException( "Unsupported TestNG [parallel] setting: " + parallel, e );
}
}
else
{
throw new TestSetFailedException(
"Failed loading TestNG [ParallelMode] enumeration to convert [parallel] setting: " + parallel );
}
Class enumClass = tryLoadClass( XmlSuite.class.getClassLoader(), "org.testng.xml.XmlSuite$ParallelMode" );
Enum<?> parallelEnum = Enum.valueOf( enumClass, parallel.toUpperCase() );
invokeSetter( suite, "setParallel", enumClass, parallelEnum );
}

String dataProviderThreadCount = options.get( "dataproviderthreadcount" );
if ( dataProviderThreadCount != null )
{
suite.setDataProviderThreadCount( Integer.parseInt( dataProviderThreadCount ) );
}
}
}
Expand Up @@ -60,11 +60,22 @@ public void configure( TestNG testng, Map<String, String> options )
@Override
public void configure( XmlSuite suite, Map<String, String> options )
throws TestSetFailedException
{
configureThreadCount( suite, options );
configureParallel( suite, options );
}

protected void configureThreadCount( XmlSuite suite, Map<String, String> options )
throws TestSetFailedException
{
String threadCountAsString = options.get( THREADCOUNT_PROP );
int threadCount = threadCountAsString == null ? 1 : parseInt( threadCountAsString );
suite.setThreadCount( threadCount );

}

protected void configureParallel( XmlSuite suite, Map<String, String> options )
throws TestSetFailedException
{
String parallel = options.get( PARALLEL_PROP );
if ( parallel != null )
{
Expand Down

0 comments on commit d30e429

Please sign in to comment.