< prev index next >

test/javax/xml/jaxp/functional/javax/xml/parsers/ptests/DocumentBuilderFactoryTest.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -21,431 +21,442 @@
  * questions.
  */
 
 package javax.xml.parsers.ptests;
 
-import static jaxp.library.JAXPTestUtilities.FILE_SEP;
-import static jaxp.library.JAXPTestUtilities.failUnexpected;
 import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertNull;
-import static org.testng.Assert.assertTrue;
-
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FilePermission;
 import java.io.FileReader;
-import java.io.IOException;
-
+import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
-
+import static javax.xml.parsers.ptests.ParserTestConst.GOLDEN_DIR;
+import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXResult;
+import jaxp.library.JAXPFileBaseTest;
+import static jaxp.library.JAXPTestUtilities.USER_DIR;
+import static jaxp.library.JAXPTestUtilities.compareWithGold;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
 import org.testng.annotations.Test;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.DefaultHandler;
 
 /**
- * This checks the methods of DocumentBuilderFactoryImpl
+ * This checks the methods of DocumentBuilderFactoryImpl.
  */
-public class DocumentBuilderFactory01 {
+public class DocumentBuilderFactoryTest extends JAXPFileBaseTest {
     /**
-     * Testcase to test the default functionality of schema support method.
+     * Test the default functionality of schema support method.
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckSchemaSupport1() {
-        try {
+    public void testCheckSchemaSupport1() throws Exception {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setValidating(true);
             dbf.setNamespaceAware(true);
-            dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
+        dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 
+                W3C_XML_SCHEMA_NS_URI);
             MyErrorHandler eh = MyErrorHandler.newInstance();
             DocumentBuilder db = dbf.newDocumentBuilder();
             db.setErrorHandler(eh);
-            Document doc = db.parse(new File(TestUtils.XML_DIR, "test.xml"));
-            assertFalse(eh.errorOccured);
-        } catch (ParserConfigurationException | SAXException | IOException e) {
-            failUnexpected(e);
-        }
+        db.parse(new File(XML_DIR, "test.xml"));
+        assertFalse(eh.isErrorOccured());
     }
 
     /**
-     * Testcase to test the default functionality of schema support method. In
+     * Test the default functionality of schema support method. In
      * this case the schema source property is set.
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckSchemaSupport2() {
-        try {
+    public void testCheckSchemaSupport2() throws Exception {
+        try (FileInputStream fis = new FileInputStream(new File(
+                XML_DIR, "test.xsd"))) {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setValidating(true);
             dbf.setNamespaceAware(true);
-            dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
-            dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", new InputSource(new FileInputStream(
-                    new File(TestUtils.XML_DIR, "test.xsd"))));
+            dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 
+                    W3C_XML_SCHEMA_NS_URI);
+            dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", 
+                    new InputSource(fis));
             MyErrorHandler eh = MyErrorHandler.newInstance();
             DocumentBuilder db = dbf.newDocumentBuilder();
             db.setErrorHandler(eh);
-            Document doc = db.parse(new File(TestUtils.XML_DIR, "test1.xml"));
-            assertFalse(eh.errorOccured);
-        } catch (IllegalArgumentException | ParserConfigurationException | SAXException | IOException e) {
-            failUnexpected(e);
+            db.parse(new File(XML_DIR, "test1.xml"));
+            assertFalse(eh.isErrorOccured());
         }
-
     }
 
     /**
-     * Testcase to test the default functionality of schema support method. In
+     * Test the default functionality of schema support method. In
      * this case the schema source property is set.
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckSchemaSupport3() {
-        try {
+    public void testCheckSchemaSupport3() throws Exception {
+        try (FileInputStream fis = new FileInputStream(new File(
+                XML_DIR, "test.xsd"))) {
             SAXParserFactory spf = SAXParserFactory.newInstance();
             spf.setNamespaceAware(true);
             spf.setValidating(true);
             spf.setNamespaceAware(true);
             SAXParser sp = spf.newSAXParser();
-            sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
+            sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 
+                    W3C_XML_SCHEMA_NS_URI);
             sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource",
-                    new InputSource(new FileInputStream(new File(TestUtils.XML_DIR, "test.xsd"))));
+                    new InputSource(fis));
             DefaultHandler dh = new DefaultHandler();
-            sp.parse(new File(TestUtils.XML_DIR, "test1.xml"), dh);
-        } catch (ParserConfigurationException | SAXException | IOException e) {
-            failUnexpected(e);
+            // Not expect any unrecoverable error here.
+            sp.parse(new File(XML_DIR, "test1.xml"), dh);
         }
     }
 
     /**
-     * Testcase to test the default functionality of newInstance method. To test
+     * Test the default functionality of newInstance method. To test
      * the isCoalescing method and setCoalescing This checks to see if the CDATA
      * and text nodes got combined In that case it will print "&lt;xml&gt;This
      * is not parsed&lt;/xml&gt; yet".
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckDocumentBuilderFactory02() {
-        try {
+    public void testCheckDocumentBuilderFactory02() throws Exception {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setCoalescing(true);
             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
-            Document doc = docBuilder.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory01.xml"));
+        Document doc = docBuilder.parse(new File(XML_DIR, "DocumentBuilderFactory01.xml"));
             Element e = (Element) doc.getElementsByTagName("html").item(0);
             NodeList nl = e.getChildNodes();
-            assertEquals(nl.item(0).getNodeValue().trim(), "<xml>This is not parsed</xml> yet");
-        } catch (IOException | SAXException | ParserConfigurationException e) {
-            failUnexpected(e);
-        }
+        assertEquals(nl.getLength(), 1);
     }
 
     /**
-     * Testcase to test the isIgnoringComments. By default it is false.
+     * Test the isIgnoringComments. By default it is false.
      */
     @Test
     public void testCheckDocumentBuilderFactory03() {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         assertFalse(dbf.isIgnoringComments());
     }
 
     /**
-     * Testcase to test the isValidating. By default it is false, set it to true
-     * and then use a document which is not valid. It should throw a warning or
+     * Test the isValidating. By default it is false, set it to true and then 
+     * use a document which is not valid. It should throw a warning or
      * an error at least. The test passes in case retval 0 is set in the error
      * method .
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckDocumentBuilderFactory04() {
-        try {
+    public void testCheckDocumentBuilderFactory04() throws Exception {
             MyErrorHandler eh = MyErrorHandler.newInstance();
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setValidating(true);
             DocumentBuilder db = dbf.newDocumentBuilder();
             db.setErrorHandler(eh);
-            Document doc = db.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory05.xml"));
-            assertTrue(eh.errorOccured);
-        } catch (ParserConfigurationException | IOException | SAXException e) {
-            failUnexpected(e);
-        }
+        db.parse(new File(XML_DIR, "DocumentBuilderFactory05.xml"));
+        assertTrue(eh.isErrorOccured());
     }
 
     /**
-     * Testcase to test the setValidating. By default it is false, use a
+     * Test the setValidating. By default it is false, use a
      * document which is not valid. It should not throw a warning or an error.
-     * The test passes in case the retval equals 1 .
+     * The test passes in case the return value equals 1.
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckDocumentBuilderFactory16() {
-        try {
+    public void testCheckDocumentBuilderFactory16() throws Exception {
             MyErrorHandler eh = MyErrorHandler.newInstance();
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder db = dbf.newDocumentBuilder();
             db.setErrorHandler(eh);
-            Document doc = db.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory05.xml"));
-            assertFalse(eh.errorOccured);
-        } catch (ParserConfigurationException | IOException | SAXException e) {
-            failUnexpected(e);
-        }
-
+        db.parse(new File(XML_DIR, "DocumentBuilderFactory05.xml"));
+        assertFalse(eh.isErrorOccured());
     }
 
     /**
-     * Testcase to test the setValidating. By default it is false, use a
+     * Test the setValidating. By default it is false, use a
      * document which is valid. It should not throw a warning or an error. The
-     * test passes in case the retval equals 1.
+     * test passes in case the return value equals 1.
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckDocumentBuilderFactory17() {
-        try {
+    public void testCheckDocumentBuilderFactory17() throws Exception {
             MyErrorHandler eh = MyErrorHandler.newInstance();
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder db = dbf.newDocumentBuilder();
             db.setErrorHandler(eh);
-            Document doc = db.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory04.xml"));
-            assertFalse(eh.errorOccured);
-        } catch (ParserConfigurationException | IOException | SAXException e) {
-            failUnexpected(e);
-        }
-
+        db.parse(new File(XML_DIR, "DocumentBuilderFactory04.xml"));
+        assertFalse(eh.isErrorOccured());
     }
 
     /**
-     * To test the isExpandEntityReferences. By default it is true.
+     * Test the isExpandEntityReferences. By default it is true.
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckDocumentBuilderFactory05() {
-        try {
+    public void testCheckDocumentBuilderFactory05() throws Exception {
+        try(FileInputStream fis = new FileInputStream(new File(
+                XML_DIR, "DocumentBuilderFactory02.xml"))) {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
-            Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory02.xml")));
+            Document doc = docBuilder.parse(fis);
             Element e = (Element) doc.getElementsByTagName("title").item(0);
             NodeList nl = e.getChildNodes();
             assertTrue(dbf.isExpandEntityReferences());
             assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W');
-        } catch (ParserConfigurationException | IOException | SAXException e) {
-            failUnexpected(e);
         }
     }
 
     /**
-     * Testcase to test the default functionality of setValidating method. The
-     * xml file has a DTD which has namespaces defined. The parser takes care to
+     * Test the default functionality of setValidating method. The
+     * XML file has a DTD which has namespaces defined. The parser takes care to
      * check if the namespaces using elements and defined attributes are there
      * or not.
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckDocumentBuilderFactory06() {
-        try {
+    public void testCheckDocumentBuilderFactory06() throws Exception {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setValidating(true);
             DocumentBuilder db = dbf.newDocumentBuilder();
             MyErrorHandler eh = MyErrorHandler.newInstance();
             db.setErrorHandler(eh);
-            Document doc = db.parse(new File(TestUtils.XML_DIR, "DocumentBuilderFactory04.xml"));
+        Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory04.xml"));
             assertTrue(doc instanceof Document);
-            assertFalse(eh.errorOccured);
-        } catch (ParserConfigurationException | IOException | SAXException e) {
-            failUnexpected(e);
-        }
-
+        assertFalse(eh.isErrorOccured());
     }
 
     /**
-     * Testcase to test the setExpandEntityReferences.
+     * Test the setExpandEntityReferences.
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckDocumentBuilderFactory07() {
-        try {
+    public void testCheckDocumentBuilderFactory07() throws Exception {
+        try (FileInputStream fis = new FileInputStream(new File(
+                XML_DIR, "DocumentBuilderFactory02.xml"))) {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setExpandEntityReferences(true);
             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
-            Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory02.xml")));
+            Document doc = docBuilder.parse(fis);
             Element e = (Element) doc.getElementsByTagName("title").item(0);
             NodeList nl = e.getChildNodes();
             assertTrue(dbf.isExpandEntityReferences());
             assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W');
-        } catch (ParserConfigurationException | IOException | SAXException e) {
-            failUnexpected(e);
         }
     }
 
     /**
-     * Testcase to test the setExpandEntityReferences.
+     * Test the setExpandEntityReferences.
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckDocumentBuilderFactory08() {
-        try {
+    public void testCheckDocumentBuilderFactory08() throws Exception {
+        try (FileInputStream fis = new FileInputStream(new File(
+                XML_DIR, "DocumentBuilderFactory02.xml"))) {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setExpandEntityReferences(false);
             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
-            Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory02.xml")));
+            Document doc = docBuilder.parse(fis);
             Element e = (Element) doc.getElementsByTagName("title").item(0);
             NodeList nl = e.getChildNodes();
             assertNull(nl.item(0).getNodeValue());
-        } catch (ParserConfigurationException | IOException | SAXException e) {
-            failUnexpected(e);
         }
     }
 
     /**
-     * Testcase to test the setIgnoringComments. By default it is set to false.
+     * Test the setIgnoringComments. By default it is set to false.
      * explicitly setting it to false, it recognizes the comment which is in
      * Element Node Hence the Element's child node is not null.
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckDocumentBuilderFactory09() {
-        try {
+    public void testCheckDocumentBuilderFactory09() throws Exception {
+        try (FileInputStream fis = new FileInputStream(new File(
+                XML_DIR, "DocumentBuilderFactory07.xml"))) {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setIgnoringComments(false);
             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
-            Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory07.xml")));
+            Document doc = docBuilder.parse(fis);
             Element e = (Element) doc.getElementsByTagName("body").item(0);
             NodeList nl = e.getChildNodes();
             assertNotNull(nl.item(0).getNodeValue());
-        } catch (ParserConfigurationException | IOException | SAXException e) {
-            failUnexpected(e);
         }
-
     }
 
     /**
      * This tests for the parse(InputSource).
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckDocumentBuilderFactory10() {
-        try {
+    public void testCheckDocumentBuilderFactory10() throws Exception {
+        try (BufferedReader br = new BufferedReader(new FileReader(new File(
+                XML_DIR, "DocumentBuilderFactory07.xml")))) {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
-            Document doc = docBuilder.parse(new InputSource(new BufferedReader(new FileReader(new File(TestUtils.XML_DIR, "DocumentBuilderFactory07.xml")))));
-            assertTrue(doc instanceof Document);
-        } catch (IllegalArgumentException | ParserConfigurationException | IOException | SAXException e) {
-            failUnexpected(e);
+            Document doc = docBuilder.parse(new InputSource(br));
+            assertNotNull(doc);
         }
     }
 
     /**
      * This tests for the parse InputStream with SystemID as a second parameter.
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckDocumentBuilderFactory11() {
-        try {
+    public void testCheckDocumentBuilderFactory11() throws Exception {
+        try (FileInputStream fis = new FileInputStream(new File(
+                XML_DIR, "dbf10import.xsl"))) {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
-            Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "dbf10import.xsl")), new File(TestUtils.XML_DIR).toURI()
+            Document doc = docBuilder.parse(fis, new File(XML_DIR).toURI()
                     .toASCIIString());
-            assertTrue(doc instanceof Document);
-        } catch (IllegalArgumentException | ParserConfigurationException | IOException | SAXException e) {
-            failUnexpected(e);
+            assertNotNull(doc);
         }
     }
 
     /**
      * This tests for the parse InputStream with empty SystemID as a second
      * parameter.
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckDocumentBuilderFactory12() {
-        try {
+    public void testCheckDocumentBuilderFactory12() throws Exception {
+        try (FileInputStream fis = new FileInputStream(new File(
+                XML_DIR, "dbf10import.xsl"))) {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
-            Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "dbf10import.xsl")), " ");
-            assertTrue(doc instanceof Document);
-        } catch (IllegalArgumentException | ParserConfigurationException | IOException | SAXException e) {
-            failUnexpected(e);
+            Document doc = docBuilder.parse(fis, " ");
+            assertNotNull(doc);
         }
     }
 
     /**
      * This tests for the parse(uri).
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckDocumentBuilderFactory13() {
-        try {
+    public void testCheckDocumentBuilderFactory13() throws Exception {
+        // Accesing default working directory.
+        String workingDir = getSystemProperty("user.dir");
+        setPermissions(new FilePermission(workingDir + "/*", "read"));
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
-            Document doc = docBuilder.parse(new File(TestUtils.XML_DIR + FILE_SEP + "dbf10import.xsl").toURI().toASCIIString());
-            assertTrue(doc instanceof Document);
-        } catch (IllegalArgumentException | ParserConfigurationException | IOException | SAXException e) {
-            failUnexpected(e);
-        }
+        Document doc = docBuilder.parse(new File(XML_DIR + "dbf10import.xsl")
+                .toURI().toASCIIString());
+        assertNotNull(doc);  
     }
 
     /**
-     * This tests for the parse (uri) with empty string as parameter should
+     * This tests for the parse(uri) with empty string as parameter should
      * throw Sax Exception.
-     *
-     * @throws SAXException
-     *             If any parse errors occur.
+     * @throws Exception If any errors occur.
      */
     @Test(expectedExceptions = SAXException.class)
-    public void testCheckDocumentBuilderFactory14() throws SAXException {
-        try {
+    public void testCheckDocumentBuilderFactory14() throws Exception {
+        // Accesing default working directory.  
+        String workingDir = getSystemProperty("user.dir");
+        setPermissions(new FilePermission(workingDir, "read"));
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
             docBuilder.parse("");
-        } catch (ParserConfigurationException | IOException e) {
-            failUnexpected(e);
-        }
     }
 
     /**
      * This tests for the parse (uri) with null uri as parameter should throw
      * IllegalArgumentException.
+     * @throws Exception If any errors occur.
      *
      */
     @Test(expectedExceptions = IllegalArgumentException.class)
-    public void testCheckDocumentBuilderFactory15() {
-        try {
+    public void testCheckDocumentBuilderFactory15() throws Exception {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
             String uri = null;
             docBuilder.parse(uri);
-        } catch (ParserConfigurationException | SAXException | IOException e) {
-            failUnexpected(e);
-        }
     }
 
     /**
-     * Testcase to test the setIgnoringComments. By default it is set to false,
+     * Test the setIgnoringComments. By default it is set to false,
      * setting this to true, It does not recognize the comment, Here the
      * nodelist has a length 0 because the ignoring comments is true.
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckIgnoringComments() {
-        try {
+    public void testCheckIgnoringComments() throws Exception {
+        try (FileInputStream fis = new FileInputStream(new File(
+                XML_DIR, "DocumentBuilderFactory08.xml"))) {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setIgnoringComments(true);
             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
-            Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory08.xml")));
+            Document doc = docBuilder.parse(fis);
             Element e = (Element) doc.getElementsByTagName("body").item(0);
             NodeList nl = e.getChildNodes();
             assertEquals(nl.getLength(), 0);
-        } catch (ParserConfigurationException | SAXException | IOException e) {
-            failUnexpected(e);
         }
-
     }
 
     /**
-     * Testcase to test the default behaviour of setIgnoringComments. By default
+     * Test the default behaviour of setIgnoringComments. By default
      * it is set to false, this is similar to case 9 but not setIgnoringComments
      * explicitly, it does not recognize the comment.
+     * @throws Exception If any errors occur.
      */
     @Test
-    public void testCheckIgnoringComments1() {
-        try {
+    public void testCheckIgnoringComments1() throws Exception {
+        try (FileInputStream fis = new FileInputStream(new File(
+                XML_DIR, "DocumentBuilderFactory07.xml"))) {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbf.newDocumentBuilder();
-            Document doc = docBuilder.parse(new FileInputStream(new File(TestUtils.XML_DIR, "DocumentBuilderFactory07.xml")));
+            Document doc = docBuilder.parse(fis);
             Element e = (Element) doc.getElementsByTagName("body").item(0);
             NodeList nl = e.getChildNodes();
             assertFalse(dbf.isIgnoringComments());
             assertNotNull(nl.item(0).getNodeValue());
-        } catch (ParserConfigurationException | SAXException | IOException e) {
-            failUnexpected(e);
         }
     }
+
+    /**
+     * Test for the isIgnoringElementContentWhitespace and the
+     * setIgnoringElementContentWhitespace. The xml file has all kinds of
+     * whitespace,tab and newline characters, it uses the MyNSContentHandler
+     * which does not invoke the characters callback when this
+     * setIgnoringElementContentWhitespace is set to true.
+     * @throws Exception If any errors occur.
+     */
+    @Test
+    public void testCheckElementContentWhitespace() throws Exception {
+        String goldFile = GOLDEN_DIR + "dbfactory02GF.out";
+        String outputFile = USER_DIR + "dbfactory02.out";
+        MyErrorHandler eh = MyErrorHandler.newInstance();
+        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+        dbf.setValidating(true);
+        assertFalse(dbf.isIgnoringElementContentWhitespace());
+        dbf.setIgnoringElementContentWhitespace(true);
+        DocumentBuilder db = dbf.newDocumentBuilder();
+        db.setErrorHandler(eh);
+        Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory06.xml"));
+        assertFalse(eh.isErrorOccured());
+        DOMSource domSource = new DOMSource(doc);
+        TransformerFactory tfactory = TransformerFactory.newInstance();
+        Transformer transformer = tfactory.newTransformer();
+        SAXResult saxResult = new SAXResult();
+        try(MyCHandler handler = MyCHandler.newInstance(new File(outputFile))) {
+            saxResult.setHandler(handler);
+            transformer.transform(domSource, saxResult);
+        }
+        assertTrue(compareWithGold(goldFile, outputFile));
+    }
 }
< prev index next >