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