1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
42
43
44
45
46
47
48
49
50
51
52
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 }