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 java.io.File; |
23 | |
import java.io.FileInputStream; |
24 | |
import java.io.InputStream; |
25 | |
import java.io.PrintStream; |
26 | |
import java.lang.reflect.InvocationTargetException; |
27 | |
import org.apache.maven.surefire.report.LegacyPojoStackTraceWriter; |
28 | |
import org.apache.maven.surefire.report.StackTraceWriter; |
29 | |
import org.apache.maven.surefire.suite.RunResult; |
30 | |
import org.apache.maven.surefire.testset.TestSetFailedException; |
31 | |
import org.apache.maven.surefire.util.LazyTestsToRun; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class ForkedBooter |
44 | |
{ |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public static void main( String[] args ) |
54 | |
throws Throwable |
55 | |
{ |
56 | 0 | final PrintStream originalOut = System.out; |
57 | |
try |
58 | |
{ |
59 | 0 | if ( args.length > 1 ) |
60 | |
{ |
61 | 0 | SystemPropertyManager.setSystemProperties( new File( args[1] ) ); |
62 | |
} |
63 | |
|
64 | 0 | File surefirePropertiesFile = new File( args[0] ); |
65 | 0 | InputStream stream = surefirePropertiesFile.exists() ? new FileInputStream( surefirePropertiesFile ) : null; |
66 | 0 | BooterDeserializer booterDeserializer = new BooterDeserializer( stream ); |
67 | 0 | ProviderConfiguration providerConfiguration = booterDeserializer.deserialize(); |
68 | 0 | final StartupConfiguration startupConfiguration = booterDeserializer.getProviderConfiguration(); |
69 | |
|
70 | 0 | TypeEncodedValue forkedTestSet = providerConfiguration.getTestForFork(); |
71 | 0 | boolean readTestsFromInputStream = providerConfiguration.isReadTestsFromInStream(); |
72 | |
|
73 | 0 | final ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration(); |
74 | 0 | final ClassLoader testClassLoader = classpathConfiguration.createForkingTestClassLoader( |
75 | |
startupConfiguration.isManifestOnlyJarRequestedAndUsable() ); |
76 | |
|
77 | 0 | startupConfiguration.writeSurefireTestClasspathProperty(); |
78 | |
|
79 | |
Object testSet; |
80 | 0 | if ( forkedTestSet != null ) |
81 | |
{ |
82 | 0 | testSet = forkedTestSet.getDecodedValue( testClassLoader ); |
83 | |
} |
84 | 0 | else if ( readTestsFromInputStream ) |
85 | |
{ |
86 | 0 | testSet = new LazyTestsToRun( System.in, testClassLoader, originalOut ); |
87 | |
} |
88 | |
else |
89 | |
{ |
90 | 0 | testSet = null; |
91 | |
} |
92 | |
|
93 | |
try |
94 | |
{ |
95 | 0 | runSuitesInProcess( testSet, testClassLoader, startupConfiguration, providerConfiguration, |
96 | |
originalOut ); |
97 | |
} |
98 | 0 | catch ( InvocationTargetException t ) |
99 | |
{ |
100 | |
|
101 | 0 | LegacyPojoStackTraceWriter stackTraceWriter = |
102 | |
new LegacyPojoStackTraceWriter( "test subystem", "no method", t.getTargetException() ); |
103 | 0 | StringBuffer stringBuffer = new StringBuffer(); |
104 | 0 | ForkingRunListener.encode( stringBuffer, stackTraceWriter, false ); |
105 | 0 | originalOut.println( ( (char) ForkingRunListener.BOOTERCODE_ERROR ) + ",0," + stringBuffer.toString() ); |
106 | |
} |
107 | 0 | catch ( Throwable t ) |
108 | |
{ |
109 | 0 | StackTraceWriter stackTraceWriter = new LegacyPojoStackTraceWriter( "test subystem", "no method", t ); |
110 | 0 | StringBuffer stringBuffer = new StringBuffer(); |
111 | 0 | ForkingRunListener.encode( stringBuffer, stackTraceWriter, false ); |
112 | 0 | originalOut.println( ( (char) ForkingRunListener.BOOTERCODE_ERROR ) + ",0," + stringBuffer.toString() ); |
113 | 0 | } |
114 | |
|
115 | 0 | originalOut.println( ( (char) ForkingRunListener.BOOTERCODE_BYE ) + ",0,BYE!" ); |
116 | 0 | originalOut.flush(); |
117 | |
|
118 | 0 | exit( 0 ); |
119 | |
} |
120 | 0 | catch ( Throwable t ) |
121 | |
{ |
122 | |
|
123 | |
|
124 | 0 | t.printStackTrace( System.err ); |
125 | |
|
126 | 0 | exit( 1 ); |
127 | 0 | } |
128 | 0 | } |
129 | |
|
130 | |
private final static long SYSTEM_EXIT_TIMEOUT = 30 * 1000; |
131 | |
|
132 | |
private static void exit( final int returnCode ) |
133 | |
{ |
134 | 0 | launchLastDitchDaemonShutdownThread( returnCode ); |
135 | 0 | System.exit( returnCode ); |
136 | 0 | } |
137 | |
|
138 | |
|
139 | |
private static RunResult runSuitesInProcess( Object testSet, ClassLoader testsClassLoader, |
140 | |
StartupConfiguration startupConfiguration, |
141 | |
ProviderConfiguration providerConfiguration, |
142 | |
PrintStream originalSystemOut ) |
143 | |
throws SurefireExecutionException, TestSetFailedException, InvocationTargetException |
144 | |
{ |
145 | 0 | final ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration(); |
146 | 0 | ClassLoader surefireClassLoader = classpathConfiguration.createSurefireClassLoader( testsClassLoader ); |
147 | |
|
148 | 0 | SurefireReflector surefireReflector = new SurefireReflector( surefireClassLoader ); |
149 | |
|
150 | 0 | final Object factory = |
151 | |
createForkingReporterFactory( surefireReflector, providerConfiguration, originalSystemOut ); |
152 | |
|
153 | 0 | return ProviderFactory.invokeProvider( testSet, testsClassLoader, surefireClassLoader, factory, |
154 | |
providerConfiguration, true, startupConfiguration, false ); |
155 | |
} |
156 | |
|
157 | |
private static Object createForkingReporterFactory( SurefireReflector surefireReflector, |
158 | |
ProviderConfiguration providerConfiguration, |
159 | |
PrintStream originalSystemOut ) |
160 | |
{ |
161 | 0 | final Boolean trimStackTrace = providerConfiguration.getReporterConfiguration().isTrimStackTrace(); |
162 | 0 | return surefireReflector.createForkingReporterFactory( trimStackTrace, originalSystemOut ); |
163 | |
} |
164 | |
|
165 | |
private static void launchLastDitchDaemonShutdownThread( final int returnCode ) |
166 | |
{ |
167 | 0 | Thread lastExit = new Thread( new Runnable() |
168 | 0 | { |
169 | |
public void run() |
170 | |
{ |
171 | |
try |
172 | |
{ |
173 | 0 | Thread.sleep( SYSTEM_EXIT_TIMEOUT ); |
174 | 0 | Runtime.getRuntime().halt( returnCode ); |
175 | |
} |
176 | 0 | catch ( InterruptedException ignore ) |
177 | |
{ |
178 | 0 | } |
179 | 0 | } |
180 | |
} ); |
181 | 0 | lastExit.setDaemon( true ); |
182 | 0 | lastExit.start(); |
183 | 0 | } |
184 | |
|
185 | |
} |