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.booter.ProviderParameterNames;
30 import org.apache.maven.surefire.testset.TestSetFailedException;
31 import org.apache.maven.surefire.util.NestedRuntimeException;
32 import org.testng.TestNG;
33 import org.testng.xml.XmlSuite;
34
35 public abstract class AbstractDirectConfigurator
36 implements Configurator
37 {
38 final Map setters;
39
40 AbstractDirectConfigurator()
41 {
42 Map options = new HashMap();
43
44
45
46 options.put( "junit", new Setter( "setJUnit", Boolean.class ) );
47 options.put( ProviderParameterNames.THREADCOUNT_PROP, new Setter( "setThreadCount", int.class ) );
48 options.put( "usedefaultlisteners", new Setter( "setUseDefaultListeners", boolean.class ) );
49 this.setters = options;
50 }
51
52 public void configure( TestNG testng, Map options )
53 throws TestSetFailedException
54 {
55 System.out.println( "\n\n\n\nCONFIGURING TESTNG\n\n\n\n" );
56
57 final String listeners = (String) options.remove( "listener" );
58
59
60 testng.setUseDefaultListeners( false );
61 configureInstance( testng, options );
62
63 testng.setListenerClasses( loadListenerClasses( listeners ) );
64 }
65
66 public void configure( XmlSuite suite, Map options )
67 throws TestSetFailedException
68 {
69 Map filtered = filterForSuite( options );
70 configureInstance( suite, filtered );
71 }
72
73
74 protected Map filterForSuite( Map options )
75 {
76 Map result = new HashMap();
77 addPropIfNotNull( options, result, ProviderParameterNames.PARALLEL_PROP );
78 addPropIfNotNull( options, result, ProviderParameterNames.THREADCOUNT_PROP );
79 return result;
80 }
81
82 private void addPropIfNotNull( Map options, Map result, String prop )
83 {
84 if ( options.containsKey( prop ) )
85 {
86 result.put( prop, options.get( prop ) );
87 }
88 }
89
90 private void configureInstance( Object testngInstance, Map options )
91 {
92 for ( Iterator it = options.entrySet().iterator(); it.hasNext(); )
93 {
94 Map.Entry entry = (Map.Entry) it.next();
95 String key = (String) entry.getKey();
96 Object val = entry.getValue();
97
98 Setter setter = (Setter) setters.get( key );
99 if ( setter != null )
100 {
101 try
102 {
103 setter.invoke( testngInstance, val );
104 }
105 catch ( Exception ex )
106 {
107 throw new NestedRuntimeException( "Cannot set option " + key + " with value " + val, ex );
108 }
109
110 }
111 }
112 }
113
114 public static List loadListenerClasses( String listenerClasses )
115 throws TestSetFailedException
116 {
117 if ( listenerClasses == null || "".equals( listenerClasses.trim() ) )
118 {
119 return new ArrayList();
120 }
121
122 List classes = new ArrayList();
123 String[] classNames = listenerClasses.split( " *, *" );
124 for ( int i = 0; i < classNames.length; i++ )
125 {
126 String className = classNames[i];
127 Class clazz = loadClass( className );
128 classes.add( clazz );
129 }
130
131 return classes;
132 }
133
134 public static Class loadClass( String className )
135 throws TestSetFailedException
136 {
137 try
138 {
139 return Class.forName( className );
140 }
141 catch ( Exception ex )
142 {
143 throw new TestSetFailedException( "Cannot find listener class " + className, ex );
144 }
145 }
146
147 public static final class Setter
148 {
149 private final String setterName;
150
151 private final Class paramClass;
152
153 public Setter( String name, Class clazz )
154 {
155 this.setterName = name;
156 this.paramClass = clazz;
157 }
158
159 public void invoke( Object target, Object value )
160 throws Exception
161 {
162 Method setter = target.getClass().getMethod( this.setterName, new Class[]{ this.paramClass } );
163 if ( setter != null )
164 {
165 setter.invoke( target, new Object[]{ convertValue( value ) } );
166 }
167 }
168
169 Object convertValue( Object value )
170 {
171 if ( value == null )
172 {
173 return value;
174 }
175 if ( this.paramClass.isAssignableFrom( value.getClass() ) )
176 {
177 return value;
178 }
179
180 if ( Boolean.class.equals( this.paramClass ) || boolean.class.equals( this.paramClass ) )
181 {
182 return Boolean.valueOf( value.toString() );
183 }
184 if ( Integer.class.equals( this.paramClass ) || int.class.equals( this.paramClass ) )
185 {
186 return new Integer( value.toString() );
187 }
188
189 return value;
190 }
191 }
192 }