View Javadoc
1   package org.apache.maven.surefire.api.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 org.apache.maven.surefire.api.cli.CommandLineOption;
23  import org.apache.maven.surefire.api.provider.CommandChainReader;
24  import org.apache.maven.surefire.api.provider.ProviderParameters;
25  import org.apache.maven.surefire.api.report.ConsoleStream;
26  import org.apache.maven.surefire.api.report.DefaultDirectConsoleReporter;
27  import org.apache.maven.surefire.api.report.ReporterConfiguration;
28  import org.apache.maven.surefire.api.report.ReporterFactory;
29  import org.apache.maven.surefire.api.testset.DirectoryScannerParameters;
30  import org.apache.maven.surefire.api.testset.RunOrderParameters;
31  import org.apache.maven.surefire.api.testset.TestArtifactInfo;
32  import org.apache.maven.surefire.api.testset.TestRequest;
33  import org.apache.maven.surefire.api.util.DefaultDirectoryScanner;
34  import org.apache.maven.surefire.api.util.DefaultRunOrderCalculator;
35  import org.apache.maven.surefire.api.util.DefaultScanResult;
36  import org.apache.maven.surefire.api.util.DirectoryScanner;
37  import org.apache.maven.surefire.api.util.RunOrderCalculator;
38  import org.apache.maven.surefire.api.util.ScanResult;
39  
40  import java.util.Collections;
41  import java.util.List;
42  import java.util.Map;
43  
44  import static java.util.Collections.emptyList;
45  
46  /**
47   * @author Kristian Rosenvold
48   */
49  public class BaseProviderFactory
50      implements ProviderParameters
51  {
52      private final boolean insideFork;
53  
54      private ReporterFactory reporterFactory;
55  
56      private MasterProcessChannelEncoder masterProcessChannelEncoder;
57  
58      private List<CommandLineOption> mainCliOptions = emptyList();
59  
60      private Map<String, String> providerProperties;
61  
62      private DirectoryScannerParameters directoryScannerParameters;
63  
64      private ReporterConfiguration reporterConfiguration;
65  
66      private RunOrderParameters runOrderParameters;
67  
68      private ClassLoader testClassLoader;
69  
70      private TestRequest testRequest;
71  
72      private TestArtifactInfo testArtifactInfo;
73  
74      private int skipAfterFailureCount;
75  
76      private Integer systemExitTimeout;
77  
78      private CommandChainReader commandReader;
79  
80      public BaseProviderFactory( boolean insideFork )
81      {
82          this.insideFork = insideFork;
83      }
84  
85      @Override
86      public CommandChainReader getCommandReader()
87      {
88          return commandReader;
89      }
90  
91      public void setCommandReader( CommandChainReader commandReader )
92      {
93          this.commandReader = commandReader;
94      }
95  
96      @Override
97      @Deprecated
98      public DirectoryScanner getDirectoryScanner()
99      {
100         return directoryScannerParameters == null
101                 ? null : new DefaultDirectoryScanner( directoryScannerParameters.getTestClassesDirectory(),
102                                             directoryScannerParameters.getIncludes(),
103                                             directoryScannerParameters.getExcludes(),
104                                             directoryScannerParameters.getSpecificTests() );
105     }
106 
107     @Override
108     public ScanResult getScanResult()
109     {
110         return DefaultScanResult.from( providerProperties );
111     }
112 
113     private int getThreadCount()
114     {
115         final String threadcount = providerProperties.get( ProviderParameterNames.THREADCOUNT_PROP );
116         return threadcount == null ? 1 : Math.max( Integer.parseInt( threadcount ), 1 );
117     }
118 
119     @Override
120     public RunOrderCalculator getRunOrderCalculator()
121     {
122         return directoryScannerParameters == null
123                 ? null : new DefaultRunOrderCalculator( runOrderParameters, getThreadCount() );
124     }
125 
126     public void setReporterFactory( ReporterFactory reporterFactory )
127     {
128         this.reporterFactory = reporterFactory;
129     }
130 
131     @Override
132     public ReporterFactory getReporterFactory()
133     {
134         return reporterFactory;
135     }
136 
137     public void setDirectoryScannerParameters( DirectoryScannerParameters directoryScannerParameters )
138     {
139         this.directoryScannerParameters = directoryScannerParameters;
140     }
141 
142     public void setReporterConfiguration( ReporterConfiguration reporterConfiguration )
143     {
144         this.reporterConfiguration = reporterConfiguration;
145     }
146 
147     public void setClassLoaders( ClassLoader testClassLoader )
148     {
149         this.testClassLoader = testClassLoader;
150     }
151 
152     @Override
153     public ConsoleStream getConsoleLogger()
154     {
155         return insideFork
156             ? new ForkingRunListener( masterProcessChannelEncoder, reporterConfiguration.isTrimStackTrace() )
157             : new DefaultDirectConsoleReporter( reporterConfiguration.getOriginalSystemOut() );
158     }
159 
160     public void setTestRequest( TestRequest testRequest )
161     {
162         this.testRequest = testRequest;
163     }
164 
165     @Override
166     public DirectoryScannerParameters getDirectoryScannerParameters()
167     {
168         return directoryScannerParameters;
169     }
170 
171     @Override
172     public ReporterConfiguration getReporterConfiguration()
173     {
174         return reporterConfiguration;
175     }
176 
177     @Override
178     public TestRequest getTestRequest()
179     {
180         return testRequest;
181     }
182 
183     @Override
184     public ClassLoader getTestClassLoader()
185     {
186         return testClassLoader;
187     }
188 
189     public void setProviderProperties( Map<String, String> providerProperties )
190     {
191         this.providerProperties = providerProperties;
192     }
193 
194     @Override
195     public Map<String, String> getProviderProperties()
196     {
197         return providerProperties;
198     }
199 
200     @Override
201     public TestArtifactInfo getTestArtifactInfo()
202     {
203         return testArtifactInfo;
204     }
205 
206     public void setTestArtifactInfo( TestArtifactInfo testArtifactInfo )
207     {
208         this.testArtifactInfo = testArtifactInfo;
209     }
210 
211     public void setRunOrderParameters( RunOrderParameters runOrderParameters )
212     {
213         this.runOrderParameters = runOrderParameters;
214     }
215 
216     @Override
217     public List<CommandLineOption> getMainCliOptions()
218     {
219         return mainCliOptions;
220     }
221 
222     /**
223      * CLI options in plugin (main) JVM process.
224      *
225      * @param mainCliOptions options
226      */
227     public void setMainCliOptions( List<CommandLineOption> mainCliOptions )
228     {
229         this.mainCliOptions = mainCliOptions == null ? Collections.<CommandLineOption>emptyList() : mainCliOptions;
230     }
231 
232     @Override
233     public int getSkipAfterFailureCount()
234     {
235         return skipAfterFailureCount;
236     }
237 
238     /**
239      * See the plugin configuration parameter "skipAfterFailureCount".
240      *
241      * @param skipAfterFailureCount the value in config parameter "skipAfterFailureCount"
242      */
243     public void setSkipAfterFailureCount( int skipAfterFailureCount )
244     {
245         this.skipAfterFailureCount = skipAfterFailureCount;
246     }
247 
248     @Override
249     public boolean isInsideFork()
250     {
251         return insideFork;
252     }
253 
254     @Override
255     public Integer getSystemExitTimeout()
256     {
257         return systemExitTimeout;
258     }
259 
260     public void setSystemExitTimeout( Integer systemExitTimeout )
261     {
262         this.systemExitTimeout = systemExitTimeout;
263     }
264 
265     public void setForkedChannelEncoder( MasterProcessChannelEncoder masterProcessChannelEncoder )
266     {
267         this.masterProcessChannelEncoder = masterProcessChannelEncoder;
268     }
269 }