1 package org.apache.maven.surefire.testng.conf;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38
39
40
41
42
43
44
45
46
47
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
95
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
110 case "suitethreadpoolsize":
111
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
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 }