01: /*
02: * ========================================================================
03: *
04: * Copyright 2001-2003 The Apache Software Foundation.
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: *
18: * ========================================================================
19: */
20: package org.apache.cactus.internal.util;
21:
22: import java.lang.reflect.Method;
23:
24: import org.apache.cactus.util.ChainedRuntimeException;
25:
26: import junit.framework.Test;
27: import junit.framework.TestCase;
28:
29: /**
30: * Work around for some changes to the public JUnit API between
31: * different JUnit releases.
32: *
33: * @version $Id: JUnitVersionHelper.java 238991 2004-05-22 11:34:50Z vmassol $
34: */
35: public class JUnitVersionHelper {
36: /**
37: * The <code>Method</code> to use to get the test name from a
38: * <code>TestCase</code> object.
39: */
40: private static Method testCaseName = null;
41:
42: static {
43: try {
44: testCaseName = TestCase.class.getMethod("getName",
45: new Class[0]);
46: } catch (NoSuchMethodException e) {
47: // pre JUnit 3.7
48: try {
49: testCaseName = TestCase.class.getMethod("name",
50: new Class[0]);
51: } catch (NoSuchMethodException e2) {
52: throw new ChainedRuntimeException(
53: "Cannot find method name()");
54: }
55: }
56: }
57:
58: /**
59: * JUnit 3.7 introduces TestCase.getName() and subsequent versions
60: * of JUnit remove the old name() method. This method provides
61: * access to the name of a TestCase via reflection that is
62: * supposed to work with version before and after JUnit 3.7.
63: *
64: * @param theTest the test case for which to retrieve the name
65: * @return the test case name
66: */
67: public static String getTestCaseName(Test theTest) {
68: String name;
69:
70: if (theTest instanceof TestCase && (testCaseName != null)) {
71: try {
72: name = (String) testCaseName.invoke(theTest,
73: new Object[0]);
74: } catch (Throwable e) {
75: name = "unknown";
76: }
77: } else {
78: name = "unknown";
79: }
80:
81: return name;
82: }
83: }
|