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