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 java.util.Properties;
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  /**
40   * @author Kristian Rosenvold
41   */
42  public class BaseProviderFactory
43      implements DirectoryScannerParametersAware, ReporterConfigurationAware, SurefireClassLoadersAware, TestRequestAware,
44      ProviderPropertiesAware, ProviderParameters, TestArtifactInfoAware, RunOrderParametersAware
45  {
46  
47      private Properties providerProperties;
48  
49      private DirectoryScannerParameters directoryScannerParameters;
50  
51      private ReporterConfiguration reporterConfiguration;
52  
53      private RunOrderParameters runOrderParameters;
54  
55      private ClassLoader testClassLoader;
56  
57      private TestRequest testRequest;
58  
59      private TestArtifactInfo testArtifactInfo;
60  
61      private static final Integer ROOT_CHANNEl = 0;
62  
63  
64      private final ReporterFactory reporterFactory;
65  
66      private final boolean insideFork;
67  
68  
69      public BaseProviderFactory( ReporterFactory reporterFactory, Boolean insideFork )
70      {
71          this.reporterFactory = reporterFactory;
72          this.insideFork = insideFork;
73      }
74  
75      public DirectoryScanner getDirectoryScanner()
76      {
77          if ( directoryScannerParameters == null )
78          {
79              return null;
80          }
81          return new DefaultDirectoryScanner( directoryScannerParameters.getTestClassesDirectory(),
82                                              directoryScannerParameters.getIncludes(),
83                                              directoryScannerParameters.getExcludes(),
84                                              directoryScannerParameters.getSpecificTests() );
85      }
86  
87      public ScanResult getScanResult()
88      {
89          return DefaultScanResult.from( providerProperties );
90      }
91  
92      private int getThreadCount()
93      {
94          final String threadcount = (String) providerProperties.get( ProviderParameterNames.THREADCOUNT_PROP );
95          return threadcount == null ? 1 : Math.max( Integer.parseInt( threadcount ), 1 );
96      }
97  
98      public RunOrderCalculator getRunOrderCalculator()
99      {
100         if ( directoryScannerParameters == null )
101         {
102             return null;
103         }
104         return new DefaultRunOrderCalculator( runOrderParameters, getThreadCount() );
105     }
106 
107     public ReporterFactory getReporterFactory()
108     {
109         return reporterFactory;
110     }
111 
112     public void setDirectoryScannerParameters( DirectoryScannerParameters directoryScannerParameters )
113     {
114         this.directoryScannerParameters = directoryScannerParameters;
115     }
116 
117     public void setReporterConfiguration( ReporterConfiguration reporterConfiguration )
118     {
119         this.reporterConfiguration = reporterConfiguration;
120     }
121 
122     public void setClassLoaders( ClassLoader testClassLoader )
123     {
124         this.testClassLoader = testClassLoader;
125     }
126 
127     public ConsoleLogger getConsoleLogger()
128     {
129         if ( insideFork )
130         {
131             return new ForkingRunListener( reporterConfiguration.getOriginalSystemOut(), ROOT_CHANNEl,
132                                            reporterConfiguration.isTrimStackTrace() );
133         }
134         return 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( Properties providerProperties )
163     {
164         this.providerProperties = providerProperties;
165     }
166 
167     public Properties 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 }