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