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   */
32  public final class JUnitTestSetExecutor implements SurefireTestSetExecutor {
33      private final JUnit3Reflector reflector;
34  
35      private final JUnit3Reporter reporter;
36  
37      public JUnitTestSetExecutor(JUnit3Reflector reflector, JUnit3Reporter reporter) {
38          this.reflector = reflector;
39          this.reporter = reporter;
40  
41          // ----------------------------------------------------------------------
42          // Strategy for executing JUnit tests
43          //
44          // o look for the suite method and if that is present execute that method
45          //   to get the test object.
46          //
47          // o look for test classes that are assignable from TestCase
48          //
49          // o look for test classes that only implement the Test interface
50          // ----------------------------------------------------------------------
51  
52          // The interface implemented by the dynamic proxy (TestListener), happens to be
53          // the same as the param types of TestResult.addTestListener
54      }
55  
56      @Override
57      public void execute(Class<?> testClass, ClassLoader loader) throws TestSetFailedException {
58          try {
59              Object testObject = reflector.constructTestObject(testClass);
60              final Method runMethod;
61  
62              if (reflector.getTestInterface().isAssignableFrom(testObject.getClass())) {
63                  runMethod = reflector.getTestInterfaceRunMethod();
64              } else {
65                  runMethod = reflector.getRunMethod(testClass);
66              }
67  
68              Object instanceOfTestResult = reflector.getTestResultClass().newInstance();
69  
70              TestListenerInvocationHandler invocationHandler = new TestListenerInvocationHandler(reporter);
71  
72              Object testListener = Proxy.newProxyInstance(
73                      loader, reflector.getInterfacesImplementedByDynamicProxy(), invocationHandler);
74  
75              Object[] addTestListenerParams = {testListener};
76  
77              reflector.getAddListenerMethod().invoke(instanceOfTestResult, addTestListenerParams);
78  
79              Object[] runParams = {instanceOfTestResult};
80  
81              runMethod.invoke(testObject, runParams);
82          } catch (InvocationTargetException e) {
83              throw new TestSetFailedException(testClass.getName(), e.getTargetException());
84          } catch (NoSuchMethodException e) {
85              throw new TestSetFailedException("Class is not a JUnit TestCase", e);
86          } catch (ReflectiveOperationException e) {
87              throw new TestSetFailedException(testClass.getName(), e);
88          }
89      }
90  }