View Javadoc
1   package org.apache.maven.surefire.booter;
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.io.InputStream;
25  import java.util.Collection;
26  import java.util.List;
27  import org.apache.maven.surefire.report.ReporterConfiguration;
28  import org.apache.maven.surefire.testset.DirectoryScannerParameters;
29  import org.apache.maven.surefire.testset.RunOrderParameters;
30  import org.apache.maven.surefire.testset.TestArtifactInfo;
31  import org.apache.maven.surefire.testset.TestListResolver;
32  import org.apache.maven.surefire.testset.TestRequest;
33  
34  // CHECKSTYLE_OFF: imports
35  import static org.apache.maven.surefire.booter.BooterConstants.*;
36  import static org.apache.maven.surefire.cli.CommandLineOption.*;
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 Kristian Rosenvold
50   */
51  public class BooterDeserializer
52  {
53      private final PropertiesWrapper properties;
54  
55      public BooterDeserializer( InputStream inputStream )
56          throws IOException
57      {
58          properties = SystemPropertyManager.loadProperties( inputStream );
59      }
60  
61      public ProviderConfiguration deserialize()
62      {
63          final File reportsDirectory = new File( properties.getProperty( REPORTSDIRECTORY ) );
64          final String testNgVersion = properties.getProperty( TESTARTIFACT_VERSION );
65          final String testArtifactClassifier = properties.getProperty( TESTARTIFACT_CLASSIFIER );
66  
67          final TypeEncodedValue typeEncodedTestForFork = properties.getTypeEncodedValue( FORKTESTSET );
68          final boolean preferTestsFromInStream =
69              properties.getBooleanProperty( FORKTESTSET_PREFER_TESTS_FROM_IN_STREAM );
70  
71          final String requestedTest = properties.getProperty( REQUESTEDTEST );
72          final File sourceDirectory = properties.getFileProperty( SOURCE_DIRECTORY );
73  
74          final List<String> excludes = properties.getStringList( EXCLUDES_PROPERTY_PREFIX );
75          final List<String> includes = properties.getStringList( INCLUDES_PROPERTY_PREFIX );
76          final List<String> specificTests = properties.getStringList( SPECIFIC_TEST_PROPERTY_PREFIX );
77  
78          final List<String> testSuiteXmlFiles = properties.getStringList( TEST_SUITE_XML_FILES );
79          final File testClassesDirectory = properties.getFileProperty( TEST_CLASSES_DIRECTORY );
80          final String runOrder = properties.getProperty( RUN_ORDER );
81          final String runStatisticsFile = properties.getProperty( RUN_STATISTICS_FILE );
82  
83          final int rerunFailingTestsCount = properties.getIntProperty( RERUN_FAILING_TESTS_COUNT );
84  
85          DirectoryScannerParameters dirScannerParams =
86              new DirectoryScannerParameters( testClassesDirectory, includes, excludes, specificTests,
87                                              properties.getBooleanProperty( FAILIFNOTESTS ), runOrder );
88  
89          RunOrderParameters runOrderParameters
90                  = new RunOrderParameters( runOrder, runStatisticsFile == null ? null : new File( runStatisticsFile ) );
91  
92          TestArtifactInfo testNg = new TestArtifactInfo( testNgVersion, testArtifactClassifier );
93          TestRequest testSuiteDefinition =
94              new TestRequest( testSuiteXmlFiles, sourceDirectory, new TestListResolver( requestedTest ),
95                               rerunFailingTestsCount );
96  
97          ReporterConfiguration reporterConfiguration =
98              new ReporterConfiguration( reportsDirectory, properties.getBooleanProperty( ISTRIMSTACKTRACE ) );
99  
100         Collection<String> cli = properties.getStringList( MAIN_CLI_OPTIONS );
101 
102         int failFastCount = properties.getIntProperty( FAIL_FAST_COUNT );
103 
104         Shutdown shutdown = Shutdown.valueOf( properties.getProperty( SHUTDOWN ) );
105 
106         String systemExitTimeoutAsString = properties.getProperty( SYSTEM_EXIT_TIMEOUT );
107         Integer systemExitTimeout =
108                 systemExitTimeoutAsString == null ? null : Integer.valueOf( systemExitTimeoutAsString );
109 
110         return new ProviderConfiguration( dirScannerParams, runOrderParameters,
111                                           properties.getBooleanProperty( FAILIFNOTESTS ), reporterConfiguration, testNg,
112                                           testSuiteDefinition, properties.getProperties(), typeEncodedTestForFork,
113                                           preferTestsFromInStream, fromStrings( cli ), failFastCount, shutdown,
114                                           systemExitTimeout );
115     }
116 
117     public StartupConfiguration getProviderConfiguration()
118     {
119         boolean useSystemClassLoader = properties.getBooleanProperty( USESYSTEMCLASSLOADER );
120         boolean useManifestOnlyJar = properties.getBooleanProperty( USEMANIFESTONLYJAR );
121         String providerConfiguration = properties.getProperty( PROVIDER_CONFIGURATION );
122 
123         ClassLoaderConfiguration classLoaderConfiguration =
124             new ClassLoaderConfiguration( useSystemClassLoader, useManifestOnlyJar );
125 
126         ClasspathConfiguration classpathConfiguration = new ClasspathConfiguration( properties );
127 
128         return StartupConfiguration.inForkedVm( providerConfiguration, classpathConfiguration,
129                                                 classLoaderConfiguration );
130     }
131 }