1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.maven.surefire.junitcore; |
20 | |
|
21 | |
|
22 | |
import org.apache.maven.surefire.report.ReporterManagerFactory; |
23 | |
import org.apache.maven.surefire.testset.TestSetFailedException; |
24 | |
import org.junit.experimental.ParallelComputer; |
25 | |
import org.junit.runner.Computer; |
26 | |
import org.junit.runner.JUnitCore; |
27 | |
import org.junit.runner.notification.RunListener; |
28 | |
|
29 | |
import java.lang.reflect.Constructor; |
30 | |
import java.lang.reflect.InvocationTargetException; |
31 | |
import java.lang.reflect.Method; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
class JUnitCoreTestSet |
43 | |
{ |
44 | |
private final Class testClass; |
45 | |
|
46 | |
private static final String className = "org.jdogma.junit.ConfigurableParallelComputer"; |
47 | |
|
48 | |
public String getName() |
49 | |
{ |
50 | 0 | return testClass.getName(); |
51 | |
} |
52 | |
|
53 | |
Class getTestClass() |
54 | |
{ |
55 | 0 | return testClass; |
56 | |
} |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
protected JUnitCoreTestSet( Class testClasses ) |
64 | 0 | { |
65 | 0 | this.testClass = testClasses; |
66 | 0 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public void execute( ReporterManagerFactory reportManager, JUnitCoreParameters JUnitCoreParameters ) |
77 | |
throws TestSetFailedException |
78 | |
{ |
79 | |
|
80 | 0 | Class[] classes = new Class[1]; |
81 | 0 | classes[0] = testClass; |
82 | 0 | execute( classes, reportManager, JUnitCoreParameters ); |
83 | 0 | } |
84 | |
|
85 | |
public static void execute( Class[] classes, ReporterManagerFactory reporterManagerFactory, |
86 | |
JUnitCoreParameters jUnitCoreParameters ) |
87 | |
throws TestSetFailedException |
88 | |
{ |
89 | 0 | final ConcurrentReportingRunListener listener = |
90 | |
ConcurrentReportingRunListener.createInstance( reporterManagerFactory, jUnitCoreParameters.isParallelClasses(), |
91 | |
jUnitCoreParameters.isParallelBoth() ); |
92 | 0 | Computer computer = getComputer( jUnitCoreParameters ); |
93 | |
try |
94 | |
{ |
95 | 0 | runJunitCore( classes, computer, listener ); |
96 | |
} |
97 | |
finally |
98 | |
{ |
99 | 0 | closeIfConfigurable( computer ); |
100 | 0 | } |
101 | 0 | } |
102 | |
|
103 | |
private static void closeIfConfigurable( Computer computer ) |
104 | |
throws TestSetFailedException |
105 | |
{ |
106 | 0 | if ( computer.getClass().getName().startsWith( className ) ) |
107 | |
{ |
108 | |
try |
109 | |
{ |
110 | 0 | Class<?> cpcClass = Class.forName( className ); |
111 | 0 | Method method = cpcClass.getMethod( "close" ); |
112 | 0 | method.invoke( computer ); |
113 | |
} |
114 | 0 | catch ( ClassNotFoundException e ) |
115 | |
{ |
116 | 0 | throw new TestSetFailedException( e ); |
117 | |
} |
118 | 0 | catch ( NoSuchMethodException e ) |
119 | |
{ |
120 | 0 | throw new TestSetFailedException( e ); |
121 | |
} |
122 | 0 | catch ( InvocationTargetException e ) |
123 | |
{ |
124 | 0 | throw new TestSetFailedException( e ); |
125 | |
} |
126 | 0 | catch ( IllegalAccessException e ) |
127 | |
{ |
128 | 0 | throw new TestSetFailedException( e ); |
129 | 0 | } |
130 | |
} |
131 | 0 | } |
132 | |
|
133 | |
private static Computer getComputer( JUnitCoreParameters jUnitCoreParameters ) |
134 | |
throws TestSetFailedException |
135 | |
{ |
136 | 0 | if ( jUnitCoreParameters.isNoThreading() ) |
137 | |
{ |
138 | 0 | return new Computer(); |
139 | |
} |
140 | 0 | return jUnitCoreParameters.isConfigurableParallelComputerPresent() ? getConfigurableParallelComputer( |
141 | |
jUnitCoreParameters ) : getParallelComputer( jUnitCoreParameters ); |
142 | |
} |
143 | |
|
144 | |
private static Computer getParallelComputer( JUnitCoreParameters JUnitCoreParameters ) |
145 | |
{ |
146 | 0 | if ( JUnitCoreParameters.isUseUnlimitedThreads() ) |
147 | |
{ |
148 | 0 | return new ParallelComputer( true, true ); |
149 | |
} |
150 | |
else |
151 | |
{ |
152 | 0 | return new ParallelComputer( JUnitCoreParameters.isParallelClasses(), |
153 | |
JUnitCoreParameters.isParallelMethod() ); |
154 | |
} |
155 | |
} |
156 | |
|
157 | |
private static Computer getConfigurableParallelComputer( JUnitCoreParameters JUnitCoreParameters ) |
158 | |
throws TestSetFailedException |
159 | |
{ |
160 | |
|
161 | |
try |
162 | |
{ |
163 | 0 | Class<?> cpcClass = Class.forName( className ); |
164 | 0 | if ( JUnitCoreParameters.isUseUnlimitedThreads() ) |
165 | |
{ |
166 | 0 | Constructor<?> constructor = cpcClass.getConstructor(); |
167 | 0 | return (Computer) constructor.newInstance(); |
168 | |
} |
169 | |
else |
170 | |
{ |
171 | 0 | Constructor<?> constructor = |
172 | |
cpcClass.getConstructor( boolean.class, boolean.class, Integer.class, boolean.class ); |
173 | 0 | return (Computer) constructor.newInstance( JUnitCoreParameters.isParallelClasses(), |
174 | |
JUnitCoreParameters.isParallelMethod(), |
175 | |
JUnitCoreParameters.getThreadCount(), |
176 | |
JUnitCoreParameters.isPerCoreThreadCount() ); |
177 | |
} |
178 | |
} |
179 | 0 | catch ( ClassNotFoundException e ) |
180 | |
{ |
181 | 0 | throw new TestSetFailedException( e ); |
182 | |
} |
183 | 0 | catch ( NoSuchMethodException e ) |
184 | |
{ |
185 | 0 | throw new TestSetFailedException( e ); |
186 | |
} |
187 | 0 | catch ( InvocationTargetException e ) |
188 | |
{ |
189 | 0 | throw new TestSetFailedException( e ); |
190 | |
} |
191 | 0 | catch ( InstantiationException e ) |
192 | |
{ |
193 | 0 | throw new TestSetFailedException( e ); |
194 | |
} |
195 | 0 | catch ( IllegalAccessException e ) |
196 | |
{ |
197 | 0 | throw new TestSetFailedException( e ); |
198 | |
} |
199 | |
} |
200 | |
|
201 | |
private static void runJunitCore( Class[] classes, Computer computer, RunListener real ) |
202 | |
throws TestSetFailedException |
203 | |
{ |
204 | 0 | JUnitCore junitCore = new JUnitCore(); |
205 | 0 | junitCore.addListener( real ); |
206 | |
try |
207 | |
{ |
208 | 0 | junitCore.run( computer, classes ); |
209 | |
} |
210 | |
finally |
211 | |
{ |
212 | 0 | junitCore.removeListener( real ); |
213 | 0 | } |
214 | 0 | } |
215 | |
|
216 | |
|
217 | |
} |