View Javadoc
1   package org.apache.maven.surefire.testng.conf;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.lang.reflect.Method;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.maven.surefire.testset.TestSetFailedException;
28  import org.testng.TestNG;
29  import org.testng.xml.XmlSuite;
30  
31  import static java.lang.Integer.parseInt;
32  import static org.apache.maven.surefire.booter.ProviderParameterNames.PARALLEL_PROP;
33  import static org.apache.maven.surefire.booter.ProviderParameterNames.THREADCOUNT_PROP;
34  import static org.apache.maven.surefire.testng.conf.AbstractDirectConfigurator.loadListenerClasses;
35  
36  /**
37   * TestNG configurator for 5.3+ versions. TestNG exposes a {@link org.testng.TestNG#configure(java.util.Map)} method.
38   * All supported TestNG options are passed in String format, except
39   * {@link org.testng.TestNGCommandLineArgs#LISTENER_COMMAND_OPT} which is {@link java.util.List List>Class<},
40   * {@link org.testng.TestNGCommandLineArgs#JUNIT_DEF_OPT} which is a {@link Boolean},
41   * {@link org.testng.TestNGCommandLineArgs#SKIP_FAILED_INVOCATION_COUNT_OPT} which is a {@link Boolean},
42   * {@link org.testng.TestNGCommandLineArgs#OBJECT_FACTORY_COMMAND_OPT} which is a {@link Class},
43   * {@link org.testng.TestNGCommandLineArgs#REPORTERS_LIST} which is a {@link java.util.List List>ReporterConfig<}.
44   * <br>
45   * Test classes and/or suite files are not passed along as options parameters, but configured separately.
46   *
47   * @author <a href='mailto:the[dot]mindstorm[at]gmail[dot]com'>Alex Popescu</a>
48   */
49  public class TestNGMapConfigurator
50      implements Configurator
51  {
52      @Override
53      public void configure( TestNG testng, Map<String, String> options )
54          throws TestSetFailedException
55      {
56          Map convertedOptions = getConvertedOptions( options );
57          testng.configure( convertedOptions );
58      }
59  
60      @Override
61      public void configure( XmlSuite suite, Map<String, String> options )
62          throws TestSetFailedException
63      {
64          String threadCountAsString = options.get( THREADCOUNT_PROP );
65          int threadCount = threadCountAsString == null ? 1 : parseInt( threadCountAsString );
66          suite.setThreadCount( threadCount );
67  
68          String parallel = options.get( PARALLEL_PROP );
69          if ( parallel != null )
70          {
71              suite.setParallel( parallel );
72          }
73      }
74  
75      Map<String, Object> getConvertedOptions( Map<String, String> options )
76          throws TestSetFailedException
77      {
78          Map<String, Object> convertedOptions = new HashMap<String, Object>();
79          convertedOptions.put( "-mixed", false );
80          for ( Map.Entry<String, String> entry : options.entrySet() )
81          {
82              String key = entry.getKey();
83              Object val = entry.getValue();
84              switch ( key )
85              {
86                  case "listener":
87                      val = convertListeners( entry.getValue() );
88                      break;
89                  case "objectfactory":
90                  case "testrunfactory":
91                      val = AbstractDirectConfigurator.loadClass( entry.getValue() );
92                      break;
93                  case "reporter":
94                      // for TestNG 5.6 or higher
95                      // TODO support multiple reporters?
96                      val = convertReporterConfig( val );
97                      key = "reporterslist";
98                      break;
99                  case "junit":
100                 case "skipfailedinvocationcounts":
101                 case "mixed":
102                 case "group-by-instances":
103                     val = convert( val, Boolean.class );
104                     break;
105                 case "configfailurepolicy":
106                 case THREADCOUNT_PROP:
107                     val = convert( val, String.class );
108                     break;
109                 // for TestNG 6.9.7 or higher
110                 case "suitethreadpoolsize":
111                     // for TestNG 5.10 or higher
112                 case "dataproviderthreadcount":
113                     val = convert( val, Integer.class );
114                     break;
115                 default:
116                     break;
117             }
118 
119             if ( key.startsWith( "-" ) )
120             {
121                 convertedOptions.put( key, val );
122             }
123             else
124             {
125                 convertedOptions.put( "-" + key, val );
126             }
127         }
128         return convertedOptions;
129     }
130 
131     // ReporterConfig only became available in later versions of TestNG
132     protected Object convertReporterConfig( Object val )
133     {
134         try
135         {
136             Class<?> reporterConfig = Class.forName( "org.testng.ReporterConfig" );
137             Method deserialize = reporterConfig.getMethod( "deserialize", String.class );
138             Object rc = deserialize.invoke( null, val );
139             ArrayList<Object> reportersList = new ArrayList<Object>();
140             reportersList.add( rc );
141             return reportersList;
142         }
143         catch ( Exception e )
144         {
145             return val;
146         }
147     }
148 
149     protected Object convertListeners( String listenerClasses ) throws TestSetFailedException
150     {
151         return loadListenerClasses( listenerClasses );
152     }
153 
154     protected Object convert( Object val, Class<?> type )
155     {
156         if ( val == null )
157         {
158             return null;
159         }
160         else if ( type.isAssignableFrom( val.getClass() ) )
161         {
162             return val;
163         }
164         else if ( ( type == Boolean.class || type == boolean.class ) && val.getClass() == String.class )
165         {
166             return Boolean.valueOf( (String) val );
167         }
168         else if ( ( type == Integer.class || type == int.class ) && val.getClass() == String.class )
169         {
170             return Integer.valueOf( (String) val );
171         }
172         else if ( type == String.class )
173         {
174             return val.toString();
175         }
176         else
177         {
178             return val;
179         }
180     }
181 }