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