1 | |
package org.apache.maven.surefire.junit; |
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.report.PojoStackTraceWriter; |
23 | |
import org.apache.maven.surefire.report.RunListener; |
24 | |
import org.apache.maven.surefire.report.ReportEntry; |
25 | |
import org.apache.maven.surefire.report.SimpleReportEntry; |
26 | |
import org.apache.maven.surefire.testset.TestSetFailedException; |
27 | |
|
28 | |
import java.lang.reflect.InvocationTargetException; |
29 | |
import java.lang.reflect.Method; |
30 | |
import java.lang.reflect.Modifier; |
31 | |
import java.util.ArrayList; |
32 | |
import java.util.List; |
33 | |
|
34 | |
public class PojoTestSet |
35 | |
implements SurefireTestSet |
36 | |
{ |
37 | |
|
38 | |
private static final String TEST_METHOD_PREFIX = "test"; |
39 | |
|
40 | 0 | private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; |
41 | |
|
42 | |
private final Object testObject; |
43 | |
|
44 | |
private List testMethods; |
45 | |
|
46 | |
private Method setUpMethod; |
47 | |
|
48 | |
private Method tearDownMethod; |
49 | |
|
50 | |
private final Class testClass; |
51 | |
|
52 | |
public PojoTestSet( Class testClass ) |
53 | |
throws TestSetFailedException |
54 | 0 | { |
55 | 0 | if ( testClass == null ) |
56 | |
{ |
57 | 0 | throw new NullPointerException( "testClass is null" ); |
58 | |
} |
59 | |
|
60 | 0 | this.testClass = testClass; |
61 | |
|
62 | |
try |
63 | |
{ |
64 | 0 | testObject = testClass.newInstance(); |
65 | |
} |
66 | 0 | catch ( InstantiationException e ) |
67 | |
{ |
68 | 0 | throw new TestSetFailedException( "Unable to instantiate POJO '" + testClass + "'", e ); |
69 | |
} |
70 | 0 | catch ( IllegalAccessException e ) |
71 | |
{ |
72 | 0 | throw new TestSetFailedException( "Unable to instantiate POJO '" + testClass + "'", e ); |
73 | 0 | } |
74 | 0 | } |
75 | |
|
76 | |
public void execute( RunListener reportManager, ClassLoader loader ) |
77 | |
throws TestSetFailedException |
78 | |
{ |
79 | 0 | if ( reportManager == null ) |
80 | |
{ |
81 | 0 | throw new NullPointerException( "reportManager is null" ); |
82 | |
} |
83 | |
|
84 | 0 | executeTestMethods( reportManager ); |
85 | 0 | } |
86 | |
|
87 | |
private void executeTestMethods( RunListener reportManager ) |
88 | |
{ |
89 | 0 | if ( reportManager == null ) |
90 | |
{ |
91 | 0 | throw new NullPointerException( "reportManager is null" ); |
92 | |
} |
93 | |
|
94 | 0 | if ( testMethods == null ) |
95 | |
{ |
96 | 0 | discoverTestMethods(); |
97 | |
} |
98 | |
|
99 | 0 | boolean abort = false; |
100 | |
|
101 | 0 | for ( int i = 0; i < testMethods.size() && !abort; ++i ) |
102 | |
{ |
103 | 0 | abort = executeTestMethod( (Method) testMethods.get( i ), EMPTY_OBJECT_ARRAY, reportManager ); |
104 | |
} |
105 | 0 | } |
106 | |
|
107 | |
private boolean executeTestMethod( Method method, Object[] args, RunListener reportManager ) |
108 | |
{ |
109 | 0 | if ( method == null || args == null || reportManager == null ) |
110 | |
{ |
111 | 0 | throw new NullPointerException(); |
112 | |
} |
113 | |
|
114 | 0 | String userFriendlyMethodName = method.getName() + '('; |
115 | |
|
116 | 0 | if ( args.length != 0 ) |
117 | |
{ |
118 | 0 | userFriendlyMethodName += "Reporter"; |
119 | |
} |
120 | |
|
121 | 0 | userFriendlyMethodName += ')'; |
122 | |
|
123 | 0 | ReportEntry report = |
124 | |
new SimpleReportEntry( testObject.getClass().getName(), getTestName( userFriendlyMethodName ) ); |
125 | |
|
126 | 0 | reportManager.testStarting( report ); |
127 | |
|
128 | |
try |
129 | |
{ |
130 | 0 | setUpFixture(); |
131 | |
} |
132 | 0 | catch ( Throwable e ) |
133 | |
{ |
134 | 0 | report = new SimpleReportEntry( testObject.getClass().getName(), getTestName( userFriendlyMethodName ), |
135 | |
new PojoStackTraceWriter( testObject.getClass().getName(), method.getName(), |
136 | |
e ) ); |
137 | |
|
138 | 0 | reportManager.testFailed( report ); |
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | 0 | return true; |
146 | 0 | } |
147 | |
|
148 | |
|
149 | |
try |
150 | |
{ |
151 | 0 | method.invoke( testObject, args ); |
152 | |
|
153 | 0 | report = new SimpleReportEntry( testObject.getClass().getName(), getTestName( userFriendlyMethodName ) ); |
154 | |
|
155 | 0 | reportManager.testSucceeded( report ); |
156 | |
} |
157 | 0 | catch ( InvocationTargetException ite ) |
158 | |
{ |
159 | 0 | Throwable t = ite.getTargetException(); |
160 | |
|
161 | 0 | report = new SimpleReportEntry( testObject.getClass().getName(), getTestName( userFriendlyMethodName ), |
162 | |
new PojoStackTraceWriter( testObject.getClass().getName(), method.getName(), |
163 | |
t ) ); |
164 | |
|
165 | 0 | reportManager.testFailed( report ); |
166 | |
|
167 | |
|
168 | |
} |
169 | 0 | catch ( Throwable t ) |
170 | |
{ |
171 | 0 | report = new SimpleReportEntry( testObject.getClass().getName(), getTestName( userFriendlyMethodName ), |
172 | |
new PojoStackTraceWriter( testObject.getClass().getName(), method.getName(), |
173 | |
t ) ); |
174 | |
|
175 | 0 | reportManager.testFailed( report ); |
176 | |
|
177 | |
|
178 | 0 | } |
179 | |
|
180 | |
try |
181 | |
{ |
182 | 0 | tearDownFixture(); |
183 | |
} |
184 | 0 | catch ( Throwable t ) |
185 | |
{ |
186 | |
|
187 | 0 | report = new SimpleReportEntry( testObject.getClass().getName(), getTestName( userFriendlyMethodName ), |
188 | |
new PojoStackTraceWriter( testObject.getClass().getName(), method.getName(), |
189 | |
t ) ); |
190 | |
|
191 | 0 | reportManager.testFailed( report ); |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | 0 | return true; |
199 | 0 | } |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | 0 | return false; |
207 | |
} |
208 | |
|
209 | |
private String getTestName( String testMethodName ) |
210 | |
{ |
211 | 0 | if ( testMethodName == null ) |
212 | |
{ |
213 | 0 | throw new NullPointerException( "testMethodName is null" ); |
214 | |
} |
215 | |
|
216 | 0 | return getTestClass().getName() + "." + testMethodName; |
217 | |
} |
218 | |
|
219 | |
private void setUpFixture() throws Throwable |
220 | |
{ |
221 | 0 | if (setUpMethod != null) setUpMethod.invoke( testObject, new Object[0] ); |
222 | 0 | } |
223 | |
|
224 | |
private void tearDownFixture() throws Throwable |
225 | |
{ |
226 | 0 | if (tearDownMethod != null) tearDownMethod.invoke( testObject, new Object[0] ); |
227 | 0 | } |
228 | |
|
229 | |
private void discoverTestMethods() |
230 | |
{ |
231 | 0 | if ( testMethods == null ) |
232 | |
{ |
233 | 0 | testMethods = new ArrayList(); |
234 | |
|
235 | 0 | Method[] methods = getTestClass().getMethods(); |
236 | |
|
237 | 0 | for ( int i = 0; i < methods.length; ++i ) |
238 | |
{ |
239 | 0 | Method m = methods[i]; |
240 | |
|
241 | 0 | if ( isValidTestMethod( m ) ) |
242 | |
{ |
243 | 0 | String simpleName = m.getName(); |
244 | |
|
245 | |
|
246 | 0 | if ( simpleName.length() > 4 ) |
247 | |
{ |
248 | 0 | String firstFour = simpleName.substring( 0, 4 ); |
249 | |
|
250 | |
|
251 | 0 | if ( firstFour.equals( TEST_METHOD_PREFIX ) ) |
252 | |
{ |
253 | 0 | testMethods.add( m ); |
254 | |
} |
255 | |
} |
256 | 0 | } |
257 | 0 | else if (m.getName().equals("setUp") && m.getParameterTypes().length == 0) |
258 | |
{ |
259 | 0 | setUpMethod = m; |
260 | |
} |
261 | 0 | else if (m.getName().equals("tearDown") && m.getParameterTypes().length == 0) |
262 | |
{ |
263 | 0 | tearDownMethod = m; |
264 | |
} |
265 | |
} |
266 | |
} |
267 | 0 | } |
268 | |
|
269 | |
private static boolean isValidTestMethod( Method m ) |
270 | |
{ |
271 | 0 | boolean isInstanceMethod = !Modifier.isStatic( m.getModifiers() ); |
272 | |
|
273 | 0 | boolean returnsVoid = m.getReturnType().equals( void.class ); |
274 | |
|
275 | 0 | boolean hasNoParams = m.getParameterTypes().length == 0; |
276 | |
|
277 | 0 | return isInstanceMethod && returnsVoid && hasNoParams; |
278 | |
} |
279 | |
|
280 | |
public String getName() |
281 | |
{ |
282 | 0 | return testClass.getName(); |
283 | |
} |
284 | |
|
285 | |
public Class getTestClass() |
286 | |
{ |
287 | 0 | return testClass; |
288 | |
} |
289 | |
} |