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