1 package org.apache.maven.surefire.common.junit3;
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.Constructor;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.lang.reflect.Modifier;
26 import org.apache.maven.surefire.util.ReflectionUtils;
27
28
29
30
31
32 public final class JUnit3Reflector
33 {
34 private static final String TEST_CASE = "junit.framework.Test";
35
36 private static final String TEST_RESULT = "junit.framework.TestResult";
37
38 private static final String TEST_LISTENER = "junit.framework.TestListener";
39
40 private static final String TEST = "junit.framework.Test";
41
42 private static final String ADD_LISTENER_METHOD = "addListener";
43
44 private static final String RUN_METHOD = "run";
45
46 private static final String TEST_SUITE = "junit.framework.TestSuite";
47
48 private final Class[] interfacesImplementedByDynamicProxy;
49
50 private final Class<?> testResultClass;
51
52 private final Method addListenerMethod;
53
54 private final Method testInterfaceRunMethod;
55
56 private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
57
58 private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
59
60 private final Class<?> testInterface;
61
62 private final Class<?> testCase;
63
64 private final Constructor testsSuiteConstructor;
65
66 public JUnit3Reflector( ClassLoader testClassLoader )
67 {
68 testResultClass = ReflectionUtils.tryLoadClass( testClassLoader, TEST_RESULT );
69 testCase = ReflectionUtils.tryLoadClass( testClassLoader, TEST_CASE );
70 testInterface = ReflectionUtils.tryLoadClass( testClassLoader, TEST );
71 interfacesImplementedByDynamicProxy =
72 new Class[]{ ReflectionUtils.tryLoadClass( testClassLoader, TEST_LISTENER ) };
73 Class<?>[] constructorParamTypes = { Class.class };
74
75 Class<?> testSuite = ReflectionUtils.tryLoadClass( testClassLoader, TEST_SUITE );
76
77
78
79 Class[] addListenerParamTypes = interfacesImplementedByDynamicProxy;
80
81 if ( isJUnit3Available() )
82 {
83 testsSuiteConstructor = ReflectionUtils.getConstructor( testSuite, constructorParamTypes );
84 addListenerMethod = tryGetMethod( testResultClass, ADD_LISTENER_METHOD, addListenerParamTypes );
85 testInterfaceRunMethod = getMethod( testInterface, RUN_METHOD, testResultClass );
86 }
87 else
88 {
89 testsSuiteConstructor = null;
90 addListenerMethod = null;
91 testInterfaceRunMethod = null;
92 }
93 }
94
95
96 private static Method tryGetMethod( Class<?> clazz, String methodName, Class<?>... parameters )
97 {
98 try
99 {
100 return clazz.getMethod( methodName, parameters );
101 }
102 catch ( NoSuchMethodException e )
103 {
104 return null;
105 }
106 }
107
108 private static Method getMethod( Class<?> clazz, String methodName, Class<?>... parameters )
109 {
110 try
111 {
112 return clazz.getMethod( methodName, parameters );
113 }
114 catch ( NoSuchMethodException e )
115 {
116 throw new RuntimeException( "When finding method " + methodName, e );
117 }
118 }
119
120
121 public Object constructTestObject( Class testClass )
122 throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException
123 {
124 Object testObject = createInstanceFromSuiteMethod( testClass );
125
126 if ( testObject == null && testCase.isAssignableFrom( testClass ) )
127 {
128 testObject = testsSuiteConstructor.newInstance( testClass );
129 }
130
131 if ( testObject == null )
132 {
133 Constructor testConstructor = getTestConstructor( testClass );
134
135 if ( testConstructor.getParameterTypes().length == 0 )
136 {
137 testObject = testConstructor.newInstance( EMPTY_OBJECT_ARRAY );
138 }
139 else
140 {
141 testObject = testConstructor.newInstance( testClass.getName() );
142 }
143 }
144 return testObject;
145 }
146
147 private static Object createInstanceFromSuiteMethod( Class<?> testClass )
148 throws IllegalAccessException, InvocationTargetException
149 {
150 Object testObject = null;
151 try
152 {
153 Method suiteMethod = testClass.getMethod( "suite", EMPTY_CLASS_ARRAY );
154
155 if ( Modifier.isPublic( suiteMethod.getModifiers() ) && Modifier.isStatic( suiteMethod.getModifiers() ) )
156 {
157 testObject = suiteMethod.invoke( null, EMPTY_CLASS_ARRAY );
158 }
159 }
160 catch ( NoSuchMethodException e )
161 {
162
163 }
164 return testObject;
165 }
166
167 private static Constructor getTestConstructor( Class<?> testClass )
168 throws NoSuchMethodException
169 {
170 try
171 {
172 return testClass.getConstructor( String.class );
173 }
174 catch ( NoSuchMethodException e )
175 {
176 return testClass.getConstructor( EMPTY_CLASS_ARRAY );
177 }
178 }
179
180 public Class[] getInterfacesImplementedByDynamicProxy()
181 {
182 return interfacesImplementedByDynamicProxy;
183 }
184
185 public Class<?> getTestResultClass()
186 {
187 return testResultClass;
188 }
189
190 public Method getAddListenerMethod()
191 {
192 return addListenerMethod;
193 }
194
195 public Method getTestInterfaceRunMethod()
196 {
197 return testInterfaceRunMethod;
198 }
199
200 public Class<?> getTestInterface()
201 {
202 return testInterface;
203 }
204
205 public Method getRunMethod( Class<?> testClass )
206 {
207 return getMethod( testClass, RUN_METHOD, getTestResultClass() );
208 }
209
210 public boolean isJUnit3Available()
211 {
212 return testResultClass != null;
213 }
214 }