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