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