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