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