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.Properties;
25  import org.apache.maven.surefire.booter.BooterConstants;
26  import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
27  import org.apache.maven.surefire.booter.KeyValueSource;
28  import org.apache.maven.surefire.booter.PropertiesWrapper;
29  import org.apache.maven.surefire.booter.ProviderConfiguration;
30  import org.apache.maven.surefire.booter.StartupConfiguration;
31  import org.apache.maven.surefire.booter.SystemPropertyManager;
32  import org.apache.maven.surefire.report.ReporterConfiguration;
33  import org.apache.maven.surefire.testset.DirectoryScannerParameters;
34  import org.apache.maven.surefire.testset.RunOrderParameters;
35  import org.apache.maven.surefire.testset.TestArtifactInfo;
36  import org.apache.maven.surefire.testset.TestRequest;
37  import org.apache.maven.surefire.util.RunOrder;
38  
39  /**
40   * Knows how to serialize and deserialize the booter configuration.
41   * <p/>
42   * The internal serialization format is through a properties file. The long-term goal of this
43   * class is not to expose this implementation information to its clients. This still leaks somewhat,
44   * and there are some cases where properties are being accessed as "Properties" instead of
45   * more representative domain objects.
46   * <p/>
47   *
48   * @author Jason van Zyl
49   * @author Emmanuel Venisse
50   * @author Brett Porter
51   * @author Dan Fabulich
52   * @author Kristian Rosenvold
53   */
54  class BooterSerializer
55  {
56      private final ForkConfiguration forkConfiguration;
57  
58      public BooterSerializer( ForkConfiguration forkConfiguration )
59      {
60          this.forkConfiguration = forkConfiguration;
61      }
62  
63  
64      /*
65      DOes not modify sourceProperties
66       */
67      public File serialize( KeyValueSource sourceProperties, ProviderConfiguration booterConfiguration,
68                             StartupConfiguration providerConfiguration, Object testSet, boolean readTestsFromInStream )
69          throws IOException
70      {
71  
72          PropertiesWrapper properties = new PropertiesWrapper( new Properties() );
73          sourceProperties.copyTo( properties.getProperties() );
74  
75          providerConfiguration.getClasspathConfiguration().addForkProperties( properties );
76  
77          TestArtifactInfo testNg = booterConfiguration.getTestArtifact();
78          if ( testNg != null )
79          {
80              properties.setProperty( BooterConstants.TESTARTIFACT_VERSION, testNg.getVersion() );
81              properties.setProperty( BooterConstants.TESTARTIFACT_CLASSIFIER, testNg.getClassifier() );
82          }
83  
84          properties.setProperty( BooterConstants.FORKTESTSET_PREFER_TESTS_FROM_IN_STREAM,
85                                  Boolean.valueOf( readTestsFromInStream ) );
86          properties.setProperty( BooterConstants.FORKTESTSET, getTypeEncoded( testSet ) );
87  
88          TestRequest testSuiteDefinition = booterConfiguration.getTestSuiteDefinition();
89          if ( testSuiteDefinition != null )
90          {
91              properties.setProperty( BooterConstants.SOURCE_DIRECTORY, testSuiteDefinition.getTestSourceDirectory() );
92              properties.addList( testSuiteDefinition.getSuiteXmlFiles(), BooterConstants.TEST_SUITE_XML_FILES );
93              properties.setProperty( BooterConstants.REQUESTEDTEST, testSuiteDefinition.getRequestedTest() );
94              properties.setProperty( BooterConstants.REQUESTEDTESTMETHOD, testSuiteDefinition.getRequestedTestMethod() );
95          }
96  
97          DirectoryScannerParameters directoryScannerParameters = booterConfiguration.getDirScannerParams();
98          if ( directoryScannerParameters != null )
99          {
100             properties.setProperty( BooterConstants.FAILIFNOTESTS,
101                                     String.valueOf( directoryScannerParameters.isFailIfNoTests() ) );
102             properties.addList( directoryScannerParameters.getIncludes(), BooterConstants.INCLUDES_PROPERTY_PREFIX );
103             properties.addList( directoryScannerParameters.getExcludes(), BooterConstants.EXCLUDES_PROPERTY_PREFIX );
104             properties.addList( directoryScannerParameters.getSpecificTests(),
105                                 BooterConstants.SPECIFIC_TEST_PROPERTY_PREFIX );
106 
107             properties.setProperty( BooterConstants.TEST_CLASSES_DIRECTORY,
108                                     directoryScannerParameters.getTestClassesDirectory() );
109         }
110 
111         final RunOrderParameters runOrderParameters = booterConfiguration.getRunOrderParameters();
112         if ( runOrderParameters != null )
113         {
114             properties.setProperty( BooterConstants.RUN_ORDER, RunOrder.asString( runOrderParameters.getRunOrder() ) );
115             properties.setProperty( BooterConstants.RUN_STATISTICS_FILE, runOrderParameters.getRunStatisticsFile() );
116         }
117 
118         ReporterConfiguration reporterConfiguration = booterConfiguration.getReporterConfiguration();
119 
120         Boolean rep = reporterConfiguration.isTrimStackTrace();
121         properties.setProperty( BooterConstants.ISTRIMSTACKTRACE, rep );
122         properties.setProperty( BooterConstants.REPORTSDIRECTORY, reporterConfiguration.getReportsDirectory() );
123         ClassLoaderConfiguration classLoaderConfiguration = providerConfiguration.getClassLoaderConfiguration();
124         properties.setProperty( BooterConstants.USESYSTEMCLASSLOADER,
125                                 String.valueOf( classLoaderConfiguration.isUseSystemClassLoader() ) );
126         properties.setProperty( BooterConstants.USEMANIFESTONLYJAR,
127                                 String.valueOf( classLoaderConfiguration.isUseManifestOnlyJar() ) );
128         properties.setProperty( BooterConstants.FAILIFNOTESTS,
129                                 String.valueOf( booterConfiguration.isFailIfNoTests() ) );
130         properties.setProperty( BooterConstants.PROVIDER_CONFIGURATION, providerConfiguration.getProviderClassName() );
131 
132         return SystemPropertyManager.writePropertiesFile( properties.getProperties(),
133                                                           forkConfiguration.getTempDirectory(), "surefire",
134                                                           forkConfiguration.isDebug() );
135     }
136 
137 
138     private String getTypeEncoded( Object value )
139     {
140         if ( value == null )
141         {
142             return null;
143         }
144         String valueToUse;
145         if ( value instanceof Class )
146         {
147             valueToUse = ( (Class<?>) value ).getName();
148         }
149         else
150         {
151             valueToUse = value.toString();
152         }
153         return value.getClass().getName() + "|" + valueToUse;
154     }
155 
156 }