~ubuntu-branches/ubuntu/maverick/ant/maverick

« back to all changes in this revision

Viewing changes to src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitVersionHelper.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-09-30 14:47:45 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080930144745-0x8uzivd9t15dua3
Tags: 1.7.1-0ubuntu1
* New upstream version (bug fix release).
  - mainly a bugfix release.
  - has extended support for Java6 features.
  - <script> now has support for JavaFX.
  - release notes: http://apache.linux-mirror.org/ant/README.html
* Remove debian/patches/05_ant-bug433444.patch. Obsoleted.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
/**
26
26
 * Work around for some changes to the public JUnit API between
27
27
 * different JUnit releases.
 
28
 * @since Ant 1.7
28
29
 */
 
30
// CheckStyle:HideUtilityClassConstructorCheck OFF (bc)
29
31
public class JUnitVersionHelper {
30
32
 
31
33
    private static Method testCaseName = null;
 
34
 
 
35
    /**
 
36
     * Name of the JUnit4 class we look for.
 
37
     * {@value}
 
38
     * @since Ant 1.7.1
 
39
     */
 
40
    public static final String JUNIT_FRAMEWORK_JUNIT4_TEST_CASE_FACADE
 
41
        = "junit.framework.JUnit4TestCaseFacade";
 
42
    private static final String UNKNOWN_TEST_CASE_NAME = "unknown";
 
43
 
32
44
    static {
33
45
        try {
34
46
            testCaseName = TestCase.class.getMethod("getName", new Class[0]);
36
48
            // pre JUnit 3.7
37
49
            try {
38
50
                testCaseName = TestCase.class.getMethod("name", new Class[0]);
39
 
            } catch (NoSuchMethodException e2) {
 
51
            } catch (NoSuchMethodException ignored) {
40
52
                // ignore
41
53
            }
42
54
        }
58
70
     * @return the name of the test.
59
71
     */
60
72
    public static String getTestCaseName(Test t) {
61
 
        if (t != null && t.getClass().getName().equals("junit.framework.JUnit4TestCaseFacade")) {
 
73
        if (t == null) {
 
74
            return UNKNOWN_TEST_CASE_NAME;
 
75
        }
 
76
        if (t.getClass().getName().equals(JUNIT_FRAMEWORK_JUNIT4_TEST_CASE_FACADE)) {
62
77
            // Self-describing as of JUnit 4 (#38811). But trim "(ClassName)".
63
78
            String name = t.toString();
64
79
            if (name.endsWith(")")) {
71
86
        if (t instanceof TestCase && testCaseName != null) {
72
87
            try {
73
88
                return (String) testCaseName.invoke(t, new Object[0]);
74
 
            } catch (Throwable e) {
 
89
            } catch (Throwable ignored) {
75
90
                // ignore
76
91
            }
77
92
        } else {
88
103
                    && getNameMethod.getReturnType() == String.class) {
89
104
                    return (String) getNameMethod.invoke(t, new Object[0]);
90
105
                }
91
 
            } catch (Throwable e) {
 
106
            } catch (Throwable ignored) {
92
107
                // ignore
93
108
            }
94
109
        }
95
 
        return "unknown";
 
110
        return UNKNOWN_TEST_CASE_NAME;
96
111
    }
97
112
 
98
113
    /**
99
114
     * Tries to find the name of the class which a test represents
100
 
     * across JUnit 3 and 4.
 
115
     * across JUnit 3 and 4. For Junit4 it parses the toString() value of the
 
116
     * test, and extracts it from there.
 
117
     * @since Ant 1.7.1 (it was private until then)
 
118
     * @param test test case to look at
 
119
     * @return the extracted class name.
101
120
     */
102
 
    static String getTestCaseClassName(Test test) {
 
121
    public static String getTestCaseClassName(Test test) {
103
122
        String className = test.getClass().getName();
104
123
        if (test instanceof JUnitTaskMirrorImpl.VmExitErrorTest) {
105
124
            className = ((JUnitTaskMirrorImpl.VmExitErrorTest) test).getClassName();
106
125
        } else
107
 
        if (className.equals("junit.framework.JUnit4TestCaseFacade")) {
 
126
        if (className.equals(JUNIT_FRAMEWORK_JUNIT4_TEST_CASE_FACADE)) {
108
127
            // JUnit 4 wraps solo tests this way. We can extract
109
128
            // the original test name with a little hack.
110
129
            String name = test.toString();