1 package org.apache.maven.plugin.surefire.booterclient;
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.plugin.surefire.SurefireProperties;
23 import org.apache.maven.surefire.booter.AbstractPathConfiguration;
24 import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
25 import org.apache.maven.surefire.booter.KeyValueSource;
26 import org.apache.maven.surefire.booter.ProcessCheckerType;
27 import org.apache.maven.surefire.booter.ProviderConfiguration;
28 import org.apache.maven.surefire.booter.StartupConfiguration;
29 import org.apache.maven.surefire.cli.CommandLineOption;
30 import org.apache.maven.surefire.report.ReporterConfiguration;
31 import org.apache.maven.surefire.testset.DirectoryScannerParameters;
32 import org.apache.maven.surefire.testset.RunOrderParameters;
33 import org.apache.maven.surefire.testset.TestArtifactInfo;
34 import org.apache.maven.surefire.testset.TestListResolver;
35 import org.apache.maven.surefire.testset.TestRequest;
36 import org.apache.maven.surefire.util.RunOrder;
37
38 import java.io.File;
39 import java.io.IOException;
40 import java.util.Collections;
41 import java.util.List;
42
43 import static org.apache.maven.plugin.surefire.SurefireHelper.replaceForkThreadsInPath;
44 import static org.apache.maven.surefire.booter.AbstractPathConfiguration.CHILD_DELEGATION;
45 import static org.apache.maven.surefire.booter.AbstractPathConfiguration.CLASSPATH;
46 import static org.apache.maven.surefire.booter.AbstractPathConfiguration.ENABLE_ASSERTIONS;
47 import static org.apache.maven.surefire.booter.AbstractPathConfiguration.SUREFIRE_CLASSPATH;
48 import static org.apache.maven.surefire.booter.BooterConstants.EXCLUDES_PROPERTY_PREFIX;
49 import static org.apache.maven.surefire.booter.BooterConstants.FAIL_FAST_COUNT;
50 import static org.apache.maven.surefire.booter.BooterConstants.FAILIFNOTESTS;
51 import static org.apache.maven.surefire.booter.BooterConstants.FORKTESTSET;
52 import static org.apache.maven.surefire.booter.BooterConstants.FORKTESTSET_PREFER_TESTS_FROM_IN_STREAM;
53 import static org.apache.maven.surefire.booter.BooterConstants.INCLUDES_PROPERTY_PREFIX;
54 import static org.apache.maven.surefire.booter.BooterConstants.ISTRIMSTACKTRACE;
55 import static org.apache.maven.surefire.booter.BooterConstants.MAIN_CLI_OPTIONS;
56 import static org.apache.maven.surefire.booter.BooterConstants.PLUGIN_PID;
57 import static org.apache.maven.surefire.booter.BooterConstants.PROCESS_CHECKER;
58 import static org.apache.maven.surefire.booter.BooterConstants.PROVIDER_CONFIGURATION;
59 import static org.apache.maven.surefire.booter.BooterConstants.REPORTSDIRECTORY;
60 import static org.apache.maven.surefire.booter.BooterConstants.REQUESTEDTEST;
61 import static org.apache.maven.surefire.booter.BooterConstants.RERUN_FAILING_TESTS_COUNT;
62 import static org.apache.maven.surefire.booter.BooterConstants.RUN_ORDER;
63 import static org.apache.maven.surefire.booter.BooterConstants.RUN_STATISTICS_FILE;
64 import static org.apache.maven.surefire.booter.BooterConstants.SHUTDOWN;
65 import static org.apache.maven.surefire.booter.BooterConstants.SOURCE_DIRECTORY;
66 import static org.apache.maven.surefire.booter.BooterConstants.SPECIFIC_TEST_PROPERTY_PREFIX;
67 import static org.apache.maven.surefire.booter.BooterConstants.SYSTEM_EXIT_TIMEOUT;
68 import static org.apache.maven.surefire.booter.BooterConstants.TEST_CLASSES_DIRECTORY;
69 import static org.apache.maven.surefire.booter.BooterConstants.TEST_SUITE_XML_FILES;
70 import static org.apache.maven.surefire.booter.BooterConstants.TESTARTIFACT_CLASSIFIER;
71 import static org.apache.maven.surefire.booter.BooterConstants.TESTARTIFACT_VERSION;
72 import static org.apache.maven.surefire.booter.BooterConstants.USEMANIFESTONLYJAR;
73 import static org.apache.maven.surefire.booter.BooterConstants.USESYSTEMCLASSLOADER;
74 import static org.apache.maven.surefire.booter.SystemPropertyManager.writePropertiesFile;
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 class BooterSerializer
92 {
93 private final ForkConfiguration forkConfiguration;
94
95 BooterSerializer( ForkConfiguration forkConfiguration )
96 {
97 this.forkConfiguration = forkConfiguration;
98 }
99
100
101
102
103 File serialize( KeyValueSource sourceProperties, ProviderConfiguration providerConfiguration,
104 StartupConfiguration startupConfiguration, Object testSet, boolean readTestsFromInStream,
105 Long pid, int forkNumber )
106 throws IOException
107 {
108 SurefireProperties properties = new SurefireProperties( sourceProperties );
109
110 properties.setProperty( PLUGIN_PID, pid );
111
112 AbstractPathConfiguration cp = startupConfiguration.getClasspathConfiguration();
113 properties.setClasspath( CLASSPATH, cp.getTestClasspath() );
114 properties.setClasspath( SUREFIRE_CLASSPATH, cp.getProviderClasspath() );
115 properties.setProperty( ENABLE_ASSERTIONS, toString( cp.isEnableAssertions() ) );
116 properties.setProperty( CHILD_DELEGATION, toString( cp.isChildDelegation() ) );
117 ProcessCheckerType processChecker = startupConfiguration.getProcessChecker();
118 properties.setNullableProperty( PROCESS_CHECKER, processChecker == null ? null : processChecker.getType() );
119
120 TestArtifactInfo testNg = providerConfiguration.getTestArtifact();
121 if ( testNg != null )
122 {
123 properties.setProperty( TESTARTIFACT_VERSION, testNg.getVersion() );
124 properties.setNullableProperty( TESTARTIFACT_CLASSIFIER, testNg.getClassifier() );
125 }
126
127 properties.setProperty( FORKTESTSET_PREFER_TESTS_FROM_IN_STREAM, readTestsFromInStream );
128 properties.setNullableProperty( FORKTESTSET, getTypeEncoded( testSet ) );
129
130 TestRequest testSuiteDefinition = providerConfiguration.getTestSuiteDefinition();
131 if ( testSuiteDefinition != null )
132 {
133 properties.setProperty( SOURCE_DIRECTORY, testSuiteDefinition.getTestSourceDirectory() );
134 if ( testSet instanceof File )
135 {
136 properties.addList( Collections.singletonList( (File) testSet ), TEST_SUITE_XML_FILES );
137 }
138 else
139 {
140 properties.addList( testSuiteDefinition.getSuiteXmlFiles(), TEST_SUITE_XML_FILES );
141 }
142 TestListResolver testFilter = testSuiteDefinition.getTestListResolver();
143 properties.setProperty( REQUESTEDTEST, testFilter == null ? "" : testFilter.getPluginParameterTest() );
144 int rerunFailingTestsCount = testSuiteDefinition.getRerunFailingTestsCount();
145 properties.setNullableProperty( RERUN_FAILING_TESTS_COUNT, toString( rerunFailingTestsCount ) );
146 }
147
148 DirectoryScannerParameters directoryScannerParameters = providerConfiguration.getDirScannerParams();
149 if ( directoryScannerParameters != null )
150 {
151 properties.setProperty( FAILIFNOTESTS, toString( directoryScannerParameters.isFailIfNoTests() ) );
152 properties.addList( directoryScannerParameters.getIncludes(), INCLUDES_PROPERTY_PREFIX );
153 properties.addList( directoryScannerParameters.getExcludes(), EXCLUDES_PROPERTY_PREFIX );
154 properties.addList( directoryScannerParameters.getSpecificTests(), SPECIFIC_TEST_PROPERTY_PREFIX );
155 properties.setProperty( TEST_CLASSES_DIRECTORY, directoryScannerParameters.getTestClassesDirectory() );
156 }
157
158 final RunOrderParameters runOrderParameters = providerConfiguration.getRunOrderParameters();
159 if ( runOrderParameters != null )
160 {
161 properties.setProperty( RUN_ORDER, RunOrder.asString( runOrderParameters.getRunOrder() ) );
162 properties.setProperty( RUN_STATISTICS_FILE, runOrderParameters.getRunStatisticsFile() );
163 }
164
165 ReporterConfiguration reporterConfiguration = providerConfiguration.getReporterConfiguration();
166 boolean rep = reporterConfiguration.isTrimStackTrace();
167 File reportsDirectory = replaceForkThreadsInPath( reporterConfiguration.getReportsDirectory(), forkNumber );
168 properties.setProperty( ISTRIMSTACKTRACE, rep );
169 properties.setProperty( REPORTSDIRECTORY, reportsDirectory );
170 ClassLoaderConfiguration classLoaderConfig = startupConfiguration.getClassLoaderConfiguration();
171 properties.setProperty( USESYSTEMCLASSLOADER, toString( classLoaderConfig.isUseSystemClassLoader() ) );
172 properties.setProperty( USEMANIFESTONLYJAR, toString( classLoaderConfig.isUseManifestOnlyJar() ) );
173 properties.setProperty( FAILIFNOTESTS, toString( providerConfiguration.isFailIfNoTests() ) );
174 properties.setProperty( PROVIDER_CONFIGURATION, startupConfiguration.getProviderClassName() );
175 properties.setProperty( FAIL_FAST_COUNT, toString( providerConfiguration.getSkipAfterFailureCount() ) );
176 properties.setProperty( SHUTDOWN, providerConfiguration.getShutdown().name() );
177 List<CommandLineOption> mainCliOptions = providerConfiguration.getMainCliOptions();
178 if ( mainCliOptions != null )
179 {
180 properties.addList( mainCliOptions, MAIN_CLI_OPTIONS );
181 }
182 properties.setNullableProperty( SYSTEM_EXIT_TIMEOUT, toString( providerConfiguration.getSystemExitTimeout() ) );
183
184 File surefireTmpDir = forkConfiguration.getTempDirectory();
185 boolean debug = forkConfiguration.isDebug();
186 return writePropertiesFile( properties, surefireTmpDir, "surefire", debug );
187 }
188
189 private static String getTypeEncoded( Object value )
190 {
191 if ( value == null )
192 {
193 return null;
194 }
195 String valueToUse = value instanceof Class ? ( (Class<?>) value ).getName() : value.toString();
196 return value.getClass().getName() + "|" + valueToUse;
197 }
198
199 private static String toString( Object o )
200 {
201 return String.valueOf( o );
202 }
203 }