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      @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      public ScanResult getScanResult()
99      {
100         return DefaultScanResult.from( providerProperties );
101     }
102 
103     private int getThreadCount()
104     {
105         final String threadcount = providerProperties.get( ProviderParameterNames.THREADCOUNT_PROP );
106         return threadcount == null ? 1 : Math.max( Integer.parseInt( threadcount ), 1 );
107     }
108 
109     public RunOrderCalculator getRunOrderCalculator()
110     {
111         return directoryScannerParameters == null
112                 ? null : new DefaultRunOrderCalculator( runOrderParameters, getThreadCount() );
113     }
114 
115     public ReporterFactory getReporterFactory()
116     {
117         return reporterFactory;
118     }
119 
120     public void setDirectoryScannerParameters( DirectoryScannerParameters directoryScannerParameters )
121     {
122         this.directoryScannerParameters = directoryScannerParameters;
123     }
124 
125     public void setReporterConfiguration( ReporterConfiguration reporterConfiguration )
126     {
127         this.reporterConfiguration = reporterConfiguration;
128     }
129 
130     public void setClassLoaders( ClassLoader testClassLoader )
131     {
132         this.testClassLoader = testClassLoader;
133     }
134 
135     public ConsoleStream getConsoleLogger()
136     {
137         boolean trim = reporterConfiguration.isTrimStackTrace();
138         PrintStream out = reporterConfiguration.getOriginalSystemOut();
139         return insideFork ? new ForkingRunListener( out, ROOT_CHANNEL, trim ) : new DefaultDirectConsoleReporter( out );
140     }
141 
142     public void setTestRequest( TestRequest testRequest )
143     {
144         this.testRequest = testRequest;
145     }
146 
147     public DirectoryScannerParameters getDirectoryScannerParameters()
148     {
149         return directoryScannerParameters;
150     }
151 
152     public ReporterConfiguration getReporterConfiguration()
153     {
154         return reporterConfiguration;
155     }
156 
157     public TestRequest getTestRequest()
158     {
159         return testRequest;
160     }
161 
162     public ClassLoader getTestClassLoader()
163     {
164         return testClassLoader;
165     }
166 
167     public void setProviderProperties( Map<String, String> providerProperties )
168     {
169         this.providerProperties = providerProperties;
170     }
171 
172     public Map<String, String> getProviderProperties()
173     {
174         return providerProperties;
175     }
176 
177     public TestArtifactInfo getTestArtifactInfo()
178     {
179         return testArtifactInfo;
180     }
181 
182     public void setTestArtifactInfo( TestArtifactInfo testArtifactInfo )
183     {
184         this.testArtifactInfo = testArtifactInfo;
185     }
186 
187     public void setRunOrderParameters( RunOrderParameters runOrderParameters )
188     {
189         this.runOrderParameters = runOrderParameters;
190     }
191 
192     public List<CommandLineOption> getMainCliOptions()
193     {
194         return mainCliOptions;
195     }
196 
197     public void setMainCliOptions( List<CommandLineOption> mainCliOptions )
198     {
199         this.mainCliOptions = mainCliOptions == null ? Collections.<CommandLineOption>emptyList() : mainCliOptions;
200     }
201 
202     public int getSkipAfterFailureCount()
203     {
204         return skipAfterFailureCount;
205     }
206 
207     public void setSkipAfterFailureCount( int skipAfterFailureCount )
208     {
209         this.skipAfterFailureCount = skipAfterFailureCount;
210     }
211 
212     public boolean isInsideFork()
213     {
214         return insideFork;
215     }
216 
217     public Shutdown getShutdown()
218     {
219         return shutdown;
220     }
221 
222     public void setShutdown( Shutdown shutdown )
223     {
224         this.shutdown = shutdown;
225     }
226 
227     public Integer getSystemExitTimeout()
228     {
229         return systemExitTimeout;
230     }
231 
232     public void setSystemExitTimeout( Integer systemExitTimeout )
233     {
234         this.systemExitTimeout = systemExitTimeout;
235     }
236 }