KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > util > JUnitVersionHelper


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
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 JavaDoc;
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,v 1.1 2004/05/22 11:34:48 vmassol Exp $
34  */

35 public class JUnitVersionHelper
36 {
37     /**
38      * The <code>Method</code> to use to get the test name from a
39      * <code>TestCase</code> object.
40      */

41     private static Method JavaDoc testCaseName = null;
42
43     static
44     {
45         try
46         {
47             testCaseName = TestCase.class.getMethod("getName", new Class JavaDoc[0]);
48         }
49         catch (NoSuchMethodException JavaDoc e)
50         {
51             // pre JUnit 3.7
52
try
53             {
54                 testCaseName = TestCase.class.getMethod("name", new Class JavaDoc[0]);
55             }
56             catch (NoSuchMethodException JavaDoc e2)
57             {
58                 throw new ChainedRuntimeException("Cannot find method name()");
59             }
60         }
61     }
62
63     /**
64      * JUnit 3.7 introduces TestCase.getName() and subsequent versions
65      * of JUnit remove the old name() method. This method provides
66      * access to the name of a TestCase via reflection that is
67      * supposed to work with version before and after JUnit 3.7.
68      *
69      * @param theTest the test case for which to retrieve the name
70      * @return the test case name
71      */

72     public static String JavaDoc getTestCaseName(Test theTest)
73     {
74         String JavaDoc name;
75         
76         if (theTest instanceof TestCase && (testCaseName != null))
77         {
78             try
79             {
80                 name = (String JavaDoc) testCaseName.invoke(theTest, new Object JavaDoc[0]);
81             }
82             catch (Throwable JavaDoc e)
83             {
84                 name = "unknown";
85             }
86         }
87         else
88         {
89             name = "unknown";
90         }
91
92         return name;
93     }
94 }
95
Popular Tags