1 45 package org.openejb.test; 46 47 import java.lang.reflect.InvocationTargetException ; 48 import java.lang.reflect.Method ; 49 import java.util.Iterator ; 50 51 import junit.framework.Assert; 52 import junit.framework.Protectable; 53 import junit.framework.Test; 54 import junit.framework.TestResult; 55 60 public class NumberedTestCase extends Assert implements Test{ 61 62 Method [] testMethods = new Method []{}; 63 protected static final String standardPrefix = "test##_"; 64 65 class MethodComparator implements java.util.Comparator { 66 67 public int compare(Object o1, Object o2){ 68 Method m1 = (Method )o1; 69 Method m2 = (Method )o2; 70 return m1.getName().compareTo(m2.getName()); 71 } 72 public boolean eqauls(Object other){ 73 if(other instanceof MethodComparator) 74 return true; 75 else 76 return false; 77 } 78 } 79 80 81 public NumberedTestCase(){ 82 try{ 83 Method [] methods = getClass().getMethods(); 85 java.util.TreeSet tm = new java.util.TreeSet (new MethodComparator()); 86 87 for (int i=0; i < methods.length; i++){ 89 if (methods[i].getName().startsWith("test")){ 90 tm.add(methods[i]); 91 } 92 } 93 testMethods = new Method [tm.size()]; 94 Iterator orderedMethods = tm.iterator(); 95 for(int i=0;orderedMethods.hasNext();i++){ 96 testMethods[i]=(Method )orderedMethods.next(); 97 } 98 } catch (Exception e){ 99 throw new RuntimeException (e.getMessage()); 100 } 101 } 102 103 protected void setUp() throws Exception { 104 } 105 106 protected void tearDown() throws Exception { 107 } 108 109 112 public int countTestCases() { 113 return testMethods.length; 114 } 115 116 119 public void run(TestResult result) { 120 try{ 121 setUp(); 122 } catch (Exception e){ 123 Test test = new Test(){ 124 public int countTestCases() { 125 return 0; 126 } 127 public void run(TestResult result) { 128 } 129 public String toString(){ 130 return name()+".setUp()"; 131 } 132 }; 133 134 result.addError(test, e); 135 return; 136 } 137 for (int i=0; i < testMethods.length; i++){ 138 run(result, testMethods[i]); 139 } 140 try{ 141 tearDown(); 142 } catch (Exception e){ 143 Test test = new Test(){ 144 public int countTestCases() { 145 return 0; 146 } 147 public void run(TestResult result) { 148 } 149 public String toString(){ 150 return name()+".tearDown()"; 151 } 152 }; 153 154 result.addError(test, e); 155 return; 156 } 157 } 158 159 protected void run(final TestResult result, final Method testMethod) { 160 Test test = createTest(testMethod); 161 result.startTest(test); 162 Protectable p= new Protectable() { 163 public void protect() throws Throwable { 164 runTestMethod(testMethod); 165 } 166 }; 167 result.runProtected(test, p); 168 result.endTest(test); 169 } 170 171 172 protected Test createTest(final Method testMethod){ 173 Test test = new Test(){ 174 public int countTestCases() {return 1;} 175 public void run(TestResult result) {} 176 public String toString(){ 177 return createTestName(testMethod); 178 } 179 }; 180 return test; 181 } 182 183 protected void runTestMethod(Method testMethod) throws Throwable { 184 try { 185 testMethod.invoke(this, new Class [0]); 186 } catch (InvocationTargetException e) { 187 e.fillInStackTrace(); 188 throw e.getTargetException(); 189 } catch (IllegalAccessException e) { 190 e.fillInStackTrace(); 191 throw e; 192 } 193 } 194 195 196 public String toString(){ 197 return name(); 198 } 199 200 public String name(){ 201 return ""; 202 } 203 204 protected String createTestName(Method testMethod){ 205 return name() + removePrefix(testMethod.getName()); 206 } 207 208 protected static String removePrefix(String name){ 209 return removePrefix(standardPrefix, name); 210 } 211 212 protected static String removePrefix(String prefix, String name){ 213 return name.substring(prefix.length()); 214 } 215 } 216 217 218 | Popular Tags |