View Javadoc

1   /*
2    * Copyright  2001-2002,2004 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.woopi.ant.taskdefs.junit;
19  import java.lang.reflect.Method;
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  
23  /***
24   * Work around for some changes to the public JUnit API between
25   * different JUnit releases.
26   *
27   * @version $Revision: 1.10.2.4 $
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 &quot;<code>public
52       * String getName()</code>&quot; 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  }