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