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