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