1 /* 2 * Copyright (c) 2014, 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 dom; 24 25 import java.io.IOException; 26 import java.io.StringReader; 27 import java.net.URISyntaxException; 28 29 import javax.xml.XMLConstants; 30 import javax.xml.parsers.DocumentBuilder; 31 import javax.xml.parsers.DocumentBuilderFactory; 32 import javax.xml.parsers.FactoryConfigurationError; 33 import javax.xml.parsers.ParserConfigurationException; 34 35 import org.testng.Assert; 36 import org.testng.annotations.Listeners; 37 import org.testng.annotations.Test; 38 import org.w3c.dom.Attr; 39 import org.w3c.dom.CDATASection; 40 import org.w3c.dom.Comment; 41 import org.w3c.dom.DOMConfiguration; 42 import org.w3c.dom.DOMError; 43 import org.w3c.dom.DOMErrorHandler; 44 import org.w3c.dom.DOMException; 45 import org.w3c.dom.DOMImplementation; 46 import org.w3c.dom.Document; 47 import org.w3c.dom.Element; 48 import org.w3c.dom.Entity; 49 import org.w3c.dom.NamedNodeMap; 50 import org.w3c.dom.Node; 51 import org.w3c.dom.ProcessingInstruction; 52 import org.w3c.dom.Text; 53 import org.w3c.dom.ls.DOMImplementationLS; 54 import org.w3c.dom.ls.LSInput; 55 import org.w3c.dom.ls.LSParser; 56 import org.xml.sax.InputSource; 57 import org.xml.sax.SAXException; 58 59 /* 60 * @summary Test DOMConfiguration for supported properties. 61 */ 62 @Listeners({jaxp.library.FilePolicy.class}) 63 public class DOMConfigurationTest { 64 65 static class TestHandler implements DOMErrorHandler { 66 private String warning; 67 private String error; 68 private String fatalError; 69 70 public String getError() { 71 return error; 72 } 73 74 public String getFatalError() { 75 return fatalError; 76 } 77 78 public String getWarning() { 79 return warning; 80 } 81 82 public boolean handleError(DOMError error) { 83 if (error.getSeverity() == DOMError.SEVERITY_ERROR) { 84 this.error = "" + error.getMessage(); 85 return false; 86 } 87 if (error.getSeverity() == DOMError.SEVERITY_FATAL_ERROR) { 88 this.fatalError = "" + error.getMessage(); 89 return false; 90 } 91 this.warning = "" + error.getMessage(); 92 return true; // warning 93 } 94 } 95 96 static class TestFailureHandler implements DOMErrorHandler { 97 public boolean handleError(DOMError error) { 98 if (error.getSeverity() == DOMError.SEVERITY_ERROR) { 99 Assert.fail("Error: " + error.getMessage()); 100 } 101 if (error.getSeverity() == DOMError.SEVERITY_FATAL_ERROR) { 102 Assert.fail("Fatal error: " + error.getMessage()); 103 } 104 return true; // warning 105 } 106 } 107 108 void setHandler(Document doc) { 109 doc.getDomConfig().setParameter("error-handler", new TestFailureHandler()); 110 } 111 112 static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 113 114 static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; 115 116 static final String XMLNS = "http://www.w3.org/2000/xmlns/"; 117 118 static Document loadDocument(String schemaURL, String instanceText) { 119 Document document = null; 120 try { 121 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 122 dbf.setNamespaceAware(true); 123 dbf.setValidating(true); 124 if (schemaURL != null) { 125 dbf.setAttribute(SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI); 126 dbf.setAttribute(SCHEMA_SOURCE, schemaURL); 127 } 128 DocumentBuilder parser = dbf.newDocumentBuilder(); 129 130 InputSource inSource = new InputSource(new StringReader(instanceText)); 131 inSource.setSystemId("doc.xml"); 132 document = parser.parse(inSource); 133 } catch (ParserConfigurationException e) { 134 Assert.fail(e.toString()); 135 } catch (IOException e) { 136 Assert.fail(e.toString()); 137 } catch (SAXException e) { 138 Assert.fail(e.toString()); 139 } 140 141 return document; 142 } 143 144 static final String test_xml = "<?xml version=\"1.0\"?>\n" + "<test:root xmlns:test=\"test\" \n" 145 + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + ">	

 1 </test:root>\n"; 146 147 static final String test1_xml = "<?xml version=\"1.0\"?>\n" + "<!DOCTYPE root [\n" + " <!ELEMENT root ANY>\n" + " <!ENTITY x \"X\">\n" + "]>\n" 148 + "<root/>\n"; 149 150 static final String test2_xml = "<?xml version=\"1.0\"?>\n" + "<!DOCTYPE root [\n" + " <!ELEMENT root ANY>\n" 151 + " <!ATTLIST root attr CDATA #REQUIRED>\n" + " <!ENTITY x \"<\">\n" + "]>\n" + "<root attr=\"x\"/>\n"; 152 153 static final String test3_xml = "<?xml version=\"1.0\"?>\n" + "<!DOCTYPE root [\n" + " <!ELEMENT root (elem*)>\n" + " <!ELEMENT elem EMPTY>\n" 154 + "]>\n" + "<root/>\n"; 155 156 static String test1_xsd_url; 157 static { 158 try { 159 test1_xsd_url = DOMConfigurationTest.class.getResource("DOMConfigurationTest.xsd").toURI().toString(); 160 } catch (URISyntaxException uriSyntaxException) { 161 Assert.fail(uriSyntaxException.toString()); 162 } 163 } 164 165 /** 166 * Equivalence class partitioning with state and input values orientation 167 * for public void setParameter(String name, Object value) throws 168 * DOMException, <br> 169 * <b>pre-conditions</b>: the doc contains two subsequent processing 170 * instrictions, <br> 171 * <b>name</b>: canonical-form <br> 172 * <b>value</b>: true. <br> 173 * <b>Expected results</b>: the subsequent processing instrictions are 174 * separated with a single line break 175 */ 176 @Test 177 public void testCanonicalForm001() { 178 DOMImplementation domImpl = null; 179 try { 180 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 181 } catch (ParserConfigurationException pce) { 182 Assert.fail(pce.toString()); 183 } catch (FactoryConfigurationError fce) { 184 Assert.fail(fce.toString()); 185 } 186 187 Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); 188 189 DOMConfiguration config = doc.getDomConfig(); 190 191 Element root = doc.getDocumentElement(); 192 ProcessingInstruction pi1 = doc.createProcessingInstruction("target1", "data1"); 193 ProcessingInstruction pi2 = doc.createProcessingInstruction("target2", "data2"); 194 195 root.appendChild(pi1); 196 root.appendChild(pi2); 197 198 if (!config.canSetParameter("canonical-form", Boolean.TRUE)) { 199 System.out.println("OK, setting 'canonical-form' to true is not supported"); 200 return; 201 } 202 203 config.setParameter("canonical-form", Boolean.TRUE); 204 setHandler(doc); 205 doc.normalizeDocument(); 206 207 Node child1 = root.getFirstChild(); 208 Node child2 = child1.getNextSibling(); 209 210 if (child2.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { 211 Assert.fail("the second child is expected to be a" + "single line break, returned: " + child2); 212 } 213 214 // return Status.passed("OK"); 215 } 216 217 /** 218 * Equivalence class partitioning with state and input values orientation 219 * for public void setParameter(String name, Object value) throws 220 * DOMException, <br> 221 * <b>pre-conditions</b>: the parameters "namespaces", 222 * "namespace-declarations", "well-formed", "element-content-whitespace" are 223 * set to false if possible; the parameters "entities", 224 * "normalize-characters", "cdata-sections" are set to true if possible, <br> 225 * <b>name</b>: canonical-form <br> 226 * <b>value</b>: true. <br> 227 * <b>Expected results</b>: the parameters "namespaces", 228 * "namespace-declarations", "well-formed", "element-content-whitespace" are 229 * set to true; the parameters "entities", "normalize-characters", 230 * "cdata-sections" are set to false 231 */ 232 @Test 233 public void testCanonicalForm002() { 234 Object[][] params = { { "namespaces", Boolean.TRUE }, { "namespace-declarations", Boolean.TRUE }, { "well-formed", Boolean.TRUE }, 235 { "element-content-whitespace", Boolean.TRUE }, 236 237 { "entities", Boolean.FALSE }, { "normalize-characters", Boolean.FALSE }, { "cdata-sections", Boolean.FALSE }, }; 238 239 DOMImplementation domImpl = null; 240 try { 241 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 242 } catch (ParserConfigurationException pce) { 243 Assert.fail(pce.toString()); 244 } catch (FactoryConfigurationError fce) { 245 Assert.fail(fce.toString()); 246 } 247 248 Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); 249 250 DOMConfiguration config = doc.getDomConfig(); 251 252 if (!config.canSetParameter("canonical-form", Boolean.TRUE)) { 253 System.out.println("OK, setting 'canonical-form' to true is not supported"); 254 return; 255 } 256 257 for (int i = params.length; --i >= 0;) { 258 Boolean reset = params[i][1].equals(Boolean.TRUE) ? Boolean.FALSE : Boolean.TRUE; 259 if (config.canSetParameter(params[i][0].toString(), reset)) { 260 config.setParameter(params[i][0].toString(), reset); 261 } 262 } 263 264 config.setParameter("canonical-form", Boolean.TRUE); 265 266 StringBuffer result = new StringBuffer(); 267 268 for (int i = params.length; --i >= 0;) { 269 Object param = config.getParameter(params[i][0].toString()); 270 if (!params[i][1].equals(param)) { 271 result.append("; the parameter \'" + params[i][0] + "\' is set to " + param + ", expected: " + params[i][1]); 272 } 273 } 274 275 if (result.length() > 0) { 276 Assert.fail(result.toString().substring(2)); 277 } 278 279 return; // Status.passed("OK"); 280 } 281 282 /** 283 * Equivalence class partitioning with state and input values orientation 284 * for public void setParameter(String name, Object value) throws 285 * DOMException, <br> 286 * <b>pre-conditions</b>: the doc's root element contains superfluous 287 * namespace declarations, <br> 288 * <b>name</b>: canonical-form <br> 289 * <b>value</b>: true. <br> 290 * <b>Expected results</b>: the superfluous namespace declarations are 291 * removed 292 */ 293 @Test 294 public void testCanonicalForm003() { 295 DOMImplementation domImpl = null; 296 try { 297 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 298 } catch (ParserConfigurationException pce) { 299 Assert.fail(pce.toString()); 300 } catch (FactoryConfigurationError fce) { 301 Assert.fail(fce.toString()); 302 } 303 304 Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); 305 306 DOMConfiguration config = doc.getDomConfig(); 307 308 Element root = doc.getDocumentElement(); 309 String XMLNS = "http://www.w3.org/2000/xmlns/"; 310 root.setAttributeNS(XMLNS, "xmlns:extra1", "ExtraNS1"); 311 root.setAttributeNS(XMLNS, "xmlns:extra2", "ExtraNS2"); 312 313 if (!config.canSetParameter("canonical-form", Boolean.TRUE)) { 314 System.out.println("OK, setting 'canonical-form' to true is not supported"); 315 return; 316 } 317 config.setParameter("canonical-form", Boolean.TRUE); 318 setHandler(doc); 319 doc.normalizeDocument(); 320 321 String xmlns2 = root.getAttributeNS(XMLNS, "extra1"); 322 if (xmlns2 == null || xmlns2.length() != 0) { 323 Assert.fail("superfluous namespace declarations is not removed: xmlns:extra2 = " + xmlns2); 324 } 325 326 return; // Status.passed("OK"); 327 } 328 329 /** 330 * Equivalence class partitioning with state and input values orientation 331 * for public void setParameter(String name, Object value) throws 332 * DOMException, <br> 333 * <b>pre-conditions</b>: setting the "canonical-form" to true is supported, <br> 334 * <b>name</b>: canonical-form <br> 335 * <b>value</b>: true. <br> 336 * <b>Expected results</b>: one of the following parameters is changed: 337 * "namespaces", "namespace-declarations", "well-formed", 338 * "element-content-whitespace", "entities", "normalize-characters", 339 * "cdata-sections" then "canonical-form" becomes false 340 */ 341 @Test 342 public void testCanonicalForm004() { 343 Object[][] params = { { "namespaces", Boolean.TRUE }, { "namespace-declarations", Boolean.TRUE }, { "well-formed", Boolean.TRUE }, 344 { "element-content-whitespace", Boolean.TRUE }, 345 346 { "entities", Boolean.FALSE }, { "normalize-characters", Boolean.FALSE }, { "cdata-sections", Boolean.FALSE }, }; 347 348 DOMImplementation domImpl = null; 349 try { 350 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 351 } catch (ParserConfigurationException pce) { 352 Assert.fail(pce.toString()); 353 } catch (FactoryConfigurationError fce) { 354 Assert.fail(fce.toString()); 355 } 356 357 Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); 358 359 DOMConfiguration config = doc.getDomConfig(); 360 361 if (!config.canSetParameter("canonical-form", Boolean.TRUE)) { 362 System.out.println("OK, setting 'canonical-form' to true is not supported"); 363 return; 364 } 365 366 StringBuffer result = new StringBuffer(); 367 368 for (int i = params.length; --i >= 0;) { 369 config.setParameter("canonical-form", Boolean.TRUE); 370 Boolean changedValue = (params[i][1].equals(Boolean.TRUE)) ? Boolean.FALSE : Boolean.TRUE; 371 if (config.canSetParameter(params[i][0].toString(), changedValue)) { 372 config.setParameter(params[i][0].toString(), changedValue); 373 Object param = config.getParameter("canonical-form"); 374 if (!Boolean.FALSE.equals(param)) { 375 result.append("; setting the parameter '" + params[i][0] + "' to " + changedValue + " does not change 'canonical-form' to false"); 376 } 377 } 378 } 379 380 if (result.length() > 0) { 381 Assert.fail(result.toString().substring(2)); 382 } 383 384 return; // Status.passed("OK"); 385 } 386 387 /** 388 * Equivalence class partitioning with state and input values orientation 389 * for public void setParameter(String name, Object value) throws 390 * DOMException, <br> 391 * <b>pre-conditions</b>: the root element has one CDATASection followed by 392 * one Text node, <br> 393 * <b>name</b>: cdata-sections <br> 394 * <b>value</b>: true. <br> 395 * <b>Expected results</b>: the CDATASection is left intact 396 */ 397 @Test 398 public void testCdataSections001() { 399 DOMImplementation domImpl = null; 400 try { 401 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 402 } catch (ParserConfigurationException pce) { 403 Assert.fail(pce.toString()); 404 } catch (FactoryConfigurationError fce) { 405 Assert.fail(fce.toString()); 406 } 407 408 Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); 409 410 String cdataText = "CDATA CDATA CDATA"; 411 String textText = "text text text"; 412 413 CDATASection cdata = doc.createCDATASection(cdataText); 414 Text text = doc.createTextNode(textText); 415 416 DOMConfiguration config = doc.getDomConfig(); 417 config.setParameter("cdata-sections", Boolean.TRUE); 418 419 Element root = doc.getDocumentElement(); 420 root.appendChild(cdata); 421 root.appendChild(text); 422 423 setHandler(doc); 424 doc.normalizeDocument(); 425 426 Node returned = root.getFirstChild(); 427 428 if (returned.getNodeType() != Node.CDATA_SECTION_NODE) { 429 Assert.fail("reurned: " + returned + ", expected: CDATASection"); 430 } 431 432 return; // Status.passed("OK"); 433 434 } 435 436 /** 437 * Equivalence class partitioning with state and input values orientation 438 * for public void setParameter(String name, Object value) throws 439 * DOMException, <br> 440 * <b>pre-conditions</b>: the root element has one CDATASection followed by 441 * one Text node, <br> 442 * <b>name</b>: cdata-sections <br> 443 * <b>value</b>: false. <br> 444 * <b>Expected results</b>: the root element has one Text node with text of 445 * the CDATASection and the Text node 446 */ 447 @Test 448 public void testCdataSections002() { 449 DOMImplementation domImpl = null; 450 try { 451 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 452 } catch (ParserConfigurationException pce) { 453 Assert.fail(pce.toString()); 454 } catch (FactoryConfigurationError fce) { 455 Assert.fail(fce.toString()); 456 } 457 458 Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); 459 460 String cdataText = "CDATA CDATA CDATA"; 461 String textText = "text text text"; 462 463 CDATASection cdata = doc.createCDATASection(cdataText); 464 Text text = doc.createTextNode(textText); 465 466 DOMConfiguration config = doc.getDomConfig(); 467 config.setParameter("cdata-sections", Boolean.FALSE); 468 469 Element root = doc.getDocumentElement(); 470 root.appendChild(cdata); 471 root.appendChild(text); 472 473 setHandler(doc); 474 doc.normalizeDocument(); 475 476 Node returned = root.getFirstChild(); 477 478 if (returned.getNodeType() != Node.TEXT_NODE) { 479 Assert.fail("reurned: " + returned + ", expected: TEXT_NODE"); 480 } 481 482 String returnedText = returned.getNodeValue(); 483 if (!(cdataText + textText).equals(returnedText)) { 484 Assert.fail("reurned: " + returnedText + ", expected: \"" + cdataText + textText + "\""); 485 } 486 487 return; // Status.passed("OK"); 488 489 } 490 491 /** 492 * Equivalence class partitioning with state and input values orientation 493 * for public void setParameter(String name, Object value) throws 494 * DOMException, <br> 495 * <b>pre-conditions</b>: the root element has one Text node with not fully 496 * normalized characters, the 'check-character-normalization' parameter set 497 * to true, <br> 498 * <b>name</b>: error-handler <br> 499 * <b>value</b>: DOMErrorHandler. <br> 500 * <b>Expected results</b>: LSParser calls the specified error handler 501 */ 502 @Test 503 public void testCheckCharNorm001() { 504 DOMImplementation domImpl = null; 505 try { 506 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 507 } catch (ParserConfigurationException pce) { 508 Assert.fail(pce.toString()); 509 } catch (FactoryConfigurationError fce) { 510 Assert.fail(fce.toString()); 511 } 512 513 DOMImplementationLS lsImpl = (DOMImplementationLS) domImpl.getFeature("LS", "3.0"); 514 515 if (lsImpl == null) { 516 System.out.println("OK, the DOM implementation does not support the LS 3.0"); 517 return; 518 } 519 520 LSParser lsParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); 521 522 DOMConfiguration config = lsParser.getDomConfig(); 523 524 if (!config.canSetParameter("check-character-normalization", Boolean.TRUE)) { 525 System.out.println("OK, setting 'check-character-normalization' to true is not supported"); 526 return; 527 } 528 529 config.setParameter("check-character-normalization", Boolean.TRUE); 530 531 TestHandler testHandler = new TestHandler(); 532 config.setParameter("error-handler", testHandler); 533 534 LSInput lsInput = lsImpl.createLSInput(); 535 lsInput.setStringData("<root>\u0073\u0075\u0063\u0327\u006F\u006E</root>"); 536 Document doc = lsParser.parse(lsInput); 537 538 if (null == testHandler.getError()) { 539 Assert.fail("no error is reported, expected 'check-character-normalization-failure'"); 540 541 } 542 543 return; // Status.passed("OK"); 544 545 } 546 547 /** 548 * Equivalence class partitioning with state and input values orientation 549 * for public void setParameter(String name, Object value) throws 550 * DOMException, <br> 551 * <b>pre-conditions</b>: the root element contains a fully-normalized text, <br> 552 * <b>name</b>: check-character-normalization <br> 553 * <b>value</b>: false. <br> 554 * <b>Expected results</b>: LSParser reports no errors 555 */ 556 @Test 557 public void testCheckCharNorm002() { 558 DOMImplementation domImpl = null; 559 try { 560 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 561 } catch (ParserConfigurationException pce) { 562 Assert.fail(pce.toString()); 563 } catch (FactoryConfigurationError fce) { 564 Assert.fail(fce.toString()); 565 } 566 567 DOMImplementationLS lsImpl = (DOMImplementationLS) domImpl.getFeature("LS", "3.0"); 568 569 if (lsImpl == null) { 570 System.out.println("OK, the DOM implementation does not support the LS 3.0"); 571 return; 572 } 573 574 LSParser lsParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); 575 576 DOMConfiguration config = lsParser.getDomConfig(); 577 578 if (!config.canSetParameter("check-character-normalization", Boolean.FALSE)) { 579 Assert.fail("setting 'check-character-normalization' to false is not supported"); 580 } 581 582 config.setParameter("check-character-normalization", Boolean.FALSE); 583 584 TestHandler testHandler = new TestHandler(); 585 config.setParameter("error-handler", testHandler); 586 587 LSInput lsInput = lsImpl.createLSInput(); 588 lsInput.setStringData("<root>fully-normalized</root>"); 589 Document doc = lsParser.parse(lsInput); 590 591 if (null != testHandler.getError()) { 592 Assert.fail("no error is expected, but reported: " + testHandler.getError()); 593 594 } 595 596 return; // Status.passed("OK"); 597 598 } 599 600 /** 601 * Equivalence class partitioning with state and input values orientation 602 * for public void setParameter(String name, Object value) throws 603 * DOMException, <br> 604 * <b>pre-conditions</b>: the root element has two Comment nodes, <br> 605 * <b>name</b>: comments <br> 606 * <b>value</b>: true. <br> 607 * <b>Expected results</b>: the Comment nodes belong to the root element 608 */ 609 @Test 610 public void testComments001() { 611 DOMImplementation domImpl = null; 612 try { 613 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 614 } catch (ParserConfigurationException pce) { 615 Assert.fail(pce.toString()); 616 } catch (FactoryConfigurationError fce) { 617 Assert.fail(fce.toString()); 618 } 619 620 Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); 621 622 Comment comment1 = doc.createComment("comment1"); 623 Comment comment2 = doc.createComment("comment2"); 624 625 DOMConfiguration config = doc.getDomConfig(); 626 config.setParameter("comments", Boolean.TRUE); 627 628 Element root = doc.getDocumentElement(); 629 root.appendChild(comment1); 630 root.appendChild(comment2); 631 632 setHandler(doc); 633 doc.normalizeDocument(); 634 635 if (comment1.getParentNode() != root) { 636 Assert.fail("comment1 is attached to " + comment1.getParentNode() + ", but expected to be a child of root"); 637 } 638 639 if (comment2.getParentNode() != root) { 640 Assert.fail("comment1 is attached to " + comment2.getParentNode() + ", but expected to be a child of root"); 641 } 642 643 return; // Status.passed("OK"); 644 645 } 646 647 /** 648 * Equivalence class partitioning with state and input values orientation 649 * for public void setParameter(String name, Object value) throws 650 * DOMException, <br> 651 * <b>pre-conditions</b>: the root element has two Comment nodes, <br> 652 * <b>name</b>: comments <br> 653 * <b>value</b>: false. <br> 654 * <b>Expected results</b>: the root element has no children 655 */ 656 @Test 657 public void testComments002() { 658 DOMImplementation domImpl = null; 659 try { 660 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 661 } catch (ParserConfigurationException pce) { 662 Assert.fail(pce.toString()); 663 } catch (FactoryConfigurationError fce) { 664 Assert.fail(fce.toString()); 665 } 666 667 Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); 668 669 Comment comment1 = doc.createComment("comment1"); 670 Comment comment2 = doc.createComment("comment2"); 671 672 DOMConfiguration config = doc.getDomConfig(); 673 config.setParameter("comments", Boolean.FALSE); 674 675 Element root = doc.getDocumentElement(); 676 root.appendChild(comment1); 677 root.appendChild(comment2); 678 679 doc.normalizeDocument(); 680 681 if (root.getFirstChild() != null) { 682 Assert.fail("root has a child " + root.getFirstChild() + ", but expected to has none"); 683 } 684 685 return; // Status.passed("OK"); 686 687 } 688 689 /** 690 * Equivalence class partitioning with state and input values orientation 691 * for public void setParameter(String name, Object value) throws 692 * DOMException, <br> 693 * <b>pre-conditions</b>: the root element is declared as int and its value 694 * has subsequent characters #x9 (tab), #xA (line feed) and #xD (carriage 695 * return) , #x20 (space), '1', #x20 (space), <br> 696 * <b>name</b>: datatype-normalization <br> 697 * <b>value</b>: true. <br> 698 * <b>Expected results</b>: after Document.normalizeDocument() is called the 699 * content of the root is '1' 700 */ 701 @Test 702 public void testDatatypeNormalization001() { 703 Document doc = null; 704 try { 705 doc = loadDocument(test1_xsd_url, test_xml); 706 } catch (Exception e) { 707 Assert.fail(e.getMessage()); 708 } 709 710 DOMConfiguration config = doc.getDomConfig(); 711 712 if (!config.canSetParameter("schema-location", test1_xsd_url) || !config.canSetParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI)) { 713 System.out.println("cannot set the parameters 'schema-location' and 'schema-type'" + " to '" + test1_xsd_url + "' and '" 714 + XMLConstants.W3C_XML_SCHEMA_NS_URI + "' respectively"); 715 return; 716 } 717 config.setParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI); 718 config.setParameter("schema-location", test1_xsd_url); 719 720 if (!config.canSetParameter("validate", Boolean.TRUE)) { 721 System.out.println("OK, setting 'validate' to true is not supported"); 722 return; 723 } 724 config.setParameter("validate", Boolean.TRUE); 725 726 if (!config.canSetParameter("datatype-normalization", Boolean.TRUE)) { 727 System.out.println("OK, setting 'datatype-normalization' to true is not supported"); 728 return; 729 } 730 config.setParameter("datatype-normalization", Boolean.TRUE); 731 732 Element root = doc.getDocumentElement(); 733 while (root.getFirstChild() != null) { 734 root.removeChild(root.getFirstChild()); 735 } 736 root.appendChild(doc.createTextNode("\t\r\n 1 ")); 737 738 setHandler(doc); 739 doc.normalizeDocument(); 740 741 Node child = root.getFirstChild(); 742 if (child == null || child.getNodeType() != Node.TEXT_NODE || !"1".equals(child.getNodeValue())) { 743 Assert.fail("child: " + child + ", expected: text node '1'"); 744 } 745 746 return; // Status.passed("OK"); 747 748 } 749 750 /** 751 * Equivalence class partitioning with state and input values orientation 752 * for public void setParameter(String name, Object value) throws 753 * DOMException, <br> 754 * <b>pre-conditions</b>: the root element is declared as int and its value 755 * has subsequent characters #x9 (tab), #xA (line feed) and #xD (carriage 756 * return) , #x20 (space), '1', #x20 (space), <br> 757 * <b>name</b>: datatype-normalization <br> 758 * <b>value</b>: false. <br> 759 * <b>Expected results</b>: after Document.normalizeDocument() is called the 760 * value is left unchanged 761 */ 762 @Test 763 public void testDatatypeNormalization002() { 764 Document doc = null; 765 try { 766 doc = loadDocument(test1_xsd_url, test_xml); 767 } catch (Exception e) { 768 Assert.fail(e.getMessage()); 769 } 770 771 DOMConfiguration config = doc.getDomConfig(); 772 773 if (!config.canSetParameter("schema-location", test1_xsd_url) || !config.canSetParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI)) { 774 System.out.println("cannot set the parameters 'schema-location' and 'schema-type'" + " to '" + test1_xsd_url + "' and '" 775 + XMLConstants.W3C_XML_SCHEMA_NS_URI + "' respectively"); 776 return; 777 } 778 config.setParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI); 779 config.setParameter("schema-location", test1_xsd_url); 780 781 if (config.canSetParameter("validate", Boolean.TRUE)) { 782 config.setParameter("validate", Boolean.TRUE); 783 } 784 785 if (!config.canSetParameter("datatype-normalization", Boolean.FALSE)) { 786 Assert.fail("datatype-normalization' to false is not supported"); 787 } 788 config.setParameter("datatype-normalization", Boolean.FALSE); 789 790 Element root = doc.getDocumentElement(); 791 while (root.getFirstChild() != null) { 792 root.removeChild(root.getFirstChild()); 793 } 794 String value = "\t\r\n 1 "; 795 root.appendChild(doc.createTextNode(value)); 796 797 setHandler(doc); 798 doc.normalizeDocument(); 799 800 Node child = root.getFirstChild(); 801 if (child == null || child.getNodeType() != Node.TEXT_NODE || !value.equals(child.getNodeValue())) { 802 Assert.fail("child: " + child + ", expected: '\\t\\r\\n 1 '"); 803 } 804 805 return; // Status.passed("OK"); 806 807 } 808 809 /** 810 * Equivalence class partitioning with state and input values orientation 811 * for public void setParameter(String name, Object value) throws 812 * DOMException, <br> 813 * <b>pre-conditions</b>: the doc contains one entity and one entity 814 * reference, <br> 815 * <b>name</b>: entities <br> 816 * <b>value</b>: true. <br> 817 * <b>Expected results</b>: the entity and the entity reference are left 818 * unchanged 819 */ 820 @Test 821 public void testEntities001() { 822 Document doc = null; 823 try { 824 doc = loadDocument(null, test1_xml); 825 } catch (Exception e) { 826 Assert.fail(e.getMessage()); 827 } 828 829 DOMConfiguration config = doc.getDomConfig(); 830 if (!config.canSetParameter("entities", Boolean.TRUE)) { 831 Assert.fail("setting 'entities' to true is not supported"); 832 } 833 834 Element root = doc.getDocumentElement(); 835 root.appendChild(doc.createEntityReference("x")); 836 837 config.setParameter("entities", Boolean.TRUE); 838 839 setHandler(doc); 840 doc.normalizeDocument(); 841 Node child = root.getFirstChild(); 842 if (child == null) { 843 Assert.fail("root has no child"); 844 } 845 if (child.getNodeType() != Node.ENTITY_REFERENCE_NODE) { 846 Assert.fail("root's child is " + child + ", expected entity reference &x;"); 847 } 848 849 if (doc.getDoctype() == null) { 850 Assert.fail("no doctype found"); 851 } 852 853 if (doc.getDoctype().getEntities() == null) { 854 Assert.fail("no entitiy found"); 855 } 856 857 if (doc.getDoctype().getEntities().getNamedItem("x") == null) { 858 Assert.fail("no entitiy with name 'x' found"); 859 } 860 861 return; // Status.passed("OK"); 862 } 863 864 /** 865 * Equivalence class partitioning with state and input values orientation 866 * for public void setParameter(String name, Object value) throws 867 * DOMException, <br> 868 * <b>pre-conditions</b>: the doc contains one entity and one entity 869 * reference, <br> 870 * <b>name</b>: entities <br> 871 * <b>value</b>: false. <br> 872 * <b>Expected results</b>: the entity and the entity reference are removed 873 */ 874 @Test 875 public void testEntities002() { 876 Document doc = null; 877 try { 878 doc = loadDocument(null, test1_xml); 879 } catch (Exception e) { 880 Assert.fail(e.getMessage()); 881 } 882 883 DOMConfiguration config = doc.getDomConfig(); 884 if (!config.canSetParameter("entities", Boolean.FALSE)) { 885 Assert.fail("setting 'entities' to false is not supported"); 886 } 887 888 Element root = doc.getDocumentElement(); 889 root.appendChild(doc.createEntityReference("x")); 890 891 // TODO: remove debug 892 NamedNodeMap entities = doc.getDoctype().getEntities(); 893 Entity entityX = (Entity) entities.getNamedItem("x"); 894 System.err.println(); 895 System.err.println("Entity x: " + entityX.getTextContent()); 896 System.err.println(); 897 898 config.setParameter("entities", Boolean.FALSE); 899 900 setHandler(doc); 901 doc.normalizeDocument(); 902 Node child = root.getFirstChild(); 903 904 // TODO: restore test, exclude for now to allow other tests to run 905 /* 906 * if (child == null) { fail("root has no child"); } if 907 * (child.getNodeType() != Node.TEXT_NODE || 908 * !"X".equals(child.getNodeValue())) { fail("root's child is " + child 909 * + ", expected text node with value 'X'"); } 910 * 911 * if (doc.getDoctype() == null) { fail("no doctype found"); } 912 * 913 * if (doc.getDoctype().getEntities() != null && 914 * doc.getDoctype().getEntities().getNamedItem("x") != null) { 915 * fail("entity with name 'x' is found, expected to be removed"); } 916 */ 917 918 return; // Status.passed("OK"); 919 } 920 921 /** 922 * Equivalence class partitioning with state and input values orientation 923 * for public void setParameter(String name, Object value) throws 924 * DOMException, <br> 925 * <b>pre-conditions</b>: the 'infoset' parameter is set to true, <br> 926 * <b>name</b>: infoset <br> 927 * <b>value</b>: false. <br> 928 * <b>Expected results</b>: the parameters "validate-if-schema", "entities", 929 * "datatype-normalization", "cdata-sections", "namespace-declarations", 930 * "well-formed", "element-content-whitespace", "comments", "namespaces" are 931 * left unchanged 932 */ 933 @Test 934 public void testInfoset001() { 935 Object[][] params = { { "validate-if-schema", Boolean.FALSE }, { "entities", Boolean.FALSE }, { "datatype-normalization", Boolean.FALSE }, 936 { "cdata-sections", Boolean.FALSE }, 937 938 { "namespace-declarations", Boolean.TRUE }, { "well-formed", Boolean.TRUE }, { "element-content-whitespace", Boolean.TRUE }, 939 { "comments", Boolean.TRUE }, { "namespaces", Boolean.TRUE }, }; 940 941 DOMImplementation domImpl = null; 942 try { 943 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 944 } catch (ParserConfigurationException pce) { 945 Assert.fail(pce.toString()); 946 } catch (FactoryConfigurationError fce) { 947 Assert.fail(fce.toString()); 948 } 949 950 Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); 951 952 DOMConfiguration config = doc.getDomConfig(); 953 954 if (!config.canSetParameter("infoset", Boolean.TRUE)) { 955 Assert.fail("setting 'infoset' to true is not supported"); 956 } 957 958 for (int i = params.length; --i >= 0;) { 959 Boolean reset = params[i][1].equals(Boolean.TRUE) ? Boolean.FALSE : Boolean.TRUE; 960 if (config.canSetParameter(params[i][0].toString(), reset)) { 961 config.setParameter(params[i][0].toString(), reset); 962 } 963 } 964 965 config.setParameter("infoset", Boolean.TRUE); 966 config.setParameter("infoset", Boolean.FALSE); // has no effect 967 968 StringBuffer result = new StringBuffer(); 969 970 for (int i = params.length; --i >= 0;) { 971 Object param = config.getParameter(params[i][0].toString()); 972 if (!params[i][1].equals(param)) { 973 result.append("; the parameter \'" + params[i][0] + "\' is set to " + param + ", expected: " + params[i][1]); 974 } 975 } 976 977 if (result.length() > 0) { 978 Assert.fail(result.toString().substring(2)); 979 } 980 981 return; // Status.passed("OK"); 982 } 983 984 /** 985 * Equivalence class partitioning with state and input values orientation 986 * for public void setParameter(String name, Object value) throws 987 * DOMException, <br> 988 * <b>pre-conditions</b>: A document with one root element created. The 989 * prefix 'ns' is bound to 'namespaceURI'. The 'namespaces' parameter is set 990 * to true, <br> 991 * <b>name</b>: namespace-declarations <br> 992 * <b>value</b>: false. <br> 993 * <b>Expected results</b>: Attribute xmlns:ns="namespaceURI" is not added 994 * to the root element 995 */ 996 @Test 997 public void testNamespaces001() { 998 DOMImplementation domImpl = null; 999 try { 1000 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 1001 dbf.setNamespaceAware(true); 1002 domImpl = dbf.newDocumentBuilder().getDOMImplementation(); 1003 } catch (ParserConfigurationException pce) { 1004 Assert.fail(pce.toString()); 1005 } catch (FactoryConfigurationError fce) { 1006 Assert.fail(fce.toString()); 1007 } 1008 1009 Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); 1010 setHandler(doc); 1011 Element root = doc.getDocumentElement(); 1012 DOMConfiguration config = doc.getDomConfig(); 1013 1014 StringBuffer result = new StringBuffer(); 1015 if (config.canSetParameter("namespaces", Boolean.FALSE)) { 1016 config.setParameter("namespaces", Boolean.FALSE); 1017 1018 // namespaces = false 1019 // namespace-declarations = true (default) 1020 doc.normalizeDocument(); 1021 String xmlnsNS = root.getAttributeNS(XMLNS, "ns"); 1022 if (xmlnsNS.length() > 0) { 1023 result.append("; the 'namespaces' parameter is set to false but" + "Namespace normalization is performed, attribute" + " xmlns:ns=\"" + xmlnsNS 1024 + "\" is added"); 1025 } 1026 } 1027 1028 doc = domImpl.createDocument("namespaceURI", "ns:root", null); 1029 root = doc.getDocumentElement(); 1030 config = doc.getDomConfig(); 1031 1032 if (!config.canSetParameter("namespaces", Boolean.TRUE)) { 1033 result.append("; setting 'namespaces' to true is not supported"); 1034 } else { 1035 1036 config.setParameter("namespaces", Boolean.TRUE); 1037 1038 if (!config.canSetParameter("namespace-declarations", Boolean.FALSE)) { 1039 result.append("; setting 'namespace-declarations' to false is not supported"); 1040 } else { 1041 config.setParameter("namespace-declarations", Boolean.FALSE); 1042 1043 // namespaces = true 1044 // namespace-declarations = false 1045 doc.normalizeDocument(); 1046 1047 String xmlnsNS = root.getAttributeNS(XMLNS, "ns"); 1048 if (xmlnsNS.length() > 0) { 1049 result.append("; namespaces = true, namespace-declarations = false, but" + " xmlns:ns=\"" + xmlnsNS + "\""); 1050 } 1051 } 1052 1053 doc = domImpl.createDocument("namespaceURI", "ns:root", null); 1054 setHandler(doc); 1055 root = doc.getDocumentElement(); 1056 config = doc.getDomConfig(); 1057 1058 config.setParameter("namespaces", Boolean.TRUE); 1059 1060 if (!config.canSetParameter("namespace-declarations", Boolean.TRUE)) { 1061 result.append("; setting 'namespace-declarations' to true is not supported"); 1062 } else { 1063 config.setParameter("namespace-declarations", Boolean.TRUE); 1064 1065 // namespaces = true 1066 // namespace-declarations = true 1067 doc.normalizeDocument(); 1068 1069 String xmlnsNS = root.getAttributeNS(XMLNS, "ns"); 1070 if (!"namespaceURI".equals(xmlnsNS)) { 1071 result.append("; namespaces = true, namespace-declarations = true, but" + " xmlns:ns=\"" + xmlnsNS + "\""); 1072 } 1073 } 1074 } 1075 1076 if (result.length() > 0) { 1077 Assert.fail(result.toString().substring(2)); 1078 } 1079 return; // Status.passed("OK"); 1080 } 1081 1082 /** 1083 * Equivalence class partitioning with state and input values orientation 1084 * for public void setParameter(String name, Object value) throws 1085 * DOMException, <br> 1086 * <b>pre-conditions</b>: an attribute value is not fully normalized, <br> 1087 * <b>name</b>: normalize-characters <br> 1088 * <b>value</b>: false. <br> 1089 * <b>Expected results</b>: Node.normalize() leaves the value unchanged 1090 */ 1091 @Test 1092 public void testNormalizeCharacters001() { 1093 DOMImplementation domImpl = null; 1094 try { 1095 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 1096 } catch (ParserConfigurationException pce) { 1097 Assert.fail(pce.toString()); 1098 } catch (FactoryConfigurationError fce) { 1099 Assert.fail(fce.toString()); 1100 } 1101 1102 Document doc = domImpl.createDocument(null, null, null); 1103 1104 Attr attr = doc.createAttribute("attr"); 1105 String notNormalized = " \u0073\u0075\u0063\u0327\u006F\u006E "; 1106 attr.setValue(notNormalized); 1107 1108 DOMConfiguration config = doc.getDomConfig(); 1109 1110 StringBuffer result = new StringBuffer(); 1111 if (!config.canSetParameter("normalize-characters", Boolean.FALSE)) { 1112 result.append("; setting 'normalize-characters' to false is not supported"); 1113 } else { 1114 1115 config.setParameter("normalize-characters", Boolean.FALSE); 1116 1117 attr.normalize(); 1118 1119 String value = attr.getValue(); 1120 if (!notNormalized.equals(value)) { 1121 result.append("; the value is normalized to '" + value + "', expected to stay unchanged"); 1122 } 1123 } 1124 1125 if (config.canSetParameter("normalize-characters", Boolean.TRUE)) { 1126 config.setParameter("normalize-characters", Boolean.TRUE); 1127 1128 attr.setValue(notNormalized); 1129 attr.normalize(); 1130 1131 String value = attr.getValue(); 1132 if (notNormalized.equals(value)) { 1133 result.append("; the value is not normalized: '" + value + "', expected: '\u0073\u0075\u00E7\u006F\u006E'"); 1134 } 1135 } 1136 1137 if (result.length() > 0) { 1138 Assert.fail(result.toString().substring(2)); 1139 } 1140 return; // Status.passed("OK"); 1141 1142 } 1143 1144 /** 1145 * Equivalence class partitioning with state and input values orientation 1146 * for public void setParameter(String name, Object value) throws 1147 * DOMException, <br> 1148 * <b>pre-conditions</b>: The root element has invalid content. The 1149 * 'validate' parameter is set to true. The 'schema-location' parameter is 1150 * set to 'DOMConfigurationTest.xsd'., <br> 1151 * <b>name</b>: schema-type <br> 1152 * <b>value</b>: http://www.w3.org/2001/XMLSchema. <br> 1153 * <b>Expected results</b>: An error is reported 1154 */ 1155 @Test 1156 public void testValidate001() { 1157 DOMImplementation domImpl = null; 1158 try { 1159 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 1160 dbf.setNamespaceAware(true); 1161 dbf.setValidating(true); 1162 domImpl = dbf.newDocumentBuilder().getDOMImplementation(); 1163 } catch (ParserConfigurationException pce) { 1164 Assert.fail(pce.toString()); 1165 } catch (FactoryConfigurationError fce) { 1166 Assert.fail(fce.toString()); 1167 } 1168 1169 Document doc = domImpl.createDocument("test", "ns:root", null); 1170 1171 Element root = doc.getDocumentElement(); 1172 root.appendChild(doc.createTextNode("xxx")); // invalid value 1173 1174 DOMConfiguration config = doc.getDomConfig(); 1175 1176 if (!config.canSetParameter("schema-location", test1_xsd_url) || !config.canSetParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI)) { 1177 System.out.println("cannot set the parameters 'schema-location' and 'schema-type'" + " to '" + test1_xsd_url + "' and '" 1178 + XMLConstants.W3C_XML_SCHEMA_NS_URI + "' respectively"); 1179 return; 1180 } 1181 config.setParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI); 1182 config.setParameter("schema-location", test1_xsd_url); 1183 1184 String resultOK = "OK"; 1185 StringBuffer result = new StringBuffer(); 1186 if (!config.canSetParameter("validate", Boolean.TRUE)) { 1187 resultOK = "OK, setting the parameter 'validate' to true is not supported"; 1188 } else { 1189 config.setParameter("validate", Boolean.TRUE); 1190 TestHandler testHandler = new TestHandler(); 1191 config.setParameter("error-handler", testHandler); 1192 doc.normalizeDocument(); 1193 if (testHandler.getError() == null && null == testHandler.getFatalError()) { 1194 result.append("; no error was reported when the 'validate' is set to true"); 1195 } 1196 } 1197 1198 if (!config.canSetParameter("validate", Boolean.FALSE)) { 1199 result.append("; cannot set the parameters 'validate' to false"); 1200 } else { 1201 config.setParameter("validate", Boolean.FALSE); 1202 TestHandler testHandler = new TestHandler(); 1203 config.setParameter("error-handler", testHandler); 1204 doc.normalizeDocument(); 1205 if (testHandler.getError() != null || null != testHandler.getFatalError()) { 1206 result.append("; unexpected error: " + testHandler.getFatalError() + "; " + testHandler.getError()); 1207 } 1208 } 1209 1210 if (result.length() > 0) { 1211 Assert.fail(result.toString().substring(2)); 1212 } 1213 return; // Status.passed(resultOK); 1214 1215 } 1216 1217 /** 1218 * Equivalence class partitioning with state and input values orientation 1219 * for public void setParameter(String name, Object value) throws 1220 * DOMException, <br> 1221 * <b>pre-conditions</b>: The root contains a CDATASection with the 1222 * termination marker ']]>', <br> 1223 * <b>name</b>: split-cdata-sections <br> 1224 * <b>value</b>: true. <br> 1225 * <b>Expected results</b>: A warning is reported when the section is 1226 * splitted 1227 */ 1228 @Test 1229 public void testSplitCDATA001() { 1230 DOMImplementation domImpl = null; 1231 try { 1232 domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation(); 1233 } catch (ParserConfigurationException pce) { 1234 Assert.fail(pce.toString()); 1235 } catch (FactoryConfigurationError fce) { 1236 Assert.fail(fce.toString()); 1237 } 1238 1239 Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); 1240 1241 DOMConfiguration config = doc.getDomConfig(); 1242 CDATASection cdata = doc.createCDATASection("text]" + "]>text"); 1243 doc.getDocumentElement().appendChild(cdata); 1244 1245 TestHandler testHandler = new TestHandler(); 1246 config.setParameter("error-handler", testHandler); 1247 1248 if (!config.canSetParameter("split-cdata-sections", Boolean.TRUE)) { 1249 Assert.fail("cannot set the parameters 'split-cdata-sections' to true"); 1250 } 1251 config.setParameter("split-cdata-sections", Boolean.TRUE); 1252 1253 doc.normalizeDocument(); 1254 if (null == testHandler.getWarning()) { 1255 Assert.fail("no warning is reported"); 1256 } 1257 1258 return; // Status.passed("OK"); 1259 1260 } 1261 1262 /** 1263 * Equivalence class partitioning with state and input values orientation 1264 * for public void setParameter(String name, Object value) throws 1265 * DOMException, <br> 1266 * <b>pre-conditions</b>: The root contains a CDATASection with the 1267 * termination marker ']]>', <br> 1268 * <b>name</b>: split-cdata-sections <br> 1269 * <b>value</b>: false. <br> 1270 * <b>Expected results</b>: No warning is reported 1271 */ 1272 @Test 1273 public void testSplitCDATA002() { 1274 DOMImplementation domImpl = null; 1275 try { 1276 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 1277 dbf.setNamespaceAware(true); 1278 dbf.setValidating(true); 1279 domImpl = dbf.newDocumentBuilder().getDOMImplementation(); 1280 } catch (ParserConfigurationException pce) { 1281 Assert.fail(pce.toString()); 1282 } catch (FactoryConfigurationError fce) { 1283 Assert.fail(fce.toString()); 1284 } 1285 1286 Document doc = domImpl.createDocument("namespaceURI", "ns:root", null); 1287 1288 DOMConfiguration config = doc.getDomConfig(); 1289 CDATASection cdata = doc.createCDATASection("text]" + "]>text"); 1290 doc.getDocumentElement().appendChild(cdata); 1291 1292 TestHandler testHandler = new TestHandler(); 1293 config.setParameter("error-handler", testHandler); 1294 1295 if (!config.canSetParameter("split-cdata-sections", Boolean.FALSE)) { 1296 Assert.fail("cannot set the parameters 'split-cdata-sections' to false"); 1297 } 1298 config.setParameter("split-cdata-sections", Boolean.FALSE); 1299 1300 doc.normalizeDocument(); 1301 if (null == testHandler.getError()) { 1302 Assert.fail("no error is reported"); 1303 } 1304 1305 return; // Status.passed("OK"); 1306 1307 } 1308 1309 /** 1310 * Equivalence class partitioning with state and input values orientation 1311 * for public void setParameter(String name, Object value) throws 1312 * DOMException, <br> 1313 * <b>pre-conditions</b>: The root element has invalid content. The schema 1314 * is specified by setting the 'schema-location' and the 'schema-type' 1315 * parameters., <br> 1316 * <b>name</b>: validate-if-schema <br> 1317 * <b>value</b>: false. <br> 1318 * <b>Expected results</b>: No error is reported 1319 */ 1320 @Test 1321 public void testValidateIfSchema001() { 1322 DOMImplementation domImpl = null; 1323 try { 1324 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 1325 dbf.setNamespaceAware(true); 1326 dbf.setValidating(true); 1327 domImpl = dbf.newDocumentBuilder().getDOMImplementation(); 1328 } catch (ParserConfigurationException pce) { 1329 Assert.fail(pce.toString()); 1330 } catch (FactoryConfigurationError fce) { 1331 Assert.fail(fce.toString()); 1332 } 1333 1334 Document doc = domImpl.createDocument("test", "ns:root", null); 1335 1336 Element root = doc.getDocumentElement(); 1337 root.appendChild(doc.createTextNode("xxx")); // invalid value 1338 1339 DOMConfiguration config = doc.getDomConfig(); 1340 1341 if (!config.canSetParameter("schema-location", test1_xsd_url) || !config.canSetParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI)) { 1342 System.out.println("cannot set the parameters 'schema-location' and 'schema-type'" + " to 'DOMConfigurationTest.xsd' and '" 1343 + XMLConstants.W3C_XML_SCHEMA_NS_URI + "' respectively"); 1344 return; 1345 } 1346 config.setParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI); 1347 config.setParameter("schema-location", test1_xsd_url); 1348 1349 String resultOK = "OK"; 1350 StringBuffer result = new StringBuffer(); 1351 if (!config.canSetParameter("validate-if-schema", Boolean.FALSE)) { 1352 result.append("; cannot set the parameters 'validate-if-schema' to false"); 1353 } else { 1354 config.setParameter("validate-if-schema", Boolean.FALSE); 1355 TestHandler testHandler = new TestHandler(); 1356 config.setParameter("error-handler", testHandler); 1357 doc.normalizeDocument(); 1358 if (testHandler.getError() != null || null != testHandler.getFatalError()) { 1359 result.append("; unexpected error: " + testHandler.getFatalError() + "; " + testHandler.getError()); 1360 } 1361 } 1362 1363 if (!config.canSetParameter("validate-if-schema", Boolean.TRUE)) { 1364 resultOK = "OK, setting the parameter 'validate-if-schema' to true is not supported"; 1365 } else { 1366 config.setParameter("validate-if-schema", Boolean.TRUE); 1367 TestHandler testHandler = new TestHandler(); 1368 config.setParameter("error-handler", testHandler); 1369 doc.normalizeDocument(); 1370 if (testHandler.getError() == null && null == testHandler.getFatalError()) { 1371 result.append("; no error was reported when the 'validate-if-schema' is set to true"); 1372 } 1373 } 1374 1375 if (result.length() > 0) { 1376 Assert.fail(result.toString().substring(2)); 1377 } 1378 return; // Status.passed(resultOK); 1379 1380 } 1381 1382 /** 1383 * Equivalence class partitioning with state and input values orientation 1384 * for public void setParameter(String name, Object value) throws 1385 * DOMException, <br> 1386 * <b>pre-conditions</b>: The root element is not declared in the schema 1387 * specified by setting the 'schema-location' and the 'schema-type' 1388 * parameters., <br> 1389 * <b>name</b>: validate-if-schema <br> 1390 * <b>value</b>: true. <br> 1391 * <b>Expected results</b>: No error is reported 1392 */ 1393 @Test 1394 public void testValidateIfSchema002() { 1395 DOMImplementation domImpl = null; 1396 try { 1397 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 1398 dbf.setNamespaceAware(true); 1399 dbf.setValidating(true); 1400 domImpl = dbf.newDocumentBuilder().getDOMImplementation(); 1401 } catch (ParserConfigurationException pce) { 1402 Assert.fail(pce.toString()); 1403 } catch (FactoryConfigurationError fce) { 1404 Assert.fail(fce.toString()); 1405 } 1406 1407 Document doc = domImpl.createDocument("test", "ns:undeclared_root", null); 1408 1409 Element root = doc.getDocumentElement(); 1410 root.appendChild(doc.createTextNode("xxx")); 1411 1412 DOMConfiguration config = doc.getDomConfig(); 1413 1414 if (!config.canSetParameter("schema-location", test1_xsd_url) || !config.canSetParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI)) { 1415 System.out.println("cannot set the parameters 'schema-location' and 'schema-type'" + " to 'DOMConfigurationTest.xsd' and '" 1416 + XMLConstants.W3C_XML_SCHEMA_NS_URI + "' respectively"); 1417 return; 1418 } 1419 config.setParameter("schema-type", XMLConstants.W3C_XML_SCHEMA_NS_URI); 1420 config.setParameter("schema-location", test1_xsd_url); 1421 1422 if (!config.canSetParameter("validate-if-schema", Boolean.TRUE)) { 1423 System.out.println("OK, setting the parameter 'validate-if-schema'" + " to true is not supported"); 1424 return; 1425 } 1426 1427 config.setParameter("validate-if-schema", Boolean.TRUE); 1428 TestHandler testHandler = new TestHandler(); 1429 config.setParameter("error-handler", testHandler); 1430 doc.normalizeDocument(); 1431 if (testHandler.getError() != null || null != testHandler.getFatalError()) { 1432 Assert.fail("unexpected error: " + testHandler.getFatalError() + "; " + testHandler.getError()); 1433 } 1434 return; // Status.passed("OK"); 1435 1436 } 1437 1438 /** 1439 * Equivalence class partitioning with state and input values orientation 1440 * for public void setParameter(String name, Object value) throws 1441 * DOMException, <br> 1442 * <b>pre-conditions</b>: the attribute has EntityReference to '<', <br> 1443 * <b>name</b>: well-formed <br> 1444 * <b>value</b>: true. <br> 1445 * <b>Expected results</b>: An error is reported 1446 */ 1447 @Test 1448 public void testWellFormed001() { 1449 Document doc = null; 1450 try { 1451 doc = loadDocument(null, test2_xml); 1452 } catch (Exception e) { 1453 Assert.fail(e.getMessage()); 1454 } 1455 1456 DOMConfiguration config = doc.getDomConfig(); 1457 if (!config.canSetParameter("well-formed", Boolean.TRUE)) { 1458 Assert.fail("setting 'well-formed' to true is not supported"); 1459 } 1460 config.setParameter("well-formed", Boolean.TRUE); 1461 1462 Element root = doc.getDocumentElement(); 1463 1464 Attr attr = doc.createAttributeNS(null, "attr"); 1465 1466 try { 1467 attr.appendChild(doc.createEntityReference("<")); 1468 } catch (DOMException domException) { 1469 System.out.println("testWellFormed001: Expected DOMException for Attribute value = '<'" + domException.toString()); 1470 return; // OK 1471 } 1472 1473 root.setAttributeNode(attr); 1474 1475 TestHandler testHandler = new TestHandler(); 1476 config.setParameter("error-handler", testHandler); 1477 1478 doc.normalizeDocument(); 1479 1480 if (testHandler.getError() == null && null == testHandler.getFatalError()) { 1481 Assert.fail("no error was reported when attribute has <"); 1482 } 1483 1484 return; // Status.passed("OK"); 1485 } 1486 1487 /** 1488 * Equivalence class partitioning with state and input values orientation 1489 * for public void setParameter(String name, Object value) throws 1490 * DOMException, <br> 1491 * <b>pre-conditions</b>: the attribute has EntityReference to '<', <br> 1492 * <b>name</b>: well-formed <br> 1493 * <b>value</b>: false. <br> 1494 * <b>Expected results</b>: No error is reported 1495 */ 1496 @Test 1497 public void testWellFormed002() { 1498 Document doc = null; 1499 try { 1500 doc = loadDocument(null, test2_xml); 1501 } catch (Exception e) { 1502 Assert.fail(e.getMessage()); 1503 } 1504 1505 DOMConfiguration config = doc.getDomConfig(); 1506 if (!config.canSetParameter("well-formed", Boolean.FALSE)) { 1507 System.out.println("OK, setting 'well-formed' to false is not supported"); 1508 return; 1509 } 1510 config.setParameter("well-formed", Boolean.FALSE); 1511 1512 Element root = doc.getDocumentElement(); 1513 1514 Attr attr = doc.createAttributeNS(null, "attr"); 1515 attr.appendChild(doc.createEntityReference("x")); 1516 1517 root.setAttributeNode(attr); 1518 1519 TestHandler testHandler = new TestHandler(); 1520 config.setParameter("error-handler", testHandler); 1521 1522 doc.normalizeDocument(); 1523 1524 if (testHandler.getError() != null || null != testHandler.getFatalError()) { 1525 Assert.fail("unexpected error: " + testHandler.getFatalError() + "; " + testHandler.getError()); 1526 } 1527 1528 return; // Status.passed("OK"); 1529 1530 } 1531 1532 /** 1533 * Equivalence class partitioning with state and input values orientation 1534 * for public void setParameter(String name, Object value) throws 1535 * DOMException, <br> 1536 * <b>pre-conditions</b>: the document root element has a text node with 1537 * four white space characters, <br> 1538 * <b>name</b>: element-content-whitespace <br> 1539 * <b>value</b>: true. <br> 1540 * <b>Expected results</b>: the text node is preserved 1541 */ 1542 @Test 1543 public void testECWhitespace001() { 1544 Document doc = null; 1545 try { 1546 doc = loadDocument(null, test3_xml); 1547 } catch (Exception e) { 1548 Assert.fail(e.getMessage()); 1549 } 1550 1551 Element root = doc.getDocumentElement(); 1552 Text text = doc.createTextNode("\t\n\r "); 1553 root.appendChild(text); 1554 1555 DOMConfiguration config = doc.getDomConfig(); 1556 if (!config.canSetParameter("element-content-whitespace", Boolean.TRUE)) { 1557 Assert.fail("setting 'element-content-whitespace' to true is not supported"); 1558 } 1559 config.setParameter("element-content-whitespace", Boolean.TRUE); 1560 1561 if (!config.canSetParameter("validate", Boolean.TRUE)) { 1562 System.out.println("OK, setting 'validate' to true is not supported"); 1563 return; 1564 } 1565 config.setParameter("validate", Boolean.TRUE); 1566 1567 setHandler(doc); 1568 doc.normalizeDocument(); 1569 1570 Node firstChild = root.getFirstChild(); 1571 if (firstChild == null || firstChild.getNodeType() != Node.TEXT_NODE || !((Text) firstChild).isElementContentWhitespace()) { 1572 Assert.fail("the first child is " + firstChild + ", expected a text node with the four whitespace characters"); 1573 } 1574 1575 return; // Status.passed("OK"); 1576 1577 } 1578 1579 /** 1580 * Equivalence class partitioning with state and input values orientation 1581 * for public void setParameter(String name, Object value) throws 1582 * DOMException, <br> 1583 * <b>pre-conditions</b>: the document root element has a text node with 1584 * four white space characters, <br> 1585 * <b>name</b>: element-content-whitespace <br> 1586 * <b>value</b>: false. <br> 1587 * <b>Expected results</b>: the text node is discarded 1588 */ 1589 @Test 1590 public void testECWhitespace002() { 1591 Document doc = null; 1592 try { 1593 doc = loadDocument(null, test3_xml); 1594 } catch (Exception e) { 1595 Assert.fail(e.getMessage()); 1596 } 1597 1598 Element root = doc.getDocumentElement(); 1599 Text text = doc.createTextNode("\t\n\r "); 1600 root.appendChild(text); 1601 1602 DOMConfiguration config = doc.getDomConfig(); 1603 if (!config.canSetParameter("element-content-whitespace", Boolean.FALSE)) { 1604 System.out.println("OK, setting 'element-content-whitespace' to false is not supported"); 1605 return; 1606 } 1607 config.setParameter("element-content-whitespace", Boolean.FALSE); 1608 1609 if (!config.canSetParameter("validate", Boolean.TRUE)) { 1610 System.out.println("OK, setting 'validate' to true is not supported"); 1611 return; 1612 } 1613 config.setParameter("validate", Boolean.TRUE); 1614 1615 setHandler(doc); 1616 doc.normalizeDocument(); 1617 1618 Node firstChild = root.getFirstChild(); 1619 if (firstChild != null) { 1620 Assert.fail("the first child is " + firstChild + ", but no child is expected"); 1621 } 1622 1623 return; // Status.passed("OK"); 1624 1625 } 1626 }