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