From d30e4297b52cf48e72170edb1508b743b1d0f53f Mon Sep 17 00:00:00 2001 From: Scott Babcock Date: Thu, 7 Apr 2022 17:59:22 -0700 Subject: [PATCH] Allow all supported values of [parallel] option --- .../testng/conf/TestNG740Configurator.java | 55 ++++++++++++------- .../testng/conf/TestNGMapConfigurator.java | 13 ++++- 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/conf/TestNG740Configurator.java b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/conf/TestNG740Configurator.java index f68cf2bec8..205fc2e6f6 100644 --- a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/conf/TestNG740Configurator.java +++ b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/conf/TestNG740Configurator.java @@ -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; @@ -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. + *

+ * NOTE: Since TestNG 7.4, the value of the {@code parallel} setting of the {@link XmlSuite} class has been + * specified via a ParallelMode 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 options ) + protected void configureParallel( XmlSuite suite, Map 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 ) ); } } } diff --git a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/conf/TestNGMapConfigurator.java b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/conf/TestNGMapConfigurator.java index 6caf82459c..186110d691 100755 --- a/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/conf/TestNGMapConfigurator.java +++ b/surefire-providers/surefire-testng/src/main/java/org/apache/maven/surefire/testng/conf/TestNGMapConfigurator.java @@ -60,11 +60,22 @@ public void configure( TestNG testng, Map options ) @Override public void configure( XmlSuite suite, Map options ) throws TestSetFailedException + { + configureThreadCount( suite, options ); + configureParallel( suite, options ); + } + + protected void configureThreadCount( XmlSuite suite, Map 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 options ) + throws TestSetFailedException + { String parallel = options.get( PARALLEL_PROP ); if ( parallel != null ) {