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 8, 2022
1 parent 157d2d9 commit 86f5e28
Showing 1 changed file with 30 additions and 20 deletions.
Expand Up @@ -23,8 +23,6 @@
import org.testng.xml.XmlSuite;

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;
Expand All @@ -37,33 +35,45 @@
*
* @since 3.0.0-M6
*/
public class TestNG740Configurator extends TestNG60Configurator
public class TestNG740Configurator
extends TestNG60Configurator
{
@Override
public void configure( 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 != null )
// if [threadcount] option in unspecified
if ( !options.containsKey( THREADCOUNT_PROP ) )
{
if ( !"methods".equalsIgnoreCase( parallel ) && !"classes".equalsIgnoreCase( parallel ) )
{
throw new TestSetFailedException( "Unsupported TestNG parallel setting: "
+ parallel + " ( only METHODS or CLASSES supported )" );
}
Class enumClass = tryLoadClass( XmlSuite.class.getClassLoader(), "org.testng.xml.XmlSuite$ParallelMode" );
Enum<?> parallelEnum = Enum.valueOf( enumClass, parallel.toUpperCase() );
invokeSetter( suite, "setParallel", enumClass, parallelEnum );
// acquire default [threadcount] value to avoid superclass hardcoding to 1
options.put( THREADCOUNT_PROP, Integer.toString( suite.getThreadCount() ) );
}

String dataProviderThreadCount = options.get( "dataproviderthreadcount" );
if ( dataProviderThreadCount != null )
// if [parallel] option is specified
if ( options.containsKey( PARALLEL_PROP ) )
{
suite.setDataProviderThreadCount( Integer.parseInt( dataProviderThreadCount ) );
// try to load the [ParallelMode] enumeration
Class enumClass = tryLoadClass( XmlSuite.class.getClassLoader(), "org.testng.xml.XmlSuite$ParallelMode" );
// if enumeration loaded
if ( enumClass != null )
{
// extract [parallel] option
String parallel = options.remove( PARALLEL_PROP );
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 );
}
}
}

// invoke superclass handler
super.configure( suite, options );
}
}

0 comments on commit 86f5e28

Please sign in to comment.