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.ProviderConfiguration;
27  import org.apache.maven.surefire.booter.StartupConfiguration;
28  import org.apache.maven.surefire.cli.CommandLineOption;
29  import org.apache.maven.surefire.report.ReporterConfiguration;
30  import org.apache.maven.surefire.testset.DirectoryScannerParameters;
31  import org.apache.maven.surefire.testset.RunOrderParameters;
32  import org.apache.maven.surefire.testset.TestArtifactInfo;
33  import org.apache.maven.surefire.testset.TestListResolver;
34  import org.apache.maven.surefire.testset.TestRequest;
35  import org.apache.maven.surefire.util.RunOrder;
36  
37  import java.io.File;
38  import java.io.IOException;
39  import java.util.Collections;
40  import java.util.List;
41  
42  import static org.apache.maven.plugin.surefire.SurefireHelper.replaceForkThreadsInPath;
43  import static org.apache.maven.surefire.booter.AbstractPathConfiguration.CHILD_DELEGATION;
44  import static org.apache.maven.surefire.booter.AbstractPathConfiguration.CLASSPATH;
45  import static org.apache.maven.surefire.booter.AbstractPathConfiguration.ENABLE_ASSERTIONS;
46  import static org.apache.maven.surefire.booter.AbstractPathConfiguration.SUREFIRE_CLASSPATH;
47  import static org.apache.maven.surefire.booter.BooterConstants.EXCLUDES_PROPERTY_PREFIX;
48  import static org.apache.maven.surefire.booter.BooterConstants.FAIL_FAST_COUNT;
49  import static org.apache.maven.surefire.booter.BooterConstants.FAILIFNOTESTS;
50  import static org.apache.maven.surefire.booter.BooterConstants.FORKTESTSET;
51  import static org.apache.maven.surefire.booter.BooterConstants.FORKTESTSET_PREFER_TESTS_FROM_IN_STREAM;
52  import static org.apache.maven.surefire.booter.BooterConstants.INCLUDES_PROPERTY_PREFIX;
53  import static org.apache.maven.surefire.booter.BooterConstants.ISTRIMSTACKTRACE;
54  import static org.apache.maven.surefire.booter.BooterConstants.MAIN_CLI_OPTIONS;
55  import static org.apache.maven.surefire.booter.BooterConstants.PLUGIN_PID;
56  import static org.apache.maven.surefire.booter.BooterConstants.PROVIDER_CONFIGURATION;
57  import static org.apache.maven.surefire.booter.BooterConstants.REPORTSDIRECTORY;
58  import static org.apache.maven.surefire.booter.BooterConstants.REQUESTEDTEST;
59  import static org.apache.maven.surefire.booter.BooterConstants.RERUN_FAILING_TESTS_COUNT;
60  import static org.apache.maven.surefire.booter.BooterConstants.RUN_ORDER;
61  import static org.apache.maven.surefire.booter.BooterConstants.RUN_STATISTICS_FILE;
62  import static org.apache.maven.surefire.booter.BooterConstants.SHUTDOWN;
63  import static org.apache.maven.surefire.booter.BooterConstants.SOURCE_DIRECTORY;
64  import static org.apache.maven.surefire.booter.BooterConstants.SPECIFIC_TEST_PROPERTY_PREFIX;
65  import static org.apache.maven.surefire.booter.BooterConstants.SYSTEM_EXIT_TIMEOUT;
66  import static org.apache.maven.surefire.booter.BooterConstants.TEST_CLASSES_DIRECTORY;
67  import static org.apache.maven.surefire.booter.BooterConstants.TEST_SUITE_XML_FILES;
68  import static org.apache.maven.surefire.booter.BooterConstants.TESTARTIFACT_CLASSIFIER;
69  import static org.apache.maven.surefire.booter.BooterConstants.TESTARTIFACT_VERSION;
70  import static org.apache.maven.surefire.booter.BooterConstants.USEMANIFESTONLYJAR;
71  import static org.apache.maven.surefire.booter.BooterConstants.USESYSTEMCLASSLOADER;
72  import static org.apache.maven.surefire.booter.SystemPropertyManager.writePropertiesFile;
73  
74  /**
75   * Knows how to serialize and deserialize the booter configuration.
76   * <br>
77   * The internal serialization format is through a properties file. The long-term goal of this
78   * class is not to expose this implementation information to its clients. This still leaks somewhat,
79   * and there are some cases where properties are being accessed as "Properties" instead of
80   * more representative domain objects.
81   * <br>
82   *
83   * @author Jason van Zyl
84   * @author Emmanuel Venisse
85   * @author Brett Porter
86   * @author Dan Fabulich
87   * @author Kristian Rosenvold
88   */
89  class BooterSerializer
90  {
91      private final ForkConfiguration forkConfiguration;
92  
93      BooterSerializer( ForkConfiguration forkConfiguration )
94      {
95          this.forkConfiguration = forkConfiguration;
96      }
97  
98      /**
99       * Does not modify sourceProperties
100      */
101     File serialize( KeyValueSource sourceProperties, ProviderConfiguration booterConfiguration,
102                     StartupConfiguration providerConfiguration, Object testSet, boolean readTestsFromInStream,
103                     Long pid, int forkNumber )
104         throws IOException
105     {
106         SurefireProperties properties = new SurefireProperties( sourceProperties );
107 
108         properties.setProperty( PLUGIN_PID, pid );
109 
110         AbstractPathConfiguration cp = providerConfiguration.getClasspathConfiguration();
111         properties.setClasspath( CLASSPATH, cp.getTestClasspath() );
112         properties.setClasspath( SUREFIRE_CLASSPATH, cp.getProviderClasspath() );
113         properties.setProperty( ENABLE_ASSERTIONS, toString( cp.isEnableAssertions() ) );
114         properties.setProperty( CHILD_DELEGATION, toString( cp.isChildDelegation() ) );
115 
116         TestArtifactInfo testNg = booterConfiguration.getTestArtifact();
117         if ( testNg != null )
118         {
119             properties.setProperty( TESTARTIFACT_VERSION, testNg.getVersion() );
120             properties.setNullableProperty( TESTARTIFACT_CLASSIFIER, testNg.getClassifier() );
121         }
122 
123         properties.setProperty( FORKTESTSET_PREFER_TESTS_FROM_IN_STREAM, readTestsFromInStream );
124         properties.setNullableProperty( FORKTESTSET, getTypeEncoded( testSet ) );
125 
126         TestRequest testSuiteDefinition = booterConfiguration.getTestSuiteDefinition();
127         if ( testSuiteDefinition != null )
128         {
129             properties.setProperty( SOURCE_DIRECTORY, testSuiteDefinition.getTestSourceDirectory() );
130             if ( testSet instanceof File )
131             {
132                 properties.addList( Collections.singletonList( (File) testSet ), TEST_SUITE_XML_FILES );
133             }
134             else
135             {
136                 properties.addList( testSuiteDefinition.getSuiteXmlFiles(), TEST_SUITE_XML_FILES );
137             }
138             TestListResolver testFilter = testSuiteDefinition.getTestListResolver();
139             properties.setProperty( REQUESTEDTEST, testFilter == null ? "" : testFilter.getPluginParameterTest() );
140             int rerunFailingTestsCount = testSuiteDefinition.getRerunFailingTestsCount();
141             properties.setNullableProperty( RERUN_FAILING_TESTS_COUNT, toString( rerunFailingTestsCount ) );
142         }
143 
144         DirectoryScannerParameters directoryScannerParameters = booterConfiguration.getDirScannerParams();
145         if ( directoryScannerParameters != null )
146         {
147             properties.setProperty( FAILIFNOTESTS, toString( directoryScannerParameters.isFailIfNoTests() ) );
148             properties.addList( directoryScannerParameters.getIncludes(), INCLUDES_PROPERTY_PREFIX );
149             properties.addList( directoryScannerParameters.getExcludes(), EXCLUDES_PROPERTY_PREFIX );
150             properties.addList( directoryScannerParameters.getSpecificTests(), SPECIFIC_TEST_PROPERTY_PREFIX );
151             properties.setProperty( TEST_CLASSES_DIRECTORY, directoryScannerParameters.getTestClassesDirectory() );
152         }
153 
154         final RunOrderParameters runOrderParameters = booterConfiguration.getRunOrderParameters();
155         if ( runOrderParameters != null )
156         {
157             properties.setProperty( RUN_ORDER, RunOrder.asString( runOrderParameters.getRunOrder() ) );
158             properties.setProperty( RUN_STATISTICS_FILE, runOrderParameters.getRunStatisticsFile() );
159         }
160 
161         ReporterConfiguration reporterConfiguration = booterConfiguration.getReporterConfiguration();
162         boolean rep = reporterConfiguration.isTrimStackTrace();
163         File reportsDirectory = replaceForkThreadsInPath( reporterConfiguration.getReportsDirectory(), forkNumber );
164         properties.setProperty( ISTRIMSTACKTRACE, rep );
165         properties.setProperty( REPORTSDIRECTORY, reportsDirectory );
166         ClassLoaderConfiguration classLoaderConfig = providerConfiguration.getClassLoaderConfiguration();
167         properties.setProperty( USESYSTEMCLASSLOADER, toString( classLoaderConfig.isUseSystemClassLoader() ) );
168         properties.setProperty( USEMANIFESTONLYJAR, toString( classLoaderConfig.isUseManifestOnlyJar() ) );
169         properties.setProperty( FAILIFNOTESTS, toString( booterConfiguration.isFailIfNoTests() ) );
170         properties.setProperty( PROVIDER_CONFIGURATION, providerConfiguration.getProviderClassName() );
171         properties.setProperty( FAIL_FAST_COUNT, toString( booterConfiguration.getSkipAfterFailureCount() ) );
172         properties.setProperty( SHUTDOWN, booterConfiguration.getShutdown().name() );
173         List<CommandLineOption> mainCliOptions = booterConfiguration.getMainCliOptions();
174         if ( mainCliOptions != null )
175         {
176             properties.addList( mainCliOptions, MAIN_CLI_OPTIONS );
177         }
178         properties.setNullableProperty( SYSTEM_EXIT_TIMEOUT, toString( booterConfiguration.getSystemExitTimeout() ) );
179 
180         File surefireTmpDir = forkConfiguration.getTempDirectory();
181         boolean debug = forkConfiguration.isDebug();
182         return writePropertiesFile( properties, surefireTmpDir, "surefire", debug );
183     }
184 
185     private static String getTypeEncoded( Object value )
186     {
187         if ( value == null )
188         {
189             return null;
190         }
191         String valueToUse = value instanceof Class ? ( (Class<?>) value ).getName() : value.toString();
192         return value.getClass().getName() + "|" + valueToUse;
193     }
194 
195     private static String toString( Object o )
196     {
197         return String.valueOf( o );
198     }
199 }