KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > junit > JUnitVersionHelper


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional.junit;
20
21 import java.lang.reflect.Method JavaDoc;
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24
25 /**
26  * Work around for some changes to the public JUnit API between
27  * different JUnit releases.
28  */

29 public class JUnitVersionHelper {
30
31     private static Method JavaDoc testCaseName = null;
32     static {
33         try {
34             testCaseName = TestCase.class.getMethod("getName", new Class JavaDoc[0]);
35         } catch (NoSuchMethodException JavaDoc e) {
36             // pre JUnit 3.7
37
try {
38                 testCaseName = TestCase.class.getMethod("name", new Class JavaDoc[0]);
39             } catch (NoSuchMethodException JavaDoc 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 &quot;<code>public
52      * String getName()</code>&quot; on any implementation of Test if
53      * it exists.</p>
54      *
55      * <p>Since Ant 1.7 also checks for JUnit4TestCaseFacade explicitly.
56      * This is used by junit.framework.JUnit4TestAdapter.</p>
57      * @param t the test.
58      * @return the name of the test.
59      */

60     public static String JavaDoc getTestCaseName(Test t) {
61         if (t != null && t.getClass().getName().equals("junit.framework.JUnit4TestCaseFacade")) {
62             // Self-describing as of JUnit 4 (#38811). But trim "(ClassName)".
63
String JavaDoc name = t.toString();
64             if (name.endsWith(")")) {
65                 int paren = name.lastIndexOf('(');
66                 return name.substring(0, paren);
67             } else {
68                 return name;
69             }
70         }
71         if (t instanceof TestCase && testCaseName != null) {
72             try {
73                 return (String JavaDoc) testCaseName.invoke(t, new Object JavaDoc[0]);
74             } catch (Throwable JavaDoc e) {
75                 // ignore
76
}
77         } else {
78             try {
79                 Method JavaDoc getNameMethod = null;
80                 try {
81                     getNameMethod =
82                         t.getClass().getMethod("getName", new Class JavaDoc [0]);
83                 } catch (NoSuchMethodException JavaDoc e) {
84                     getNameMethod = t.getClass().getMethod("name",
85                                                            new Class JavaDoc [0]);
86                 }
87                 if (getNameMethod != null
88                     && getNameMethod.getReturnType() == String JavaDoc.class) {
89                     return (String JavaDoc) getNameMethod.invoke(t, new Object JavaDoc[0]);
90                 }
91             } catch (Throwable JavaDoc e) {
92                 // ignore
93
}
94         }
95         return "unknown";
96     }
97
98     /**
99      * Tries to find the name of the class which a test represents
100      * across JUnit 3 and 4.
101      */

102     static String JavaDoc getTestCaseClassName(Test test) {
103         String JavaDoc className = test.getClass().getName();
104         if (test instanceof JUnitTaskMirrorImpl.VmExitErrorTest) {
105             className = ((JUnitTaskMirrorImpl.VmExitErrorTest) test).getClassName();
106         } else
107         if (className.equals("junit.framework.JUnit4TestCaseFacade")) {
108             // JUnit 4 wraps solo tests this way. We can extract
109
// the original test name with a little hack.
110
String JavaDoc name = test.toString();
111             int paren = name.lastIndexOf('(');
112             if (paren != -1 && name.endsWith(")")) {
113                 className = name.substring(paren + 1, name.length() - 1);
114             }
115         }
116         return className;
117     }
118
119 }
120
Popular Tags