View Javadoc
1   package org.apache.maven.plugin.surefire.booterclient;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Knows how to serialize and deserialize the booter configuration.
78   * <br>
79   * The internal serialization format is through a properties file. The long-term goal of this
80   * class is not to expose this implementation information to its clients. This still leaks somewhat,
81   * and there are some cases where properties are being accessed as "Properties" instead of
82   * more representative domain objects.
83   * <br>
84   *
85   * @author Jason van Zyl
86   * @author Emmanuel Venisse
87   * @author Brett Porter
88   * @author Dan Fabulich
89   * @author Kristian Rosenvold
90   */
91  class BooterSerializer
92  {
93      private final ForkConfiguration forkConfiguration;
94  
95      BooterSerializer( ForkConfiguration forkConfiguration )
96      {
97          this.forkConfiguration = forkConfiguration;
98      }
99  
100     /**
101      * Does not modify sourceProperties
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 }