1 7 package org.jboss.dtf; 8 9 import junit.framework.Test; 10 import junit.framework.TestCase; 11 import junit.framework.TestResult; 12 import org.jboss.logging.Logger; 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.lang.reflect.Method ; 16 import java.lang.reflect.Modifier ; 17 import java.util.ArrayList ; 18 import java.util.List ; 19 20 26 public class MultipleTestCase extends TestCase 27 { 28 private List testMethods = new ArrayList (); 29 30 private static final Logger log = Logger.getLogger(MultipleTestCase.class); 31 32 public MultipleTestCase(String name) 33 { 34 super(name); 35 findTestMethods(); 36 } 37 38 protected TestResult createResult() 39 { 40 return new MultipleTestResult(); 41 } 42 43 public List getTestMethods() 44 { 45 return testMethods; 46 } 47 48 51 public int countTestCases() 52 { 53 return testMethods.size(); 54 } 55 56 private void findTestMethods() 57 { 58 Class superClass = this.getClass(); 59 while(Test.class.isAssignableFrom(superClass)) 60 { 61 Method [] methods = superClass.getDeclaredMethods(); 62 for(int i = 0; i < methods.length; i++) 63 { 64 addTestMethod(methods[i], testMethods); 65 } 66 superClass = superClass.getSuperclass(); 67 } 68 if(testMethods.size() == 0) 69 { 70 log.error(this + " does not contain any test methods."); 71 } 73 74 } 75 76 private void addTestMethod(Method m, List names) 77 { 78 String name = m.getName(); 79 if(names.contains(name)) 80 { 81 return; 82 } 83 if(!isPublicTestMethod(m)) 84 { 85 if(isTestMethod(m)) 86 { 87 log.warn("Method " + name + " is a test method, but must be made public."); 88 } 89 return; 90 } 91 names.add(name); 92 } 93 94 private boolean isPublicTestMethod(Method m) 95 { 96 return isTestMethod(m) && Modifier.isPublic(m.getModifiers()); 97 } 98 99 private boolean isTestMethod(Method m) 100 { 101 String name = m.getName(); 102 Class [] parameters = m.getParameterTypes(); 103 Class returnType = m.getReturnType(); 104 return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE); 105 } 106 107 public void runBare(String testMethod) throws Throwable 108 { 109 setUp(); 110 try 111 { 112 runTest(testMethod); 113 } 114 finally 115 { 116 tearDown(); 117 } 118 } 119 120 protected void runTest(String testMethod) throws Throwable 121 { 122 assertNotNull(testMethod); 123 Method runMethod = null; 124 try 125 { 126 runMethod = getClass().getMethod(testMethod, null); 131 } 132 catch(NoSuchMethodException e) 133 { 134 fail("Method \"" + testMethod + "\" not found"); 135 } 136 if(!Modifier.isPublic(runMethod.getModifiers())) 137 { 138 fail("Method \"" + testMethod + "\" should be public"); 139 } 140 141 try 142 { 143 runMethod.invoke(this, new Class [0]); 144 } 145 catch(InvocationTargetException e) 146 { 147 e.fillInStackTrace(); 148 throw e.getTargetException(); 149 } 150 catch(IllegalAccessException e) 151 { 152 e.fillInStackTrace(); 153 throw e; 154 } 155 } 156 157 } | Popular Tags |