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 
  24 package javax.xml.parsers.ptests;
  25 
  26 import static org.testng.Assert.assertEquals;
  27 import static org.testng.Assert.assertFalse;
  28 import static org.testng.Assert.assertNotNull;
  29 import static org.testng.Assert.assertNull;
  30 import static org.testng.Assert.assertTrue;
  31 import java.io.BufferedReader;
  32 import java.io.File;
  33 import java.io.FileInputStream;
  34 import java.io.FilePermission;
  35 import java.io.FileReader;
  36 import java.io.IOException;
  37 import java.util.PropertyPermission;
  38 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
  39 import javax.xml.parsers.DocumentBuilder;
  40 import javax.xml.parsers.DocumentBuilderFactory;
  41 import javax.xml.parsers.ParserConfigurationException;
  42 import javax.xml.parsers.SAXParser;
  43 import javax.xml.parsers.SAXParserFactory;
  44 import static javax.xml.parsers.ptests.TestUtils.XML_DIR;
  45 import jaxp.library.JAXPFileBaseTest;
  46 import org.testng.annotations.Test;
  47 import org.w3c.dom.Document;
  48 import org.w3c.dom.Element;
  49 import org.w3c.dom.NodeList;
  50 import org.xml.sax.InputSource;
  51 import org.xml.sax.SAXException;
  52 import org.xml.sax.helpers.DefaultHandler;
  53 
  54 /**
  55  * This checks the methods of DocumentBuilderFactoryImpl.
  56  */
  57 public class DocumentBuilderFactory01 extends JAXPFileBaseTest {
  58     /**
  59      * Test the default functionality of schema support method.
  60      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  61      *         created which satisfies the configuration requested.
  62      * @throws SAXException If any parse errors occur.
  63      * @throws IOException if the file exists but is a directory rather than
  64      *         a regular file, does not exist but cannot be created, or cannot 
  65      *         be opened for any other reason.
  66      */
  67     @Test
  68     public void testCheckSchemaSupport1() throws ParserConfigurationException, 
  69             SAXException, IOException {
  70         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  71         dbf.setValidating(true);
  72         dbf.setNamespaceAware(true);
  73         dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 
  74                 W3C_XML_SCHEMA_NS_URI);
  75         MyErrorHandler eh = MyErrorHandler.newInstance();
  76         DocumentBuilder db = dbf.newDocumentBuilder();
  77         db.setErrorHandler(eh);
  78         Document doc = db.parse(new File(XML_DIR, "test.xml"));
  79         assertFalse(eh.isErrorOccured());
  80     }
  81 
  82     /**
  83      * Test the default functionality of schema support method. In
  84      * this case the schema source property is set.
  85      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  86      *         created which satisfies the configuration requested.
  87      * @throws SAXException If any parse errors occur.
  88      * @throws IOException if the file exists but is a directory rather than
  89      *         a regular file, does not exist but cannot be created, or cannot 
  90      *         be opened for any other reason.
  91      */
  92     @Test
  93     public void testCheckSchemaSupport2() throws ParserConfigurationException, 
  94             SAXException, IOException {
  95         try (FileInputStream fis = new FileInputStream(new File(
  96                 XML_DIR, "test.xsd"))) {
  97             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  98             dbf.setValidating(true);
  99             dbf.setNamespaceAware(true);
 100             dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 
 101                     W3C_XML_SCHEMA_NS_URI);
 102             dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", 
 103                     new InputSource(fis));
 104             MyErrorHandler eh = MyErrorHandler.newInstance();
 105             DocumentBuilder db = dbf.newDocumentBuilder();
 106             db.setErrorHandler(eh);
 107             db.parse(new File(XML_DIR, "test1.xml"));
 108             assertFalse(eh.isErrorOccured());
 109         }
 110     }
 111 
 112     /**
 113      * Test the default functionality of schema support method. In
 114      * this case the schema source property is set.
 115      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 116      *         created which satisfies the configuration requested.
 117      * @throws SAXException If any parse errors occur.
 118      * @throws IOException if the file exists but is a directory rather than
 119      *         a regular file, does not exist but cannot be created, or cannot 
 120      *         be opened for any other reason.
 121      */
 122     @Test
 123     public void testCheckSchemaSupport3() throws SAXException,
 124             ParserConfigurationException, IOException {
 125         try (FileInputStream fis = new FileInputStream(new File(
 126                 XML_DIR, "test.xsd"))) {
 127             SAXParserFactory spf = SAXParserFactory.newInstance();
 128             spf.setNamespaceAware(true);
 129             spf.setValidating(true);
 130             spf.setNamespaceAware(true);
 131             SAXParser sp = spf.newSAXParser();
 132             sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 
 133                     W3C_XML_SCHEMA_NS_URI);
 134             sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource",
 135                     new InputSource(fis));
 136             DefaultHandler dh = new DefaultHandler();
 137             sp.parse(new File(XML_DIR, "test1.xml"), dh);
 138         }
 139     }
 140 
 141     /**
 142      * Test the default functionality of newInstance method. To test
 143      * the isCoalescing method and setCoalescing This checks to see if the CDATA
 144      * and text nodes got combined In that case it will print "<xml>This
 145      * is not parsed</xml> yet".
 146      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 147      *         created which satisfies the configuration requested.
 148      * @throws SAXException If any parse errors occur.
 149      * @throws IOException if the file exists but is a directory rather than
 150      *         a regular file, does not exist but cannot be created, or cannot 
 151      *         be opened for any other reason.
 152      */
 153     @Test
 154     public void testCheckDocumentBuilderFactory02() throws SAXException,
 155             ParserConfigurationException, IOException {
 156         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 157         dbf.setCoalescing(true);
 158         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 159         Document doc = docBuilder.parse(new File(XML_DIR, "DocumentBuilderFactory01.xml"));
 160         Element e = (Element) doc.getElementsByTagName("html").item(0);
 161         NodeList nl = e.getChildNodes();
 162         assertEquals(nl.getLength(), 1);
 163     }
 164 
 165     /**
 166      * Test the isIgnoringComments. By default it is false.
 167      */
 168     @Test
 169     public void testCheckDocumentBuilderFactory03() {
 170         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 171         assertFalse(dbf.isIgnoringComments());
 172     }
 173 
 174     /**
 175      * Test the isValidating. By default it is false, set it to true and then 
 176      * use a document which is not valid. It should throw a warning or
 177      * an error at least. The test passes in case retval 0 is set in the error
 178      * method .
 179      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 180      *         created which satisfies the configuration requested.
 181      * @throws SAXException If any parse errors occur.
 182      * @throws IOException if the file exists but is a directory rather than
 183      *         a regular file, does not exist but cannot be created, or cannot 
 184      *         be opened for any other reason.
 185      */
 186     @Test
 187     public void testCheckDocumentBuilderFactory04() throws 
 188             ParserConfigurationException, SAXException, IOException {
 189         MyErrorHandler eh = MyErrorHandler.newInstance();
 190         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 191         dbf.setValidating(true);
 192         DocumentBuilder db = dbf.newDocumentBuilder();
 193         db.setErrorHandler(eh);
 194         db.parse(new File(XML_DIR, "DocumentBuilderFactory05.xml"));
 195         assertTrue(eh.isErrorOccured());
 196     }
 197 
 198     /**
 199      * Test the setValidating. By default it is false, use a
 200      * document which is not valid. It should not throw a warning or an error.
 201      * The test passes in case the return value equals 1.
 202      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 203      *         created which satisfies the configuration requested.
 204      * @throws SAXException If any parse errors occur.
 205      * @throws IOException if the file exists but is a directory rather than
 206      *         a regular file, does not exist but cannot be created, or cannot 
 207      *         be opened for any other reason.
 208      */
 209     @Test
 210     public void testCheckDocumentBuilderFactory16() throws 
 211             ParserConfigurationException, SAXException, IOException {
 212         MyErrorHandler eh = MyErrorHandler.newInstance();
 213         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 214         DocumentBuilder db = dbf.newDocumentBuilder();
 215         db.setErrorHandler(eh);
 216         db.parse(new File(XML_DIR, "DocumentBuilderFactory05.xml"));
 217         assertFalse(eh.isErrorOccured());
 218     }
 219 
 220     /**
 221      * Test the setValidating. By default it is false, use a
 222      * document which is valid. It should not throw a warning or an error. The
 223      * test passes in case the return value equals 1.
 224      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 225      *         created which satisfies the configuration requested.
 226      * @throws SAXException If any parse errors occur.
 227      * @throws IOException if the file exists but is a directory rather than
 228      *         a regular file, does not exist but cannot be created, or cannot 
 229      *         be opened for any other reason.
 230      */
 231     @Test
 232     public void testCheckDocumentBuilderFactory17() throws 
 233             ParserConfigurationException, SAXException, IOException {
 234         MyErrorHandler eh = MyErrorHandler.newInstance();
 235         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 236         DocumentBuilder db = dbf.newDocumentBuilder();
 237         db.setErrorHandler(eh);
 238         db.parse(new File(XML_DIR, "DocumentBuilderFactory04.xml"));
 239         assertFalse(eh.isErrorOccured());
 240     }
 241 
 242     /**
 243      * Test the isExpandEntityReferences. By default it is true.
 244      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 245      *         created which satisfies the configuration requested.
 246      * @throws SAXException If any parse errors occur.
 247      * @throws IOException if the file exists but is a directory rather than
 248      *         a regular file, does not exist but cannot be created, or cannot 
 249      *         be opened for any other reason.
 250      */
 251     @Test
 252     public void testCheckDocumentBuilderFactory05() throws 
 253             ParserConfigurationException, SAXException, IOException {
 254         try(FileInputStream fis = new FileInputStream(new File(
 255                 XML_DIR, "DocumentBuilderFactory02.xml"))) {
 256             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 257             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 258             Document doc = docBuilder.parse(fis);
 259             Element e = (Element) doc.getElementsByTagName("title").item(0);
 260             NodeList nl = e.getChildNodes();
 261             assertTrue(dbf.isExpandEntityReferences());
 262             assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W');
 263         }
 264     }
 265 
 266     /**
 267      * Test the default functionality of setValidating method. The
 268      * XML file has a DTD which has namespaces defined. The parser takes care to
 269      * check if the namespaces using elements and defined attributes are there
 270      * or not.
 271      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 272      *         created which satisfies the configuration requested.
 273      * @throws SAXException If any parse errors occur.
 274      * @throws IOException if the file exists but is a directory rather than
 275      *         a regular file, does not exist but cannot be created, or cannot 
 276      *         be opened for any other reason.
 277      */
 278     @Test
 279     public void testCheckDocumentBuilderFactory06() throws 
 280             ParserConfigurationException, SAXException, IOException {
 281         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 282         dbf.setValidating(true);
 283         DocumentBuilder db = dbf.newDocumentBuilder();
 284         MyErrorHandler eh = MyErrorHandler.newInstance();
 285         db.setErrorHandler(eh);
 286         Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory04.xml"));
 287         assertTrue(doc instanceof Document);
 288         assertFalse(eh.isErrorOccured());
 289     }
 290 
 291     /**
 292      * Test the setExpandEntityReferences.
 293      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 294      *         created which satisfies the configuration requested.
 295      * @throws SAXException If any parse errors occur.
 296      * @throws IOException if the file exists but is a directory rather than
 297      *         a regular file, does not exist but cannot be created, or cannot 
 298      *         be opened for any other reason.
 299      */
 300     @Test
 301     public void testCheckDocumentBuilderFactory07() throws 
 302             ParserConfigurationException, SAXException, IOException {
 303         try (FileInputStream fis = new FileInputStream(new File(
 304                 XML_DIR, "DocumentBuilderFactory02.xml"))) {
 305             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 306             dbf.setExpandEntityReferences(true);
 307             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 308             Document doc = docBuilder.parse(fis);
 309             Element e = (Element) doc.getElementsByTagName("title").item(0);
 310             NodeList nl = e.getChildNodes();
 311             assertTrue(dbf.isExpandEntityReferences());
 312             assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W');
 313         } 
 314     }
 315 
 316     /**
 317      * Test the setExpandEntityReferences.
 318      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 319      *         created which satisfies the configuration requested.
 320      * @throws SAXException If any parse errors occur.
 321      * @throws IOException if the file exists but is a directory rather than
 322      *         a regular file, does not exist but cannot be created, or cannot 
 323      *         be opened for any other reason.
 324      */
 325     @Test
 326     public void testCheckDocumentBuilderFactory08() throws 
 327             ParserConfigurationException, SAXException, IOException {
 328         try (FileInputStream fis = new FileInputStream(new File(
 329                 XML_DIR, "DocumentBuilderFactory02.xml"))) {
 330             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 331             dbf.setExpandEntityReferences(false);
 332             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 333             Document doc = docBuilder.parse(fis);
 334             Element e = (Element) doc.getElementsByTagName("title").item(0);
 335             NodeList nl = e.getChildNodes();
 336             assertNull(nl.item(0).getNodeValue());
 337         }
 338     }
 339 
 340     /**
 341      * Test the setIgnoringComments. By default it is set to false.
 342      * explicitly setting it to false, it recognizes the comment which is in
 343      * Element Node Hence the Element's child node is not null.
 344      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 345      *         created which satisfies the configuration requested.
 346      * @throws SAXException If any parse errors occur.
 347      * @throws IOException if the file exists but is a directory rather than
 348      *         a regular file, does not exist but cannot be created, or cannot 
 349      *         be opened for any other reason.
 350      */
 351     @Test
 352     public void testCheckDocumentBuilderFactory09() throws SAXException, 
 353             IOException, ParserConfigurationException {
 354         try (FileInputStream fis = new FileInputStream(new File(
 355                 XML_DIR, "DocumentBuilderFactory07.xml"))) {
 356             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 357             dbf.setIgnoringComments(false);
 358             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 359             Document doc = docBuilder.parse(fis);
 360             Element e = (Element) doc.getElementsByTagName("body").item(0);
 361             NodeList nl = e.getChildNodes();
 362             assertNotNull(nl.item(0).getNodeValue());
 363         }
 364     }
 365 
 366     /**
 367      * This tests for the parse(InputSource).
 368      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 369      *         created which satisfies the configuration requested.
 370      * @throws SAXException If any parse errors occur.
 371      * @throws IOException if the file exists but is a directory rather than
 372      *         a regular file, does not exist but cannot be created, or cannot 
 373      *         be opened for any other reason.
 374      */
 375     @Test
 376     public void testCheckDocumentBuilderFactory10() throws 
 377             ParserConfigurationException, SAXException, IOException {
 378         try (BufferedReader br = new BufferedReader(new FileReader(new File(
 379                 XML_DIR, "DocumentBuilderFactory07.xml")))) {
 380             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 381             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 382             Document doc = docBuilder.parse(new InputSource(br));
 383             assertTrue(doc instanceof Document);
 384         } 
 385     }
 386 
 387     /**
 388      * This tests for the parse InputStream with SystemID as a second parameter.
 389      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 390      *         created which satisfies the configuration requested.
 391      * @throws SAXException If any parse errors occur.
 392      * @throws IOException if the file exists but is a directory rather than
 393      *         a regular file, does not exist but cannot be created, or cannot 
 394      *         be opened for any other reason.
 395      */
 396     @Test
 397     public void testCheckDocumentBuilderFactory11() throws 
 398             ParserConfigurationException, SAXException, IOException {
 399         try (FileInputStream fis = new FileInputStream(new File(
 400                 XML_DIR, "dbf10import.xsl"))) {
 401             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 402             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 403             Document doc = docBuilder.parse(fis, new File(XML_DIR).toURI()
 404                     .toASCIIString());
 405             assertTrue(doc instanceof Document);
 406         }
 407     }
 408 
 409     /**
 410      * This tests for the parse InputStream with empty SystemID as a second
 411      * parameter.
 412      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 413      *         created which satisfies the configuration requested.
 414      * @throws SAXException If any parse errors occur.
 415      * @throws IOException if the file exists but is a directory rather than
 416      *         a regular file, does not exist but cannot be created, or cannot 
 417      *         be opened for any other reason.
 418      */
 419     @Test
 420     public void testCheckDocumentBuilderFactory12() throws 
 421             ParserConfigurationException, SAXException, IOException {
 422         try (FileInputStream fis = new FileInputStream(new File(
 423                 XML_DIR, "dbf10import.xsl"))) {
 424             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 425             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 426             Document doc = docBuilder.parse(fis, " ");
 427             assertTrue(doc instanceof Document);
 428         }
 429     }
 430 
 431     /**
 432      * This tests for the parse(uri).
 433      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 434      *         created which satisfies the configuration requested.
 435      * @throws SAXException If any parse errors occur.
 436      * @throws IOException if the file exists but is a directory rather than
 437      *         a regular file, does not exist but cannot be created, or cannot 
 438      *         be opened for any other reason.
 439      */
 440     @Test
 441     public void testCheckDocumentBuilderFactory13() throws 
 442             ParserConfigurationException, SAXException, IOException {
 443         // Accesing default working directory.
 444         String workingDir = System.getProperty("test.classes") + "../../../../../../..";
 445         setPermissions(new PropertyPermission("user.dir", "read"),
 446                 new FilePermission(workingDir + "/-", "read"));
 447         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 448         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 449         Document doc = docBuilder.parse(new File(XML_DIR + "dbf10import.xsl")
 450                 .toURI().toASCIIString());
 451         assertTrue(doc instanceof Document);  
 452     }
 453 
 454     /**
 455      * This tests for the parse(uri) with empty string as parameter should
 456      * throw Sax Exception.
 457      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 458      *         created which satisfies the configuration requested.
 459      * @throws SAXException If any parse errors occur.
 460      * @throws IOException if the file exists but is a directory rather than
 461      *         a regular file, does not exist but cannot be created, or cannot 
 462      *         be opened for any other reason.
 463      */
 464     @Test(expectedExceptions = SAXException.class)
 465     public void testCheckDocumentBuilderFactory14() throws SAXException, 
 466             ParserConfigurationException, IOException {
 467         // Accesing default working directory.
 468         String workingDir = System.getProperty("test.classes") + "../../../../../../..";
 469         setPermissions(new FilePermission(workingDir + "/-", "read"));
 470 
 471         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 472         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 473         docBuilder.parse("");
 474     }
 475 
 476     /**
 477      * This tests for the parse (uri) with null uri as parameter should throw
 478      * IllegalArgumentException.
 479      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 480      *         created which satisfies the configuration requested.
 481      * @throws SAXException If any parse errors occur.
 482      * @throws IOException if the file exists but is a directory rather than
 483      *         a regular file, does not exist but cannot be created, or cannot 
 484      *         be opened for any other reason.
 485      *
 486      */
 487     @Test(expectedExceptions = IllegalArgumentException.class)
 488     public void testCheckDocumentBuilderFactory15() throws SAXException, 
 489             ParserConfigurationException, IOException {
 490         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 491         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 492         String uri = null;
 493         docBuilder.parse(uri);
 494     }
 495 
 496     /**
 497      * Test the setIgnoringComments. By default it is set to false,
 498      * setting this to true, It does not recognize the comment, Here the
 499      * nodelist has a length 0 because the ignoring comments is true.
 500      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 501      *         created which satisfies the configuration requested.
 502      * @throws SAXException If any parse errors occur.
 503      * @throws IOException if the file exists but is a directory rather than
 504      *         a regular file, does not exist but cannot be created, or cannot 
 505      *         be opened for any other reason.
 506      */
 507     @Test
 508     public void testCheckIgnoringComments() throws SAXException, 
 509             ParserConfigurationException, IOException {
 510         try (FileInputStream fis = new FileInputStream(new File(
 511                 XML_DIR, "DocumentBuilderFactory08.xml"))) {
 512             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 513             dbf.setIgnoringComments(true);
 514             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 515             Document doc = docBuilder.parse(fis);
 516             Element e = (Element) doc.getElementsByTagName("body").item(0);
 517             NodeList nl = e.getChildNodes();
 518             assertEquals(nl.getLength(), 0);
 519         }
 520     }
 521 
 522     /**
 523      * Test the default behaviour of setIgnoringComments. By default
 524      * it is set to false, this is similar to case 9 but not setIgnoringComments
 525      * explicitly, it does not recognize the comment.
 526      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
 527      *         created which satisfies the configuration requested.
 528      * @throws SAXException If any parse errors occur.
 529      * @throws IOException if the file exists but is a directory rather than
 530      *         a regular file, does not exist but cannot be created, or cannot 
 531      *         be opened for any other reason.
 532      */
 533     @Test
 534     public void testCheckIgnoringComments1() throws SAXException, 
 535             ParserConfigurationException, IOException {
 536         try (FileInputStream fis = new FileInputStream(new File(
 537                 XML_DIR, "DocumentBuilderFactory07.xml"))) {
 538             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 539             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
 540             Document doc = docBuilder.parse(fis);
 541             Element e = (Element) doc.getElementsByTagName("body").item(0);
 542             NodeList nl = e.getChildNodes();
 543             assertFalse(dbf.isIgnoringComments());
 544             assertNotNull(nl.item(0).getNodeValue());
 545         }
 546     }
 547 }