1 18 19 package org.apache.tools.ant.taskdefs.optional.junit; 20 21 import java.lang.reflect.Method ; 22 import junit.framework.Test; 23 import junit.framework.TestCase; 24 25 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 try { 38 testCaseName = TestCase.class.getMethod("name", new Class [0]); 39 } catch (NoSuchMethodException e2) { 40 } 42 } 43 } 44 45 60 public static String getTestCaseName(Test t) { 61 if (t != null && t.getClass().getName().equals("junit.framework.JUnit4TestCaseFacade")) { 62 String 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 ) testCaseName.invoke(t, new Object [0]); 74 } catch (Throwable e) { 75 } 77 } else { 78 try { 79 Method getNameMethod = null; 80 try { 81 getNameMethod = 82 t.getClass().getMethod("getName", new Class [0]); 83 } catch (NoSuchMethodException e) { 84 getNameMethod = t.getClass().getMethod("name", 85 new Class [0]); 86 } 87 if (getNameMethod != null 88 && getNameMethod.getReturnType() == String .class) { 89 return (String ) getNameMethod.invoke(t, new Object [0]); 90 } 91 } catch (Throwable e) { 92 } 94 } 95 return "unknown"; 96 } 97 98 102 static String getTestCaseClassName(Test test) { 103 String 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 String 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 |