1 package junit.framework; 2 3 import java.lang.reflect.InvocationTargetException ; 4 import java.lang.reflect.Method ; 5 import java.lang.reflect.Modifier ; 6 7 76 public abstract class TestCase extends Assert implements Test { 77 80 private String fName; 81 82 86 public TestCase() { 87 fName= null; 88 } 89 92 public TestCase(String name) { 93 fName= name; 94 } 95 98 public int countTestCases() { 99 return 1; 100 } 101 106 protected TestResult createResult() { 107 return new TestResult(); 108 } 109 115 public TestResult run() { 116 TestResult result= createResult(); 117 run(result); 118 return result; 119 } 120 123 public void run(TestResult result) { 124 result.run(this); 125 } 126 130 public void runBare() throws Throwable { 131 Throwable exception= null; 132 setUp(); 133 try { 134 runTest(); 135 } catch (Throwable running) { 136 exception= running; 137 } 138 finally { 139 try { 140 tearDown(); 141 } catch (Throwable tearingDown) { 142 if (exception == null) exception= tearingDown; 143 } 144 } 145 if (exception != null) throw exception; 146 } 147 151 protected void runTest() throws Throwable { 152 assertNotNull("TestCase.fName cannot be null", fName); Method runMethod= null; 154 try { 155 runMethod= getClass().getMethod(fName, (Class [])null); 160 } catch (NoSuchMethodException e) { 161 fail("Method \""+fName+"\" not found"); 162 } 163 if (!Modifier.isPublic(runMethod.getModifiers())) { 164 fail("Method \""+fName+"\" should be public"); 165 } 166 167 try { 168 runMethod.invoke(this); 169 } 170 catch (InvocationTargetException e) { 171 e.fillInStackTrace(); 172 throw e.getTargetException(); 173 } 174 catch (IllegalAccessException e) { 175 e.fillInStackTrace(); 176 throw e; 177 } 178 } 179 183 protected void setUp() throws Exception { 184 } 185 189 protected void tearDown() throws Exception { 190 } 191 194 @Override 195 public String toString() { 196 return getName() + "(" + getClass().getName() + ")"; 197 } 198 202 public String getName() { 203 return fName; 204 } 205 209 public void setName(String name) { 210 fName= name; 211 } 212 } 213 | Popular Tags |