From bdcf3a06108057896410e1943b295295eee603b9 Mon Sep 17 00:00:00 2001 From: Scott Babcock Date: Sat, 9 Apr 2022 13:06:44 -0700 Subject: [PATCH] Refactor option handling to simplify revisions --- .../testng/conf/TestNG740Configurator.java | 43 +++++++++++++------ .../testng/conf/TestNGMapConfigurator.java | 13 +++++- 2 files changed, 41 insertions(+), 15 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 7be1a08f58..c98fb022a5 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 @@ -23,6 +23,7 @@ import org.testng.xml.XmlSuite; import java.util.Map; + 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; @@ -39,26 +40,38 @@ public class TestNG740Configurator extends TestNG60Configurator { @Override - public void configure( XmlSuite suite, Map options ) + protected void configureThreadCount( XmlSuite suite, Map options ) throws TestSetFailedException { - // if [threadcount] option in unspecified - if ( !options.containsKey( THREADCOUNT_PROP ) ) + String threadCount = options.get( THREADCOUNT_PROP ); + // if [threadcount] spec'd + if ( threadCount != null ) { - // acquire default [threadcount] value to avoid superclass hardcoding to 1 - options.put( THREADCOUNT_PROP, Integer.toString( suite.getThreadCount() ) ); + try + { + // convert and apply [threadcount] setting + suite.setThreadCount( Integer.parseInt( threadCount ) ); + } + catch ( NumberFormatException e ) + { + throw new TestSetFailedException( "Non-integer TestNG [threadcount] setting: " + threadCount, e ); + } } - - // if [parallel] option is specified - if ( options.containsKey( PARALLEL_PROP ) ) + } + + @Override + protected void configureParallel( XmlSuite suite, Map options ) + throws TestSetFailedException + { + String parallel = options.get( PARALLEL_PROP ); + // if [parallel] spec'd + if ( parallel != null ) { // 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 @@ -68,12 +81,14 @@ public void configure( XmlSuite suite, Map options ) } catch ( IllegalArgumentException e ) { - throw new TestSetFailedException( "Unsupported TestNG parallel setting: " + parallel, e ); + throw new TestSetFailedException( "Unsupported TestNG [parallel] setting: " + parallel, e ); } } + else + { + throw new TestSetFailedException( + "Failed loading TestNG [ParallelMode] enumeration to convert [parallel] setting: " + parallel ); + } } - - // invoke superclass handler - super.configure( suite, options ); } } 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 ) {