View Javadoc
1   package org.apache.maven.surefire.common.junit3;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Reflection facade for JUnit3 classes
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 static final Class[] EMPTY_CLASS_ARRAY = { };
49  
50      private static final Object[] EMPTY_OBJECT_ARRAY = { };
51  
52      private final Class[] interfacesImplementedByDynamicProxy;
53  
54      private final Class<?> testResultClass;
55  
56      private final Method addListenerMethod;
57  
58      private final Method testInterfaceRunMethod;
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          // The interface implemented by the dynamic proxy (TestListener), happens to be
78          // the same as the param types of TestResult.addTestListener
79  
80          if ( isJUnit3Available() )
81          {
82              testsSuiteConstructor = ReflectionUtils.getConstructor( testSuite, constructorParamTypes );
83              addListenerMethod =
84                      tryGetMethod( testResultClass, ADD_LISTENER_METHOD, interfacesImplementedByDynamicProxy );
85              testInterfaceRunMethod = getMethod( testInterface, RUN_METHOD, testResultClass );
86          }
87          else
88          {
89              testsSuiteConstructor = null;
90              addListenerMethod = null;
91              testInterfaceRunMethod = null;
92          }
93      }
94  
95      // Switch to reflectionutils when building with 2.7.2
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_OBJECT_ARRAY );
158             }
159         }
160         catch ( NoSuchMethodException e )
161         {
162             // No suite method
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 }