1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package tests.api.javax.xml.parsers; 17 18 import dalvik.annotation.KnownFailure; 19 import java.io.File; 20 import java.io.FileInputStream; 21 import java.io.FileNotFoundException; 22 import java.io.IOException; 23 import java.io.InputStream; 24 import java.io.InputStreamReader; 25 import java.util.HashMap; 26 import java.util.Vector; 27 import javax.xml.parsers.SAXParser; 28 import javax.xml.parsers.SAXParserFactory; 29 import junit.framework.TestCase; 30 import org.xml.sax.HandlerBase; 31 import org.xml.sax.InputSource; 32 import org.xml.sax.Parser; 33 import org.xml.sax.SAXException; 34 import org.xml.sax.SAXNotRecognizedException; 35 import org.xml.sax.SAXNotSupportedException; 36 import org.xml.sax.XMLReader; 37 import org.xml.sax.ext.LexicalHandler; 38 import org.xml.sax.helpers.DefaultHandler; 39 import tests.api.javax.xml.parsers.SAXParserTestSupport.MyDefaultHandler; 40 import tests.api.javax.xml.parsers.SAXParserTestSupport.MyHandler; 41 import tests.api.org.xml.sax.support.BrokenInputStream; 42 import tests.api.org.xml.sax.support.MethodLogger; 43 import tests.api.org.xml.sax.support.MockHandler; 44 import tests.support.resource.Support_Resources; 45 46 @SuppressWarnings("deprecation") 47 public class SAXParserTest extends TestCase { 48 49 private class MockSAXParser extends SAXParser { 50 public MockSAXParser() { 51 super(); 52 } 53 54 /* 55 * @see javax.xml.parsers.SAXParser#getParser() 56 */ 57 @Override 58 public Parser getParser() throws SAXException { 59 // it is a fake 60 return null; 61 } 62 63 /* 64 * @see javax.xml.parsers.SAXParser#getProperty(java.lang.String) 65 */ 66 @Override 67 public Object getProperty(String name) throws SAXNotRecognizedException, 68 SAXNotSupportedException { 69 // it is a fake 70 return null; 71 } 72 73 /* 74 * @see javax.xml.parsers.SAXParser#getXMLReader() 75 */ 76 @Override 77 public XMLReader getXMLReader() throws SAXException { 78 // it is a fake 79 return null; 80 } 81 82 /* 83 * @see javax.xml.parsers.SAXParser#isNamespaceAware() 84 */ 85 @Override 86 public boolean isNamespaceAware() { 87 // it is a fake 88 return false; 89 } 90 91 /* 92 * @see javax.xml.parsers.SAXParser#isValidating() 93 */ 94 @Override 95 public boolean isValidating() { 96 // it is a fake 97 return false; 98 } 99 100 /* 101 * @see javax.xml.parsers.SAXParser#setProperty(java.lang.String, 102 * java.lang.Object) 103 */ 104 @Override 105 public void setProperty(String name, Object value) throws 106 SAXNotRecognizedException, SAXNotSupportedException { 107 // it is a fake 108 } 109 } 110 111 private static final String LEXICAL_HANDLER_PROPERTY 112 = "http://xml.org/sax/properties/lexical-handler"; 113 114 SAXParserFactory spf; 115 116 SAXParser parser; 117 118 static HashMap<String, String> ns; 119 120 static Vector<String> el; 121 122 static HashMap<String, String> attr; 123 124 SAXParserTestSupport sp = new SAXParserTestSupport(); 125 126 File [] list_wf; 127 File [] list_nwf; 128 File [] list_out_dh; 129 File [] list_out_hb; 130 131 boolean validating = false; 132 133 private InputStream getResource(String name) { 134 return this.getClass().getResourceAsStream(name); 135 } 136 137 public void initFiles() throws Exception { 138 // we differntiate between a validating and a non validating parser 139 try { 140 SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); 141 validating = parser.isValidating(); 142 } catch (Exception e) { 143 fail("could not obtain a SAXParser"); 144 } 145 146 String tmpPath = System.getProperty("java.io.tmpdir"); 147 148 // nwf = not well formed, wf = well formed 149 list_wf = new File[] {new File(tmpPath + "/" + 150 SAXParserTestSupport.XML_WF + "staff.xml")}; 151 list_nwf = new File[] {new File(tmpPath + "/" + 152 SAXParserTestSupport.XML_NWF + "staff.xml")}; 153 list_out_dh = new File[] {new File(tmpPath + "/" + 154 SAXParserTestSupport.XML_WF_OUT_DH + "staff.out")}; 155 list_out_hb = new File[] {new File(tmpPath + "/" + 156 SAXParserTestSupport.XML_WF_OUT_HB + "staff.out")}; 157 158 list_wf[0].deleteOnExit(); 159 list_nwf[0].deleteOnExit(); 160 list_out_hb[0].deleteOnExit(); 161 list_out_dh[0].deleteOnExit(); 162 163 164 Support_Resources.copyLocalFileto(list_wf[0], 165 getResource(SAXParserTestSupport.XML_WF + "staff.xml")); 166 Support_Resources.copyLocalFileto(new File( 167 tmpPath + "/" + SAXParserTestSupport.XML_WF + "staff.dtd"), 168 getResource(SAXParserTestSupport.XML_WF + "staff.dtd")); 169 170 Support_Resources.copyLocalFileto(list_nwf[0], 171 getResource(SAXParserTestSupport.XML_NWF + "staff.xml")); 172 Support_Resources.copyLocalFileto(new File( 173 tmpPath + "/" + SAXParserTestSupport.XML_NWF + "staff.dtd"), 174 getResource(SAXParserTestSupport.XML_NWF + "staff.dtd")); 175 176 Support_Resources.copyLocalFileto(list_out_dh[0], 177 getResource(SAXParserTestSupport.XML_WF_OUT_DH + "staff.out")); 178 Support_Resources.copyLocalFileto(list_out_hb[0], 179 getResource(SAXParserTestSupport.XML_WF_OUT_HB + "staff.out")); 180 } 181 182 @Override 183 protected void setUp() throws Exception { 184 spf = SAXParserFactory.newInstance(); 185 parser = spf.newSAXParser(); 186 assertNotNull(parser); 187 188 ns = new HashMap<String, String>(); 189 attr = new HashMap<String, String>(); 190 el = new Vector<String>(); 191 initFiles(); 192 } 193 194 @Override 195 protected void tearDown() throws Exception { 196 } 197 198 // public static void main(String[] args) throws Exception { 199 // SAXParserTest st = new SAXParserTest(); 200 // st.setUp(); 201 // st.generateDataFromReferenceImpl(); 202 // 203 // } 204 // 205 // private void generateDataFromReferenceImpl() { 206 // try { 207 // for(int i = 0; i < list_wf.length; i++) { 208 // MyDefaultHandler dh = new MyDefaultHandler(); 209 // InputStream is = new FileInputStream(list_wf[i]); 210 // parser.parse(is, dh, ParsingSupport.XML_SYSTEM_ID); 211 // HashMap refHm = dh.createData(); 212 // 213 // StringBuilder sb = new StringBuilder(); 214 // for (int j = 0; j < ParsingSupport.KEYS.length; j++) { 215 // String key = ParsingSupport.KEYS[j]; 216 // sb.append(refHm.get(key)).append( 217 // ParsingSupport.SEPARATOR_DATA); 218 // } 219 // FileWriter fw = new FileWriter("/tmp/build_dh"+i+".out"); 220 // fw.append(sb.toString()); 221 // fw.close(); 222 // } 223 // 224 // for(int i = 0; i < list_nwf.length; i++) { 225 // MyHandler hb = new MyHandler(); 226 // InputStream is = new FileInputStream(list_wf[i]); 227 // parser.parse(is, hb, ParsingSupport.XML_SYSTEM_ID); 228 // HashMap refHm = hb.createData(); 229 // 230 // StringBuilder sb = new StringBuilder(); 231 // for (int j = 0; j < ParsingSupport.KEYS.length; j++) { 232 // String key = ParsingSupport.KEYS[j]; 233 // sb.append(refHm.get(key)).append( 234 // ParsingSupport.SEPARATOR_DATA); 235 // } 236 // FileWriter fw = new FileWriter("/tmp/build_hb"+i+".out"); 237 // fw.append(sb.toString()); 238 // fw.close(); 239 // } 240 // 241 // 242 // } catch (Exception e) { 243 // e.printStackTrace(); 244 // } 245 // } 246 247 public void testSAXParser() { 248 try { 249 new MockSAXParser(); 250 } catch (Exception e) { 251 fail("unexpected exception " + e.toString()); 252 } 253 } 254 255 /** 256 * javax.xml.parser.SAXParser#getSchema(). 257 * TODO getSchema() IS NOT SUPPORTED 258 */ 259 /* public void test_getSchema() { 260 assertNull(parser.getSchema()); 261 SchemaFactory sf = 262 SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 263 try { 264 Schema schema = sf.newSchema(); 265 spf.setSchema(schema); 266 assertNotNull(spf.newSAXParser().getSchema()); 267 } catch (ParserConfigurationException pce) { 268 fail("Unexpected ParserConfigurationException " + pce.toString()); 269 } catch (SAXException sax) { 270 fail("Unexpected SAXException " + sax.toString()); 271 } 272 } 273 */ 274 275 public void testIsNamespaceAware() { 276 try { 277 spf.setNamespaceAware(true); 278 assertTrue(spf.newSAXParser().isNamespaceAware()); 279 spf.setNamespaceAware(false); 280 assertFalse(spf.newSAXParser().isNamespaceAware()); 281 } catch (Exception e) { 282 throw new RuntimeException("Unexpected exception", e); 283 } 284 } 285 286 public void testIsValidating() { 287 try { 288 spf.setValidating(false); 289 assertFalse(spf.newSAXParser().isValidating()); 290 } catch (Exception e) { 291 throw new RuntimeException("Unexpected exception", e); 292 } 293 } 294 295 public void testIsXIncludeAware() { 296 try { 297 spf.setXIncludeAware(false); 298 assertFalse(spf.newSAXParser().isXIncludeAware()); 299 } catch (Exception e) { 300 throw new RuntimeException("Unexpected exception", e); 301 } 302 } 303 304 public void test_parseLjava_io_FileLorg_xml_sax_helpers_DefaultHandler() throws Exception { 305 306 for(int i = 0; i < list_wf.length; i++) { 307 HashMap<String, String> hm = 308 new SAXParserTestSupport().readFile(list_out_dh[i].getPath()); 309 MyDefaultHandler dh = new MyDefaultHandler(); 310 parser.parse(list_wf[i], dh); 311 assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData())); 312 } 313 314 for(int i = 0; i < list_nwf.length; i++) { 315 try { 316 MyDefaultHandler dh = new MyDefaultHandler(); 317 parser.parse(list_nwf[i], dh); 318 fail("SAXException is not thrown"); 319 } catch(org.xml.sax.SAXException se) { 320 //expected 321 } 322 } 323 324 try { 325 MyDefaultHandler dh = new MyDefaultHandler(); 326 parser.parse((File) null, dh); 327 fail("java.lang.IllegalArgumentException is not thrown"); 328 } catch(java.lang.IllegalArgumentException iae) { 329 //expected 330 } 331 332 try { 333 parser.parse(list_wf[0], (DefaultHandler) null); 334 } catch(java.lang.IllegalArgumentException iae) { 335 fail("java.lang.IllegalArgumentException is thrown"); 336 } 337 } 338 339 public void testParseFileHandlerBase() { 340 for(int i = 0; i < list_wf.length; i++) { 341 try { 342 HashMap<String, String> hm = sp.readFile( 343 list_out_hb[i].getPath()); 344 MyHandler dh = new MyHandler(); 345 parser.parse(list_wf[i], dh); 346 assertTrue(SAXParserTestSupport.equalsMaps(hm, 347 dh.createData())); 348 } catch (IOException ioe) { 349 fail("Unexpected IOException " + ioe.toString()); 350 } catch (SAXException sax) { 351 fail("Unexpected SAXException " + sax.toString()); 352 } 353 } 354 355 for(int i = 0; i < list_nwf.length; i++) { 356 try { 357 MyHandler dh = new MyHandler(); 358 parser.parse(list_nwf[i], dh); 359 fail("SAXException is not thrown"); 360 } catch(org.xml.sax.SAXException se) { 361 //expected 362 } catch (FileNotFoundException fne) { 363 fail("Unexpected FileNotFoundException " + fne.toString()); 364 } catch (IOException ioe) { 365 fail("Unexpected IOException " + ioe.toString()); 366 } 367 } 368 369 try { 370 MyHandler dh = new MyHandler(); 371 parser.parse((File) null, dh); 372 fail("java.lang.IllegalArgumentException is not thrown"); 373 } catch(java.lang.IllegalArgumentException iae) { 374 //expected 375 } catch (IOException ioe) { 376 fail("Unexpected IOException " + ioe.toString()); 377 } catch(SAXException sax) { 378 fail("Unexpected SAXException " + sax.toString()); 379 } 380 381 try { 382 parser.parse(list_wf[0], (HandlerBase) null); 383 } catch(java.lang.IllegalArgumentException iae) { 384 fail("java.lang.IllegalArgumentException is thrown"); 385 } catch (FileNotFoundException fne) { 386 fail("Unexpected FileNotFoundException " + fne.toString()); 387 } catch(IOException ioe) { 388 fail("Unexpected IOException " + ioe.toString()); 389 } catch(SAXException sax) { 390 fail("Unexpected SAXException " + sax.toString()); 391 } 392 } 393 394 public void test_parseLorg_xml_sax_InputSourceLorg_xml_sax_helpers_DefaultHandler() 395 throws Exception { 396 for(int i = 0; i < list_wf.length; i++) { 397 HashMap<String, String> hm = new SAXParserTestSupport().readFile( 398 list_out_dh[i].getPath()); 399 MyDefaultHandler dh = new MyDefaultHandler(); 400 InputSource is = new InputSource(new FileInputStream(list_wf[i])); 401 parser.parse(is, dh); 402 assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData())); 403 } 404 405 for (File file : list_nwf) { 406 try { 407 MyDefaultHandler dh = new MyDefaultHandler(); 408 InputSource is = new InputSource(new FileInputStream(file)); 409 parser.parse(is, dh); 410 fail("SAXException is not thrown"); 411 } catch (SAXException expected) { 412 } 413 } 414 415 try { 416 MyDefaultHandler dh = new MyDefaultHandler(); 417 parser.parse((InputSource) null, dh); 418 fail("java.lang.IllegalArgumentException is not thrown"); 419 } catch (IllegalArgumentException expected) { 420 } 421 422 InputSource is = new InputSource(new FileInputStream(list_wf[0])); 423 parser.parse(is, (DefaultHandler) null); 424 425 InputStream in = null; 426 try { 427 in = new BrokenInputStream(new FileInputStream(list_wf[0]), 10); 428 is = new InputSource(in); 429 parser.parse(is, (DefaultHandler) null); 430 fail("IOException expected"); 431 } catch(IOException expected) { 432 } finally { 433 in.close(); 434 } 435 } 436 437 public void testParseInputSourceHandlerBase() throws Exception { 438 for(int i = 0; i < list_wf.length; i++) { 439 HashMap<String, String> hm = sp.readFile(list_out_hb[i].getPath()); 440 MyHandler dh = new MyHandler(); 441 InputSource is = new InputSource(new FileInputStream(list_wf[i])); 442 parser.parse(is, dh); 443 assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData())); 444 } 445 446 for (File file : list_nwf) { 447 try { 448 MyHandler dh = new MyHandler(); 449 InputSource is = new InputSource(new FileInputStream(file)); 450 parser.parse(is, dh); 451 fail("SAXException is not thrown"); 452 } catch (SAXException expected) { 453 } 454 } 455 456 try { 457 MyHandler dh = new MyHandler(); 458 parser.parse((InputSource) null, dh); 459 fail("java.lang.IllegalArgumentException is not thrown"); 460 } catch(IllegalArgumentException expected) { 461 } 462 463 InputSource is = new InputSource(new FileInputStream(list_wf[0])); 464 parser.parse(is, (HandlerBase) null); 465 466 // Reader case 467 is = new InputSource(new InputStreamReader(new FileInputStream(list_wf[0]))); 468 parser.parse(is, (HandlerBase) null); 469 470 // SystemID case 471 is = new InputSource(list_wf[0].toURI().toString()); 472 parser.parse(is, (HandlerBase) null); 473 474 // Inject IOException 475 InputStream in = null; 476 try { 477 in = new BrokenInputStream(new FileInputStream(list_wf[0]), 10); 478 parser.parse(in, (HandlerBase) null, SAXParserTestSupport.XML_SYSTEM_ID); 479 fail("IOException expected"); 480 } catch(IOException expected) { 481 } finally { 482 in.close(); 483 } 484 } 485 486 public void test_parseLjava_io_InputStreamLorg_xml_sax_helpers_DefaultHandler() throws Exception { 487 488 for(int i = 0; i < list_wf.length; i++) { 489 490 HashMap<String, String> hm = new SAXParserTestSupport().readFile( 491 list_out_dh[i].getPath()); 492 MyDefaultHandler dh = new MyDefaultHandler(); 493 InputStream is = new FileInputStream(list_wf[i]); 494 parser.parse(is, dh); 495 assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData())); 496 } 497 498 for(int i = 0; i < list_nwf.length; i++) { 499 try { 500 MyDefaultHandler dh = new MyDefaultHandler(); 501 InputStream is = new FileInputStream(list_nwf[i]); 502 parser.parse(is, dh); 503 fail("SAXException is not thrown"); 504 } catch(org.xml.sax.SAXException se) { 505 //expected 506 } 507 } 508 509 try { 510 MyDefaultHandler dh = new MyDefaultHandler(); 511 parser.parse((InputStream) null, dh); 512 fail("java.lang.IllegalArgumentException is not thrown"); 513 } catch(java.lang.IllegalArgumentException iae) { 514 //expected 515 } 516 517 try { 518 InputStream is = new FileInputStream(list_wf[0]); 519 parser.parse(is, (DefaultHandler) null); 520 } catch(java.lang.IllegalArgumentException iae) { 521 fail("java.lang.IllegalArgumentException is thrown"); 522 } 523 } 524 525 @KnownFailure("We supply optional qnames, but this test doesn't expect them") 526 public void test_parseLjava_io_InputStreamLorg_xml_sax_helpers_DefaultHandlerLjava_lang_String() { 527 for(int i = 0; i < list_wf.length; i++) { 528 try { 529 HashMap<String, String> hm = sp.readFile( 530 list_out_hb[i].getPath()); 531 MyDefaultHandler dh = new MyDefaultHandler(); 532 InputStream is = new FileInputStream(list_wf[i]); 533 parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID); 534 assertEquals(hm, dh.createData()); 535 } catch (IOException ioe) { 536 fail("Unexpected IOException " + ioe.toString()); 537 } catch (SAXException sax) { 538 fail("Unexpected SAXException " + sax.toString()); 539 } 540 } 541 542 for(int i = 0; i < list_nwf.length; i++) { 543 try { 544 MyDefaultHandler dh = new MyDefaultHandler(); 545 InputStream is = new FileInputStream(list_nwf[i]); 546 parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID); 547 fail("SAXException is not thrown"); 548 } catch(org.xml.sax.SAXException se) { 549 //expected 550 } catch (FileNotFoundException fne) { 551 fail("Unexpected FileNotFoundException " + fne.toString()); 552 } catch (IOException ioe) { 553 fail("Unexpected IOException " + ioe.toString()); 554 } 555 } 556 557 try { 558 MyDefaultHandler dh = new MyDefaultHandler(); 559 parser.parse((InputStream) null, dh, 560 SAXParserTestSupport.XML_SYSTEM_ID); 561 fail("java.lang.IllegalArgumentException is not thrown"); 562 } catch(java.lang.IllegalArgumentException iae) { 563 //expected 564 } catch (IOException ioe) { 565 fail("Unexpected IOException " + ioe.toString()); 566 } catch(SAXException sax) { 567 fail("Unexpected SAXException " + sax.toString()); 568 } 569 570 try { 571 InputStream is = new FileInputStream(list_wf[0]); 572 parser.parse(is, (DefaultHandler) null, 573 SAXParserTestSupport.XML_SYSTEM_ID); 574 } catch(java.lang.IllegalArgumentException iae) { 575 fail("java.lang.IllegalArgumentException is thrown"); 576 } catch (FileNotFoundException fne) { 577 fail("Unexpected FileNotFoundException " + fne.toString()); 578 } catch(IOException ioe) { 579 fail("Unexpected IOException " + ioe.toString()); 580 } catch(SAXException sax) { 581 fail("Unexpected SAXException " + sax.toString()); 582 } 583 // 584 // for(int i = 0; i < list_wf.length; i++) { 585 // 586 // HashMap<String, String> hm = new SAXParserTestSupport().readFile( 587 // list_out_dh[i].getPath()); 588 // MyDefaultHandler dh = new MyDefaultHandler(); 589 // InputStream is = new FileInputStream(list_wf[i]); 590 // parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID); 591 // assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData())); 592 // } 593 // 594 // for(int i = 0; i < list_nwf.length; i++) { 595 // try { 596 // MyDefaultHandler dh = new MyDefaultHandler(); 597 // InputStream is = new FileInputStream(list_nwf[i]); 598 // parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID); 599 // fail("SAXException is not thrown"); 600 // } catch(org.xml.sax.SAXException se) { 601 // //expected 602 // } 603 // } 604 // 605 // try { 606 // MyDefaultHandler dh = new MyDefaultHandler(); 607 // parser.parse((InputStream) null, dh, 608 // SAXParserTestSupport.XML_SYSTEM_ID); 609 // fail("java.lang.IllegalArgumentException is not thrown"); 610 // } catch(java.lang.IllegalArgumentException iae) { 611 // //expected 612 // } 613 // 614 // try { 615 // InputStream is = new FileInputStream(list_wf[0]); 616 // parser.parse(is, (DefaultHandler) null, 617 // SAXParserTestSupport.XML_SYSTEM_ID); 618 // } catch(java.lang.IllegalArgumentException iae) { 619 // fail("java.lang.IllegalArgumentException is thrown"); 620 // } 621 // 622 // // TODO commented out since our parser is nonvalidating and thus never 623 // // tries to load staff.dtd in "/" ... and therefore never can fail with 624 // // an IOException 625 // /*try { 626 // MyDefaultHandler dh = new MyDefaultHandler(); 627 // InputStream is = new FileInputStream(list_wf[0]); 628 // parser.parse(is, dh, "/"); 629 // fail("Expected IOException was not thrown"); 630 // } catch(IOException ioe) { 631 // // expected 632 // }*/ 633 } 634 635 public void testParseInputStreamHandlerBase() throws Exception { 636 for(int i = 0; i < list_wf.length; i++) { 637 HashMap<String, String> hm = sp.readFile(list_out_hb[i].getPath()); 638 MyHandler dh = new MyHandler(); 639 InputStream is = new FileInputStream(list_wf[i]); 640 parser.parse(is, dh); 641 assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData())); 642 } 643 644 for (File file : list_nwf) { 645 try { 646 MyHandler dh = new MyHandler(); 647 InputStream is = new FileInputStream(file); 648 parser.parse(is, dh); 649 fail("SAXException is not thrown"); 650 } catch (SAXException expected) { 651 } 652 } 653 654 try { 655 MyHandler dh = new MyHandler(); 656 parser.parse((InputStream) null, dh); 657 fail("java.lang.IllegalArgumentException is not thrown"); 658 } catch (IllegalArgumentException expected) { 659 } 660 661 InputStream is = new FileInputStream(list_wf[0]); 662 parser.parse(is, (HandlerBase) null); 663 664 // Inject IOException 665 try { 666 is = new BrokenInputStream(new FileInputStream(list_wf[0]), 10); 667 parser.parse(is, (HandlerBase) null); 668 fail("IOException expected"); 669 } catch(IOException e) { 670 // Expected 671 } finally { 672 is.close(); 673 } 674 } 675 676 public void testParseInputStreamHandlerBaseString() throws Exception { 677 for(int i = 0; i < list_wf.length; i++) { 678 HashMap<String, String> hm = sp.readFile(list_out_hb[i].getPath()); 679 MyHandler dh = new MyHandler(); 680 InputStream is = new FileInputStream(list_wf[i]); 681 parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID); 682 assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData())); 683 } 684 685 for (File file : list_nwf) { 686 try { 687 MyHandler dh = new MyHandler(); 688 InputStream is = new FileInputStream(file); 689 parser.parse(is, dh, SAXParserTestSupport.XML_SYSTEM_ID); 690 fail("SAXException is not thrown"); 691 } catch (SAXException expected) { 692 } 693 } 694 695 try { 696 MyHandler dh = new MyHandler(); 697 parser.parse(null, dh, SAXParserTestSupport.XML_SYSTEM_ID); 698 fail("java.lang.IllegalArgumentException is not thrown"); 699 } catch(IllegalArgumentException expected) { 700 } 701 702 InputStream is = new FileInputStream(list_wf[0]); 703 parser.parse(is, (HandlerBase) null, SAXParserTestSupport.XML_SYSTEM_ID); 704 705 // Inject IOException 706 try { 707 is = new BrokenInputStream(new FileInputStream(list_wf[0]), 10); 708 parser.parse(is, (HandlerBase) null, SAXParserTestSupport.XML_SYSTEM_ID); 709 fail("IOException expected"); 710 } catch(IOException expected) { 711 } finally { 712 is.close(); 713 } 714 } 715 716 public void test_parseLjava_lang_StringLorg_xml_sax_helpers_DefaultHandler() throws Exception { 717 718 for(int i = 0; i < list_wf.length; i++) { 719 720 HashMap<String, String> hm = new SAXParserTestSupport().readFile( 721 list_out_dh[i].getPath()); 722 MyDefaultHandler dh = new MyDefaultHandler(); 723 parser.parse(list_wf[i].toURI().toString(), dh); 724 assertTrue(SAXParserTestSupport.equalsMaps(hm, dh.createData())); 725 } 726 727 for(int i = 0; i < list_nwf.length; i++) { 728 try { 729 MyDefaultHandler dh = new MyDefaultHandler(); 730 parser.parse(list_nwf[i].toURI().toString(), dh); 731 fail("SAXException is not thrown"); 732 } catch(org.xml.sax.SAXException se) { 733 //expected 734 } 735 } 736 737 try { 738 MyDefaultHandler dh = new MyDefaultHandler(); 739 parser.parse((String) null, dh); 740 fail("java.lang.IllegalArgumentException is not thrown"); 741 } catch(java.lang.IllegalArgumentException iae) { 742 //expected 743 } 744 745 try { 746 parser.parse(list_wf[0].toURI().toString(), (DefaultHandler) null); 747 } catch(java.lang.IllegalArgumentException iae) { 748 fail("java.lang.IllegalArgumentException is thrown"); 749 } 750 } 751 752 public void testParseStringHandlerBase() { 753 for(int i = 0; i < list_wf.length; i++) { 754 try { 755 HashMap<String, String> hm = sp.readFile( 756 list_out_hb[i].getPath()); 757 MyHandler dh = new MyHandler(); 758 parser.parse(list_wf[i].toURI().toString(), dh); 759 assertTrue(SAXParserTestSupport.equalsMaps(hm, 760 dh.createData())); 761 } catch (IOException ioe) { 762 fail("Unexpected IOException " + ioe.toString()); 763 } catch (SAXException sax) { 764 fail("Unexpected SAXException " + sax.toString()); 765 } 766 } 767 768 for(int i = 0; i < list_nwf.length; i++) { 769 try { 770 MyHandler dh = new MyHandler(); 771 parser.parse(list_nwf[i].toURI().toString(), dh); 772 fail("SAXException is not thrown"); 773 } catch(org.xml.sax.SAXException se) { 774 //expected 775 } catch (FileNotFoundException fne) { 776 fail("Unexpected FileNotFoundException " + fne.toString()); 777 } catch (IOException ioe) { 778 fail("Unexpected IOException " + ioe.toString()); 779 } 780 } 781 782 try { 783 MyHandler dh = new MyHandler(); 784 parser.parse((String) null, dh); 785 fail("java.lang.IllegalArgumentException is not thrown"); 786 } catch(java.lang.IllegalArgumentException iae) { 787 //expected 788 } catch (IOException ioe) { 789 fail("Unexpected IOException " + ioe.toString()); 790 } catch(SAXException sax) { 791 fail("Unexpected SAXException " + sax.toString()); 792 } 793 794 try { 795 parser.parse(list_wf[0].toURI().toString(), (HandlerBase) null); 796 } catch(java.lang.IllegalArgumentException iae) { 797 fail("java.lang.IllegalArgumentException is thrown"); 798 } catch (FileNotFoundException fne) { 799 fail("Unexpected FileNotFoundException " + fne.toString()); 800 } catch(IOException ioe) { 801 fail("Unexpected IOException " + ioe.toString()); 802 } catch(SAXException sax) { 803 fail("Unexpected SAXException " + sax.toString()); 804 } 805 } 806 807 public void testReset() { 808 try { 809 spf = SAXParserFactory.newInstance(); 810 parser = spf.newSAXParser(); 811 812 parser.setProperty(LEXICAL_HANDLER_PROPERTY, new MockHandler(new MethodLogger())); 813 parser.reset(); 814 assertEquals(null, parser.getProperty(LEXICAL_HANDLER_PROPERTY)); 815 } catch (Exception e) { 816 throw new RuntimeException("Unexpected exception", e); 817 } 818 } 819 820 public void testGetParser() { 821 spf = SAXParserFactory.newInstance(); 822 try { 823 Parser parser = spf.newSAXParser().getParser(); 824 assertNotNull(parser); 825 } catch (Exception e) { 826 throw new RuntimeException("Unexpected exception", e); 827 } 828 } 829 830 public void testGetReader() { 831 spf = SAXParserFactory.newInstance(); 832 try { 833 XMLReader reader = spf.newSAXParser().getXMLReader(); 834 assertNotNull(reader); 835 } catch (Exception e) { 836 throw new RuntimeException("Unexpected exception", e); 837 } 838 } 839 840 public void testSetGetProperty() { 841 // Ordinary case 842 String validName = "http://xml.org/sax/properties/lexical-handler"; 843 LexicalHandler validValue = new MockHandler(new MethodLogger()); 844 845 try { 846 SAXParser parser = spf.newSAXParser(); 847 parser.setProperty(validName, validValue); 848 assertEquals(validValue, parser.getProperty(validName)); 849 850 parser.setProperty(validName, null); 851 assertEquals(null, parser.getProperty(validName)); 852 } catch (Exception e) { 853 throw new RuntimeException("Unexpected exception", e); 854 } 855 856 // Unsupported property 857 try { 858 SAXParser parser = spf.newSAXParser(); 859 parser.setProperty("foo", "bar"); 860 fail("SAXNotRecognizedException expected"); 861 } catch (SAXNotRecognizedException e) { 862 // Expected 863 } catch (Exception e) { 864 throw new RuntimeException("Unexpected exception", e); 865 } 866 867 try { 868 SAXParser parser = spf.newSAXParser(); 869 parser.getProperty("foo"); 870 fail("SAXNotRecognizedException expected"); 871 } catch (SAXNotRecognizedException e) { 872 // Expected 873 } catch (Exception e) { 874 throw new RuntimeException("Unexpected exception", e); 875 } 876 877 // No name case 878 try { 879 SAXParser parser = spf.newSAXParser(); 880 parser.setProperty(null, "bar"); 881 fail("NullPointerException expected"); 882 } catch (NullPointerException e) { 883 // Expected 884 } catch (Exception e) { 885 throw new RuntimeException("Unexpected exception", e); 886 } 887 888 try { 889 SAXParser parser = spf.newSAXParser(); 890 parser.getProperty(null); 891 fail("NullPointerException expected"); 892 } catch (NullPointerException e) { 893 // Expected 894 } catch (Exception e) { 895 throw new RuntimeException("Unexpected exception", e); 896 } 897 } 898 899 } 900