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