2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
2    * ========================================================================
3    * 
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * 
19   * ========================================================================
20   */
21  package org.apache.cactus.internal.util;
22  
23  import java.lang.reflect.Method;
24  
25  import org.apache.cactus.util.ChainedRuntimeException;
26  
27  import junit.framework.Test;
28  import junit.framework.TestCase;
29  
30  /**
31   * Work around for some changes to the public JUnit API between
32   * different JUnit releases.
33   *
34   * @version $Id: JUnitVersionHelper.java 238991 2004-05-22 11:34:50Z vmassol $
35   */
36  public class JUnitVersionHelper
37  {
38      /**
39       * The <code>Method</code> to use to get the test name from a
40       * <code>TestCase</code> object.
41       */
42      private static Method testCaseName = null;
43  
44      static
45      {
46          try
47          {
48              testCaseName = TestCase.class.getMethod("getName", new Class[0]);
49          }
50          catch (NoSuchMethodException e)
51          {
52              // pre JUnit 3.7
53              try
54              {
55                  testCaseName = TestCase.class.getMethod("name", new Class[0]);
56              }
57              catch (NoSuchMethodException e2)
58              {
59                  throw new ChainedRuntimeException("Cannot find method name()");
60              }
61          }
62      }
63  
64      /**
65       * JUnit 3.7 introduces TestCase.getName() and subsequent versions
66       * of JUnit remove the old name() method.  This method provides
67       * access to the name of a TestCase via reflection that is
68       * supposed to work with version before and after JUnit 3.7.
69       *
70       * @param theTest the test case for which to retrieve the name
71       * @return the test case name
72       */
73      public static String getTestCaseName(Test theTest)
74      {
75          String name;
76          
77          if (theTest instanceof TestCase && (testCaseName != null))
78          {
79              try
80              {
81                  name = (String) testCaseName.invoke(theTest, new Object[0]);
82              }
83              catch (Throwable e)
84              {
85                  name = "unknown";
86              }
87          }
88          else
89          {
90              name = "unknown";
91          }
92  
93          return name;
94      }
95  }