1 | |
package org.apache.maven.surefire.booter; |
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.providerapi.SurefireProvider; |
23 | |
import org.apache.maven.surefire.report.ReporterConfiguration; |
24 | |
import org.apache.maven.surefire.report.ReporterException; |
25 | |
import org.apache.maven.surefire.suite.RunResult; |
26 | |
import org.apache.maven.surefire.testset.TestSetFailedException; |
27 | |
import org.apache.maven.surefire.util.NestedRuntimeException; |
28 | |
|
29 | |
import java.io.File; |
30 | |
import java.io.IOException; |
31 | |
import java.io.PrintStream; |
32 | |
import java.util.Properties; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public class SurefireStarter |
48 | |
{ |
49 | |
private static final int NO_TESTS = 254; |
50 | |
|
51 | |
private final ProviderConfiguration providerConfiguration; |
52 | |
|
53 | |
private final StartupConfiguration startupConfiguration; |
54 | |
|
55 | 0 | private final String SUREFIRE_TEST_CLASSPATH = "surefire.test.class.path"; |
56 | |
|
57 | |
public SurefireStarter( StartupConfiguration startupConfiguration, ProviderConfiguration providerConfiguration ) |
58 | 0 | { |
59 | 0 | this.providerConfiguration = providerConfiguration; |
60 | 0 | this.startupConfiguration = startupConfiguration; |
61 | 0 | } |
62 | |
|
63 | |
public int runSuitesInProcess( Object testSet, File surefirePropertiesFile, Properties p ) |
64 | |
throws SurefireExecutionException, IOException |
65 | |
{ |
66 | 0 | writeSurefireTestClasspathProperty(); |
67 | 0 | final ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration(); |
68 | |
|
69 | 0 | ClassLoader testsClassLoader = classpathConfiguration.createTestClassLoaderConditionallySystem( |
70 | |
startupConfiguration.useSystemClassLoader() ); |
71 | |
|
72 | 0 | ClassLoader surefireClassLoader = classpathConfiguration.createSurefireClassLoader( testsClassLoader ); |
73 | |
|
74 | 0 | final RunResult runResult = invokeProvider( testSet, testsClassLoader, surefireClassLoader ); |
75 | 0 | updateResultsProperties( runResult, p ); |
76 | 0 | SystemPropertyManager.writePropertiesFile( surefirePropertiesFile, "surefire", p ); |
77 | 0 | return processRunCount( runResult ); |
78 | |
} |
79 | |
|
80 | |
public int runSuitesInProcess() |
81 | |
throws SurefireExecutionException |
82 | |
{ |
83 | |
|
84 | |
|
85 | 0 | ClassLoader testsClassLoader = createInProcessTestClassLoader(); |
86 | |
|
87 | 0 | final ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration(); |
88 | |
|
89 | 0 | ClassLoader surefireClassLoader = classpathConfiguration.createSurefireClassLoader( testsClassLoader ); |
90 | |
|
91 | 0 | final RunResult runResult = invokeProvider( null, testsClassLoader, surefireClassLoader ); |
92 | 0 | return processRunCount( runResult); |
93 | |
} |
94 | |
|
95 | |
private ClassLoader createInProcessTestClassLoader() |
96 | |
throws SurefireExecutionException |
97 | |
{ |
98 | 0 | writeSurefireTestClasspathProperty(); |
99 | 0 | ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration(); |
100 | 0 | if ( startupConfiguration.isManifestOnlyJarRequestedAndUsable() ) |
101 | |
{ |
102 | 0 | ClassLoader testsClassLoader = getClass().getClassLoader(); |
103 | |
|
104 | |
|
105 | 0 | System.setProperty( "surefire.real.class.path", System.getProperty( "java.class.path" ) ); |
106 | 0 | classpathConfiguration.getTestClasspath().writeToSystemProperty( "java.class.path" ); |
107 | 0 | return testsClassLoader; |
108 | |
} |
109 | |
else |
110 | |
{ |
111 | 0 | return classpathConfiguration.createTestClassLoader(); |
112 | |
} |
113 | |
} |
114 | |
|
115 | |
private void writeSurefireTestClasspathProperty() |
116 | |
{ |
117 | 0 | ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration(); |
118 | 0 | classpathConfiguration.getTestClasspath().writeToSystemProperty( SUREFIRE_TEST_CLASSPATH ); |
119 | 0 | } |
120 | |
|
121 | |
private static final String RESULTS_ERRORS = "errors"; |
122 | |
|
123 | |
private static final String RESULTS_COMPLETED_COUNT = "completedCount"; |
124 | |
|
125 | |
private static final String RESULTS_FAILURES = "failures"; |
126 | |
|
127 | |
private static final String RESULTS_SKIPPED = "skipped"; |
128 | |
|
129 | |
|
130 | |
private void updateResultsProperties( RunResult runResult, Properties results ) |
131 | |
{ |
132 | 0 | results.setProperty( RESULTS_ERRORS, String.valueOf( runResult.getErrors() ) ); |
133 | 0 | results.setProperty( RESULTS_COMPLETED_COUNT, String.valueOf( runResult.getCompletedCount() ) ); |
134 | 0 | results.setProperty( RESULTS_FAILURES, String.valueOf( runResult.getFailures() ) ); |
135 | 0 | results.setProperty( RESULTS_SKIPPED, String.valueOf( runResult.getSkipped() ) ); |
136 | 0 | } |
137 | |
|
138 | |
private RunResult invokeProvider( Object testSet, ClassLoader testsClassLoader, ClassLoader surefireClassLoader ) |
139 | |
{ |
140 | 0 | final PrintStream orgSystemOut = System.out; |
141 | 0 | final PrintStream orgSystemErr = System.err; |
142 | |
|
143 | |
|
144 | 0 | ProviderFactory providerFactory = |
145 | |
new ProviderFactory( startupConfiguration, providerConfiguration, surefireClassLoader, testsClassLoader ); |
146 | 0 | final SurefireProvider provider = providerFactory.createProvider( ); |
147 | |
|
148 | |
try |
149 | |
{ |
150 | 0 | return provider.invoke( testSet ); |
151 | |
} |
152 | 0 | catch ( TestSetFailedException e ) |
153 | |
{ |
154 | 0 | throw new NestedRuntimeException( e ); |
155 | |
} |
156 | 0 | catch ( ReporterException e ) |
157 | |
{ |
158 | 0 | throw new NestedRuntimeException( e ); |
159 | |
} |
160 | |
finally |
161 | |
{ |
162 | 0 | System.setOut( orgSystemOut ); |
163 | 0 | System.setErr( orgSystemErr ); |
164 | |
} |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
private int processRunCount( RunResult runCount ) |
175 | |
throws SurefireExecutionException |
176 | |
{ |
177 | |
|
178 | 0 | if ( runCount.getCompletedCount() == 0 && providerConfiguration.isFailIfNoTests().booleanValue() ) |
179 | |
{ |
180 | 0 | return NO_TESTS; |
181 | |
} |
182 | |
|
183 | 0 | return runCount.getBooterCode(); |
184 | |
} |
185 | |
} |