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