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