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