View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.testng.conf;
20  
21  import java.util.Map;
22  
23  import org.apache.maven.surefire.api.testset.TestSetFailedException;
24  import org.testng.xml.XmlSuite;
25  
26  import static org.apache.maven.surefire.api.booter.ProviderParameterNames.PARALLEL_PROP;
27  import static org.apache.maven.surefire.api.util.ReflectionUtils.invokeSetter;
28  import static org.apache.maven.surefire.api.util.ReflectionUtils.loadClass;
29  
30  /**
31   * TestNG 7.4.0 configurator. Changed setParallel type to enum value.
32   * Uses reflection since ParallelMode enum doesn't exist in supported
33   * TestNG 5.x versions.
34   *
35   * @since 3.0.0-M6
36   */
37  public class TestNG740Configurator extends TestNG60Configurator {
38      /**
39       * Convert and apply the value of the [parallel] setting.
40       * <p>
41       * <b>NOTE</b>: Since TestNG 7.4, the value of the {@code parallel} setting of the {@link XmlSuite} class has been
42       * specified via a <b>ParallelMode</b> enumeration. This method converts the [parallel] setting specified in the
43       * Surefire plugin configuration to its corresponding constant and applies this to the specified suite object.
44       *
45       * @param suite TestNG {@link XmlSuite} object
46       * @param options Surefire plugin configuration options
47       * @throws TestSetFailedException if unable to convert specified [parallel] setting
48       */
49      @Override
50      protected void configureParallel(XmlSuite suite, Map<String, String> options) throws TestSetFailedException {
51          String parallel = options.get(PARALLEL_PROP);
52          if (parallel != null) {
53              Class enumClass = loadClass(XmlSuite.class.getClassLoader(), "org.testng.xml.XmlSuite$ParallelMode");
54              try {
55                  Enum<?> parallelEnum = Enum.valueOf(enumClass, parallel.toUpperCase());
56                  invokeSetter(suite, "setParallel", enumClass, parallelEnum);
57              } catch (IllegalArgumentException e) {
58                  throw new TestSetFailedException("Unsupported TestNG [parallel] setting: " + parallel, e);
59              }
60          }
61      }
62  }