View Javadoc

1   package org.apache.maven.surefire.junitcore;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Simple test of ConfigurableParallelComputer.
35   *
36   * @author Kristian Rosenvold
37   */
38  public class ConfigurableParallelComputerTest
39      extends TestCase
40  {
41      private static final int NUMTESTS = 1000;
42  
43      // I'm sorry about all the sout's in this test; but if you deadlock when building you will appreciate it.
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 ); // 300 ms in methods, 600 in classes
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<Class>();
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     public static class Dummy
245     {
246         @Test
247         public void testNotMuch()
248         {
249         }
250 
251         @Test
252         public void testStub1()
253         {
254             // Add your code here
255         }
256 
257         @Test
258         public void testStub2()
259         {
260             // Add your code here
261         }
262     }
263 
264     public static class Dummy2
265     {
266         @Test
267         public void testNotMuch()
268         {
269 
270         }
271 
272         @Test
273         public void testDummy2()
274         {
275             // Add your code here
276         }
277     }
278 
279     public static class SlowTest
280     {
281         final int scaling = 100;
282 
283         @Test
284         public void testNotMuch()
285             throws InterruptedException
286         {
287             sleepReallyEvenOnWindows( scaling );
288         }
289 
290         @Test
291         public void testNotMuch2()
292             throws InterruptedException
293         {
294             sleepReallyEvenOnWindows( 3 * scaling );
295         }
296 
297         @Test
298         public void testNotMuch3()
299             throws InterruptedException
300         {
301             sleepReallyEvenOnWindows( 2 * scaling );
302         }
303     }
304 
305     public static class FailingAssertions
306     {
307         @Test
308         public void testNotMuch()
309         {
310         }
311 
312         @Test
313         public void testNotMuch2()
314         {
315         }
316 
317         @Test
318         public void testWithFail()
319         {
320             fail( "We excpect this" );
321         }
322     }
323 
324     public static class Failure
325     {
326         @Test
327         public void testNotMuch()
328         {
329         }
330 
331         @Test
332         public void testNotMuch2()
333         {
334         }
335 
336         @Test
337         public void testWithException()
338         {
339             throw new RuntimeException( "We expect this" );
340         }
341     }
342 }