1 23 24 package org.enhydra.xml.xmlc.driver; 25 26 import java.io.File ; 27 import java.io.PrintWriter ; 28 import java.util.ArrayList ; 29 30 import org.enhydra.xml.driver.TestError; 31 import org.enhydra.xml.driver.TestException; 32 import org.enhydra.xml.driver.TestFileOps; 33 import org.enhydra.xml.io.OutputOptions; 34 import org.enhydra.xml.xmlc.XMLObject; 35 36 60 public class XmlcBasicTest { 61 62 private XmlcTestCaseBase fTest; 63 64 65 private File fSrcFile; 66 67 72 private ArrayList fExtraSrc; 73 74 75 private String fDocFileExt; 76 77 78 private boolean fCompileShouldFail; 79 80 81 private OutputOptions fOutputOptions; 82 83 84 private PrintWriter fVerboseOut; 85 86 87 private ExecXmlc fComp1Xmlc; 88 89 90 private ExecXmlc fComp2Xmlc; 91 92 93 private String fExpectedEncoding; 94 95 96 private boolean fUseToDoc; 97 98 99 private boolean fEditReloadSource = true; 100 101 102 private boolean fDiffExpected = true; 103 104 108 public XmlcBasicTest(XmlcTestCaseBase test, 109 String docFileExt, 110 File srcFile) { 111 fTest = test; 112 fDocFileExt = docFileExt; 113 fSrcFile = srcFile; 114 fComp1Xmlc = new ExecXmlc(test); 115 fComp1Xmlc.setSrcFile(fSrcFile); 116 117 fComp2Xmlc = new ExecXmlc(test); 118 } 119 120 121 public XmlcTestCaseBase getTestCase() { 122 return fTest; 123 } 124 125 130 public void disableExpectedFileDiff() { 131 fDiffExpected = false; 132 } 133 134 140 public void setEditReloadSource(boolean val) { 141 fEditReloadSource = val; 142 } 143 144 149 public void addExtraSrcFile(File extraSrcFile) { 150 if (fExtraSrc == null) { 151 fExtraSrc = new ArrayList (); 152 } 153 fExtraSrc.add(extraSrcFile); 154 if (fTest.getParams().getReloading()) { 155 fComp1Xmlc.addExtraSrcFile(extraSrcFile); 156 } 157 } 158 159 162 public void setExpectedEncoding(String encoding) { 163 fExpectedEncoding = encoding; 164 } 165 166 170 public OutputOptions getOutputOptions() { 171 if (fOutputOptions == null) { 172 fOutputOptions = new OutputOptions(); 173 } 174 return fOutputOptions; 175 } 176 177 178 public void setUseToDocument() { 179 fUseToDoc = true; 180 } 181 182 183 public void addFirst(String opt) { 184 fComp1Xmlc.addOpt(opt); 185 } 186 187 188 public void addFirst(String opt, 189 String arg) { 190 fComp1Xmlc.addOpt(opt, arg); 191 } 192 193 194 public void addFirst(String opt, 195 String arg1, 196 String arg2) { 197 fComp1Xmlc.addOpt(opt, arg1, arg2); 198 } 199 200 201 public void addBoth(String opt) { 202 fComp1Xmlc.addOpt(opt); 203 if (fComp2Xmlc != null) { 204 fComp2Xmlc.addOpt(opt); 205 } 206 } 207 208 209 public void addBoth(String opt, 210 String arg) { 211 fComp1Xmlc.addOpt(opt, arg); 212 if (fComp2Xmlc != null) { 213 fComp2Xmlc.addOpt(opt, arg); 214 } 215 } 216 217 218 public void addBoth(String opt, 219 File arg) { 220 fComp1Xmlc.addOpt(opt, arg); 221 if (fComp2Xmlc != null) { 222 fComp2Xmlc.addOpt(opt, arg); 223 } 224 } 225 226 227 public void addBoth(String opt, 228 String arg1, 229 String arg2) { 230 fComp1Xmlc.addOpt(opt, arg1, arg2); 231 if (fComp2Xmlc != null) { 232 fComp2Xmlc.addOpt(opt, arg1, arg2); 233 } 234 } 235 236 237 public void setDomOpt(String opt, 238 String arg) { 239 fComp1Xmlc.setDomOpt(opt, arg); 240 fComp2Xmlc.setDomOpt(opt, arg); 241 } 242 243 246 public void addOptionFile(File file) { 247 fComp1Xmlc.addOptionFile(file); 248 if (fComp2Xmlc != null) { 249 fComp2Xmlc.addOptionFile(file); 250 } 251 } 252 253 256 public void setCompileShouldFail() { 257 fCompileShouldFail = true; 258 } 259 260 264 protected void addStdOpts(ExecXmlc execXmlc) { 265 } 266 267 270 private void handleCompileFail(TestException testExcept) { 271 if (fCompileShouldFail) { 272 Throwable cause = testExcept.getCause(); 273 if ((cause == null) || (cause instanceof TestException) 276 || (cause instanceof TestError)) { 277 throw new TestException("compile that should fail, failed in test code", 278 testExcept); 279 } 280 } else { 281 throw testExcept; 283 } 284 } 285 286 287 290 public void compileOriginalStep() { 291 fTest.getGenClassFile().delete(); 292 addStdOpts(fComp1Xmlc); 293 if (fTest.getParams().getReloading()) { 294 fComp1Xmlc.addOpt(ExecXmlc.OPT_FOR_RECOMP); 295 } 296 fComp1Xmlc.addOpt(ExecXmlc.OPT_DEST_DIR, fTest.getClassRoot()); 297 fComp1Xmlc.addOpt(ExecXmlc.OPT_CLASS, fTest.getTestClass()); 298 fComp1Xmlc.addOpt(ExecXmlc.OPT_METHODS); 299 fComp1Xmlc.addOpt(ExecXmlc.OPT_DUMP); 300 301 File result = fTest.getResultFile(XmlcTestCaseBase.COMPILE_OUTPUT_EXT); 303 boolean failed = false; 304 try { 305 TestFileOps.ensureFileDir(result); 306 fComp1Xmlc.compile(result); 307 } catch (TestException testExcept) { 308 failed = true; 309 handleCompileFail(testExcept); 310 } 311 if (fCompileShouldFail && !failed) { 312 throw new TestException("compile should have failed"); 314 } 315 if (fDiffExpected) { 316 File expected = fTest.getLoadInvarExpectedFile(XmlcTestCaseBase.COMPILE_OUTPUT_EXT); 317 fTest.getDiffer().diff(expected, result); 318 } 319 } 320 321 325 public File getGenerateFile() { 326 return fTest.getResultFile("gen." + fDocFileExt); 327 } 328 329 330 public File getRegenerateFile() { 331 return fTest.getResultFile("regen." + fDocFileExt); 332 } 333 334 337 private void outputDocument(XMLObject doc) { 338 if ((fOutputOptions != null) && fUseToDoc) { 339 throw new TestError("bug: both output options and toDocument() are specified"); 340 } 341 if (fUseToDoc) { 342 OutputDocument.writeWithToDoc(doc, getGenerateFile()); 343 } else { 344 OutputDocument.write(doc, getGenerateFile(), fOutputOptions); 346 } 347 } 348 349 357 private void parseGenerated() { 358 addStdOpts(fComp2Xmlc); 359 fComp2Xmlc.addOpt(ExecXmlc.OPT_NO_COMPILE); 360 fComp2Xmlc.addOpt(ExecXmlc.OPT_METHODS); 361 fComp2Xmlc.addOpt(ExecXmlc.OPT_DUMP); 362 fComp2Xmlc.addOpt(ExecXmlc.OPT_DOC_OUT, getRegenerateFile().getPath()); 363 fComp2Xmlc.setSrcFile(getGenerateFile()); 364 365 File out = fTest.getResultFile(XmlcTestCaseBase.PARSE_GEN_OUTPUT_EXT); 366 fComp2Xmlc.compile(out); 367 368 if (fDiffExpected) { 369 File expected = fTest.getExpectedFile(XmlcTestCaseBase.PARSE_GEN_OUTPUT_EXT); 370 fTest.getDiffer().diff(expected, out); 371 } 372 } 373 374 377 public void reparseStep(XMLObject doc) { 378 if (fDiffExpected) { 380 fTest.dumpVerifyDom(doc, XmlcTestCaseBase.LOAD_DOM_EXT); 381 } else { 382 fTest.dumpDom(doc, XmlcTestCaseBase.LOAD_DOM_EXT); 383 } 384 385 if ((fExpectedEncoding != null) 386 && !fExpectedEncoding.equals(doc.getEncoding())) { 387 throw new TestException("expected document encoding of \"" 388 + fExpectedEncoding +" \", got \"" 389 + doc.getEncoding() + "\""); 390 } 391 392 if (fExtraSrc != null) { 393 TestFileOps.copyFilesToDir(fExtraSrc, fTest.getResultsDir()); 394 } 395 outputDocument(doc); 396 parseGenerated(); 397 } 398 399 402 public void reparseStep() { 403 reparseStep(fTest.loadTestDocument(fTest.getTestClass())); 404 } 405 406 409 public void basicTest() { 410 compileOriginalStep(); 411 if (fCompileShouldFail) { 412 return; } 414 reparseStep(); 415 } 416 } 417 | Popular Tags |