1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.woopi.ant.taskdefs.junit;
19 import java.lang.reflect.Method;
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22
23 /***
24 * Work around for some changes to the public JUnit API between
25 * different JUnit releases.
26 *
27 * @version $Revision: 1.10.2.4 $
28 */
29 public class JUnitVersionHelper {
30
31 private static Method testCaseName = null;
32 static {
33 try {
34 testCaseName = TestCase.class.getMethod("getName", new Class[0]);
35 } catch (NoSuchMethodException e) {
36
37 try {
38 testCaseName = TestCase.class.getMethod("name", new Class[0]);
39 } catch (NoSuchMethodException e2) {
40
41 }
42 }
43 }
44
45 /***
46 * JUnit 3.7 introduces TestCase.getName() and subsequent versions
47 * of JUnit remove the old name() method. This method provides
48 * access to the name of a TestCase via reflection that is
49 * supposed to work with version before and after JUnit 3.7.
50 *
51 * <p>since Ant 1.5.1 this method will invoke "<code>public
52 * String getName()</code>" on any implementation of Test if
53 * it exists.</p>
54 */
55 public static String getTestCaseName(Test t) {
56 if (t instanceof TestCase && testCaseName != null) {
57 try {
58 return (String) testCaseName.invoke(t, new Object[0]);
59 } catch (Throwable e) {
60
61 }
62 } else {
63 try {
64 Method getNameMethod = null;
65 try {
66 getNameMethod =
67 t.getClass().getMethod("getName", new Class [0]);
68 } catch (NoSuchMethodException e) {
69 getNameMethod = t.getClass().getMethod("name",
70 new Class [0]);
71 }
72 if (getNameMethod != null
73 && getNameMethod.getReturnType() == String.class) {
74 return (String) getNameMethod.invoke(t, new Object[0]);
75 }
76 } catch (Throwable e) {
77
78 }
79 }
80 return "unknown";
81 }
82
83 }