1 23 24 package org.enhydra.xml.driver; 25 26 import java.io.File ; 27 import java.io.PrintWriter ; 28 import java.lang.reflect.Constructor ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.lang.reflect.Method ; 31 import java.lang.reflect.Modifier ; 32 import java.net.URL ; 33 import java.net.URLClassLoader ; 34 import java.util.ArrayList ; 35 36 import junit.framework.Test; 37 import junit.framework.TestCase; 38 import junit.framework.TestSuite; 39 40 55 public class TestCaseBase extends TestCase { 56 57 private static final Class [] CONST_SIGN = { 58 Method .class 59 }; 60 61 62 private static final Object [] NO_ARGS = new Object [0]; 63 64 65 private static final File INPUT_DIR = new File ("input"); 66 67 68 private static final File OUTPUT_DIR = new File ("output"); 69 70 71 private static final File CLASSES_DIR = new File (OUTPUT_DIR, "classes"); 72 73 74 private static final File RESULTS_DIR = new File (OUTPUT_DIR, "results"); 75 76 77 private static final File EXPECTED_DIR = new File ("expected"); 78 79 84 private static final String RESULT_PKG_SKIP = "org.enhydra.xml."; 85 86 87 public static final String INPUT_REPLACE = "${testCaseRoot}/input"; 88 public static final String OUTPUT_REPLACE = "${testCaseRoot}/output"; 89 90 94 public interface TestSelector { 95 96 public boolean select(Method method); 97 } 98 99 100 private Method fMethod; 101 102 106 private String fTestId; 107 108 109 private PrintWriter fMsgWriter; 110 111 112 private PrintWriter fVerboseOut; 113 114 115 private boolean fUpdateExpected; 116 117 122 private File fTestCaseRoot; 123 124 125 private File fInputRoot; 126 127 128 private File fOutputRoot; 129 130 131 private File fClassesRoot; 132 133 134 private File fResultsRoot; 135 136 137 private File fExpectedRoot; 138 139 140 private File fPackageDir; 141 142 143 private File fRelResultsDir; 144 145 146 private File fResultsDir; 147 148 149 private File fExpectedDir; 150 151 152 private ClassLoader fTestClassLoader; 153 154 155 private ArrayList fExtraFilters; 156 157 158 private TestDiff fDiffer; 159 160 161 private static boolean isTestMethod(Method method) { 162 return method.getName().startsWith("test") 164 && Modifier.isPublic(method.getModifiers()) 165 && (method.getParameterTypes().length == 0) 166 && method.getReturnType().equals(Void.TYPE); 167 } 168 169 170 private static boolean isTestMethod(Method method, 171 TestSelector selector) { 172 return (isTestMethod(method) 173 && ((selector == null) || selector.select(method))); 174 } 175 176 177 private static Test createTest(Constructor constr, 178 Method method) { 179 try { 180 return (Test)constr.newInstance(new Object []{method}); 181 } catch (InstantiationException except) { 182 throw new TestError(except); 183 } catch (IllegalAccessException except) { 184 throw new TestError(except); 185 } catch (InvocationTargetException except) { 186 throw new TestError(except); 187 } 188 } 189 190 193 public static Test createSuite(Class testCaseClass, 194 TestSelector selector) { 195 Constructor constr; 196 try { 197 constr = testCaseClass.getConstructor(CONST_SIGN); 198 } catch (NoSuchMethodException except) { 199 throw new TestError(except); 200 } 201 TestSuite suite = new TestSuite(); 202 Method [] methods= testCaseClass.getDeclaredMethods(); 203 204 for (int idx = 0; idx < methods.length; idx++) { 205 if (isTestMethod(methods[idx], selector)) { 206 suite.addTest(createTest(constr, methods[idx])); 207 } 208 } 209 return suite; 210 } 211 212 216 protected TestCaseBase(String unqualTestId, 217 Method method) { 218 super(unqualTestId); 219 fMethod = method; 220 fTestId = method.getDeclaringClass().getName() + "." 221 + unqualTestId; 222 fMsgWriter = new PrintWriter (System.err, true); 223 if (TestProperties.getVerbose()) { 224 fVerboseOut = fMsgWriter; 225 } 226 fUpdateExpected = TestProperties.getUpdate(); 227 228 fTestCaseRoot = TestProperties.getTestRoot(); 230 fInputRoot = new File (fTestCaseRoot, INPUT_DIR.getPath()); 231 fOutputRoot = new File (fTestCaseRoot, OUTPUT_DIR.getPath()); 232 fClassesRoot = new File (fTestCaseRoot, CLASSES_DIR.getPath()); 233 fResultsRoot = new File (fTestCaseRoot, RESULTS_DIR.getPath()); 234 fExpectedRoot = new File (fTestCaseRoot, EXPECTED_DIR.getPath()); 235 } 236 237 241 protected TestCaseBase(Method method) { 242 this(method.getName(), method); 243 } 244 245 248 public void runTest() { 249 try { 250 fMethod.invoke(this, NO_ARGS); 251 } catch (IllegalAccessException except) { 252 throw new TestError(except); 253 } catch (InvocationTargetException except) { 254 Throwable except2 = except.getTargetException(); 255 if (except2 instanceof TestException) { 256 throw (TestException)except2; 257 } 258 if (except2 instanceof TestError) { 259 throw (TestError)except2; 260 } 261 throw new TestException(except2); 262 } 263 if (fDiffer != null) { 265 fDiffer.throwPendingDiffFailures(); 266 } 267 } 268 269 270 public String getTestId() { 271 return fTestId; 272 } 273 274 275 public String getUnqualTestId() { 276 return getName(); 277 } 278 279 282 public String getTestName() { 283 return fMethod.getName(); 284 } 285 286 289 public PrintWriter getMsgWriter() { 290 return fMsgWriter; 291 } 292 293 294 public void msgPrintln() { 295 fMsgWriter.println(); 296 } 297 298 299 public void msgPrint(String msg) { 300 fMsgWriter.print(msg); 301 } 302 303 304 public void msgPrintln(String msg) { 305 msgPrint(msg); 306 msgPrintln(); 307 } 308 309 310 public PrintWriter getVerboseOut() { 311 return fVerboseOut; 312 } 313 314 318 public File getTestCaseRoot() { 319 return fTestCaseRoot; 320 } 321 322 328 public String getTestPackage() { 329 return getClass().getName() + "Pkg"; 331 } 332 333 337 public String getTestPackageTail() { 338 return getTestPackage().substring(RESULT_PKG_SKIP.length()); 339 } 340 341 344 public File getClassRoot() { 345 return fClassesRoot; 346 } 347 348 349 public File getPackageDir() { 350 if (fPackageDir == null) { 351 String pkgPath = getTestPackage().replace('.', File.separatorChar); 352 fPackageDir = new File (getClassRoot(), pkgPath); 353 } 354 return fPackageDir; 355 } 356 357 358 public File getInputRoot() { 359 return fInputRoot; 360 } 361 362 363 public File getInputFile(String fileName) { 364 return new File (fInputRoot, fileName); 365 } 366 367 372 public File getOtherInputFile(String otherTestCaseDir, 373 String fileName) { 374 File otherRoot = new File (fTestCaseRoot, otherTestCaseDir); 375 File otherInput = new File (otherRoot, INPUT_DIR.getPath()); 376 377 addOtherInputFileDiffFilter(otherTestCaseDir, otherRoot); 379 380 return new File (otherInput, fileName); 381 } 382 383 384 public File getOutputRoot() { 385 return fOutputRoot; 386 } 387 388 391 public File getRelResultsDir() { 392 if (fRelResultsDir == null) { 393 String pkgPath = getTestPackageTail().replace('.', File.separatorChar); 395 fRelResultsDir = new File (pkgPath); 396 } 397 return fRelResultsDir; 398 } 399 400 401 public File getResultsDir() { 402 if (fResultsDir == null) { 403 fResultsDir = new File (fResultsRoot, 404 getRelResultsDir().getPath()); 405 fResultsDir.mkdirs(); } 407 return fResultsDir; 408 } 409 410 411 public File getResultFile(String fname, 412 String suffix) { 413 return new File (getResultsDir(), fname + "." + suffix); 414 } 415 416 417 public File getResultFile(String suffix) { 418 return getResultFile(getTestName(), suffix); 419 } 420 421 422 public File getExpectedRoot() { 423 return fExpectedRoot; 424 } 425 426 427 public File getExpectedDir() { 428 if (fExpectedDir == null) { 429 fExpectedDir = new File (fExpectedRoot, 430 getRelResultsDir().getPath()); 431 } 432 return fExpectedDir; 433 } 434 435 436 public File getExpectedFile(String fname, 437 String suffix) { 438 return new File (getExpectedDir(), fname + "." + suffix); 439 } 440 441 442 public File getExpectedFile(String suffix) { 443 return getExpectedFile(getTestName(), suffix); 444 } 445 446 450 public void addDiffFilter(TestDiff.LineFilter filter) { 451 if (fExtraFilters == null) { 452 fExtraFilters = new ArrayList (); 453 } 454 fExtraFilters.add(filter); 455 } 456 457 461 private void addOtherInputFileDiffFilter(String otherTestCaseDir, 462 File otherRoot) { 463 addDiffFilter(new SimpleDiffFilter(otherRoot.getPath(), 464 "${testCaseRoot}/" + otherTestCaseDir)); 465 } 466 467 471 private void addFilters(TestDiff differ) { 472 differ.addFilter(new SimpleDiffFilter(getInputRoot().getPath(), 474 INPUT_REPLACE)); 475 differ.addFilter(new SimpleDiffFilter(getOutputRoot().getPath(), 476 OUTPUT_REPLACE)); 477 478 if (fExtraFilters != null) { 480 for (int idx = 0; idx < fExtraFilters.size(); idx++) { 481 differ.addFilter((TestDiff.LineFilter)fExtraFilters.get(idx)); 482 } 483 } 484 } 485 486 491 public TestDiff getDiffer() { 492 if (fDiffer == null) { 493 int flags = TestDiff.QUEUE_FAILURES; 494 if (fUpdateExpected) { 495 flags |= TestDiff.UPDATE_EXPECTED; 496 } 497 fDiffer = new TestDiff(fMsgWriter, flags); 498 addFilters(fDiffer); 499 } 500 return fDiffer; 501 } 502 503 507 public TestDiff createDiffer(boolean queueFailures) { 508 int flags = 0; 509 if (queueFailures) { 510 flags |= TestDiff.QUEUE_FAILURES; 511 } 512 if (fUpdateExpected) { 513 flags |= TestDiff.UPDATE_EXPECTED; 514 } 515 return new TestDiff(fMsgWriter, flags); 516 } 517 518 524 public ClassLoader createClassLoader() { 525 getClassRoot().mkdirs(); 526 URL pathUrl = TestFileOps.dirToUrl(getClassRoot()); 527 return new URLClassLoader (new URL []{pathUrl}, 528 getClass().getClassLoader()); 529 } 530 531 534 public ClassLoader getTestClassLoader() { 535 if (fTestClassLoader == null) { 536 fTestClassLoader = createClassLoader(); 537 } 538 return fTestClassLoader; 539 } 540 541 544 public void dumpInfo(PrintWriter out) { 545 out.println("testCase: " + getClass().getName()); 546 out.println(" getName(): " + getName()); 547 out.println(" getTestId(): " + getTestId()); 548 out.println(" getTestCaseRoot(): " + getTestCaseRoot()); 549 out.println(" getInputRoot(): " + getInputRoot()); 550 out.println(" getUnqualTestId(): " + getUnqualTestId()); 551 out.println(" getTestName(): " + getTestName()); 552 out.println(" getTestPackage(): " + getTestPackage()); 553 out.println(" getClassRoot(): " + getClassRoot()); 554 out.println(" getPackageDir(): " + getPackageDir()); 555 out.println(" getRelResultsDir(): " + getRelResultsDir()); 556 out.println(" getResultsDir(): " + getResultsDir()); 557 out.println(" getExpectedDir(): " + getExpectedDir()); 558 } 559 } 560 561 | Popular Tags |