1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.xml.sax.ptests; 24 25 import java.io.FileInputStream; 26 import java.io.IOException; 27 import javax.xml.parsers.ParserConfigurationException; 28 import javax.xml.parsers.SAXParserFactory; 29 import static jaxp.library.JAXPTestUtilities.failUnexpected; 30 import static org.testng.Assert.assertFalse; 31 import static org.testng.Assert.assertNotNull; 32 import static org.testng.Assert.assertNull; 33 import static org.testng.Assert.assertTrue; 34 import org.testng.annotations.Test; 35 import org.xml.sax.InputSource; 36 import org.xml.sax.SAXException; 37 import org.xml.sax.SAXNotRecognizedException; 38 import org.xml.sax.SAXNotSupportedException; 39 import org.xml.sax.XMLReader; 40 import org.xml.sax.ext.DeclHandler; 41 import org.xml.sax.ext.LexicalHandler; 42 import org.xml.sax.helpers.XMLFilterImpl; 43 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 44 45 /** 46 * Class containing the test cases for SAXParser API 47 */ 48 public class XMLReaderTest { 49 /** 50 * XML namespaces. 51 */ 52 private static final String NAMESPACES = 53 "http://xml.org/sax/features/namespaces"; 54 55 /** 56 * XML namespaces prefixes. 57 */ 58 private static final String NAMESPACE_PREFIXES = 59 "http://xml.org/sax/features/namespace-prefixes"; 60 61 /** 62 * A string intern name. 63 */ 64 private static final String STRING_INTERNING = 65 "http://xml.org/sax/features/string-interning"; 66 67 /** 68 * Validation name. 69 */ 70 private static final String VALIDATION = 71 "http://xml.org/sax/features/validation"; 72 73 /** 74 * A general external entities name 75 */ 76 private static final String EXTERNAL_G_ENTITIES = 77 "http://xml.org/sax/features/external-general-entities"; 78 79 /** 80 * A external parameter entities name 81 */ 82 private static final String EXTERNAL_P_ENTITIES = 83 "http://xml.org/sax/features/external-parameter-entities"; 84 85 /** 86 * XML DOM node name. 87 */ 88 private static final String DOM_NODE = "http://xml.org/sax/properties/dom-node"; 89 90 /** 91 * XML String name. 92 */ 93 private static final String XML_STRING = "http://xml.org/sax/properties/xml-string"; 94 95 /** 96 * Declare handler name 97 */ 98 private static final String DECL_HANDLER = 99 "http://xml.org/sax/properties/declaration-handler"; 100 101 /** 102 * Lexical handler name 103 */ 104 private static final String LEXICAL_HANDLER = 105 "http://xml.org/sax/properties/lexical-handler"; 106 107 /** 108 * According to the SAX2 specs, All XMLReaders are required to recognize the 109 * http://xml.org/sax/features/namespaces feature names. 110 * This test case is to test this. 111 */ 112 @Test 113 public void featureNS01() { 114 try { 115 SAXParserFactory spf = SAXParserFactory.newInstance(); 116 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 117 assertFalse(xmlReader.getFeature(NAMESPACES)); 118 } catch (ParserConfigurationException | SAXException ex) { 119 failUnexpected(ex); 120 } 121 } 122 123 /** 124 * According to the SAX2 specs, All XMLReaders are required to recognize the 125 * http://xml.org/sax/features/namespaces feature names. 126 * This test case is to test this. 127 */ 128 @Test 129 public void featureNS02() { 130 try { 131 SAXParserFactory spf = SAXParserFactory.newInstance(); 132 spf.setNamespaceAware(true); 133 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 134 assertTrue(xmlReader.getFeature(NAMESPACES)); 135 } catch (ParserConfigurationException | SAXException ex) { 136 failUnexpected(ex); 137 } 138 } 139 140 /** 141 * Obtain http://xml.org/sax/features/namespaces feature name after it's 142 * just set. Expect it's same as set value. 143 */ 144 @Test 145 public void featureNS03() { 146 try { 147 SAXParserFactory spf = SAXParserFactory.newInstance(); 148 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 149 xmlReader.setFeature(NAMESPACES, true); 150 assertTrue(xmlReader.getFeature(NAMESPACES)); 151 xmlReader.setFeature(NAMESPACES, false); 152 assertFalse(xmlReader.getFeature(NAMESPACES)); 153 } catch (ParserConfigurationException | SAXException ex) { 154 failUnexpected(ex); 155 } 156 } 157 158 /** 159 * According to the SAX2 specs, All XMLReaders are required to recognize the 160 * http://xml.org/sax/features/namespace-prefixes feature names. 161 * This test case is to test this. 162 */ 163 @Test 164 public void featureNSP01() { 165 try { 166 SAXParserFactory spf = SAXParserFactory.newInstance(); 167 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 168 assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES)); 169 170 } catch (ParserConfigurationException | SAXException ex) { 171 failUnexpected(ex); 172 } 173 } 174 175 /** 176 * According to the SAX2 specs, All XMLReaders are required to recognize the 177 * http://xml.org/sax/features/namespace-prefixes feature names. 178 * This test case is to test this. 179 */ 180 @Test 181 public void featureNSP02() { 182 try { 183 SAXParserFactory spf = SAXParserFactory.newInstance(); 184 spf.setNamespaceAware(true); 185 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 186 assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES)); 187 } catch (ParserConfigurationException | SAXException ex) { 188 failUnexpected(ex); 189 } 190 } 191 192 /** 193 * Obtain http://xml.org/sax/features/namespaces-prefixes feature name after 194 * it's just set. Expect it's same as set value. 195 */ 196 @Test 197 public void featureNSP03() { 198 try { 199 SAXParserFactory spf = SAXParserFactory.newInstance(); 200 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 201 xmlReader.setFeature(NAMESPACE_PREFIXES, true); 202 assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES)); 203 xmlReader.setFeature(NAMESPACE_PREFIXES, false); 204 assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES)); 205 } catch (ParserConfigurationException | SAXException ex) { 206 failUnexpected(ex); 207 } 208 } 209 210 /** 211 * getFeature returns true if a feature has not been preset when namespace 212 * awareness is set. 213 */ 214 @Test 215 public void featureSI01() { 216 try { 217 SAXParserFactory spf = SAXParserFactory.newInstance(); 218 spf.setNamespaceAware(true); 219 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 220 assertTrue(xmlReader.getFeature(STRING_INTERNING)); 221 } catch (SAXException | ParserConfigurationException ex) { 222 failUnexpected(ex); 223 } 224 } 225 226 /** 227 * getFeature with validation feature name returns the value that 228 * setValidation set. 229 */ 230 @Test 231 public void featureV01() { 232 try { 233 SAXParserFactory spf = SAXParserFactory.newInstance(); 234 assertFalse(spf.newSAXParser().getXMLReader().getFeature(VALIDATION)); 235 spf.setValidating(true); 236 assertTrue(spf.newSAXParser().getXMLReader().getFeature(VALIDATION)); 237 } catch (SAXException | ParserConfigurationException ex) { 238 failUnexpected(ex); 239 } 240 } 241 242 /** 243 * getFeature returns the value that a feature has been preset as when 244 * namespace awareness is set. 245 */ 246 @Test 247 public void featureV02() { 248 try { 249 SAXParserFactory spf = SAXParserFactory.newInstance(); 250 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 251 252 xmlReader.setFeature(VALIDATION, true); 253 assertTrue(xmlReader.getFeature(VALIDATION)); 254 255 xmlReader.setFeature(VALIDATION, false); 256 assertFalse(xmlReader.getFeature(VALIDATION)); 257 } catch (SAXException | ParserConfigurationException ex) { 258 failUnexpected(ex); 259 } 260 } 261 262 /** 263 * getFeature returns true if a feature has not been preset when namespace 264 * awareness is set. 265 */ 266 @Test 267 public void featureEGE01() { 268 try { 269 SAXParserFactory spf = SAXParserFactory.newInstance(); 270 spf.setNamespaceAware(true); 271 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 272 assertTrue(xmlReader.getFeature(EXTERNAL_G_ENTITIES)); 273 } catch (SAXException | ParserConfigurationException ex) { 274 failUnexpected(ex); 275 } 276 } 277 278 /** 279 * getFeature returns false if a feature has been preset as false when 280 * namespace awareness is set. 281 */ 282 @Test 283 public void featureEGE02() { 284 try { 285 SAXParserFactory spf = SAXParserFactory.newInstance(); 286 spf.setNamespaceAware(true); 287 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 288 xmlReader.setFeature(EXTERNAL_G_ENTITIES, false); 289 assertFalse(xmlReader.getFeature(EXTERNAL_G_ENTITIES)); 290 } catch (SAXException | ParserConfigurationException ex) { 291 failUnexpected(ex); 292 } 293 } 294 295 /** 296 * getFeature returns true if a feature has not been preset when namespace 297 * awareness is set. 298 */ 299 @Test 300 public void featureEPE01() { 301 try { 302 SAXParserFactory spf = SAXParserFactory.newInstance(); 303 spf.setNamespaceAware(true); 304 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 305 assertTrue(xmlReader.getFeature(EXTERNAL_P_ENTITIES)); 306 } catch (SAXException | ParserConfigurationException ex) { 307 failUnexpected(ex); 308 } 309 } 310 311 /** 312 * getFeature returns false if a feature has been preset as false when 313 * namespace awareness is set. 314 */ 315 @Test 316 public void featureEPE02() { 317 try { 318 SAXParserFactory spf = SAXParserFactory.newInstance(); 319 spf.setNamespaceAware(true); 320 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 321 xmlReader.setFeature(EXTERNAL_P_ENTITIES, false); 322 assertFalse(xmlReader.getFeature(EXTERNAL_P_ENTITIES)); 323 } catch (SAXException | ParserConfigurationException ex) { 324 failUnexpected(ex); 325 } 326 } 327 328 /** 329 * getFeature with a unknown feature name throws SAXNotRecognizedException. 330 * @throws SAXNotRecognizedException If the feature value can't be assigned 331 * or retrieved. 332 */ 333 @Test(expectedExceptions = SAXNotRecognizedException.class) 334 public void featureNE01() throws SAXNotRecognizedException { 335 try { 336 SAXParserFactory spf = SAXParserFactory.newInstance(); 337 spf.setNamespaceAware(true); 338 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 339 boolean noMeaningFeature = xmlReader.getFeature("no-meaning-feature"); 340 } catch(SAXNotRecognizedException ex) { 341 throw ex; 342 } catch (SAXException | ParserConfigurationException ex) { 343 failUnexpected(ex); 344 } 345 } 346 347 /** 348 * No exception expected when set entity resolver as simple entity resolver. 349 */ 350 @Test 351 public void entity01() { 352 try { 353 SAXParserFactory spf = SAXParserFactory.newInstance(); 354 spf.setNamespaceAware(true); 355 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 356 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 357 xmlReader.setEntityResolver(xmlFilter); 358 assertNotNull(xmlReader.getEntityResolver()); 359 } catch (SAXException | ParserConfigurationException ex) { 360 failUnexpected(ex); 361 } 362 } 363 364 /** 365 * No NPE expected when set entity resolver as null. 366 */ 367 @Test 368 public void entity02() { 369 try { 370 SAXParserFactory spf = SAXParserFactory.newInstance(); 371 spf.setNamespaceAware(true); 372 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 373 xmlReader.setEntityResolver(null); 374 } catch (SAXException | ParserConfigurationException ex) { 375 failUnexpected(ex); 376 } 377 } 378 379 /** 380 * No exception expected when set DTD handler as simple DTD handler. 381 */ 382 @Test 383 public void dtdhandler01() { 384 try { 385 SAXParserFactory spf = SAXParserFactory.newInstance(); 386 spf.setNamespaceAware(true); 387 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 388 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 389 xmlReader.setDTDHandler(xmlFilter); 390 assertNotNull(xmlReader.getDTDHandler()); 391 } catch (SAXException | ParserConfigurationException ex) { 392 failUnexpected(ex); 393 } 394 } 395 396 /** 397 * No NPE expected when set DTD handler as null. 398 */ 399 @Test 400 public void dtdhandler02() { 401 try { 402 SAXParserFactory spf = SAXParserFactory.newInstance(); 403 spf.setNamespaceAware(true); 404 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 405 xmlReader.setDTDHandler(null); 406 } catch (SAXException | ParserConfigurationException ex) { 407 failUnexpected(ex); 408 } 409 } 410 411 /** 412 * No exception expected when set content handler as simple content handler. 413 */ 414 @Test 415 public void contenthandler01() { 416 try { 417 SAXParserFactory spf = SAXParserFactory.newInstance(); 418 spf.setNamespaceAware(true); 419 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 420 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 421 xmlReader.setContentHandler(xmlFilter); 422 assertNotNull(xmlReader.getContentHandler()); 423 } catch (SAXException | ParserConfigurationException ex) { 424 failUnexpected(ex); 425 } 426 } 427 428 /** 429 * No NPE expected when set content handler as null. 430 */ 431 @Test 432 public void contenthandler02() { 433 try { 434 SAXParserFactory spf = SAXParserFactory.newInstance(); 435 spf.setNamespaceAware(true); 436 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 437 xmlReader.setContentHandler(null); 438 } catch (SAXException | ParserConfigurationException ex) { 439 failUnexpected(ex); 440 } 441 } 442 443 /** 444 * No exception expected when set content handler as simple error handler. 445 */ 446 @Test 447 public void errorhandler01() { 448 try { 449 SAXParserFactory spf = SAXParserFactory.newInstance(); 450 spf.setNamespaceAware(true); 451 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 452 xmlReader.setErrorHandler(new XMLFilterImpl()); 453 assertNotNull(xmlReader.getErrorHandler()); 454 } catch (SAXException | ParserConfigurationException ex) { 455 failUnexpected(ex); 456 } 457 } 458 459 /** 460 * No NPE expected when set error handler as null. 461 */ 462 @Test 463 public void errorhandler02() { 464 try { 465 SAXParserFactory spf = SAXParserFactory.newInstance(); 466 spf.setNamespaceAware(true); 467 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 468 xmlReader.setErrorHandler(null); 469 } catch (SAXException | ParserConfigurationException ex) { 470 failUnexpected(ex); 471 } 472 } 473 474 /** 475 * Parse a null input source throw NPE. 476 */ 477 @Test(expectedExceptions = NullPointerException.class) 478 public void parse01() { 479 try { 480 SAXParserFactory spf = SAXParserFactory.newInstance(); 481 spf.setNamespaceAware(true); 482 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 483 xmlReader.parse((InputSource)null); 484 } catch (SAXException | ParserConfigurationException | IOException ex) { 485 failUnexpected(ex); 486 } 487 } 488 489 /** 490 * Unit test for parse a error-formatted file. SAXException is expected. 491 * @throws org.xml.sax.SAXException parsing failed. 492 */ 493 @Test(expectedExceptions = SAXException.class) 494 public void parse02() throws SAXException { 495 try (FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")){ 496 SAXParserFactory spf = SAXParserFactory.newInstance(); 497 spf.setNamespaceAware(true); 498 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 499 InputSource is = new InputSource(fis); 500 xmlReader.parse(is); 501 } catch (ParserConfigurationException | IOException ex) { 502 failUnexpected(ex); 503 } 504 } 505 506 /** 507 * Unit test for parse a well-formatted file. No exception is expected. 508 */ 509 @Test 510 public void parse03(){ 511 try (FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) { 512 SAXParserFactory spf = SAXParserFactory.newInstance(); 513 spf.setNamespaceAware(true); 514 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 515 InputSource is = new InputSource(fis); 516 xmlReader.parse(is); 517 } catch (IOException | SAXException | ParserConfigurationException ex) { 518 failUnexpected(ex); 519 } 520 } 521 522 /** 523 * Modified by IBM 524 * Xerces does not support this feature and it is not mandatory 525 * @throws org.xml.sax.SAXNotSupportedException 526 */ 527 @Test(expectedExceptions = SAXNotSupportedException.class) 528 public void xrProperty01() throws SAXNotSupportedException { 529 try { 530 SAXParserFactory spf = SAXParserFactory.newInstance(); 531 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 532 xmlReader.getProperty(XML_STRING); 533 } catch(SAXNotSupportedException ex) { 534 throw ex; 535 } catch (SAXException | ParserConfigurationException ex){ 536 failUnexpected(ex); 537 } 538 } 539 540 /** 541 * SAXNotSupportedException thrown if property name is known but no value 542 * assigned to this property. 543 * @throws org.xml.sax.SAXNotSupportedException when XMLReader recognizes 544 * the property name but cannot determine its value at this time. 545 */ 546 @Test(expectedExceptions = SAXNotSupportedException.class) 547 public void xrProperty02() throws SAXNotSupportedException { 548 try { 549 SAXParserFactory spf = SAXParserFactory.newInstance(); 550 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 551 assertNull(xmlReader.getProperty(DOM_NODE)); 552 } catch (SAXNotSupportedException ex) { 553 throw ex; 554 } catch (SAXException | ParserConfigurationException ex){ 555 failUnexpected(ex); 556 } 557 } 558 559 560 /** 561 * XMLReader.getProperty returns null if LEXICAL_HANDLER wasn't set. 562 */ 563 @Test 564 public void xrProperty03() { 565 try { 566 SAXParserFactory spf = SAXParserFactory.newInstance(); 567 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 568 assertNull(xmlReader.getProperty(LEXICAL_HANDLER)); 569 } catch (SAXException | ParserConfigurationException ex){ 570 failUnexpected(ex); 571 } 572 } 573 574 /** 575 * XMLReader.getProperty returns null if DECL_HANDLER wasn't set. 576 */ 577 @Test 578 public void xrProperty04() { 579 try { 580 SAXParserFactory spf = SAXParserFactory.newInstance(); 581 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 582 assertNull(xmlReader.getProperty(DECL_HANDLER)); 583 } catch (SAXException | ParserConfigurationException ex){ 584 failUnexpected(ex); 585 } 586 } 587 588 /** 589 * XMLReader.setProperty/getProperty for LEXICAL_HANDLER unit test. 590 */ 591 @Test 592 public void xrProperty05() { 593 try { 594 SAXParserFactory spf = SAXParserFactory.newInstance(); 595 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 596 MyLexicalHandler myLexicalHandler = new MyLexicalHandler(); 597 xmlReader.setProperty(LEXICAL_HANDLER, myLexicalHandler); 598 assertNotNull(xmlReader.getProperty(LEXICAL_HANDLER)); 599 } catch (SAXException | ParserConfigurationException ex){ 600 failUnexpected(ex); 601 } 602 } 603 604 /** 605 * XMLReader.setProperty/getProperty for DECL_HANDLER unit test. 606 */ 607 @Test 608 public void xrProperty06() { 609 try { 610 SAXParserFactory spf = SAXParserFactory.newInstance(); 611 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 612 MyDeclHandler myDeclHandler = new MyDeclHandler(); 613 xmlReader.setProperty(DECL_HANDLER, myDeclHandler); 614 assertNotNull(xmlReader.getProperty(DECL_HANDLER)); 615 } catch (ParserConfigurationException | SAXException ex){ 616 failUnexpected(ex); 617 } 618 } 619 } 620 621 /** 622 * Simple LexicalHandler that skips every lexical event. 623 */ 624 class MyLexicalHandler implements LexicalHandler { 625 /** 626 * Report an XML comment anywhere in the document. 627 * 628 * @param ch An array holding the characters in the comment. 629 * @param start The starting position in the array. 630 * @param length The number of characters to use from the array. 631 */ 632 @Override 633 public void comment(char[] ch, int start, int length) { 634 } 635 636 /** 637 * Report the end of a CDATA section. 638 */ 639 @Override 640 public void endCDATA() { 641 } 642 643 /** 644 * Report the end of DTD declarations. 650 /** 651 * Report the end of an entity. 652 * 653 * @param name The name of the entity that is ending. 654 */ 655 @Override 656 public void endEntity(String name) { 657 } 658 659 /** 660 * Report the start of a CDATA section. 661 */ 662 @Override 663 public void startCDATA() { 664 } 665 666 /** 667 * Report the start of DTD declarations, if any. 668 * 669 * @param name The document type name. 670 * @param publicId The declared public identifier for the external DTD subset. 671 * @param systemId The declared system identifier for the external DTD subset. 672 */ 673 @Override 674 public void startDTD(String name, String publicId, String systemId) { 675 } 676 677 /** 678 * Report the beginning of some internal and external XML entities. 679 * 680 * @param name The name of the entity. 681 */ 682 @Override 683 public void startEntity(String name) { 684 } 685 } 686 687 /** 688 * Simple DeclHandler that skips every DTD declaration event. 689 */ 690 class MyDeclHandler implements DeclHandler { 691 /** 692 * Report an attribute type declaration. 693 * @param eName The name of the associated element. 694 * @param aName The name of the attribute. 695 * @param type A string representing the attribute type. 696 * @param mode A string representing the attribute defaulting mode 697 * ("#IMPLIED", "#REQUIRED", or "#FIXED") or null if 698 * none of these applies. 699 * @param value A string representing the attribute's default value, 700 * or null if there is none. 701 */ 702 @Override 703 public void attributeDecl(String eName, String aName, String type, 704 String valueDefault, String value) { 705 } 706 707 /** 708 * Report an element type declaration. 709 * @param name The element type name. 710 * @param model The content model as a normalized string. 711 */ 712 @Override 713 public void elementDecl(String name, String model) { 714 } 715 716 /** 717 * Report a parsed external entity declaration. 718 * @param name The name of the entity. If it is a parameter 719 * entity, the name will begin with '%'. 720 * @param publicId The entity's public identifier, or null if none 721 * was given. 722 * @param systemId The entity's system identifier. 723 */ 724 @Override 725 public void externalEntityDecl(String name, String publicId, 726 String systemId) { 727 } 728 729 /** 730 * Report an internal entity declaration. 731 * @param name The name of the entity. If it is a parameter 732 * entity, the name will begin with '%'. 733 * @param value The replacement text of the entity. 734 */ 735 @Override 736 public void internalEntityDecl(String name, String value) { 737 } 738 } | 1 /* 2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.xml.sax.ptests; 24 25 import java.io.FileInputStream; 26 import javax.xml.parsers.SAXParserFactory; 27 import jaxp.library.JAXPFileReadOnlyBaseTest; 28 import static org.testng.Assert.assertEquals; 29 import static org.testng.Assert.assertFalse; 30 import static org.testng.Assert.assertNotNull; 31 import static org.testng.Assert.assertNull; 32 import static org.testng.Assert.assertTrue; 33 import org.testng.annotations.Test; 34 import org.xml.sax.InputSource; 35 import org.xml.sax.SAXException; 36 import org.xml.sax.SAXNotRecognizedException; 37 import org.xml.sax.SAXNotSupportedException; 38 import org.xml.sax.XMLReader; 39 import org.xml.sax.ext.DeclHandler; 40 import org.xml.sax.ext.LexicalHandler; 41 import org.xml.sax.helpers.XMLFilterImpl; 42 import static org.xml.sax.ptests.SAXTestConst.XML_DIR; 43 44 /** 45 * Class containing the test cases for SAXParser API 46 */ 47 public class XMLReaderTest extends JAXPFileReadOnlyBaseTest { 48 49 /** 50 * XML namespaces. 51 */ 52 private static final String NAMESPACES 53 = "http://xml.org/sax/features/namespaces"; 54 55 /** 56 * XML namespaces prefixes. 57 */ 58 private static final String NAMESPACE_PREFIXES 59 = "http://xml.org/sax/features/namespace-prefixes"; 60 61 /** 62 * A string intern name. 63 */ 64 private static final String STRING_INTERNING 65 = "http://xml.org/sax/features/string-interning"; 66 67 /** 68 * Validation name. 69 */ 70 private static final String VALIDATION 71 = "http://xml.org/sax/features/validation"; 72 73 /** 74 * A general external entities name 75 */ 76 private static final String EXTERNAL_G_ENTITIES 77 = "http://xml.org/sax/features/external-general-entities"; 78 79 /** 80 * A external parameter entities name 81 */ 82 private static final String EXTERNAL_P_ENTITIES 83 = "http://xml.org/sax/features/external-parameter-entities"; 84 85 /** 86 * XML DOM node name. 87 */ 88 private static final String DOM_NODE = "http://xml.org/sax/properties/dom-node"; 89 90 /** 91 * XML String name. 92 */ 93 private static final String XML_STRING = "http://xml.org/sax/properties/xml-string"; 94 95 /** 96 * Declare handler name 97 */ 98 private static final String DECL_HANDLER 99 = "http://xml.org/sax/properties/declaration-handler"; 100 101 /** 102 * Lexical handler name 103 */ 104 private static final String LEXICAL_HANDLER 105 = "http://xml.org/sax/properties/lexical-handler"; 106 107 /** 108 * According to the SAX2 specs, All XMLReaders are required to recognize the 109 * http://xml.org/sax/features/namespaces feature names. This test case is 110 * to test this. 111 * 112 * @throws Exception If any errors occur. 113 */ 114 @Test 115 public void featureNS01() throws Exception { 116 SAXParserFactory spf = SAXParserFactory.newInstance(); 117 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 118 assertFalse(xmlReader.getFeature(NAMESPACES)); 119 } 120 121 /** 122 * According to the SAX2 specs, All XMLReaders are required to recognize the 123 * http://xml.org/sax/features/namespaces feature names. This test case is 124 * to test this. 125 * 126 * @throws Exception If any errors occur. 127 */ 128 @Test 129 public void featureNS02() throws Exception { 130 SAXParserFactory spf = SAXParserFactory.newInstance(); 131 spf.setNamespaceAware(true); 132 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 133 assertTrue(xmlReader.getFeature(NAMESPACES)); 134 } 135 136 /** 137 * Obtain http://xml.org/sax/features/namespaces feature name after it's 138 * just set. Expect it's same as set value. 139 * 140 * @throws Exception If any errors occur. 141 */ 142 @Test 143 public void featureNS03() throws Exception { 144 SAXParserFactory spf = SAXParserFactory.newInstance(); 145 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 146 xmlReader.setFeature(NAMESPACES, true); 147 assertTrue(xmlReader.getFeature(NAMESPACES)); 148 xmlReader.setFeature(NAMESPACES, false); 149 assertFalse(xmlReader.getFeature(NAMESPACES)); 150 } 151 152 /** 153 * According to the SAX2 specs, All XMLReaders are required to recognize the 154 * http://xml.org/sax/features/namespace-prefixes feature names. This test 155 * case is to test this. 156 * 157 * @throws Exception If any errors occur. 158 */ 159 @Test 160 public void featureNSP01() throws Exception { 161 SAXParserFactory spf = SAXParserFactory.newInstance(); 162 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 163 assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES)); 164 } 165 166 /** 167 * According to the SAX2 specs, All XMLReaders are required to recognize the 168 * http://xml.org/sax/features/namespace-prefixes feature names. This test 169 * case is to test this. 170 * 171 * @throws Exception If any errors occur. 172 */ 173 @Test 174 public void featureNSP02() throws Exception { 175 SAXParserFactory spf = SAXParserFactory.newInstance(); 176 spf.setNamespaceAware(true); 177 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 178 assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES)); 179 } 180 181 /** 182 * Obtain http://xml.org/sax/features/namespaces-prefixes feature name after 183 * it's just set. Expect it's same as set value. 184 * 185 * @throws Exception If any errors occur. 186 */ 187 @Test 188 public void featureNSP03() throws Exception { 189 SAXParserFactory spf = SAXParserFactory.newInstance(); 190 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 191 xmlReader.setFeature(NAMESPACE_PREFIXES, true); 192 assertTrue(xmlReader.getFeature(NAMESPACE_PREFIXES)); 193 xmlReader.setFeature(NAMESPACE_PREFIXES, false); 194 assertFalse(xmlReader.getFeature(NAMESPACE_PREFIXES)); 195 } 196 197 /** 198 * getFeature returns true if a feature has not been preset when namespace 199 * awareness is set. 200 * 201 * @throws Exception If any errors occur. 202 */ 203 @Test 204 public void featureSI01() throws Exception { 205 SAXParserFactory spf = SAXParserFactory.newInstance(); 206 spf.setNamespaceAware(true); 207 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 208 assertTrue(xmlReader.getFeature(STRING_INTERNING)); 209 } 210 211 /** 212 * getFeature with validation feature name returns the value that 213 * setValidation set. 214 * 215 * @throws Exception If any errors occur. 216 */ 217 @Test 218 public void featureV01() throws Exception { 219 SAXParserFactory spf = SAXParserFactory.newInstance(); 220 assertFalse(spf.newSAXParser().getXMLReader().getFeature(VALIDATION)); 221 spf.setValidating(true); 222 assertTrue(spf.newSAXParser().getXMLReader().getFeature(VALIDATION)); 223 } 224 225 /** 226 * getFeature returns the value that a feature has been preset as when 227 * namespace awareness is set. 228 * 229 * @throws Exception If any errors occur. 230 */ 231 @Test 232 public void featureV02() throws Exception { 233 SAXParserFactory spf = SAXParserFactory.newInstance(); 234 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 235 236 xmlReader.setFeature(VALIDATION, true); 237 assertTrue(xmlReader.getFeature(VALIDATION)); 238 xmlReader.setFeature(VALIDATION, false); 239 assertFalse(xmlReader.getFeature(VALIDATION)); 240 } 241 242 /** 243 * getFeature returns true if a feature has not been preset when namespace 244 * awareness is set. 245 * 246 * @throws Exception If any errors occur. 247 */ 248 @Test 249 public void featureEGE01() throws Exception { 250 SAXParserFactory spf = SAXParserFactory.newInstance(); 251 spf.setNamespaceAware(true); 252 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 253 assertTrue(xmlReader.getFeature(EXTERNAL_G_ENTITIES)); 254 } 255 256 /** 257 * getFeature returns false if a feature has been preset as false when 258 * namespace awareness is set. 259 * 260 * @throws Exception If any errors occur. 261 */ 262 @Test 263 public void featureEGE02() throws Exception { 264 SAXParserFactory spf = SAXParserFactory.newInstance(); 265 spf.setNamespaceAware(true); 266 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 267 xmlReader.setFeature(EXTERNAL_G_ENTITIES, false); 268 assertFalse(xmlReader.getFeature(EXTERNAL_G_ENTITIES)); 269 } 270 271 /** 272 * getFeature returns true if a feature has not been preset when namespace 273 * awareness is set. 274 * 275 * @throws Exception If any errors occur. 276 */ 277 @Test 278 public void featureEPE01() throws Exception { 279 SAXParserFactory spf = SAXParserFactory.newInstance(); 280 spf.setNamespaceAware(true); 281 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 282 assertTrue(xmlReader.getFeature(EXTERNAL_P_ENTITIES)); 283 } 284 285 /** 286 * getFeature returns false if a feature has been preset as false when 287 * namespace awareness is set. 288 * 289 * @throws Exception If any errors occur. 290 */ 291 @Test 292 public void featureEPE02() throws Exception { 293 SAXParserFactory spf = SAXParserFactory.newInstance(); 294 spf.setNamespaceAware(true); 295 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 296 xmlReader.setFeature(EXTERNAL_P_ENTITIES, false); 297 assertFalse(xmlReader.getFeature(EXTERNAL_P_ENTITIES)); 298 } 299 300 /** 301 * getFeature with a unknown feature name throws SAXNotRecognizedException. 302 * 303 * @throws Exception If any errors occur. 304 */ 305 @Test(expectedExceptions = SAXNotRecognizedException.class) 306 public void featureNE01() throws Exception { 307 SAXParserFactory spf = SAXParserFactory.newInstance(); 308 spf.setNamespaceAware(true); 309 spf.newSAXParser().getXMLReader().getFeature("no-meaning-feature"); 310 } 311 312 /** 313 * No exception expected when set entity resolver as simple entity resolver. 314 * 315 * @throws Exception If any errors occur. 316 */ 317 @Test 318 public void entity01() throws Exception { 319 SAXParserFactory spf = SAXParserFactory.newInstance(); 320 spf.setNamespaceAware(true); 321 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 322 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 323 xmlReader.setEntityResolver(xmlFilter); 324 assertEquals(xmlReader.getEntityResolver(), xmlFilter); 325 } 326 327 /** 328 * No NPE expected when set entity resolver as null. 329 * 330 * @throws Exception If any errors occur. 331 */ 332 @Test 333 public void entity02() throws Exception { 334 SAXParserFactory spf = SAXParserFactory.newInstance(); 335 spf.setNamespaceAware(true); 336 spf.newSAXParser().getXMLReader().setEntityResolver(null); 337 } 338 339 /** 340 * No exception expected when set DTD handler as simple DTD handler. 341 * 342 * @throws Exception If any errors occur. 343 */ 344 @Test 345 public void dtdhandler01() throws Exception { 346 SAXParserFactory spf = SAXParserFactory.newInstance(); 347 spf.setNamespaceAware(true); 348 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 349 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 350 xmlReader.setDTDHandler(xmlFilter); 351 assertEquals(xmlReader.getDTDHandler(), xmlFilter); 352 } 353 354 /** 355 * No NPE expected when set DTD handler as null. 356 * 357 * @throws Exception If any errors occur. 358 */ 359 @Test 360 public void dtdhandler02() throws Exception { 361 SAXParserFactory spf = SAXParserFactory.newInstance(); 362 spf.setNamespaceAware(true); 363 spf.newSAXParser().getXMLReader().setDTDHandler(null); 364 } 365 366 /** 367 * No exception expected when set content handler as simple content handler. 368 * 369 * @throws Exception If any errors occur. 370 */ 371 @Test 372 public void contenthandler01() throws Exception { 373 SAXParserFactory spf = SAXParserFactory.newInstance(); 374 spf.setNamespaceAware(true); 375 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 376 XMLFilterImpl xmlFilter = new XMLFilterImpl(); 377 xmlReader.setContentHandler(xmlFilter); 378 assertEquals(xmlReader.getContentHandler(), xmlFilter); 379 } 380 381 /** 382 * No NPE expected when set content handler as null. 383 * 384 * @throws Exception If any errors occur. 385 */ 386 @Test 387 public void contenthandler02() throws Exception { 388 SAXParserFactory spf = SAXParserFactory.newInstance(); 389 spf.setNamespaceAware(true); 390 spf.newSAXParser().getXMLReader().setContentHandler(null); 391 } 392 393 /** 394 * No exception expected when set content handler as simple error handler. 395 * 396 * @throws Exception If any errors occur. 397 */ 398 @Test 399 public void errorhandler01() throws Exception { 400 SAXParserFactory spf = SAXParserFactory.newInstance(); 401 spf.setNamespaceAware(true); 402 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 403 xmlReader.setErrorHandler(new XMLFilterImpl()); 404 assertNotNull(xmlReader.getErrorHandler()); 405 } 406 407 /** 408 * No NPE expected when set error handler as null. 409 * 410 * @throws Exception If any errors occur. 411 */ 412 @Test 413 public void errorhandler02() throws Exception { 414 SAXParserFactory spf = SAXParserFactory.newInstance(); 415 spf.setNamespaceAware(true); 416 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 417 xmlReader.setErrorHandler(null); 418 } 419 420 /** 421 * Parse a null input source throw NPE. 422 * 423 * @throws Exception If any errors occur. 424 */ 425 @Test(expectedExceptions = NullPointerException.class) 426 public void parse01() throws Exception { 427 SAXParserFactory spf = SAXParserFactory.newInstance(); 428 spf.setNamespaceAware(true); 429 spf.newSAXParser().getXMLReader().parse((InputSource) null); 430 } 431 432 /** 433 * Unit test for parse a error-formatted file. SAXException is expected. 434 * 435 * @throws Exception If any errors occur. 436 */ 437 @Test(groups = {"readLocalFiles"}, expectedExceptions = SAXException.class) 438 public void parse02() throws Exception { 439 try (FileInputStream fis = new FileInputStream(XML_DIR + "invalid.xml")) { 440 SAXParserFactory spf = SAXParserFactory.newInstance(); 441 spf.setNamespaceAware(true); 442 spf.newSAXParser().getXMLReader().parse(new InputSource(fis)); 443 } 444 } 445 446 /** 447 * Unit test for parse a well-formatted file. No exception is expected. 448 * 449 * @throws Exception If any errors occur. 450 */ 451 @Test(groups = {"readLocalFiles"}) 452 public void parse03() throws Exception { 453 try (FileInputStream fis = new FileInputStream(XML_DIR + "correct2.xml")) { 454 SAXParserFactory spf = SAXParserFactory.newInstance(); 455 spf.setNamespaceAware(true); 456 spf.newSAXParser().getXMLReader().parse(new InputSource(fis)); 457 } 458 } 459 460 /** 461 * Modified by IBM Xerces does not support this feature and it is not 462 * mandatory. 463 * 464 * @throws Exception If any errors occur. 465 */ 466 @Test(expectedExceptions = SAXNotSupportedException.class) 467 public void xrProperty01() throws Exception { 468 SAXParserFactory spf = SAXParserFactory.newInstance(); 469 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 470 xmlReader.getProperty(XML_STRING); 471 } 472 473 /** 474 * SAXNotSupportedException thrown if property name is known but no value 475 * assigned to this property. 476 * 477 * @throws Exception If any errors occur. 478 */ 479 @Test(expectedExceptions = SAXNotSupportedException.class) 480 public void xrProperty02() throws Exception { 481 SAXParserFactory spf = SAXParserFactory.newInstance(); 482 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 483 assertNull(xmlReader.getProperty(DOM_NODE)); 484 } 485 486 /** 487 * XMLReader.getProperty returns null if LEXICAL_HANDLER wasn't set. 488 * 489 * @throws Exception If any errors occur. 490 */ 491 @Test 492 public void xrProperty03() throws Exception { 493 SAXParserFactory spf = SAXParserFactory.newInstance(); 494 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 495 assertNull(xmlReader.getProperty(LEXICAL_HANDLER)); 496 } 497 498 /** 499 * XMLReader.getProperty returns null if DECL_HANDLER wasn't set. 500 * 501 * @throws Exception If any errors occur. 502 */ 503 @Test 504 public void xrProperty04() throws Exception { 505 SAXParserFactory spf = SAXParserFactory.newInstance(); 506 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 507 assertNull(xmlReader.getProperty(DECL_HANDLER)); 508 } 509 510 /** 511 * XMLReader.setProperty/getProperty for LEXICAL_HANDLER unit test. 512 * 513 * @throws Exception If any errors occur. 514 */ 515 @Test 516 public void xrProperty05() throws Exception { 517 SAXParserFactory spf = SAXParserFactory.newInstance(); 518 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 519 MyLexicalHandler myLexicalHandler = new MyLexicalHandler(); 520 xmlReader.setProperty(LEXICAL_HANDLER, myLexicalHandler); 521 assertNotNull(xmlReader.getProperty(LEXICAL_HANDLER)); 522 } 523 524 /** 525 * XMLReader.setProperty/getProperty for DECL_HANDLER unit test. 526 * 527 * @throws Exception If any errors occur. 528 */ 529 @Test 530 public void xrProperty06() throws Exception { 531 SAXParserFactory spf = SAXParserFactory.newInstance(); 532 XMLReader xmlReader = spf.newSAXParser().getXMLReader(); 533 MyDeclHandler myDeclHandler = new MyDeclHandler(); 534 xmlReader.setProperty(DECL_HANDLER, myDeclHandler); 535 assertNotNull(xmlReader.getProperty(DECL_HANDLER)); 536 } 537 } 538 539 /** 540 * Simple LexicalHandler that skips every lexical event. 541 */ 542 class MyLexicalHandler implements LexicalHandler { 543 544 /** 545 * Report an XML comment anywhere in the document. 546 * 547 * @param ch An array holding the characters in the comment. 548 * @param start The starting position in the array. 549 * @param length The number of characters to use from the array. 550 */ 551 @Override 552 public void comment(char[] ch, int start, int length) { 553 } 554 555 /** 556 * Report the end of a CDATA section. 557 */ 558 @Override 559 public void endCDATA() { 560 } 561 562 /** 563 * Report the end of DTD declarations. 569 /** 570 * Report the end of an entity. 571 * 572 * @param name The name of the entity that is ending. 573 */ 574 @Override 575 public void endEntity(String name) { 576 } 577 578 /** 579 * Report the start of a CDATA section. 580 */ 581 @Override 582 public void startCDATA() { 583 } 584 585 /** 586 * Report the start of DTD declarations, if any. 587 * 588 * @param name The document type name. 589 * @param publicId The declared public identifier for the external DTD 590 * subset. 591 * @param systemId The declared system identifier for the external DTD 592 * subset. 593 */ 594 @Override 595 public void startDTD(String name, String publicId, String systemId) { 596 } 597 598 /** 599 * Report the beginning of some internal and external XML entities. 600 * 601 * @param name The name of the entity. 602 */ 603 @Override 604 public void startEntity(String name) { 605 } 606 } 607 608 /** 609 * Simple DeclHandler that skips every DTD declaration event. 610 */ 611 class MyDeclHandler implements DeclHandler { 612 613 /** 614 * Report an attribute type declaration. 615 * 616 * @param eName The name of the associated element. 617 * @param aName The name of the attribute. 618 * @param type A string representing the attribute type. 619 * @param mode A string representing the attribute defaulting mode 620 * ("#IMPLIED", "#REQUIRED", or "#FIXED") or null if none of these applies. 621 * @param value A string representing the attribute's default value, or null 622 * if there is none. 623 */ 624 @Override 625 public void attributeDecl(String eName, String aName, String type, 626 String valueDefault, String value) { 627 } 628 629 /** 630 * Report an element type declaration. 631 * 632 * @param name The element type name. 633 * @param model The content model as a normalized string. 634 */ 635 @Override 636 public void elementDecl(String name, String model) { 637 } 638 639 /** 640 * Report a parsed external entity declaration. 641 * 642 * @param name The name of the entity. If it is a parameter entity, the name 643 * will begin with '%'. 644 * @param publicId The entity's public identifier, or null if none was 645 * given. 646 * @param systemId The entity's system identifier. 647 */ 648 @Override 649 public void externalEntityDecl(String name, String publicId, 650 String systemId) { 651 } 652 653 /** 654 * Report an internal entity declaration. 655 * 656 * @param name The name of the entity. If it is a parameter entity, the name 657 * will begin with '%'. 658 * @param value The replacement text of the entity. 659 */ 660 @Override 661 public void internalEntityDecl(String name, String value) { 662 } 663 } |