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