View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.junit;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.lang.reflect.Method;
23  import java.lang.reflect.Proxy;
24  
25  import org.apache.maven.surefire.api.testset.TestSetFailedException;
26  import org.apache.maven.surefire.common.junit3.JUnit3Reflector;
27  
28  /**
29   * JUnit3 test set.
30   */
31  public final class JUnitTestSetExecutor implements SurefireTestSetExecutor {
32      private final JUnit3Reflector reflector;
33  
34      private final JUnit3Reporter reporter;
35  
36      public JUnitTestSetExecutor(JUnit3Reflector reflector, JUnit3Reporter reporter) {
37          this.reflector = reflector;
38          this.reporter = reporter;
39  
40          // ----------------------------------------------------------------------
41          // Strategy for executing JUnit tests
42          //
43          // o look for the suite method and if that is present execute that method
44          //   to get the test object.
45          //
46          // o look for test classes that are assignable from TestCase
47          //
48          // o look for test classes that only implement the Test interface
49          // ----------------------------------------------------------------------
50  
51          // The interface implemented by the dynamic proxy (TestListener), happens to be
52          // the same as the param types of TestResult.addTestListener
53      }
54  
55      @Override
56      public void execute(Class<?> testClass, ClassLoader loader) throws TestSetFailedException {
57          try {
58              Object testObject = reflector.constructTestObject(testClass);
59              final Method runMethod;
60  
61              if (reflector.getTestInterface().isAssignableFrom(testObject.getClass())) {
62                  runMethod = reflector.getTestInterfaceRunMethod();
63              } else {
64                  runMethod = reflector.getRunMethod(testClass);
65              }
66  
67              Object instanceOfTestResult = reflector.getTestResultClass().newInstance();
68  
69              TestListenerInvocationHandler invocationHandler = new TestListenerInvocationHandler(reporter);
70  
71              Object testListener = Proxy.newProxyInstance(
72                      loader, reflector.getInterfacesImplementedByDynamicProxy(), invocationHandler);
73  
74              Object[] addTestListenerParams = {testListener};
75  
76              reflector.getAddListenerMethod().invoke(instanceOfTestResult, addTestListenerParams);
77  
78              Object[] runParams = {instanceOfTestResult};
79  
80              runMethod.invoke(testObject, runParams);
81          } catch (InvocationTargetException e) {
82              throw new TestSetFailedException(testClass.getName(), e.getTargetException());
83          } catch (NoSuchMethodException e) {
84              throw new TestSetFailedException("Class is not a JUnit TestCase", e);
85          } catch (ReflectiveOperationException e) {
86              throw new TestSetFailedException(testClass.getName(), e);
87          }
88      }
89  }