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