1 | |
package org.apache.maven.surefire.testng; |
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.booter.ProviderParameterNames; |
23 | |
import org.apache.maven.surefire.report.RunListener; |
24 | |
import org.apache.maven.surefire.testng.conf.Configurator; |
25 | |
import org.apache.maven.surefire.testset.TestSetFailedException; |
26 | |
import org.apache.maven.surefire.util.NestedRuntimeException; |
27 | |
import org.apache.maven.surefire.util.internal.StringUtils; |
28 | |
import org.testng.TestNG; |
29 | |
|
30 | |
import java.io.File; |
31 | |
import java.lang.reflect.Constructor; |
32 | |
import java.lang.reflect.InvocationTargetException; |
33 | |
import java.lang.reflect.Method; |
34 | |
import java.util.List; |
35 | |
import java.util.Map; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class TestNGExecutor |
44 | |
{ |
45 | |
|
46 | |
|
47 | |
private TestNGExecutor() |
48 | 0 | { |
49 | |
|
50 | 0 | } |
51 | |
|
52 | |
public static void run( Class[] testClasses, String testSourceDirectory, Map options, RunListener reportManager, |
53 | |
TestNgTestSuite suite, File reportsDirectory, final String methodNamePattern ) |
54 | |
throws TestSetFailedException |
55 | |
{ |
56 | 0 | TestNG testng = new TestNG( true ); |
57 | |
|
58 | 0 | applyGroupMatching( testng, options ); |
59 | 0 | if ( !StringUtils.isBlank( methodNamePattern ) ) |
60 | |
{ |
61 | 0 | applyMethodNameFiltering( testng, methodNamePattern ); |
62 | |
} |
63 | |
|
64 | 0 | Configurator configurator = getConfigurator( (String) options.get("testng.configurator" ) ); |
65 | 0 | System.out.println( "Configuring TestNG with: " + configurator.getClass().getSimpleName() ); |
66 | 0 | configurator.configure( testng, options ); |
67 | 0 | postConfigure( testng, testSourceDirectory, reportManager, suite, reportsDirectory ); |
68 | 0 | testng.setTestClasses( testClasses ); |
69 | 0 | testng.run(); |
70 | 0 | } |
71 | |
|
72 | |
private static void applyMethodNameFiltering( TestNG testng, String methodNamePattern ) |
73 | |
throws TestSetFailedException |
74 | |
{ |
75 | |
|
76 | 0 | String clazzName = "org.apache.maven.surefire.testng.utils.MethodSelector"; |
77 | |
|
78 | 0 | testng.addMethodSelector( clazzName, 10000 ); |
79 | |
try |
80 | |
{ |
81 | 0 | Class clazz = Class.forName( clazzName ); |
82 | |
|
83 | 0 | Method method = clazz.getMethod( "setMethodName", new Class[]{ String.class } ); |
84 | 0 | method.invoke( null, methodNamePattern ); |
85 | |
} |
86 | 0 | catch ( ClassNotFoundException e ) |
87 | |
{ |
88 | 0 | throw new TestSetFailedException( e.getMessage(), e ); |
89 | |
} |
90 | 0 | catch ( SecurityException e ) |
91 | |
{ |
92 | 0 | throw new TestSetFailedException( e.getMessage(), e ); |
93 | |
} |
94 | 0 | catch ( NoSuchMethodException e ) |
95 | |
{ |
96 | 0 | throw new TestSetFailedException( e.getMessage(), e ); |
97 | |
} |
98 | 0 | catch ( IllegalArgumentException e ) |
99 | |
{ |
100 | 0 | throw new TestSetFailedException( e.getMessage(), e ); |
101 | |
} |
102 | 0 | catch ( IllegalAccessException e ) |
103 | |
{ |
104 | 0 | throw new TestSetFailedException( e.getMessage(), e ); |
105 | |
} |
106 | 0 | catch ( InvocationTargetException e ) |
107 | |
{ |
108 | 0 | throw new TestSetFailedException( e.getMessage(), e ); |
109 | 0 | } |
110 | 0 | } |
111 | |
|
112 | |
private static void applyGroupMatching( TestNG testng, Map options ) |
113 | |
throws TestSetFailedException |
114 | |
{ |
115 | 0 | String groups = (String) options.get( ProviderParameterNames.TESTNG_GROUPS_PROP ); |
116 | 0 | String excludedGroups = (String) options.get( ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP ); |
117 | |
|
118 | 0 | if ( groups == null && excludedGroups == null ) |
119 | |
{ |
120 | 0 | return; |
121 | |
} |
122 | |
|
123 | |
|
124 | 0 | String clazzName = "org.apache.maven.surefire.testng.utils.GroupMatcherMethodSelector"; |
125 | |
|
126 | 0 | testng.addMethodSelector( clazzName, 9999 ); |
127 | |
try |
128 | |
{ |
129 | 0 | Class clazz = Class.forName( clazzName ); |
130 | |
|
131 | |
|
132 | 0 | Method method = clazz.getMethod( "setGroups", new Class[]{ String.class, String.class } ); |
133 | 0 | method.invoke( null, groups, excludedGroups ); |
134 | |
} |
135 | 0 | catch ( ClassNotFoundException e ) |
136 | |
{ |
137 | 0 | throw new TestSetFailedException( e.getMessage(), e ); |
138 | |
} |
139 | 0 | catch ( SecurityException e ) |
140 | |
{ |
141 | 0 | throw new TestSetFailedException( e.getMessage(), e ); |
142 | |
} |
143 | 0 | catch ( NoSuchMethodException e ) |
144 | |
{ |
145 | 0 | throw new TestSetFailedException( e.getMessage(), e ); |
146 | |
} |
147 | 0 | catch ( IllegalArgumentException e ) |
148 | |
{ |
149 | 0 | throw new TestSetFailedException( e.getMessage(), e ); |
150 | |
} |
151 | 0 | catch ( IllegalAccessException e ) |
152 | |
{ |
153 | 0 | throw new TestSetFailedException( e.getMessage(), e ); |
154 | |
} |
155 | 0 | catch ( InvocationTargetException e ) |
156 | |
{ |
157 | 0 | throw new TestSetFailedException( e.getMessage(), e ); |
158 | 0 | } |
159 | 0 | } |
160 | |
|
161 | |
public static void run( List<String> suiteFiles, String testSourceDirectory, Map options, RunListener reportManager, |
162 | |
TestNgTestSuite suite, File reportsDirectory ) |
163 | |
throws TestSetFailedException |
164 | |
{ |
165 | 0 | TestNG testng = new TestNG( true ); |
166 | 0 | Configurator configurator = getConfigurator( (String) options.get("testng.configurator" ) ); |
167 | 0 | configurator.configure( testng, options ); |
168 | 0 | postConfigure( testng, testSourceDirectory, reportManager, suite, reportsDirectory ); |
169 | 0 | testng.setTestSuites( suiteFiles ); |
170 | 0 | testng.run(); |
171 | 0 | } |
172 | |
|
173 | |
private static Configurator getConfigurator( String className ) |
174 | |
{ |
175 | |
try |
176 | |
{ |
177 | 0 | return (Configurator) Class.forName( className ).newInstance(); |
178 | |
} |
179 | 0 | catch ( InstantiationException e ) |
180 | |
{ |
181 | 0 | throw new RuntimeException( e ); |
182 | |
} |
183 | 0 | catch ( IllegalAccessException e ) |
184 | |
{ |
185 | 0 | throw new RuntimeException( e ); |
186 | |
} |
187 | 0 | catch ( ClassNotFoundException e ) |
188 | |
{ |
189 | 0 | throw new RuntimeException( e ); |
190 | |
} |
191 | |
} |
192 | |
|
193 | |
|
194 | |
private static void postConfigure( TestNG testNG, String sourcePath, RunListener reportManager, |
195 | |
TestNgTestSuite suite, File reportsDirectory ) |
196 | |
throws TestSetFailedException |
197 | |
{ |
198 | |
|
199 | 0 | testNG.setVerbose( 0 ); |
200 | |
|
201 | 0 | TestNGReporter reporter = createTestNGReporter( reportManager, suite ); |
202 | 0 | testNG.addListener( (Object) reporter ); |
203 | |
|
204 | |
|
205 | 0 | if ( sourcePath != null ) |
206 | |
{ |
207 | 0 | testNG.setSourcePath( sourcePath ); |
208 | |
} |
209 | |
|
210 | 0 | testNG.setOutputDirectory( reportsDirectory.getAbsolutePath() ); |
211 | 0 | } |
212 | |
|
213 | |
|
214 | |
|
215 | |
private static TestNGReporter createTestNGReporter( RunListener reportManager, TestNgTestSuite suite ) |
216 | |
{ |
217 | |
try |
218 | |
{ |
219 | 0 | Class.forName( "org.testng.internal.IResultListener" ); |
220 | 0 | Class c = Class.forName( "org.apache.maven.surefire.testng.ConfigurationAwareTestNGReporter" ); |
221 | |
try |
222 | |
{ |
223 | 0 | Constructor ctor = c.getConstructor( new Class[]{ RunListener.class, TestNgTestSuite.class } ); |
224 | 0 | return (TestNGReporter) ctor.newInstance( reportManager, suite ); |
225 | |
} |
226 | 0 | catch ( Exception e ) |
227 | |
{ |
228 | 0 | throw new NestedRuntimeException( "Bug in ConfigurationAwareTestNGReporter", e ); |
229 | |
} |
230 | |
} |
231 | 0 | catch ( ClassNotFoundException e ) |
232 | |
{ |
233 | 0 | return new TestNGReporter( reportManager ); |
234 | |
} |
235 | |
} |
236 | |
|
237 | |
} |