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 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  // CHECKSTYLE_OFF: imports
43  import static org.apache.maven.surefire.booter.BooterConstants.*;
44  
45  /**
46   * Knows how to serialize and deserialize the booter configuration.
47   * <p/>
48   * The internal serialization format is through a properties file. The long-term goal of this
49   * class is not to expose this implementation information to its clients. This still leaks somewhat,
50   * and there are some cases where properties are being accessed as "Properties" instead of
51   * more representative domain objects.
52   * <p/>
53   *
54   * @author Jason van Zyl
55   * @author Emmanuel Venisse
56   * @author Brett Porter
57   * @author Dan Fabulich
58   * @author Kristian Rosenvold
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       * Does not modify sourceProperties
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 }