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  
28  import org.apache.maven.surefire.api.booter.Shutdown;
29  import org.apache.maven.surefire.api.report.ReporterConfiguration;
30  import org.apache.maven.surefire.api.testset.DirectoryScannerParameters;
31  import org.apache.maven.surefire.api.testset.RunOrderParameters;
32  import org.apache.maven.surefire.api.testset.TestArtifactInfo;
33  import org.apache.maven.surefire.api.testset.TestListResolver;
34  import org.apache.maven.surefire.api.testset.TestRequest;
35  
36  // CHECKSTYLE_OFF: imports
37  import javax.annotation.Nonnull;
38  
39  import static org.apache.maven.surefire.booter.BooterConstants.*;
40  import static org.apache.maven.surefire.api.cli.CommandLineOption.*;
41  
42  /**
43   * Knows how to serialize and deserialize the booter configuration.
44   * <br>
45   * The internal serialization format is through a properties file. The long-term goal of this
46   * class is not to expose this implementation information to its clients. This still leaks somewhat,
47   * and there are some cases where properties are being accessed as "Properties" instead of
48   * more representative domain objects.
49   * <br>
50   *
51   * @author Jason van Zyl
52   * @author Emmanuel Venisse
53   * @author Kristian Rosenvold
54   */
55  public class BooterDeserializer
56  {
57      private final PropertiesWrapper properties;
58  
59      public BooterDeserializer( InputStream inputStream )
60          throws IOException
61      {
62          properties = SystemPropertyManager.loadProperties( inputStream );
63      }
64  
65      public int getForkNumber()
66      {
67          return properties.getIntProperty( FORK_NUMBER );
68      }
69  
70      /**
71       * Describes the current connection channel used by the client in the forked JVM
72       * in order to connect to the plugin process.
73       *
74       * @return connection string (must not be null)
75       */
76      @Nonnull
77      public String getConnectionString()
78      {
79          return properties.getProperty( FORK_NODE_CONNECTION_STRING );
80      }
81  
82      /**
83       * @return PID of Maven process where plugin is executed; or null if PID could not be determined.
84       */
85      public String getPluginPid()
86      {
87          return properties.getProperty( PLUGIN_PID );
88      }
89  
90      public ProviderConfiguration deserialize()
91      {
92          final File reportsDirectory = new File( properties.getProperty( REPORTSDIRECTORY ) );
93          final String testNgVersion = properties.getProperty( TESTARTIFACT_VERSION );
94          final String testArtifactClassifier = properties.getProperty( TESTARTIFACT_CLASSIFIER );
95  
96          final TypeEncodedValue typeEncodedTestForFork = properties.getTypeEncodedValue( FORKTESTSET );
97          final boolean preferTestsFromInStream =
98              properties.getBooleanProperty( FORKTESTSET_PREFER_TESTS_FROM_IN_STREAM );
99  
100         final String requestedTest = properties.getProperty( REQUESTEDTEST );
101         final File sourceDirectory = properties.getFileProperty( SOURCE_DIRECTORY );
102 
103         final List<String> excludes = properties.getStringList( EXCLUDES_PROPERTY_PREFIX );
104         final List<String> includes = properties.getStringList( INCLUDES_PROPERTY_PREFIX );
105         final List<String> specificTests = properties.getStringList( SPECIFIC_TEST_PROPERTY_PREFIX );
106 
107         final List<String> testSuiteXmlFiles = properties.getStringList( TEST_SUITE_XML_FILES );
108         final File testClassesDirectory = properties.getFileProperty( TEST_CLASSES_DIRECTORY );
109         final String runOrder = properties.getProperty( RUN_ORDER );
110         final Long runOrderRandomSeed = properties.getLongProperty( RUN_ORDER_RANDOM_SEED );
111         final String runStatisticsFile = properties.getProperty( RUN_STATISTICS_FILE );
112 
113         final int rerunFailingTestsCount = properties.getIntProperty( RERUN_FAILING_TESTS_COUNT );
114 
115         DirectoryScannerParameters dirScannerParams =
116             new DirectoryScannerParameters( testClassesDirectory, includes, excludes, specificTests, runOrder );
117 
118         RunOrderParameters runOrderParameters
119                 = new RunOrderParameters( runOrder, runStatisticsFile == null ? null : new File( runStatisticsFile ),
120                                           runOrderRandomSeed );
121 
122         TestArtifactInfo testNg = new TestArtifactInfo( testNgVersion, testArtifactClassifier );
123         TestRequest testSuiteDefinition =
124             new TestRequest( testSuiteXmlFiles, sourceDirectory, new TestListResolver( requestedTest ),
125                              rerunFailingTestsCount );
126 
127         ReporterConfiguration reporterConfiguration =
128             new ReporterConfiguration( reportsDirectory, properties.getBooleanProperty( ISTRIMSTACKTRACE ) );
129 
130         Collection<String> cli = properties.getStringList( MAIN_CLI_OPTIONS );
131 
132         int failFastCount = properties.getIntProperty( FAIL_FAST_COUNT );
133 
134         Shutdown shutdown = Shutdown.valueOf( properties.getProperty( SHUTDOWN ) );
135 
136         String systemExitTimeoutAsString = properties.getProperty( SYSTEM_EXIT_TIMEOUT );
137         Integer systemExitTimeout =
138                 systemExitTimeoutAsString == null ? null : Integer.valueOf( systemExitTimeoutAsString );
139 
140         return new ProviderConfiguration( dirScannerParams, runOrderParameters, reporterConfiguration, testNg,
141                                           testSuiteDefinition, properties.getProperties(), typeEncodedTestForFork,
142                                           preferTestsFromInStream, fromStrings( cli ), failFastCount, shutdown,
143                                           systemExitTimeout );
144     }
145 
146     public StartupConfiguration getStartupConfiguration()
147     {
148         boolean useSystemClassLoader = properties.getBooleanProperty( USESYSTEMCLASSLOADER );
149         boolean useManifestOnlyJar = properties.getBooleanProperty( USEMANIFESTONLYJAR );
150         String providerConfiguration = properties.getProperty( PROVIDER_CONFIGURATION );
151 
152         ClassLoaderConfiguration classLoaderConfiguration =
153             new ClassLoaderConfiguration( useSystemClassLoader, useManifestOnlyJar );
154 
155         ClasspathConfiguration classpathConfiguration = new ClasspathConfiguration( properties );
156 
157         String processChecker = properties.getProperty( PROCESS_CHECKER );
158         ProcessCheckerType processCheckerType = ProcessCheckerType.toEnum( processChecker );
159 
160         return StartupConfiguration.inForkedVm( providerConfiguration, classpathConfiguration,
161                                                 classLoaderConfiguration, processCheckerType );
162     }
163 }