1 23 24 package org.enhydra.xml.xmlc.driver; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.lang.reflect.Field ; 29 import java.lang.reflect.Method ; 30 31 import org.enhydra.xml.driver.TestCaseBase; 32 import org.enhydra.xml.driver.TestError; 33 import org.enhydra.xml.driver.TestException; 34 import org.enhydra.xml.xmlc.XMLCFactory; 35 import org.enhydra.xml.xmlc.XMLCLogger; 36 import org.enhydra.xml.xmlc.XMLCStdFactory; 37 import org.enhydra.xml.xmlc.XMLObject; 38 import org.enhydra.xml.xmlc.deferredparsing.XMLCDeferredParsingFactory; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 42 47 public class XmlcTestCaseBase extends TestCaseBase { 48 public static final boolean ALT_NAMES = true; 50 51 52 public static final String COMPILE_OUTPUT_EXT = "compile.out"; 53 public static final String LOAD_DOM_EXT = "load.dom"; 54 public static final String PARSE_GEN_OUTPUT_EXT = "parsegen.out"; 55 public static final String PARSE_DOM_DUMP_EXT = "parse.dom"; 56 public static final String DOC_DOM_DUMP_EXT = "doc.dom"; 57 58 59 private static final Class [] NO_PARAMS = new Class [0]; 60 private static final Class [] STR_PARAM = new Class []{ 61 String .class 62 }; 63 private static final Object [] NO_ARGS = new Object [0]; 64 65 66 protected final XmlcTestParams fParams; 67 68 69 private static final String SOURCE_DIR = "src"; 70 71 76 private boolean fUsingImplInterface; 77 78 79 private XMLCFactory fXMLCFactory; 80 81 82 private String fTestPackage; 83 84 85 private File fLoadInvarExpectedDir; 86 87 88 private static String mkTestId(Method method, 89 XmlcTestParams params) { 90 if (ALT_NAMES) { 91 return method.getName() + "." + params.getParamsStr(); 92 } else { 93 return params.getParamsStr() + "." + method.getName(); 94 } 95 } 96 97 98 public XmlcTestCaseBase(Method method, 99 XmlcTestParams params) { 100 super(mkTestId(method, params), method); 101 fParams = params; 102 if (fParams.getReloading()) { 103 setUsingImplInterface(); 104 } 105 } 106 107 110 public XmlcTestParams getParams() { 111 return fParams; 112 } 113 114 115 public void setFactory(XMLCFactory factory) { 116 fXMLCFactory = factory; 117 } 118 119 120 private XMLCFactory createFactory(ClassLoader loader, XMLCLogger logger) { 121 XMLCFactory factory; 122 123 if (fParams.getIsDeferredParsing()) { 124 factory = new XMLCDeferredParsingFactory (null, loader, logger); } else { 126 factory = new XMLCStdFactory(loader, logger); } 128 129 return factory; 130 } 131 132 133 public void setUsingImplInterface() { 134 fUsingImplInterface = true; 135 if (fXMLCFactory == null) { 136 fXMLCFactory = createFactory(getTestClassLoader(), null); 138 } 139 } 140 141 146 public String getTestPackage() { 147 if (fTestPackage == null) { 148 if (ALT_NAMES) { 149 fTestPackage = getClass().getName() + "Pkg" + "." 151 + fParams.getParamsStr(); 152 153 } else { 154 String clName = getClass().getName(); 157 int dotIdx = clName.lastIndexOf('.'); 158 String clFirst = clName.substring(0, dotIdx); 159 String clLast = clName.substring(dotIdx+1); 160 161 fTestPackage = clFirst + "." + fParams.getParamsStr() + "." 162 + clLast; 163 } 164 } 165 return fTestPackage; 166 } 167 168 173 public String getTestClass() { 174 return getTestPackage() + "." + getTestName(); 175 } 176 177 182 public String getTestClassImpl() { 183 if (fUsingImplInterface) { 184 return getTestClass() + "Impl"; 185 } else { 186 return getTestClass(); 187 } 188 } 189 190 194 public String getTestClassInterface() { 195 if (!fUsingImplInterface) { 196 throw new TestError("request for interface when one is not being used"); 197 } 198 return getTestClass(); 199 } 200 201 205 public File getLoadInvarExpectedDir() { 206 if (fLoadInvarExpectedDir == null) { 207 fLoadInvarExpectedDir 209 = new File (getExpectedRoot(), 210 getRelResultsDir().getParentFile().getPath()); 211 } 212 return fLoadInvarExpectedDir; 213 } 214 215 216 public File getLoadInvarExpectedFile(String fname, 217 String suffix) { 218 return new File (getLoadInvarExpectedDir(), 219 fname + "." + suffix); 220 } 221 222 223 public File getLoadInvarExpectedFile(String suffix) { 224 return getLoadInvarExpectedFile(getTestName(), suffix); 225 } 226 227 228 229 public File getGenSourceDir() { 230 return new File (getOutputRoot(), SOURCE_DIR); 231 } 232 233 234 public String getClassNameAsPath() { 235 return getTestClass().replace('.', File.separatorChar); 236 } 237 238 239 public File getGenSrcFile() { 240 return new File (getGenSourceDir(), getClassNameAsPath() + ".java"); 241 } 242 243 244 public File getGenClassFile() { 245 return new File (getClassRoot(), getClassNameAsPath() + ".class"); 246 } 247 248 249 public void removeFile(File file) { 250 file.delete(); 251 if (file.exists()) { 252 throw new TestException("file still exists after deleting: " 253 + file); 254 } 255 } 256 257 258 public void verifyExists(File file) { 259 if (!file.exists()) { 260 throw new TestException("file was not created: " + file); 261 } 262 } 263 264 268 private XMLObject loadDocWithFactory(Object spec) 269 throws IOException , ClassNotFoundException { 270 XMLCFactory factory = fXMLCFactory; 271 if (factory == null) { 272 factory = createFactory(getTestClassLoader(), null); 274 } 275 if (spec instanceof String ) { 276 return factory.create((String )spec); 277 } else { 278 return factory.create((Class )spec); 279 } 280 } 281 282 286 public Class loadDocClass(String className) { 287 try { 288 return getTestClassLoader().loadClass(className); 289 } catch (ClassNotFoundException except) { 290 throw new TestException(except); 291 } 292 } 293 294 297 public Class loadDocClass() { 298 return loadDocClass(getTestClassImpl()); 299 } 300 301 304 public Class loadDocClassInterface() { 305 return loadDocClass(getTestClassInterface()); 306 } 307 308 312 private XMLObject loadDocWithClassLoader(Object spec) 313 throws IllegalAccessException , InstantiationException , 314 IOException , ClassNotFoundException { 315 Class objClass = getTestClassLoader().loadClass((String )spec); 316 return (XMLObject)objClass.newInstance(); 317 } 318 319 325 public XMLObject loadTestDocument(Object spec) { 326 try { 327 return loadDocWithFactory(spec); 328 } catch (Throwable except) { 329 throw new TestException("failed to load XMLC document class: \"" 331 + spec + "\"", except); 332 } 333 } 334 335 339 public XMLObject loadTestDocument() { 340 return loadTestDocument(getTestClass()); 341 } 342 343 348 public XMLObject compileLoadDoc(File srcFile, 349 String unqualClassName, 350 Class impls) { 351 ExecXmlc execXmlc = new ExecXmlc(this); 353 String className = (unqualClassName != null) 354 ? (getTestPackage() + "." + unqualClassName) 355 : getTestClass(); 356 execXmlc.addOpt(ExecXmlc.OPT_CLASS, className); 357 if (impls != null) { 358 execXmlc.addOpt(ExecXmlc.OPT_IMPLEMENTS, 359 impls.getClass().getName()); 360 } 361 execXmlc.setSrcFile(srcFile); 362 execXmlc.compile(getResultFile(COMPILE_OUTPUT_EXT)); 363 return loadTestDocument(className); 364 } 365 366 369 public void dumpVerifyDom(Document doc, 370 File resultFile, 371 File expectFile) { 372 OutputDocument.dump(doc, resultFile); 373 getDiffer().diff(expectFile, resultFile); 374 } 375 376 379 public void dumpVerifyDom(Document doc, 380 String resultFileExt) { 381 dumpVerifyDom(doc, getResultFile(resultFileExt), 382 getExpectedFile(resultFileExt)); 383 } 384 385 388 public void dumpDom(Document doc, 389 String resultFileExt) { 390 OutputDocument.dump(doc, getResultFile(resultFileExt)); 391 } 392 393 397 public void parseVerifyDom(File srcDoc) { 398 ExecXmlc dumper = new ExecXmlc(this); 400 dumper.addOpt(ExecXmlc.OPT_DUMP); 401 dumper.addOpt(ExecXmlc.OPT_METHODS); 402 dumper.addOpt(ExecXmlc.OPT_NO_COMPILE); 403 dumper.addOpt(ExecXmlc.OPT_CLASS, getTestClass()); 404 dumper.setSrcFile(srcDoc); 405 dumper.compile(getResultFile(PARSE_DOM_DUMP_EXT)); 406 getDiffer().diff(getExpectedFile(PARSE_DOM_DUMP_EXT), 407 getResultFile(PARSE_DOM_DUMP_EXT)); 408 } 409 410 415 public String getStringField(Object obj, 416 String fieldName) { 417 try { 418 if (obj instanceof Class ) { 419 Field field = ((Class )obj).getField(fieldName); 420 return (String )field.get(null); 421 } else { 422 Field field = obj.getClass().getField(fieldName); 423 return (String )field.get(obj); 424 } 425 } catch (Throwable except) { 426 Class cl = (obj instanceof Class ) ? (Class )obj 427 : obj.getClass(); 428 throw new TestException("error getting field \"" + fieldName 429 + "\" of class \"" + cl); 430 } 431 } 432 433 434 private void verifyReturnType(Method method, 435 Class returnInstanceOf) { 436 if (!returnInstanceOf.isAssignableFrom(method.getReturnType())) { 437 throw new TestException("return type of \"" + method.getName() 438 + "\", \"" + method.getReturnType() 439 + "\", is not an instance of \"" 440 + returnInstanceOf + "\""); 441 } 442 } 443 444 450 public Element callGetElement(XMLObject xmlObj, 451 String nameSuffix, 452 Class returnInstanceOf) { 453 String methodName = "getElement" + nameSuffix; 454 try { 455 Method method = xmlObj.getClass().getMethod(nameSuffix, NO_PARAMS); 456 if (returnInstanceOf != null) { 457 verifyReturnType(method, returnInstanceOf); 458 } 459 return (Element )method.invoke(xmlObj, NO_ARGS); 460 } catch (Throwable except) { 461 throw new TestException("error calling \"" + methodName 462 + "\" of class \"" 463 + xmlObj.getClass().getName()); 464 } 465 } 466 467 471 public void callSetText(XMLObject xmlObj, 472 String nameSuffix, 473 String value) { 474 String methodName = "setText" + nameSuffix; 475 try { 476 Method method = xmlObj.getClass().getMethod(nameSuffix, STR_PARAM); 477 verifyReturnType(method, Void .class); 478 method.invoke(xmlObj, (Object [])new String []{value}); 479 } catch (Throwable except) { 480 throw new TestException("error calling \"" + methodName 481 + "\" of class \"" 482 + xmlObj.getClass().getName()); 483 } 484 } 485 486 487 private Class loadClass(String name) { 488 try { 489 return getClass().getClassLoader().loadClass(name); 490 } catch (ClassNotFoundException except) { 491 throw new TestException(except); 492 } 493 } 494 495 496 public void verifyInstanceOf(Class testClass, 497 Class expectClass) { 498 if (!expectClass.isAssignableFrom(testClass)) { 499 throw new TestException(testClass + " is not an instance of " 500 + expectClass); 501 } 502 } 503 504 505 public void verifyInstanceOf(Class testClass, 506 String expectClass) { 507 verifyInstanceOf(testClass, loadClass(expectClass)); 508 } 509 510 511 public void verifyInstanceOf(Class testClass, 512 String [] expectClasses) { 513 for (int i = 0; i < expectClasses.length; i++) { 514 verifyInstanceOf(testClass, expectClasses[i]); 515 } 516 } 517 518 519 public void verifyNotInstanceOf(Class testClass, 520 Class expectClass) { 521 if (expectClass.isAssignableFrom(testClass)) { 522 throw new TestException(testClass 523 + " should not be an instance of " 524 + expectClass); 525 } 526 } 527 528 529 public void verifyNotInstanceOf(Class testClass, 530 String expectClass) { 531 verifyNotInstanceOf(testClass, loadClass(expectClass)); 532 } 533 534 535 public void verifyNotInstanceOf(Class testClass, 536 String [] expectClasses) { 537 for (int i = 0; i < expectClasses.length; i++) { 538 verifyNotInstanceOf(testClass, expectClasses[i]); 539 } 540 } 541 542 543 public void verifyIsInterface(Class testClass) { 544 if (!testClass.isInterface()) { 545 throw new TestException("class is not an interface:" 546 + testClass); 547 } 548 } 549 550 551 public void verifyIsClass(Class testClass) { 552 if (testClass.isInterface() || testClass.isPrimitive()) { 553 throw new TestException("class is not an class:" 554 + testClass); 555 } 556 } 557 } 558 | Popular Tags |