View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.booter;
20  
21  import javax.annotation.Nonnull;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.Collection;
27  import java.util.List;
28  
29  import org.apache.maven.surefire.api.booter.Shutdown;
30  import org.apache.maven.surefire.api.report.ReporterConfiguration;
31  import org.apache.maven.surefire.api.testset.DirectoryScannerParameters;
32  import org.apache.maven.surefire.api.testset.RunOrderParameters;
33  import org.apache.maven.surefire.api.testset.TestArtifactInfo;
34  import org.apache.maven.surefire.api.testset.TestListResolver;
35  import org.apache.maven.surefire.api.testset.TestRequest;
36  
37  import static org.apache.maven.surefire.api.cli.CommandLineOption.*;
38  import static org.apache.maven.surefire.booter.BooterConstants.*;
39  
40  /**
41   * Knows how to serialize and deserialize the booter configuration.
42   * <br>
43   * The internal serialization format is through a properties file. The long-term goal of this
44   * class is not to expose this implementation information to its clients. This still leaks somewhat,
45   * and there are some cases where properties are being accessed as "Properties" instead of
46   * more representative domain objects.
47   * <br>
48   *
49   * @author Jason van Zyl
50   * @author Emmanuel Venisse
51   * @author Kristian Rosenvold
52   */
53  public class BooterDeserializer {
54      private final PropertiesWrapper properties;
55  
56      public BooterDeserializer(InputStream inputStream) throws IOException {
57          properties = SystemPropertyManager.loadProperties(inputStream);
58      }
59  
60      public int getForkNumber() {
61          return properties.getIntProperty(FORK_NUMBER);
62      }
63  
64      /**
65       * Describes the current connection channel used by the client in the forked JVM
66       * in order to connect to the plugin process.
67       *
68       * @return connection string (must not be null)
69       */
70      @Nonnull
71      public String getConnectionString() {
72          return properties.getProperty(FORK_NODE_CONNECTION_STRING);
73      }
74  
75      /**
76       * @return PID of Maven process where plugin is executed; or null if PID could not be determined.
77       */
78      public String getPluginPid() {
79          return properties.getProperty(PLUGIN_PID);
80      }
81  
82      public ProviderConfiguration deserialize() {
83          final File reportsDirectory = new File(properties.getProperty(REPORTSDIRECTORY));
84          final String testNgVersion = properties.getProperty(TESTARTIFACT_VERSION);
85          final String testArtifactClassifier = properties.getProperty(TESTARTIFACT_CLASSIFIER);
86  
87          final TypeEncodedValue typeEncodedTestForFork = properties.getTypeEncodedValue(FORKTESTSET);
88          final boolean preferTestsFromInStream = properties.getBooleanProperty(FORKTESTSET_PREFER_TESTS_FROM_IN_STREAM);
89  
90          final String requestedTest = properties.getProperty(REQUESTEDTEST);
91          final File sourceDirectory = properties.getFileProperty(SOURCE_DIRECTORY);
92  
93          final List<String> excludes = properties.getStringList(EXCLUDES_PROPERTY_PREFIX);
94          final List<String> includes = properties.getStringList(INCLUDES_PROPERTY_PREFIX);
95          final List<String> specificTests = properties.getStringList(SPECIFIC_TEST_PROPERTY_PREFIX);
96  
97          final File testClassesDirectory = properties.getFileProperty(TEST_CLASSES_DIRECTORY);
98          final String runOrder = properties.getProperty(RUN_ORDER);
99          final Long runOrderRandomSeed = properties.getLongProperty(RUN_ORDER_RANDOM_SEED);
100         final String runStatisticsFile = properties.getProperty(RUN_STATISTICS_FILE);
101 
102         final int rerunFailingTestsCount = properties.getIntProperty(RERUN_FAILING_TESTS_COUNT);
103 
104         DirectoryScannerParameters dirScannerParams =
105                 new DirectoryScannerParameters(testClassesDirectory, includes, excludes, specificTests, runOrder);
106 
107         RunOrderParameters runOrderParameters = new RunOrderParameters(
108                 runOrder, runStatisticsFile == null ? null : new File(runStatisticsFile), runOrderRandomSeed);
109 
110         TestArtifactInfo testNg = new TestArtifactInfo(testNgVersion, testArtifactClassifier);
111         TestRequest testSuiteDefinition =
112                 new TestRequest(sourceDirectory, new TestListResolver(requestedTest), rerunFailingTestsCount);
113 
114         ReporterConfiguration reporterConfiguration =
115                 new ReporterConfiguration(reportsDirectory, properties.getBooleanProperty(ISTRIMSTACKTRACE));
116 
117         Collection<String> cli = properties.getStringList(MAIN_CLI_OPTIONS);
118 
119         int failFastCount = properties.getIntProperty(FAIL_FAST_COUNT);
120 
121         Shutdown shutdown = Shutdown.valueOf(properties.getProperty(SHUTDOWN));
122 
123         String systemExitTimeoutAsString = properties.getProperty(SYSTEM_EXIT_TIMEOUT);
124         Integer systemExitTimeout =
125                 systemExitTimeoutAsString == null ? null : Integer.valueOf(systemExitTimeoutAsString);
126 
127         return new ProviderConfiguration(
128                 dirScannerParams,
129                 runOrderParameters,
130                 reporterConfiguration,
131                 testNg,
132                 testSuiteDefinition,
133                 properties.getProperties(),
134                 typeEncodedTestForFork,
135                 preferTestsFromInStream,
136                 fromStrings(cli),
137                 failFastCount,
138                 shutdown,
139                 systemExitTimeout);
140     }
141 
142     public StartupConfiguration getStartupConfiguration() {
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(
156                 providerConfiguration, classpathConfiguration,
157                 classLoaderConfiguration, processCheckerType);
158     }
159 }