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 org.apache.maven.surefire.testset.TestSetFailedException; |
23 | |
import org.apache.maven.surefire.util.TestsToRun; |
24 | |
|
25 | |
import java.util.List; |
26 | |
import java.util.concurrent.ExecutionException; |
27 | |
|
28 | |
import org.junit.runner.Computer; |
29 | |
import org.junit.runner.JUnitCore; |
30 | |
import org.junit.runner.notification.RunListener; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | class JUnitCoreWrapper |
39 | |
{ |
40 | |
public static void execute( TestsToRun testsToRun, JUnitCoreParameters jUnitCoreParameters, |
41 | |
List<RunListener> listeners ) |
42 | |
throws TestSetFailedException |
43 | |
{ |
44 | 0 | Computer computer = getComputer( jUnitCoreParameters ); |
45 | 0 | JUnitCore junitCore = new JUnitCore(); |
46 | 0 | for ( RunListener runListener : listeners ) |
47 | |
{ |
48 | 0 | junitCore.addListener( runListener ); |
49 | |
} |
50 | |
try |
51 | |
{ |
52 | 0 | junitCore.run( computer, testsToRun.getLocatedClasses() ); |
53 | |
} |
54 | |
finally |
55 | |
{ |
56 | 0 | closeIfConfigurable( computer ); |
57 | 0 | for ( RunListener runListener : listeners ) |
58 | |
{ |
59 | 0 | junitCore.removeListener( runListener ); |
60 | |
} |
61 | 0 | } |
62 | 0 | } |
63 | |
|
64 | |
private static void closeIfConfigurable( Computer computer ) |
65 | |
throws TestSetFailedException |
66 | |
{ |
67 | 0 | if ( computer instanceof ConfigurableParallelComputer ) |
68 | |
{ |
69 | |
try |
70 | |
{ |
71 | 0 | ( (ConfigurableParallelComputer) computer ).close(); |
72 | |
} |
73 | 0 | catch ( ExecutionException e ) |
74 | |
{ |
75 | 0 | throw new TestSetFailedException( e ); |
76 | 0 | } |
77 | |
} |
78 | 0 | } |
79 | |
|
80 | |
private static Computer getComputer( JUnitCoreParameters jUnitCoreParameters ) |
81 | |
throws TestSetFailedException |
82 | |
{ |
83 | 0 | if ( jUnitCoreParameters.isNoThreading() ) |
84 | |
{ |
85 | 0 | return new Computer(); |
86 | |
} |
87 | 0 | return getConfigurableParallelComputer( jUnitCoreParameters ); |
88 | |
} |
89 | |
|
90 | |
private static Computer getConfigurableParallelComputer( JUnitCoreParameters jUnitCoreParameters ) |
91 | |
throws TestSetFailedException |
92 | |
{ |
93 | 0 | if ( jUnitCoreParameters.isUseUnlimitedThreads() ) |
94 | |
{ |
95 | 0 | return new ConfigurableParallelComputer(); |
96 | |
} |
97 | |
else |
98 | |
{ |
99 | 0 | return new ConfigurableParallelComputer( |
100 | |
jUnitCoreParameters.isParallelClasses() | jUnitCoreParameters.isParallelBoth(), |
101 | |
jUnitCoreParameters.isParallelMethod() | jUnitCoreParameters.isParallelBoth(), |
102 | |
jUnitCoreParameters.getThreadCount(), jUnitCoreParameters.isPerCoreThreadCount() ); |
103 | |
} |
104 | |
} |
105 | |
} |