1 | /*
|
---|
2 | * Copyright 2001-2002,2004-2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs.optional.junit;
|
---|
19 |
|
---|
20 | import java.lang.reflect.Method;
|
---|
21 | import junit.framework.Test;
|
---|
22 | import junit.framework.TestCase;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Work around for some changes to the public JUnit API between
|
---|
26 | * different JUnit releases.
|
---|
27 | *
|
---|
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 | // pre JUnit 3.7
|
---|
37 | try {
|
---|
38 | testCaseName = TestCase.class.getMethod("name", new Class[0]);
|
---|
39 | } catch (NoSuchMethodException e2) {
|
---|
40 | // ignore
|
---|
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 | // ignore
|
---|
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 | // ignore
|
---|
78 | }
|
---|
79 | }
|
---|
80 | return "unknown";
|
---|
81 | }
|
---|
82 |
|
---|
83 | }
|
---|