jaxp/test/javax/xml/jaxp/functional/javax/xml/parsers/ptests/DocumentBuilderFactoryTest.java
author joehw
Thu, 15 Jan 2015 19:10:56 -0800
changeset 28445 5a87f52ca380
child 28793 9c016ad3173c
permissions -rw-r--r--
8051563: Update JAXP functional tests Reviewed-by: lancea, joehw Contributed-by: tristan.yan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28445
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
     1
/*
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
     2
 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
     4
 *
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
     8
 *
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    13
 * accompanied this code).
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    14
 *
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    18
 *
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    21
 * questions.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    22
 */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    23
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    24
package javax.xml.parsers.ptests;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    25
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    26
import static org.testng.Assert.assertEquals;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    27
import static org.testng.Assert.assertNotNull;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    28
import static org.testng.Assert.assertNull;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    29
import java.io.BufferedReader;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    30
import java.io.File;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    31
import java.io.FileInputStream;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    32
import java.io.FilePermission;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    33
import java.io.FileReader;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    34
import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    35
import javax.xml.parsers.DocumentBuilder;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    36
import javax.xml.parsers.DocumentBuilderFactory;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    37
import javax.xml.parsers.SAXParser;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    38
import javax.xml.parsers.SAXParserFactory;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    39
import static javax.xml.parsers.ptests.ParserTestConst.GOLDEN_DIR;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    40
import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    41
import javax.xml.transform.Transformer;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    42
import javax.xml.transform.TransformerFactory;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    43
import javax.xml.transform.dom.DOMSource;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    44
import javax.xml.transform.sax.SAXResult;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    45
import jaxp.library.JAXPFileBaseTest;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    46
import static jaxp.library.JAXPTestUtilities.USER_DIR;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    47
import static jaxp.library.JAXPTestUtilities.compareWithGold;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    48
import static org.testng.Assert.assertFalse;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    49
import static org.testng.Assert.assertTrue;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    50
import org.testng.annotations.Test;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    51
import org.w3c.dom.Document;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    52
import org.w3c.dom.Element;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    53
import org.w3c.dom.NodeList;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    54
import org.xml.sax.InputSource;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    55
import org.xml.sax.SAXException;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    56
import org.xml.sax.helpers.DefaultHandler;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    57
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    58
/**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    59
 * This checks the methods of DocumentBuilderFactoryImpl.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    60
 */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    61
