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      /**
66       * Describes the current connection channel used by the client in the forked JVM
67       * in order to connect to the plugin process.
68       *
69       * @return connection string (must not be null)
70       */
71      @Nonnull
72      public String getConnectionString()
73      {
74          return properties.getProperty( FORK_NODE_CONNECTION_STRING );
75      }
76  
77      /**
78       * @return PID of Maven process where plugin is executed; or null if PID could not be determined.
79       */
80      public String getPluginPid()
81      {
82          return properties.getProperty( PLUGIN_PID );
83      }
84  
85      public ProviderConfiguration deserialize()
86      {
87          final File reportsDirectory = new File( properties.getProperty( REPORTSDIRECTORY ) );
88          final String testNgVersion = properties.getProperty( TESTARTIFACT_VERSION );
89          final String testArtifactClassifier = properties.getProperty( TESTARTIFACT_CLASSIFIER );
90  
91          final TypeEncodedValue typeEncodedTestForFork = properties.getTypeEncodedValue( FORKTESTSET );
92          final boolean preferTestsFromInStream =
93              properties.getBooleanProperty( FORKTESTSET_PREFER_TESTS_FROM_IN_STREAM );
94  
95          final String requestedTest = properties.getProperty( REQUESTEDTEST );
96          final File sourceDirectory = properties.getFileProperty( SOURCE_DIRECTORY );
97  
98          final List<String> excludes = properties.getStringList( EXCLUDES_PROPERTY_PREFIX );
99          final List<String> includes = properties.getStringList( INCLUDES_PROPERTY_PREFIX );
100         final List<String> specificTests = properties.getStringList( SPECIFIC_TEST_PROPERTY_PREFIX );
101 
102         final List<String> testSuiteXmlFiles = properties.getStringList( TEST_SUITE_XML_FILES );
103         final File testClassesDirectory = properties.getFileProperty( TEST_CLASSES_DIRECTORY );
104         final String runOrder = properties.getProperty( RUN_ORDER );
105         final String runStatisticsFile = properties.getProperty( RUN_STATISTICS_FILE );
106 
107         final int rerunFailingTestsCount = properties.getIntProperty( RERUN_FAILING_TESTS_COUNT );
108 
109         DirectoryScannerParameters dirScannerParams =
110             new DirectoryScannerParameters( testClassesDirectory, includes, excludes, specificTests,
111                                             properties.getBooleanProperty( FAILIFNOTESTS ), runOrder );
112 
113         RunOrderParameters runOrderParameters
114                 = new RunOrderParameters( runOrder, runStatisticsFile == null ? null : new File( runStatisticsFile ) );
115 
116         TestArtifactInfo testNg = new TestArtifactInfo( testNgVersion, testArtifactClassifier );
117         TestRequest testSuiteDefinition =
118             new TestRequest( testSuiteXmlFiles, sourceDirectory, new TestListResolver( requestedTest ),
119                              rerunFailingTestsCount );
120 
121         ReporterConfiguration reporterConfiguration =
122             new ReporterConfiguration( reportsDirectory, properties.getBooleanProperty( ISTRIMSTACKTRACE ) );
123 
124         Collection<String> cli = properties.getStringList( MAIN_CLI_OPTIONS );
125 
126         int failFastCount = properties.getIntProperty( FAIL_FAST_COUNT );
127 
128         Shutdown shutdown = Shutdown.valueOf( properties.getProperty( SHUTDOWN ) );
129 
130         String systemExitTimeoutAsString = properties.getProperty( SYSTEM_EXIT_TIMEOUT );
131         Integer systemExitTimeout =
132                 systemExitTimeoutAsString == null ? null : Integer.valueOf( systemExitTimeoutAsString );
133 
134         return new ProviderConfiguration( dirScannerParams, runOrderParameters,
135                                           properties.getBooleanProperty( FAILIFNOTESTS ), reporterConfiguration, testNg,
136                                           testSuiteDefinition, properties.getProperties(), typeEncodedTestForFork,
137                                           preferTestsFromInStream, fromStrings( cli ), failFastCount, shutdown,
138                                           systemExitTimeout );
139     }
140 
141     public StartupConfiguration getStartupConfiguration()
142     {
143         boolean useSystemClassLoader = properties.getBooleanProperty( USESYSTEMCLASSLOADER );
144         boolean useManifestOnlyJar = properties.getBooleanProperty( USEMANIFESTONLYJAR );
145         String providerConfiguration = properties.getProperty( PROVIDER_CONFIGURATION );
146 
147         ClassLoaderConfiguration classLoaderConfiguration =
148             new ClassLoaderConfiguration( useSystemClassLoader, useManifestOnlyJar );
149 
150         ClasspathConfiguration classpathConfiguration = new ClasspathConfiguration( properties );
151 
152         String processChecker = properties.getProperty( PROCESS_CHECKER );
153         ProcessCheckerType processCheckerType = ProcessCheckerType.toEnum( processChecker );
154 
155         return StartupConfiguration.inForkedVm( providerConfiguration, classpathConfiguration,
156                                                 classLoaderConfiguration, processCheckerType );
157     }
158 }