From d5e3b095b17b27c83f1759e2cab5f37628cb9143 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 | 81 ++++++++++++++----- .../testng/conf/TestNGMapConfigurator.java | 13 ++- 2 files changed, 74 insertions(+), 20 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..969f87a845 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,7 +24,6 @@ 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; @@ -37,33 +36,77 @@ * * @since 3.0.0-M6 */ -public class TestNG740Configurator extends TestNG60Configurator +public class TestNG740Configurator + extends TestNG60Configurator { + /** + * Convert and apply the value of the [threadcount] option. + * + * @param suite TestNG {@link XmlSuite} object + * @param options Surefire plugin configuration options + * @throws TestSetFailedException if unable to convert specified [threadcount] setting + */ @Override - public void configure( XmlSuite suite, Map 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 ); - - String parallel = options.get( PARALLEL_PROP ); - if ( parallel != null ) + String threadCount = options.get( THREADCOUNT_PROP ); + // if [threadcount] spec'd + if ( threadCount != null ) { - if ( !"methods".equalsIgnoreCase( parallel ) && !"classes".equalsIgnoreCase( parallel ) ) + try { - throw new TestSetFailedException( "Unsupported TestNG parallel setting: " - + parallel + " ( only METHODS or CLASSES supported )" ); + // convert and apply [threadcount] setting + suite.setThreadCount( Integer.parseInt( threadCount ) ); + } + catch ( NumberFormatException e ) + { + throw new TestSetFailedException( "Non-integer TestNG [threadcount] setting: " + threadCount, e ); } - 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 ) + } + + /** + * 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 + protected void configureParallel( XmlSuite suite, Map options ) + throws TestSetFailedException + { + String parallel = options.get( PARALLEL_PROP ); + // if [parallel] spec'd + if ( parallel != null ) { - 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 ) + { + 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 ); + } } } } 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 ) {