public class DocumentBuilderFactoryTest extends JAXPFileBaseTest {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    62
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    63
     * Test the default functionality of schema support method.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    64
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    65
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    66
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    67
    public void testCheckSchemaSupport1() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    68
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    69
        dbf.setValidating(true);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    70
        dbf.setNamespaceAware(true);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    71
        dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    72
                W3C_XML_SCHEMA_NS_URI);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    73
        MyErrorHandler eh = MyErrorHandler.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    74
        DocumentBuilder db = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    75
        db.setErrorHandler(eh);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    76
        db.parse(new File(XML_DIR, "test.xml"));
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    77
        assertFalse(eh.isErrorOccured());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    78
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    79
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    80
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    81
     * Test the default functionality of schema support method. In
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    82
     * this case the schema source property is set.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    83
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    84
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    85
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    86
    public void testCheckSchemaSupport2() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    87
        try (FileInputStream fis = new FileInputStream(new File(
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    88
                XML_DIR, "test.xsd"))) {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    89
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    90
            dbf.setValidating(true);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    91
            dbf.setNamespaceAware(true);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    92
            dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    93
                    W3C_XML_SCHEMA_NS_URI);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    94
            dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    95
                    new InputSource(fis));
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    96
            MyErrorHandler eh = MyErrorHandler.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    97
            DocumentBuilder db = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    98
            db.setErrorHandler(eh);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
    99
            db.parse(new File(XML_DIR, "test1.xml"));
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   100
            assertFalse(eh.isErrorOccured());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   101
        }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   102
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   103
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   104
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   105
     * Test the default functionality of schema support method. In
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   106
     * this case the schema source property is set.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   107
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   108
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   109
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   110
    public void testCheckSchemaSupport3() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   111
        try (FileInputStream fis = new FileInputStream(new File(
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   112
                XML_DIR, "test.xsd"))) {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   113
            SAXParserFactory spf = SAXParserFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   114
            spf.setNamespaceAware(true);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   115
            spf.setValidating(true);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   116
            spf.setNamespaceAware(true);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   117
            SAXParser sp = spf.newSAXParser();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   118
            sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   119
                    W3C_XML_SCHEMA_NS_URI);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   120
            sp.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource",
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   121
                    new InputSource(fis));
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   122
            DefaultHandler dh = new DefaultHandler();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   123
            // Not expect any unrecoverable error here.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   124
            sp.parse(new File(XML_DIR, "test1.xml"), dh);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   125
        }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   126
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   127
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   128
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   129
     * Test the default functionality of newInstance method. To test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   130
     * the isCoalescing method and setCoalescing This checks to see if the CDATA
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   131
     * and text nodes got combined In that case it will print "<xml>This
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   132
     * is not parsed</xml> yet".
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   133
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   134
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   135
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   136
    public void testCheckDocumentBuilderFactory02() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   137
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   138
        dbf.setCoalescing(true);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   139
        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   140
        Document doc = docBuilder.parse(new File(XML_DIR, "DocumentBuilderFactory01.xml"));
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   141
        Element e = (Element) doc.getElementsByTagName("html").item(0);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   142
        NodeList nl = e.getChildNodes();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   143
        assertEquals(nl.getLength(), 1);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   144
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   145
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   146
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   147
     * Test the isIgnoringComments. By default it is false.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   148
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   149
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   150
    public void testCheckDocumentBuilderFactory03() {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   151
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   152
        assertFalse(dbf.isIgnoringComments());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   153
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   154
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   155
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   156
     * Test the isValidating. By default it is false, set it to true and then
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   157
     * use a document which is not valid. It should throw a warning or
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   158
     * an error at least. The test passes in case retval 0 is set in the error
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   159
     * method .
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   160
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   161
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   162
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   163
    public void testCheckDocumentBuilderFactory04() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   164
        MyErrorHandler eh = MyErrorHandler.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   165
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   166
        dbf.setValidating(true);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   167
        DocumentBuilder db = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   168
        db.setErrorHandler(eh);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   169
        db.parse(new File(XML_DIR, "DocumentBuilderFactory05.xml"));
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   170
        assertTrue(eh.isErrorOccured());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   171
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   172
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   173
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   174
     * Test the setValidating. By default it is false, use a
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   175
     * document which is not valid. It should not throw a warning or an error.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   176
     * The test passes in case the return value equals 1.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   177
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   178
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   179
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   180
    public void testCheckDocumentBuilderFactory16() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   181
        MyErrorHandler eh = MyErrorHandler.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   182
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   183
        DocumentBuilder db = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   184
        db.setErrorHandler(eh);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   185
        db.parse(new File(XML_DIR, "DocumentBuilderFactory05.xml"));
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   186
        assertFalse(eh.isErrorOccured());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   187
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   188
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   189
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   190
     * Test the setValidating. By default it is false, use a
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   191
     * document which is valid. It should not throw a warning or an error. The
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   192
     * test passes in case the return value equals 1.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   193
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   194
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   195
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   196
    public void testCheckDocumentBuilderFactory17() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   197
        MyErrorHandler eh = MyErrorHandler.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   198
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   199
        DocumentBuilder db = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   200
        db.setErrorHandler(eh);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   201
        db.parse(new File(XML_DIR, "DocumentBuilderFactory04.xml"));
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   202
        assertFalse(eh.isErrorOccured());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   203
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   204
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   205
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   206
     * Test the isExpandEntityReferences. By default it is true.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   207
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   208
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   209
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   210
    public void testCheckDocumentBuilderFactory05() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   211
        try(FileInputStream fis = new FileInputStream(new File(
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   212
                XML_DIR, "DocumentBuilderFactory02.xml"))) {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   213
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   214
            DocumentBuilder docBuilder = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   215
            Document doc = docBuilder.parse(fis);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   216
            Element e = (Element) doc.getElementsByTagName("title").item(0);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   217
            NodeList nl = e.getChildNodes();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   218
            assertTrue(dbf.isExpandEntityReferences());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   219
            assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W');
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   220
        }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   221
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   222
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   223
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   224
     * Test the default functionality of setValidating method. The
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   225
     * XML file has a DTD which has namespaces defined. The parser takes care to
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   226
     * check if the namespaces using elements and defined attributes are there
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   227
     * or not.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   228
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   229
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   230
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   231
    public void testCheckDocumentBuilderFactory06() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   232
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   233
        dbf.setValidating(true);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   234
        DocumentBuilder db = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   235
        MyErrorHandler eh = MyErrorHandler.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   236
        db.setErrorHandler(eh);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   237
        Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory04.xml"));
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   238
        assertTrue(doc instanceof Document);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   239
        assertFalse(eh.isErrorOccured());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   240
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   241
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   242
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   243
     * Test the setExpandEntityReferences.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   244
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   245
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   246
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   247
    public void testCheckDocumentBuilderFactory07() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   248
        try (FileInputStream fis = new FileInputStream(new File(
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   249
                XML_DIR, "DocumentBuilderFactory02.xml"))) {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   250
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   251
            dbf.setExpandEntityReferences(true);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   252
            DocumentBuilder docBuilder = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   253
            Document doc = docBuilder.parse(fis);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   254
            Element e = (Element) doc.getElementsByTagName("title").item(0);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   255
            NodeList nl = e.getChildNodes();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   256
            assertTrue(dbf.isExpandEntityReferences());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   257
            assertEquals(nl.item(0).getNodeValue().trim().charAt(0), 'W');
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   258
        }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   259
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   260
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   261
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   262
     * Test the setExpandEntityReferences.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   263
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   264
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   265
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   266
    public void testCheckDocumentBuilderFactory08() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   267
        try (FileInputStream fis = new FileInputStream(new File(
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   268
                XML_DIR, "DocumentBuilderFactory02.xml"))) {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   269
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   270
            dbf.setExpandEntityReferences(false);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   271
            DocumentBuilder docBuilder = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   272
            Document doc = docBuilder.parse(fis);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   273
            Element e = (Element) doc.getElementsByTagName("title").item(0);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   274
            NodeList nl = e.getChildNodes();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   275
            assertNull(nl.item(0).getNodeValue());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   276
        }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   277
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   278
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   279
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   280
     * Test the setIgnoringComments. By default it is set to false.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   281
     * explicitly setting it to false, it recognizes the comment which is in
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   282
     * Element Node Hence the Element's child node is not null.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   283
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   284
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   285
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   286
    public void testCheckDocumentBuilderFactory09() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   287
        try (FileInputStream fis = new FileInputStream(new File(
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   288
                XML_DIR, "DocumentBuilderFactory07.xml"))) {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   289
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   290
            dbf.setIgnoringComments(false);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   291
            DocumentBuilder docBuilder = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   292
            Document doc = docBuilder.parse(fis);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   293
            Element e = (Element) doc.getElementsByTagName("body").item(0);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   294
            NodeList nl = e.getChildNodes();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   295
            assertNotNull(nl.item(0).getNodeValue());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   296
        }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   297
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   298
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   299
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   300
     * This tests for the parse(InputSource).
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   301
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   302
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   303
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   304
    public void testCheckDocumentBuilderFactory10() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   305
        try (BufferedReader br = new BufferedReader(new FileReader(new File(
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   306
                XML_DIR, "DocumentBuilderFactory07.xml")))) {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   307
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   308
            DocumentBuilder docBuilder = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   309
            Document doc = docBuilder.parse(new InputSource(br));
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   310
            assertNotNull(doc);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   311
        }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   312
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   313
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   314
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   315
     * This tests for the parse InputStream with SystemID as a second parameter.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   316
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   317
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   318
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   319
    public void testCheckDocumentBuilderFactory11() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   320
        try (FileInputStream fis = new FileInputStream(new File(
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   321
                XML_DIR, "dbf10import.xsl"))) {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   322
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   323
            DocumentBuilder docBuilder = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   324
            Document doc = docBuilder.parse(fis, new File(XML_DIR).toURI()
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   325
                    .toASCIIString());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   326
            assertNotNull(doc);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   327
        }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   328
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   329
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   330
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   331
     * This tests for the parse InputStream with empty SystemID as a second
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   332
     * parameter.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   333
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   334
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   335
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   336
    public void testCheckDocumentBuilderFactory12() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   337
        try (FileInputStream fis = new FileInputStream(new File(
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   338
                XML_DIR, "dbf10import.xsl"))) {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   339
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   340
            DocumentBuilder docBuilder = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   341
            Document doc = docBuilder.parse(fis, " ");
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   342
            assertNotNull(doc);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   343
        }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   344
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   345
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   346
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   347
     * This tests for the parse(uri).
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   348
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   349
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   350
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   351
    public void testCheckDocumentBuilderFactory13() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   352
        // Accesing default working directory.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   353
        String workingDir = getSystemProperty("user.dir");
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   354
        setPermissions(new FilePermission(workingDir + "/*", "read"));
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   355
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   356
        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   357
        Document doc = docBuilder.parse(new File(XML_DIR + "dbf10import.xsl")
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   358
                .toURI().toASCIIString());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   359
        assertNotNull(doc);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   360
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   361
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   362
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   363
     * This tests for the parse(uri) with empty string as parameter should
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   364
     * throw Sax Exception.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   365
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   366
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   367
    @Test(expectedExceptions = SAXException.class)
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   368
    public void testCheckDocumentBuilderFactory14() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   369
        // Accesing default working directory.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   370
        String workingDir = getSystemProperty("user.dir");
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   371
        setPermissions(new FilePermission(workingDir, "read"));
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   372
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   373
        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   374
        docBuilder.parse("");
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   375
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   376
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   377
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   378
     * This tests for the parse (uri) with null uri as parameter should throw
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   379
     * IllegalArgumentException.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   380
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   381
     *
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   382
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   383
    @Test(expectedExceptions = IllegalArgumentException.class)
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   384
    public void testCheckDocumentBuilderFactory15() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   385
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   386
        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   387
        String uri = null;
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   388
        docBuilder.parse(uri);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   389
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   390
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   391
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   392
     * Test the setIgnoringComments. By default it is set to false,
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   393
     * setting this to true, It does not recognize the comment, Here the
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   394
     * nodelist has a length 0 because the ignoring comments is true.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   395
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   396
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   397
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   398
    public void testCheckIgnoringComments() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   399
        try (FileInputStream fis = new FileInputStream(new File(
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   400
                XML_DIR, "DocumentBuilderFactory08.xml"))) {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   401
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   402
            dbf.setIgnoringComments(true);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   403
            DocumentBuilder docBuilder = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   404
            Document doc = docBuilder.parse(fis);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   405
            Element e = (Element) doc.getElementsByTagName("body").item(0);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   406
            NodeList nl = e.getChildNodes();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   407
            assertEquals(nl.getLength(), 0);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   408
        }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   409
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   410
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   411
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   412
     * Test the default behaviour of setIgnoringComments. By default
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   413
     * it is set to false, this is similar to case 9 but not setIgnoringComments
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   414
     * explicitly, it does not recognize the comment.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   415
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   416
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   417
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   418
    public void testCheckIgnoringComments1() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   419
        try (FileInputStream fis = new FileInputStream(new File(
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   420
                XML_DIR, "DocumentBuilderFactory07.xml"))) {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   421
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   422
            DocumentBuilder docBuilder = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   423
            Document doc = docBuilder.parse(fis);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   424
            Element e = (Element) doc.getElementsByTagName("body").item(0);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   425
            NodeList nl = e.getChildNodes();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   426
            assertFalse(dbf.isIgnoringComments());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   427
            assertNotNull(nl.item(0).getNodeValue());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   428
        }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   429
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   430
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   431
    /**
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   432
     * Test for the isIgnoringElementContentWhitespace and the
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   433
     * setIgnoringElementContentWhitespace. The xml file has all kinds of
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   434
     * whitespace,tab and newline characters, it uses the MyNSContentHandler
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   435
     * which does not invoke the characters callback when this
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   436
     * setIgnoringElementContentWhitespace is set to true.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   437
     * @throws Exception If any errors occur.
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   438
     */
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   439
    @Test
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   440
    public void testCheckElementContentWhitespace() throws Exception {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   441
        String goldFile = GOLDEN_DIR + "dbfactory02GF.out";
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   442
        String outputFile = USER_DIR + "dbfactory02.out";
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   443
        MyErrorHandler eh = MyErrorHandler.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   444
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   445
        dbf.setValidating(true);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   446
        assertFalse(dbf.isIgnoringElementContentWhitespace());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   447
        dbf.setIgnoringElementContentWhitespace(true);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   448
        DocumentBuilder db = dbf.newDocumentBuilder();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   449
        db.setErrorHandler(eh);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   450
        Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory06.xml"));
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   451
        assertFalse(eh.isErrorOccured());
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   452
        DOMSource domSource = new DOMSource(doc);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   453
        TransformerFactory tfactory = TransformerFactory.newInstance();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   454
        Transformer transformer = tfactory.newTransformer();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   455
        SAXResult saxResult = new SAXResult();
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   456
        try(MyCHandler handler = MyCHandler.newInstance(new File(outputFile))) {
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   457
            saxResult.setHandler(handler);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   458
            transformer.transform(domSource, saxResult);
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   459
        }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   460
        assertTrue(compareWithGold(goldFile, outputFile));
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   461
    }
5a87f52ca380 8051563: Update JAXP functional tests
joehw
parents:
diff changeset
   462
}