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