1 package org.apache.maven.surefire.junitcore;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.concurrent.ExecutionException;
25
26 import junit.framework.TestCase;
27 import org.junit.Test;
28 import org.junit.runner.Computer;
29 import org.junit.runner.JUnitCore;
30 import org.junit.runner.Result;
31 import org.junit.runner.notification.RunListener;
32
33
34
35
36
37
38 public class ConfigurableParallelComputerTest
39 extends TestCase
40 {
41 private static final int NUMTESTS = 1000;
42
43
44
45 @Test
46 public void testAnythingYouWantToPlayWith()
47 throws Exception
48 {
49 Result result = new Result();
50 Class<?>[] realClasses = new Class[]{ Dummy.class, Dummy2.class };
51
52 DiagnosticRunListener diagnosticRunListener = new DiagnosticRunListener( false, result.createListener() );
53 JUnitCore jUnitCore = getJunitCore( diagnosticRunListener );
54 ConfigurableParallelComputer computer = new ConfigurableParallelComputer( true, false );
55 jUnitCore.run( computer, realClasses );
56 computer.close();
57 assertEquals( "All tests should succeed, right ?", 5, result.getRunCount() );
58 }
59
60 @Test
61 public void testOneMethod()
62 throws ExecutionException
63 {
64 JUnitCore jUnitCore = new JUnitCore();
65 ConfigurableParallelComputer computer = new ConfigurableParallelComputer( false, true );
66 jUnitCore.run( computer, new Class[]{ Dummy.class, Dummy.class, Dummy.class } );
67 computer.close();
68 }
69
70 @Test
71 public void testSerial()
72 throws Exception
73 {
74 Result result = new Result();
75 Class<?>[] realClasses = getClassList();
76 JUnitCore jUnitCore = getJunitCore( result );
77 Computer computer = new Computer();
78 timedRun( NUMTESTS, result, realClasses, jUnitCore, computer );
79 }
80
81
82 @Test
83 public void testFullTestRunPC()
84 throws Exception
85 {
86 Result result = new Result();
87 Class<?>[] realClasses = getClassList();
88 JUnitCore jUnitCore = getJunitCore( result );
89 Computer computer = new ConfigurableParallelComputer( true, true );
90 timedRun( NUMTESTS, result, realClasses, jUnitCore, computer );
91 }
92
93 @Test
94 public void testWithFailingAssertionCPC()
95 throws Exception
96 {
97 runWithFailingAssertion( new ConfigurableParallelComputer( false, true, 6, true ) );
98 runWithFailingAssertion( new ConfigurableParallelComputer( true, false, 12, false ) );
99 runWithFailingAssertion( new ConfigurableParallelComputer( true, true, 2, false ) );
100 }
101
102 @Test
103 public void testWithSlowTestJustAfew()
104 throws Exception
105 {
106 Result result = new Result();
107 final Computer computer = new ConfigurableParallelComputer( false, true, 3, false );
108 Class<?>[] realClasses = getClassList( SlowTest.class, 5 );
109
110 JUnitCore jUnitCore = getJunitCore( result );
111 runIt( realClasses, jUnitCore, computer );
112 }
113
114
115 private void runWithFailingAssertion( Computer computer )
116 throws ExecutionException
117 {
118 Result result = new Result();
119 Class<?>[] realClasses = getClassList( FailingAssertions.class );
120 JUnitCore jUnitCore = getJunitCore( result );
121 runIt( realClasses, jUnitCore, computer );
122 assertEquals( "No tests should fail, right ?", NUMTESTS, result.getFailures().size() );
123 assertEquals( "All tests should succeed, right ?", 0, result.getIgnoreCount() );
124 assertEquals( "All tests should succeed, right ?", NUMTESTS * 3, result.getRunCount() );
125 }
126
127 @Test
128 public void testWithFailure()
129 throws Exception
130 {
131 Computer computer = new ConfigurableParallelComputer( false, true, 4, true );
132 Result result = new Result();
133 Class<?>[] realClasses = getClassList( Failure.class );
134 JUnitCore jUnitCore = getJunitCore( result );
135 runIt( realClasses, jUnitCore, computer );
136 assertEquals( "No tests should fail, right ?", NUMTESTS, result.getFailures().size() );
137 assertEquals( "All tests should succeed, right ?", 0, result.getIgnoreCount() );
138 assertEquals( "All tests should succeed, right ?", NUMTESTS * 3, result.getRunCount() );
139 }
140
141 @Test
142 public void testFixedThreadPool()
143 throws Exception
144 {
145 Result result = new Result();
146 Class<?>[] realClasses = getClassList();
147 JUnitCore jUnitCore = getJunitCore( result );
148 ConfigurableParallelComputer computer = new ConfigurableParallelComputer( false, true, 2, false );
149 timedRun( NUMTESTS, result, realClasses, jUnitCore, computer );
150 }
151
152 @Test
153 public void testClassesUnlimited()
154 throws Exception
155 {
156 Result result = new Result();
157 Class<?>[] realClasses = getClassList();
158 JUnitCore jUnitCore = getJunitCore( result );
159 ConfigurableParallelComputer computer = new ConfigurableParallelComputer( true, false );
160 timedRun( NUMTESTS, result, realClasses, jUnitCore, computer );
161 }
162
163 @Test
164 public void testBothUnlimited()
165 throws Exception
166 {
167 Result result = new Result();
168 Class<?>[] realClasses = getClassList();
169 DiagnosticRunListener diagnosticRunListener = new DiagnosticRunListener( false, result.createListener() );
170 JUnitCore jUnitCore = getJunitCore( diagnosticRunListener );
171 ConfigurableParallelComputer computer = new ConfigurableParallelComputer( true, true );
172 timedRun( NUMTESTS, result, realClasses, jUnitCore, computer );
173 }
174
175 private JUnitCore getJunitCore( Result result )
176 {
177 RunListener listener = result.createListener();
178 JUnitCore jUnitCore = new JUnitCore();
179 jUnitCore.addListener( listener );
180 return jUnitCore;
181 }
182
183 private JUnitCore getJunitCore( RunListener listener )
184 {
185 JUnitCore jUnitCore = new JUnitCore();
186 jUnitCore.addListener( listener );
187 return jUnitCore;
188 }
189
190 private long runIt( Class<?>[] realClasses, JUnitCore jUnitCore, Computer computer )
191 throws ExecutionException
192 {
193 long start = System.currentTimeMillis();
194 jUnitCore.run( computer, realClasses );
195 if ( computer instanceof ConfigurableParallelComputer )
196 {
197 ( (ConfigurableParallelComputer) computer ).close();
198 }
199 return System.currentTimeMillis() - start;
200 }
201
202 private long timedRun( int numTests, Result result, Class<?>[] realClasses, JUnitCore jUnitCore, Computer computer )
203 throws ExecutionException
204 {
205 long time = runIt( realClasses, jUnitCore, computer );
206 assertEquals( "No tests should fail, right ?", 0, result.getFailures().size() );
207 assertEquals( "All tests should succeed, right ?", 0, result.getIgnoreCount() );
208 assertEquals( "All tests should succeed, right ?", numTests * 3, result.getRunCount() );
209 return time;
210 }
211
212 private Class<?>[] getClassList()
213 {
214 return getClassList( Dummy.class, NUMTESTS );
215 }
216
217 private Class<?>[] getClassList( Class<?> testClass )
218 {
219 return getClassList( testClass, NUMTESTS );
220 }
221
222 private Class<?>[] getClassList( Class<?> testClass, int numItems )
223 {
224 List<Class<?>> realClasses = new ArrayList<>();
225 for ( int i = 0; i < numItems; i++ )
226 {
227 realClasses.add( testClass );
228 }
229 return realClasses.toArray( new Class[realClasses.size()] );
230 }
231
232 static void sleepReallyEvenOnWindows( long ms )
233 throws InterruptedException
234 {
235 long endAt = System.currentTimeMillis() + ms;
236 Thread.sleep( ms );
237 while ( endAt > System.currentTimeMillis() )
238 {
239 Thread.sleep( ms / 10 );
240 Thread.yield();
241 }
242 }
243
244
245
246
247 public static class Dummy
248 {
249 @Test
250 public void testNotMuch()
251 {
252 }
253
254 @Test
255 public void testStub1()
256 {
257
258 }
259
260 @Test
261 public void testStub2()
262 {
263
264 }
265 }
266
267
268
269
270 public static class Dummy2
271 {
272 @Test
273 public void testNotMuch()
274 {
275
276 }
277
278 @Test
279 public void testDummy2()
280 {
281
282 }
283 }
284
285
286
287
288 public static class SlowTest
289 {
290 final int scaling = 100;
291
292 @Test
293 public void testNotMuch()
294 throws InterruptedException
295 {
296 sleepReallyEvenOnWindows( scaling );
297 }
298
299 @Test
300 public void testNotMuch2()
301 throws InterruptedException
302 {
303 sleepReallyEvenOnWindows( 3 * scaling );
304 }
305
306 @Test
307 public void testNotMuch3()
308 throws InterruptedException
309 {
310 sleepReallyEvenOnWindows( 2 * scaling );
311 }
312 }
313
314
315
316
317 public static class FailingAssertions
318 {
319 @Test
320 public void testNotMuch()
321 {
322 }
323
324 @Test
325 public void testNotMuch2()
326 {
327 }
328
329 @Test
330 public void testWithFail()
331 {
332 fail( "We excpect this" );
333 }
334 }
335
336
337
338
339 public static class Failure
340 {
341 @Test
342 public void testNotMuch()
343 {
344 }
345
346 @Test
347 public void testNotMuch2()
348 {
349 }
350
351 @Test
352 public void testWithException()
353 {
354 throw new RuntimeException( "We expect this" );
355 }
356 }
357 }