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