DocumentBuilderImpl01.java revision 968:874082a9b565
1/*
2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package javax.xml.parsers.ptests;
25
26import static javax.xml.parsers.ptests.ParserTestConst.XML_DIR;
27import static jaxp.library.JAXPTestUtilities.FILE_SEP;
28import static org.testng.Assert.assertFalse;
29import static org.testng.Assert.assertNotNull;
30
31import java.io.File;
32import java.io.FileInputStream;
33import java.io.FilePermission;
34
35import javax.xml.parsers.DocumentBuilder;
36import javax.xml.parsers.DocumentBuilderFactory;
37import javax.xml.parsers.ParserConfigurationException;
38
39import org.testng.annotations.DataProvider;
40import org.testng.annotations.Listeners;
41import org.testng.annotations.Test;
42import org.xml.sax.EntityResolver;
43import org.xml.sax.InputSource;
44
45/**
46 * This checks for the methods of DocumentBuilder
47 */
48/*
49 * @test
50 * @library /javax/xml/jaxp/libs
51 * @run testng/othervm -DrunSecMngr=true javax.xml.parsers.ptests.DocumentBuilderImpl01
52 * @run testng/othervm javax.xml.parsers.ptests.DocumentBuilderImpl01
53 */
54@Listeners({jaxp.library.FilePolicy.class})
55public class DocumentBuilderImpl01 implements EntityResolver {
56    /**
57     * Provide DocumentBuilder.
58     *
59     * @return data provider has single DocumentBuilder.
60     * @throws ParserConfigurationException if a DocumentBuilder cannot be
61     *         created which satisfies the configuration requested.
62     */
63    @DataProvider(name = "builder-provider")
64    public Object[][] getBuilder() throws ParserConfigurationException {
65        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
66        DocumentBuilder docBuilder = dbf.newDocumentBuilder();
67        return new Object[][] { { docBuilder } };
68    }
69
70    /**
71     * Test the default functionality of isValidation method. Expect
72     * to return false because not setting the validation.
73     * @param docBuilder document builder instance.
74     */
75    @Test(dataProvider = "builder-provider")
76    public void testCheckDocumentBuilderImpl01(DocumentBuilder docBuilder) {
77        assertFalse(docBuilder.isValidating());
78    }
79
80    /**
81     * Test the default functionality of isNamespaceAware method.
82     * @param docBuilder document builder instance.
83     */
84    @Test(dataProvider = "builder-provider")
85    public void testCheckDocumentBuilderImpl02(DocumentBuilder docBuilder) {
86        assertFalse(docBuilder.isNamespaceAware());
87    }
88
89    /**
90     * Test the parse(InputStream).
91     * @param docBuilder document builder instance.
92     * @throws Exception If any errors occur.
93     */
94    @Test(dataProvider = "builder-provider")
95    public void testCheckDocumentBuilderImpl04(DocumentBuilder docBuilder)
96            throws Exception {
97        try (FileInputStream fis = new FileInputStream(new File(XML_DIR,
98                "DocumentBuilderImpl01.xml"))) {
99            assertNotNull(docBuilder.parse(fis));
100        }
101    }
102
103    /**
104     * Test the parse(File).
105     *
106     * @param docBuilder document builder instance.
107     * @throws Exception If any errors occur.
108     */
109    @Test(dataProvider = "builder-provider")
110    public void testCheckDocumentBuilderImpl05(DocumentBuilder docBuilder)
111            throws Exception {
112        assertNotNull(docBuilder.parse(new File(XML_DIR,
113                "DocumentBuilderImpl01.xml")));
114    }
115
116    /**
117     * Test the parse(InputStream,systemId).
118     * @param docBuilder document builder instance.
119     * @throws Exception If any errors occur.
120     */
121    @Test(dataProvider = "builder-provider")
122    public void testCheckDocumentBuilderImpl06(DocumentBuilder docBuilder)
123            throws Exception {
124        try (FileInputStream fis = new FileInputStream(new File(XML_DIR,
125                "DocumentBuilderImpl02.xml"))) {
126            assertNotNull(docBuilder.parse(fis, new File(XML_DIR).toURI()
127                    .toASCIIString() + FILE_SEP));
128        }
129    }
130
131    /**
132     * Test the setEntityResolver.
133     * @param docBuilder document builder instance.
134     */
135    @Test(dataProvider = "builder-provider")
136    public void testCheckDocumentBuilderImpl07(DocumentBuilder docBuilder) {
137        docBuilder.setEntityResolver(this);
138        assertNotNull(resolveEntity("publicId", "http://www.myhost.com/today"));
139    }
140
141    /**
142     * Allow the application to resolve external entities.
143     *
144     * @param publicId The public identifier of the external entity
145     *        being referenced, or null if none was supplied.
146     * @param systemId The system identifier of the external entity
147     *        being referenced.
148     * @return An InputSource object describing the new input source,
149     *         or null to request that the parser open a regular
150     *         URI connection to the system identifier.
151     */
152    @Override
153    public InputSource resolveEntity(String publicId, String systemId) {
154        if (systemId.equals("http://www.myhost.com/today"))
155            return new InputSource(systemId);
156        else
157            return null;
158    }
159}
160
161
162