View Javadoc

1   package org.apache.maven.surefire.junit;
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.Iterator;
23  import org.apache.maven.surefire.common.junit3.JUnit3Reflector;
24  import org.apache.maven.surefire.common.junit3.JUnit3TestChecker;
25  import org.apache.maven.surefire.providerapi.AbstractProvider;
26  import org.apache.maven.surefire.providerapi.ProviderParameters;
27  import org.apache.maven.surefire.report.ConsoleOutputCapture;
28  import org.apache.maven.surefire.report.ConsoleOutputReceiver;
29  import org.apache.maven.surefire.report.ReportEntry;
30  import org.apache.maven.surefire.report.ReporterException;
31  import org.apache.maven.surefire.report.ReporterFactory;
32  import org.apache.maven.surefire.report.RunListener;
33  import org.apache.maven.surefire.report.SimpleReportEntry;
34  import org.apache.maven.surefire.suite.RunResult;
35  import org.apache.maven.surefire.testset.TestSetFailedException;
36  import org.apache.maven.surefire.util.ReflectionUtils;
37  import org.apache.maven.surefire.util.RunOrderCalculator;
38  import org.apache.maven.surefire.util.ScanResult;
39  import org.apache.maven.surefire.util.TestsToRun;
40  
41  /**
42   * @author Kristian Rosenvold
43   */
44  public class JUnit3Provider
45      extends AbstractProvider
46  {
47      private final ClassLoader testClassLoader;
48  
49      private final PojoAndJUnit3Checker testChecker;
50  
51      private final JUnit3TestChecker jUnit3TestChecker;
52  
53      private final JUnit3Reflector reflector;
54  
55      private final ProviderParameters providerParameters;
56  
57      private final RunOrderCalculator runOrderCalculator;
58  
59      private TestsToRun testsToRun;
60  
61      private final ScanResult scanResult;
62  
63      public JUnit3Provider( ProviderParameters booterParameters )
64      {
65          this.providerParameters = booterParameters;
66          this.testClassLoader = booterParameters.getTestClassLoader();
67          this.scanResult = booterParameters.getScanResult();
68          this.runOrderCalculator = booterParameters.getRunOrderCalculator();
69          this.reflector = new JUnit3Reflector( testClassLoader );
70          jUnit3TestChecker = new JUnit3TestChecker( testClassLoader );
71          this.testChecker = new PojoAndJUnit3Checker( jUnit3TestChecker ); // Todo; use reflector
72      }
73  
74      public RunResult invoke( Object forkTestSet )
75          throws TestSetFailedException, ReporterException
76      {
77          if ( testsToRun == null )
78          {
79              if ( forkTestSet instanceof TestsToRun )
80              {
81                  testsToRun = (TestsToRun) forkTestSet;
82              }
83              else if ( forkTestSet instanceof Class )
84              {
85                  testsToRun = TestsToRun.fromClass( (Class<?>) forkTestSet );
86              }
87              else
88              {
89                  testsToRun = scanClassPath();
90              }
91          }
92  
93          ReporterFactory reporterFactory = providerParameters.getReporterFactory();
94          final RunListener reporter = reporterFactory.createReporter();
95          ConsoleOutputCapture.startCapture( (ConsoleOutputReceiver) reporter );
96  
97          final String smClassName = System.getProperty( "surefire.security.manager" );
98          if ( smClassName != null )
99          {
100             SecurityManager securityManager =
101                 (SecurityManager) ReflectionUtils.instantiate( this.getClass().getClassLoader(), smClassName );
102             System.setSecurityManager( securityManager );
103         }
104 
105         for ( Class clazz : testsToRun )
106         {
107             SurefireTestSet surefireTestSet = createTestSet( clazz );
108             executeTestSet( surefireTestSet, reporter, testClassLoader );
109         }
110 
111         return reporterFactory.close();
112     }
113 
114     private SurefireTestSet createTestSet( Class<?> clazz )
115         throws TestSetFailedException
116     {
117         return reflector.isJUnit3Available() && jUnit3TestChecker.accept( clazz )
118             ? new JUnitTestSet( clazz, reflector )
119             : new PojoTestSet( clazz );
120 
121     }
122 
123     private void executeTestSet( SurefireTestSet testSet, RunListener reporter, ClassLoader classLoader )
124         throws ReporterException, TestSetFailedException
125     {
126 
127         ReportEntry report = new SimpleReportEntry( this.getClass().getName(), testSet.getName() );
128 
129         reporter.testSetStarting( report );
130 
131         testSet.execute( reporter, classLoader );
132 
133         reporter.testSetCompleted( report );
134     }
135 
136     private TestsToRun scanClassPath()
137     {
138         final TestsToRun testsToRun = scanResult.applyFilter( testChecker, testClassLoader );
139         return runOrderCalculator.orderTestClasses( testsToRun );
140     }
141 
142     public Iterator getSuites()
143     {
144         testsToRun = scanClassPath();
145         return testsToRun.iterator();
146     }
147 }