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