1 18 package org.apache.batik.test.xml; 19 20 import java.io.File ; 21 import java.io.StringWriter ; 22 import java.io.PrintWriter ; 23 24 import java.net.URL ; 25 import java.net.MalformedURLException ; 26 27 import java.util.HashSet ; 28 import java.util.Set ; 29 import java.util.Vector ; 30 31 import javax.xml.parsers.DocumentBuilderFactory ; 32 import javax.xml.parsers.DocumentBuilder ; 33 34 import org.apache.batik.test.DefaultTestSuite; 35 import org.apache.batik.test.DefaultTestReport; 36 import org.apache.batik.test.TestReport; 37 import org.apache.batik.test.TestSuite; 38 import org.apache.batik.test.Test; 39 import org.apache.batik.test.TestFilter; 40 import org.apache.batik.test.TestException; 41 import org.apache.batik.test.TestReportProcessor; 42 43 44 import org.w3c.dom.Element ; 45 import org.w3c.dom.Document ; 46 import org.w3c.dom.Node ; 47 import org.w3c.dom.NodeList ; 48 49 67 public class XMLTestSuiteRunner implements XTRunConstants, XTSConstants{ 68 73 public static final String MESSAGE_UNMATCHED_TEST_IDS 74 = "XMLTestSuiteRunner.messages.unmatched.test.ids"; 75 76 84 public static final String CANNOT_CREATE_TEST_REPORT_PROCESSOR 85 = "xml.XMLTestSuiteRunner.error.cannot.create.test.report.processor"; 86 87 95 public static final String TEST_SUITE_EXCEPTION 96 = "xml.XMLTestSuiteRunner.test.suite.exception"; 97 98 106 public static final String TEST_REPORT_PROCESSING_EXCEPTION 107 = "xml.XMLTestSuiteRunner.error.test.report.processing.exception"; 108 109 112 public static class AcceptAllTestsFilter implements TestFilter{ 113 public Test filter(Test t){ 114 return t; 115 } 116 } 117 118 122 public static class IdBasedTestFilter implements TestFilter { 123 protected String [] ids; 124 protected Set unmatchedIds = new HashSet (); 125 126 public IdBasedTestFilter(String [] ids){ 127 this.ids = ids; 128 for(int i=0; i<ids.length; i++){ 129 unmatchedIds.add(ids[i]); 130 } 131 } 132 133 public String traceUnusedIds(){ 134 Object [] ui = unmatchedIds.toArray(); 135 StringBuffer sb = null; 136 if(ui != null && ui.length > 0){ 137 sb = new StringBuffer (); 138 sb.append(ui[0].toString()); 139 for(int i=1; i<ui.length; i++){ 140 sb.append(", "); 141 sb.append(ui[i].toString()); 142 } 143 } 144 return sb != null ? sb.toString() : null; 145 } 146 147 151 public void filterTestSuite(TestSuite ts){ 152 Test[] t = ts.getChildrenTests(); 153 int nTests = t != null ? t.length : 0; 154 for(int i=0; i<nTests; i++){ 155 if(filter(t[i]) == null){ 156 ts.removeTest(t[i]); 157 } 158 } 159 } 160 161 168 public Test filter(Test t){ 169 String id = t.getQualifiedId(); 170 boolean isRequested = isRequestedId(id); 171 172 if(t instanceof TestSuite){ 176 TestSuite ts = (TestSuite)t; 177 filterTestSuite(ts); 178 if(ts.getChildrenCount() > 0){ 179 return t; 180 } 181 return null; 182 } 183 184 if(isRequested){ 188 return t; 189 } 190 191 return null; 192 } 193 194 protected boolean isRequestedId(String id){ 195 for(int i=0; i<ids.length; i++){ 196 if(ids[i].lastIndexOf(id) == 0){ 215 unmatchedIds.remove(ids[i]); 217 return true; 218 } 219 220 if(id.lastIndexOf(ids[i]) != -1){ 221 unmatchedIds.remove(ids[i]); 223 return true; 224 } 225 } 226 return false; 227 } 228 229 } 230 231 235 protected TestReportProcessor[] extractTestReportProcessor(Element element) 236 throws TestException 237 { 238 Vector processors = new Vector (); 239 240 NodeList children = element.getChildNodes(); 241 if(children != null && children.getLength() > 0){ 242 int n = children.getLength(); 243 for(int i=0; i<n; i++){ 244 Node child = children.item(i); 245 if(child.getNodeType() == Node.ELEMENT_NODE){ 246 Element childElement = (Element )child; 247 String tagName = childElement.getTagName().intern(); 248 if(tagName == XTRun_TEST_REPORT_PROCESSOR_TAG){ 249 processors.addElement(buildProcessor(childElement)); 250 } 251 } 252 } 253 } 254 255 TestReportProcessor[] p = null; 256 if(processors.size() > 0){ 257 p = new TestReportProcessor[processors.size()]; 258 processors.copyInto(p); 259 } 260 261 return p; 262 } 263 264 267 protected TestReportProcessor buildProcessor(Element element) 268 throws TestException { 269 try{ 270 return (TestReportProcessor)XMLReflect.buildObject(element); 271 }catch(Exception e){ 272 StringWriter sw = new StringWriter (); 273 PrintWriter pw = new PrintWriter (sw); 274 e.printStackTrace(pw); 275 throw new TestException(CANNOT_CREATE_TEST_REPORT_PROCESSOR, 276 new Object [] { element.getAttribute(XR_CLASS_ATTRIBUTE), 277 e.getClass().getName(), 278 e.getMessage(), 279 sw.toString() }, 280 e); 281 } 282 } 283 284 291 protected DefaultTestSuite buildTestRunTestSuite(Element element) 292 throws TestException { 293 DefaultTestSuite testSuite = new DefaultTestSuite(); 294 295 String name = element.getAttribute(XTRun_NAME_ATTRIBUTE); 299 testSuite.setName(name); 300 301 String id = element.getAttribute(XTRun_ID_ATTRIBUTE); 302 testSuite.setId(id); 303 304 Element [] testSuites 305 = getChildrenByTagName(element, XTRun_TEST_SUITE_TAG); 306 307 int n = testSuites != null ? testSuites.length : 0; 308 for(int i=0; i<n; i++){ 309 String suiteHref = 310 testSuites[i].getAttribute(XTRun_HREF_ATTRIBUTE); 311 312 Test test = XMLTestSuiteLoader.loadTestSuite(suiteHref, testSuite); 313 if(test != null){ 314 testSuite.addTest(test); 315 } 316 } 317 318 return testSuite; 319 } 320 321 324 protected Element [] getChildrenByTagName(Element element, 325 String tagName) 326 { 327 tagName = tagName.intern(); 328 Vector childrenWithTagName = new Vector (); 329 330 NodeList children = element.getChildNodes(); 331 if(children != null && children.getLength() > 0){ 332 int n = children.getLength(); 333 for(int i=0; i<n; i++){ 334 Node child = children.item(i); 335 if(child.getNodeType() == Node.ELEMENT_NODE){ 336 Element childElement = (Element )child; 337 String childTagName = childElement.getTagName().intern(); 338 if(childTagName == tagName){ 339 childrenWithTagName.addElement(childElement); 340 } 341 } 342 } 343 } 344 345 Element [] a = null; 346 if(childrenWithTagName.size() > 0){ 347 a = new Element [childrenWithTagName.size()]; 348 childrenWithTagName.copyInto(a); 349 } 350 351 return a; 352 } 353 354 355 362 public TestReport run(Document doc, String [] ids) 363 throws TestException { 364 Element root = doc.getDocumentElement(); 365 366 return run(root, ids); 367 } 368 369 protected TestReport runTest(Test test) 370 throws TestException { 371 try{ 372 return test.run(); 373 }catch(Exception e){ 374 StringWriter sw = new StringWriter (); 375 PrintWriter pw = new PrintWriter (sw); 376 e.printStackTrace(pw); 377 throw new TestException(TEST_SUITE_EXCEPTION, 378 new Object [] { test.getName(), 379 test.getClass().getName(), 380 e.getClass().getName(), 381 e.getMessage(), 382 sw.toString() }, 383 e); 384 } 385 } 386 387 protected void processReport(TestReport report, 388 TestReportProcessor[] processors) 389 throws TestException { 390 int n = processors.length; 391 int i=0; 392 try{ 393 for(; i<n; i++){ 394 processors[i].processReport(report); 395 } 396 }catch(Exception e){ 397 StringWriter sw = new StringWriter (); 398 PrintWriter pw = new PrintWriter (sw); 399 e.printStackTrace(pw); 400 throw new TestException(TEST_REPORT_PROCESSING_EXCEPTION, 401 new Object [] { processors[i].getClass().getName(), 402 e.getClass().getName(), 403 e.getMessage(), 404 sw.toString() }, 405 e); 406 } 407 } 408 409 protected TestReport run(Element testRunElement, String [] ids) 410 throws TestException{ 411 Test testRun 415 = buildTestRunTestSuite(testRunElement); 416 417 Test filteredTestRun = testRun; 421 if(ids != null && ids.length > 0){ 422 IdBasedTestFilter filter = new IdBasedTestFilter(ids); 423 filteredTestRun = filter.filter(testRun); 424 String unusedIds = filter.traceUnusedIds(); 425 if(unusedIds != null){ 426 System.err.println(Messages.formatMessage(MESSAGE_UNMATCHED_TEST_IDS, 427 new Object []{unusedIds})); 428 } 429 } 430 431 if(filteredTestRun == null){ 432 DefaultTestReport report 433 = new DefaultTestReport(testRun); 434 report.setPassed(true); 435 return report; 436 } 437 438 TestReportProcessor[] processors 443 = extractTestReportProcessor(testRunElement); 444 445 TestReport report = runTest(testRun); 449 450 if(processors != null){ 454 processReport(report, processors); 455 } 456 457 return report; 458 } 459 460 463 public static final String USAGE 464 = "XMLTestSuiteRunner.messages.error.usage"; 465 466 471 public static final String NOT_A_FILE_TRY_URI 472 = "XMLTestSuiteRunner.messages.error.not.a.file.try.uri"; 473 474 477 public static final String COULD_NOT_CONVERT_FILE_NAME_TO_URI 478 = "XMLTestSuiteRunner.messages.error.could.not.convert.file.name.to.uri"; 479 480 484 public static final String INVALID_URI 485 = "XMLTestSuiteRunner.messages.error.invalid.uri"; 486 487 493 public static final String INVALID_DOCUMENT 494 = "XMLTestSuiteRunner.messages.error.invalid.document"; 495 496 500 public static final String ERROR_RUNNING_TEST_SUITE 501 = "XMLTestSuiteRunner.messages.error.running.test.suite"; 502 503 public static void main(String [] args) { 504 if(args.length < 1){ 505 System.err.println(Messages.formatMessage(USAGE, null)); 506 System.exit(0); 507 } 508 509 String uriStr = args[0]; 510 String [] ids = new String [args.length - 1]; 511 System.arraycopy(args, 1, ids, 0, args.length-1); 512 513 File file = new File (uriStr); 514 URL url = null; 515 if(file.exists()){ 516 try { 517 url = file.toURL(); 518 }catch(MalformedURLException e){ 519 System.err.println(Messages.formatMessage(COULD_NOT_CONVERT_FILE_NAME_TO_URI, 520 new Object []{uriStr})); 521 System.exit(0); 522 } 523 } 524 525 else { 526 System.err.println(Messages.formatMessage(NOT_A_FILE_TRY_URI, 527 new Object []{uriStr})); 528 try{ 529 url = new URL (uriStr); 530 }catch(MalformedURLException e){ 531 System.err.println(Messages.formatMessage(INVALID_URI, 532 new Object []{uriStr})); 533 System.exit(0); 534 } 535 } 536 537 Document doc = null; 538 539 try{ 540 System.err.println("Loading document ..."); 541 542 DocumentBuilder docBuilder 543 = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 544 545 doc = docBuilder.parse(url.toString()); 546 }catch(Exception e){ 547 e.printStackTrace(); 548 System.err.println(Messages.formatMessage(INVALID_DOCUMENT, 549 new Object [] { uriStr, 550 e.getClass().getName(), 551 e.getMessage() })); 552 System.exit(0); 553 } 554 555 try{ 556 System.err.println("Running test run..."); 557 XMLTestSuiteRunner r = new XMLTestSuiteRunner(); 558 r.run(doc, ids); 559 }catch(TestException e){ 560 System.err.println(Messages.formatMessage(ERROR_RUNNING_TEST_SUITE, 561 new Object [] { e.getMessage() })); 562 System.exit(0); 563 } 564 565 System.exit(1); 566 567 } 568 } 569 | Popular Tags |