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.booter.ProviderParameterNames;
28 import org.apache.maven.surefire.testset.TestSetFailedException;
29 import org.testng.TestNG;
30 import org.testng.xml.XmlSuite;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class TestNGMapConfigurator
46 implements Configurator
47 {
48 public void configure( TestNG testng, Map<String, String> options )
49 throws TestSetFailedException
50 {
51 Map convertedOptions = getConvertedOptions( options );
52 testng.configure( convertedOptions );
53 }
54
55 public void configure( XmlSuite suite, Map<String, String> options )
56 throws TestSetFailedException
57 {
58 String threadCountString = options.get( ProviderParameterNames.THREADCOUNT_PROP );
59 int threadCount = ( null != threadCountString ) ? Integer.parseInt( threadCountString ) : 1;
60 suite.setThreadCount( threadCount );
61
62 String parallel = options.get( ProviderParameterNames.PARALLEL_PROP );
63 if ( parallel != null )
64 {
65 suite.setParallel( parallel );
66 }
67 }
68
69 Map<String, Object> getConvertedOptions( Map<String, String> options )
70 throws TestSetFailedException
71 {
72 Map<String, Object> convertedOptions = new HashMap<String, Object>();
73 convertedOptions.put( "-mixed", false );
74 for ( Map.Entry<String, String> entry : options.entrySet() )
75 {
76 String key = entry.getKey();
77 Object val = entry.getValue();
78 if ( "listener".equals( key ) )
79 {
80 val = convertListeners( entry.getValue() );
81 }
82 else if ( "objectfactory".equals( key ) )
83 {
84 val = AbstractDirectConfigurator.loadClass( entry.getValue() );
85 }
86 else if ( "testrunfactory".equals( key ) )
87 {
88 val = AbstractDirectConfigurator.loadClass( entry.getValue() );
89 }
90 else if ( "reporter".equals( key ) )
91 {
92
93
94 val = convertReporterConfig( val );
95 key = "reporterslist";
96 }
97 else if ( "junit".equals( key ) )
98 {
99 val = convert( val, Boolean.class );
100 }
101 else if ( "skipfailedinvocationcounts".equals( key ) )
102 {
103 val = convert( val, Boolean.class );
104 }
105 else if ( "mixed".equals( key ) )
106 {
107 val = convert( val, Boolean.class );
108 }
109 else if ( "configfailurepolicy".equals( key ) )
110 {
111 val = convert( val, String.class );
112 }
113 else if ( "group-by-instances".equals( key ) )
114 {
115 val = convert( val, Boolean.class );
116 }
117 else if ( ProviderParameterNames.THREADCOUNT_PROP.equals( key ) )
118 {
119 val = convert ( val, String.class );
120 }
121 else if ( "suitethreadpoolsize".equals( key ) )
122 {
123
124 val = convert( val, Integer.class );
125 }
126 else if ( "dataproviderthreadcount".equals( key ) )
127 {
128
129 val = convert( val, Integer.class );
130 }
131
132 if ( key.startsWith( "-" ) )
133 {
134 convertedOptions.put( key, val );
135 }
136 else
137 {
138 convertedOptions.put( "-" + key, val );
139 }
140 }
141 return convertedOptions;
142 }
143
144
145 protected Object convertReporterConfig( Object val )
146 {
147 final String reporterConfigClassName = "org.testng.ReporterConfig";
148 try
149 {
150 Class<?> reporterConfig = Class.forName( reporterConfigClassName );
151 Method deserialize = reporterConfig.getMethod( "deserialize", String.class );
152 Object rc = deserialize.invoke( null, val );
153 ArrayList<Object> reportersList = new ArrayList<Object>();
154 reportersList.add( rc );
155 return reportersList;
156 }
157 catch ( Exception e )
158 {
159 return val;
160 }
161 }
162
163 protected Object convertListeners( String listenerClasses ) throws TestSetFailedException
164 {
165 return AbstractDirectConfigurator.loadListenerClasses( listenerClasses );
166 }
167
168 protected Object convert( Object val, Class<?> type )
169 {
170 if ( val == null )
171 {
172 return null;
173 }
174 if ( type.isAssignableFrom( val.getClass() ) )
175 {
176 return val;
177 }
178
179 if ( ( type == Boolean.class || type == boolean.class ) && val.getClass() == String.class )
180 {
181 return Boolean.valueOf( (String) val );
182 }
183
184 if ( ( type == Integer.class || type == int.class ) && val.getClass() == String.class )
185 {
186 return Integer.valueOf( (String) val );
187 }
188
189 if ( type == String.class )
190 {
191 return val.toString();
192 }
193
194 return val;
195 }
196 }