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 * @test 61 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 62 * @run testng/othervm -DrunSecMngr=true dom.DOMConfigurationTest 63 * @run testng/othervm dom.DOMConfigurationTest 64 * @summary Test DOMConfiguration for supported properties. 65 */ 66 @Listeners({jaxp.library.FilePolicy.class}) 67 public class DOMConfigurationTest { 68 69 static class TestHandler implements DOMErrorHandler { 70 private String warning; 71 private String error; 72 private String fatalError; 73 74 public String getError() { 75 return error; 76 } 77 78 public String getFatalError() { 79 return fatalError; 80 } 81 82 public String getWarning() { 83 return warning; 84 } 85 86 public boolean handleError(DOMError error) { 87 if (error.getSeverity() == DOMError.SEVERITY_ERROR) { 88 this.error = "" + error.getMessage(); 89 return false; 90 } 91 if (error.getSeverity() == DOMError.SEVERITY_FATAL_ERROR) { 92 this.fatalError = "" + error.getMessage(); 93 return false; 94 } 95 this.warning = "" + error.getMessage(); 96 return true; // warning 97 } 98 } 99 100 static class TestFailureHandler implements DOMErrorHandler { 101 public boolean handleError(DOMError error) { 102 if (error.getSeverity() == DOMError.SEVERITY_ERROR) { 103 Assert.fail("Error: " + error.getMessage()); 104 } 105 if (error.getSeverity() == DOMError.SEVERITY_FATAL_ERROR) { 106 Assert.fail("Fatal error: " + error.getMessage()); 107 } 108 return true; // warning 109 } 110 } 111 112 void setHandler(Document doc) { 113 doc.getDomConfig().setParameter("error-handler", new TestFailureHandler()); 114 } 115 116 static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 117 118 static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; 119 120 static final String XMLNS = "http://www.w3.org/2000/xmlns/"; 121 122 static Document loadDocument(String schemaURL, String instanceText) { 123 Document document = null; 124 try { 125 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 126 dbf.setNamespaceAware(true); 127 dbf.setValidating(true); 128 if (schemaURL != null) { 129 dbf.setAttribute(SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI); 130 dbf.setAttribute(SCHEMA_SOURCE, schemaURL); 131 } 132 DocumentBuilder parser = dbf.newDocumentBuilder(); 133 134 InputSource inSource = new InputSource(new StringReader(instanceText)); 135 inSource.setSystemId("doc.xml"); 136 document = parser.parse(inSource); 137 } catch (ParserConfigurationException e) { 138 Assert.fail(e.toString()); 139 } catch (IOException e) { 140 Assert.fail(e.toString()); 141 } catch (SAXException e) { 142 Assert.fail(e.toString()); 143 } 144 145 return document; 146 } 147 148 static final String test_xml = "<?xml version=\"1.0\"?>\n" + "<test:root xmlns:test=\"test\" \n" 149 + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + ">	

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