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