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
85 if ( "listener".equals( key ) )
86 {
87 val = convertListeners( entry.getValue() );
88 }
89 else if ( "objectfactory".equals( key ) )
90 {
91 val = AbstractDirectConfigurator.loadClass( entry.getValue() );
92 }
93 else if ( "testrunfactory".equals( key ) )
94 {
95 val = AbstractDirectConfigurator.loadClass( entry.getValue() );
96 }
97 else if ( "reporter".equals( key ) )
98 {
99
100
101 val = convertReporterConfig( val );
102 key = "reporterslist";
103 }
104 else if ( "junit".equals( key ) )
105 {
106 val = convert( val, Boolean.class );
107 }
108 else if ( "skipfailedinvocationcounts".equals( key ) )
109 {
110 val = convert( val, Boolean.class );
111 }
112 else if ( "mixed".equals( key ) )
113 {
114 val = convert( val, Boolean.class );
115 }
116 else if ( "configfailurepolicy".equals( key ) )
117 {
118 val = convert( val, String.class );
119 }
120 else if ( "group-by-instances".equals( key ) )
121 {
122 val = convert( val, Boolean.class );
123 }
124 else if ( THREADCOUNT_PROP.equals( key ) )
125 {
126 val = convert ( val, String.class );
127 }
128 else if ( "suitethreadpoolsize".equals( key ) )
129 {
130
131 val = convert( val, Integer.class );
132 }
133 else if ( "dataproviderthreadcount".equals( key ) )
134 {
135
136 val = convert( val, Integer.class );
137 }
138
139 if ( key.startsWith( "-" ) )
140 {
141 convertedOptions.put( key, val );
142 }
143 else
144 {
145 convertedOptions.put( "-" + key, val );
146 }
147 }
148 return convertedOptions;
149 }
150
151
152 protected Object convertReporterConfig( Object val )
153 {
154 try
155 {
156 Class<?> reporterConfig = Class.forName( "org.testng.ReporterConfig" );
157 Method deserialize = reporterConfig.getMethod( "deserialize", String.class );
158 Object rc = deserialize.invoke( null, val );
159 ArrayList<Object> reportersList = new ArrayList<Object>();
160 reportersList.add( rc );
161 return reportersList;
162 }
163 catch ( Exception e )
164 {
165 return val;
166 }
167 }
168
169 protected Object convertListeners( String listenerClasses ) throws TestSetFailedException
170 {
171 return loadListenerClasses( listenerClasses );
172 }
173
174 protected Object convert( Object val, Class<?> type )
175 {
176 if ( val == null )
177 {
178 return null;
179 }
180 else if ( type.isAssignableFrom( val.getClass() ) )
181 {
182 return val;
183 }
184 else if ( ( type == Boolean.class || type == boolean.class ) && val.getClass() == String.class )
185 {
186 return Boolean.valueOf( (String) val );
187 }
188 else if ( ( type == Integer.class || type == int.class ) && val.getClass() == String.class )
189 {
190 return Integer.valueOf( (String) val );
191 }
192 else if ( type == String.class )
193 {
194 return val.toString();
195 }
196 else
197 {
198 return val;
199 }
200 }
201 }