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 public BooterSerializer( ForkConfiguration forkConfiguration )
65 {
66 this.forkConfiguration = forkConfiguration;
67 }
68
69
70
71
72 public File serialize( KeyValueSource sourceProperties, ProviderConfiguration booterConfiguration,
73 StartupConfiguration providerConfiguration, Object testSet, boolean readTestsFromInStream )
74 throws IOException
75 {
76
77 SurefireProperties properties = new SurefireProperties( sourceProperties );
78
79 ClasspathConfiguration cp = providerConfiguration.getClasspathConfiguration();
80 properties.setClasspath( ClasspathConfiguration.CLASSPATH, cp.getTestClasspath() );
81 properties.setClasspath( ClasspathConfiguration.SUREFIRE_CLASSPATH, cp.getProviderClasspath() );
82 properties.setProperty( ClasspathConfiguration.ENABLE_ASSERTIONS, String.valueOf( cp.isEnableAssertions() ) );
83 properties.setProperty( ClasspathConfiguration.CHILD_DELEGATION, String.valueOf( cp.isChildDelegation() ) );
84
85 TestArtifactInfo testNg = booterConfiguration.getTestArtifact();
86 if ( testNg != null )
87 {
88 properties.setProperty( TESTARTIFACT_VERSION, testNg.getVersion() );
89 properties.setNullableProperty( TESTARTIFACT_CLASSIFIER, testNg.getClassifier() );
90 }
91
92 properties.setProperty( FORKTESTSET_PREFER_TESTS_FROM_IN_STREAM, readTestsFromInStream );
93 properties.setNullableProperty( FORKTESTSET, getTypeEncoded( testSet ) );
94
95 TestRequest testSuiteDefinition = booterConfiguration.getTestSuiteDefinition();
96 if ( testSuiteDefinition != null )
97 {
98 properties.setProperty( SOURCE_DIRECTORY, testSuiteDefinition.getTestSourceDirectory() );
99 properties.addList( testSuiteDefinition.getSuiteXmlFiles(), TEST_SUITE_XML_FILES );
100 TestListResolver testFilter = testSuiteDefinition.getTestListResolver();
101 properties.setProperty( REQUESTEDTEST, testFilter == null ? "" : testFilter.getPluginParameterTest() );
102 properties.setNullableProperty( RERUN_FAILING_TESTS_COUNT,
103 String.valueOf( testSuiteDefinition.getRerunFailingTestsCount() ) );
104 }
105
106 DirectoryScannerParameters directoryScannerParameters = booterConfiguration.getDirScannerParams();
107 if ( directoryScannerParameters != null )
108 {
109 properties.setProperty( FAILIFNOTESTS, String.valueOf( directoryScannerParameters.isFailIfNoTests() ) );
110 properties.addList( directoryScannerParameters.getIncludes(), INCLUDES_PROPERTY_PREFIX );
111 properties.addList( directoryScannerParameters.getExcludes(), EXCLUDES_PROPERTY_PREFIX );
112 properties.addList( directoryScannerParameters.getSpecificTests(), SPECIFIC_TEST_PROPERTY_PREFIX );
113
114 properties.setProperty( TEST_CLASSES_DIRECTORY, directoryScannerParameters.getTestClassesDirectory() );
115 }
116
117 final RunOrderParameters runOrderParameters = booterConfiguration.getRunOrderParameters();
118 if ( runOrderParameters != null )
119 {
120 properties.setProperty( RUN_ORDER, RunOrder.asString( runOrderParameters.getRunOrder() ) );
121 properties.setProperty( RUN_STATISTICS_FILE, runOrderParameters.getRunStatisticsFile() );
122 }
123
124 ReporterConfiguration reporterConfiguration = booterConfiguration.getReporterConfiguration();
125
126 boolean rep = reporterConfiguration.isTrimStackTrace();
127 properties.setProperty( ISTRIMSTACKTRACE, rep );
128 properties.setProperty( REPORTSDIRECTORY, reporterConfiguration.getReportsDirectory() );
129 ClassLoaderConfiguration classLoaderConfig = providerConfiguration.getClassLoaderConfiguration();
130 properties.setProperty( USESYSTEMCLASSLOADER, String.valueOf( classLoaderConfig.isUseSystemClassLoader() ) );
131 properties.setProperty( USEMANIFESTONLYJAR, String.valueOf( classLoaderConfig.isUseManifestOnlyJar() ) );
132 properties.setProperty( FAILIFNOTESTS, String.valueOf( booterConfiguration.isFailIfNoTests() ) );
133 properties.setProperty( PROVIDER_CONFIGURATION, providerConfiguration.getProviderClassName() );
134 properties.setProperty( FAIL_FAST_COUNT, String.valueOf( booterConfiguration.getSkipAfterFailureCount() ) );
135 properties.setProperty( SHUTDOWN, booterConfiguration.getShutdown().name() );
136 List<CommandLineOption> mainCliOptions = booterConfiguration.getMainCliOptions();
137 if ( mainCliOptions != null )
138 {
139 properties.addList( mainCliOptions, MAIN_CLI_OPTIONS );
140 }
141
142 return SystemPropertyManager.writePropertiesFile( properties, forkConfiguration.getTempDirectory(),
143 "surefire", forkConfiguration.isDebug() );
144 }
145
146
147 private String getTypeEncoded( Object value )
148 {
149 if ( value == null )
150 {
151 return null;
152 }
153 String valueToUse;
154 if ( value instanceof Class )
155 {
156 valueToUse = ( (Class<?>) value ).getName();
157 }
158 else
159 {
160 valueToUse = value.toString();
161 }
162 return value.getClass().getName() + "|" + valueToUse;
163 }
164
165 }