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
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
43
44
45
46
47
48
49
50
51
52
53
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 }