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.List; |
27 | |
import java.util.Map; |
28 | |
|
29 | |
import org.apache.maven.surefire.testset.TestSetFailedException; |
30 | |
import org.apache.maven.surefire.util.NestedRuntimeException; |
31 | |
import org.testng.TestNG; |
32 | |
|
33 | |
public abstract class AbstractDirectConfigurator |
34 | |
implements Configurator |
35 | |
{ |
36 | |
final Map setters; |
37 | |
|
38 | |
AbstractDirectConfigurator() |
39 | 0 | { |
40 | 0 | Map options = new HashMap(); |
41 | 0 | options.put( "groups", new Setter( "setGroups", String.class ) ); |
42 | 0 | options.put( "excludedgroups", new Setter( "setExcludedGroups", String.class ) ); |
43 | 0 | options.put( "junit", new Setter( "setJUnit", Boolean.class ) ); |
44 | 0 | options.put( "threadcount", new Setter( "setThreadCount", int.class ) ); |
45 | 0 | options.put( "usedefaultlisteners", new Setter( "setUseDefaultListeners", boolean.class ) ); |
46 | 0 | this.setters = options; |
47 | 0 | } |
48 | |
|
49 | |
public void configure( TestNG testng, Map options ) |
50 | |
throws TestSetFailedException |
51 | |
{ |
52 | |
|
53 | 0 | final String listeners = (String) options.remove( "listener" ); |
54 | |
|
55 | |
|
56 | 0 | testng.setUseDefaultListeners( false ); |
57 | 0 | for ( Iterator it = options.entrySet().iterator(); it.hasNext(); ) |
58 | |
{ |
59 | 0 | Map.Entry entry = (Map.Entry) it.next(); |
60 | 0 | String key = (String) entry.getKey(); |
61 | 0 | Object val = entry.getValue(); |
62 | |
|
63 | 0 | Setter setter = (Setter) setters.get( key ); |
64 | 0 | if ( setter != null ) |
65 | |
{ |
66 | |
try |
67 | |
{ |
68 | 0 | setter.invoke( testng, val ); |
69 | |
} |
70 | 0 | catch ( Exception ex ) |
71 | |
{ |
72 | 0 | throw new NestedRuntimeException( "Cannot set option " + key + " with value " + val, ex ); |
73 | 0 | } |
74 | |
|
75 | |
} |
76 | 0 | } |
77 | |
|
78 | 0 | testng.setListenerClasses( loadListenerClasses( listeners ) ); |
79 | 0 | } |
80 | |
|
81 | |
public static List loadListenerClasses( String listenerClasses ) |
82 | |
throws TestSetFailedException |
83 | |
{ |
84 | 0 | if ( listenerClasses == null || "".equals( listenerClasses.trim() ) ) |
85 | |
{ |
86 | 0 | return new ArrayList(); |
87 | |
} |
88 | |
|
89 | 0 | List classes = new ArrayList(); |
90 | 0 | String[] classNames = listenerClasses.split( " *, *" ); |
91 | 0 | for ( int i = 0; i < classNames.length; i++ ) |
92 | |
{ |
93 | 0 | String className = classNames[i]; |
94 | 0 | Class clazz = loadClass( className ); |
95 | 0 | classes.add( clazz ); |
96 | |
} |
97 | |
|
98 | 0 | return classes; |
99 | |
} |
100 | |
|
101 | |
public static Class loadClass( String className ) |
102 | |
throws TestSetFailedException |
103 | |
{ |
104 | |
try |
105 | |
{ |
106 | 0 | return Class.forName( className ); |
107 | |
} |
108 | 0 | catch ( Exception ex ) |
109 | |
{ |
110 | 0 | throw new TestSetFailedException( "Cannot find listener class " + className, ex ); |
111 | |
} |
112 | |
} |
113 | |
|
114 | |
public static final class Setter |
115 | |
{ |
116 | |
private final String setterName; |
117 | |
|
118 | |
private final Class paramClass; |
119 | |
|
120 | |
public Setter( String name, Class clazz ) |
121 | 0 | { |
122 | 0 | this.setterName = name; |
123 | 0 | this.paramClass = clazz; |
124 | 0 | } |
125 | |
|
126 | |
public void invoke( Object target, Object value ) |
127 | |
throws Exception |
128 | |
{ |
129 | 0 | Method setter = target.getClass().getMethod( this.setterName, new Class[] { this.paramClass } ); |
130 | 0 | if ( setter != null ) |
131 | |
{ |
132 | 0 | setter.invoke( target, new Object[] { convertValue( value ) } ); |
133 | |
} |
134 | 0 | } |
135 | |
|
136 | |
Object convertValue( Object value ) |
137 | |
{ |
138 | 0 | if ( value == null ) |
139 | |
{ |
140 | 0 | return value; |
141 | |
} |
142 | 0 | if ( this.paramClass.isAssignableFrom( value.getClass() ) ) |
143 | |
{ |
144 | 0 | return value; |
145 | |
} |
146 | |
|
147 | 0 | if ( Boolean.class.equals( this.paramClass ) || boolean.class.equals( this.paramClass ) ) |
148 | |
{ |
149 | 0 | return Boolean.valueOf( value.toString() ); |
150 | |
} |
151 | 0 | if ( Integer.class.equals( this.paramClass ) || int.class.equals( this.paramClass ) ) |
152 | |
{ |
153 | 0 | return new Integer( value.toString() ); |
154 | |
} |
155 | |
|
156 | 0 | return value; |
157 | |
} |
158 | |
} |
159 | |
} |