1 | |
package org.apache.maven.surefire.junit; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.lang.reflect.InvocationTargetException; |
23 | |
import java.lang.reflect.Method; |
24 | |
import java.lang.reflect.Proxy; |
25 | |
import org.apache.maven.surefire.common.junit3.JUnit3Reflector; |
26 | |
import org.apache.maven.surefire.report.RunListener; |
27 | |
import org.apache.maven.surefire.testset.TestSetFailedException; |
28 | |
|
29 | |
public final class JUnitTestSet |
30 | |
implements SurefireTestSet |
31 | |
{ |
32 | |
private final Class testClass; |
33 | |
|
34 | |
private final JUnit3Reflector reflector; |
35 | |
|
36 | |
public JUnitTestSet( Class testClass, JUnit3Reflector reflector ) |
37 | |
throws TestSetFailedException |
38 | 1 | { |
39 | 1 | if ( testClass == null ) |
40 | |
{ |
41 | 0 | throw new NullPointerException( "testClass is null" ); |
42 | |
} |
43 | |
|
44 | 1 | this.testClass = testClass; |
45 | 1 | this.reflector = reflector; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 1 | } |
61 | |
|
62 | |
|
63 | |
public void execute( RunListener reporter, ClassLoader loader ) |
64 | |
throws TestSetFailedException |
65 | |
{ |
66 | 1 | Class testClass = getTestClass(); |
67 | |
|
68 | |
try |
69 | |
{ |
70 | 1 | Object testObject = reflector.constructTestObject( testClass ); |
71 | |
final Method runMethod; |
72 | |
|
73 | 1 | if ( this.reflector.getTestInterface().isAssignableFrom( testObject.getClass() ) ) |
74 | |
{ |
75 | 1 | runMethod = this.reflector.getTestInterfaceRunMethod(); |
76 | |
} |
77 | |
else |
78 | |
{ |
79 | 0 | runMethod = reflector.getRunMethod( this.testClass ); |
80 | |
} |
81 | |
|
82 | 1 | Object instanceOfTestResult = reflector.getTestResultClass().newInstance(); |
83 | |
|
84 | 1 | TestListenerInvocationHandler invocationHandler = new TestListenerInvocationHandler( reporter ); |
85 | |
|
86 | 1 | Object testListener = |
87 | |
Proxy.newProxyInstance( loader, reflector.getInterfacesImplementedByDynamicProxy(), invocationHandler ); |
88 | |
|
89 | 1 | Object[] addTestListenerParams = { testListener }; |
90 | |
|
91 | 1 | reflector.getAddListenerMethod().invoke( instanceOfTestResult, addTestListenerParams ); |
92 | |
|
93 | 1 | Object[] runParams = { instanceOfTestResult }; |
94 | |
|
95 | 1 | runMethod.invoke( testObject, runParams ); |
96 | |
} |
97 | 0 | catch ( IllegalArgumentException e ) |
98 | |
{ |
99 | 0 | throw new TestSetFailedException( testClass.getName(), e ); |
100 | |
} |
101 | 0 | catch ( InstantiationException e ) |
102 | |
{ |
103 | 0 | throw new TestSetFailedException( testClass.getName(), e ); |
104 | |
} |
105 | 0 | catch ( IllegalAccessException e ) |
106 | |
{ |
107 | 0 | throw new TestSetFailedException( testClass.getName(), e ); |
108 | |
} |
109 | 0 | catch ( InvocationTargetException e ) |
110 | |
{ |
111 | 0 | throw new TestSetFailedException( testClass.getName(), e.getTargetException() ); |
112 | |
} |
113 | 0 | catch ( NoSuchMethodException e ) |
114 | |
{ |
115 | 0 | throw new TestSetFailedException( "Class is not a JUnit TestCase", e ); |
116 | 1 | } |
117 | 1 | } |
118 | |
|
119 | |
public String getName() |
120 | |
{ |
121 | 0 | return testClass.getName(); |
122 | |
} |
123 | |
|
124 | |
Class getTestClass() |
125 | |
{ |
126 | 1 | return testClass; |
127 | |
} |
128 | |
} |