1/*
2 * Copyright (c) 2014, 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 dom.ls;
25
26import javax.xml.parsers.DocumentBuilderFactory;
27import javax.xml.parsers.ParserConfigurationException;
28
29import org.testng.Assert;
30import org.testng.annotations.Listeners;
31import org.testng.annotations.Test;
32import org.w3c.dom.DOMConfiguration;
33import org.w3c.dom.DOMError;
34import org.w3c.dom.DOMErrorHandler;
35import org.w3c.dom.DOMException;
36import org.w3c.dom.DOMImplementation;
37import org.w3c.dom.ls.DOMImplementationLS;
38import org.w3c.dom.ls.LSInput;
39import org.w3c.dom.ls.LSParser;
40import org.w3c.dom.ls.LSResourceResolver;
41
42/*
43 * @test
44 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
45 * @run testng/othervm -DrunSecMngr=true dom.ls.LSParserTest
46 * @run testng/othervm dom.ls.LSParserTest
47 * @summary Test LSParser's DOMConfiguration for supported properties.
48 */
49@Listeners({jaxp.library.BasePolicy.class})
50public class LSParserTest {
51
52    @Test
53    public void testDOMConfiguration() {
54
55        final DOMErrorHandler handler = new DOMErrorHandler() {
56            public boolean handleError(final DOMError error) {
57                return false;
58            }
59        };
60
61        final LSResourceResolver resolver = new LSResourceResolver() {
62            public LSInput resolveResource(final String type, final String namespaceURI, final String publicId, final String systemId, final String baseURI) {
63                return null;
64            }
65        };
66
67        final Object[][] values = {
68                // parameter, value
69                { "canonical-form", Boolean.FALSE }, { "cdata-sections", Boolean.FALSE }, { "cdata-sections", Boolean.TRUE },
70                { "check-character-normalization", Boolean.FALSE }, { "comments", Boolean.FALSE }, { "comments", Boolean.TRUE },
71                { "datatype-normalization", Boolean.FALSE }, { "entities", Boolean.FALSE }, { "entities", Boolean.TRUE }, { "error-handler", handler },
72                { "infoset", Boolean.TRUE }, { "namespaces", Boolean.TRUE }, { "namespace-declarations", Boolean.TRUE },
73                { "namespace-declarations", Boolean.FALSE }, { "normalize-characters", Boolean.FALSE }, { "split-cdata-sections", Boolean.TRUE },
74                { "split-cdata-sections", Boolean.FALSE }, { "validate", Boolean.FALSE }, { "validate-if-schema", Boolean.FALSE },
75                { "well-formed", Boolean.TRUE }, { "element-content-whitespace", Boolean.TRUE },
76
77                { "charset-overrides-xml-encoding", Boolean.TRUE }, { "charset-overrides-xml-encoding", Boolean.FALSE }, { "disallow-doctype", Boolean.FALSE },
78                { "ignore-unknown-character-denormalizations", Boolean.TRUE }, { "resource-resolver", resolver }, { "resource-resolver", null },
79                { "supported-media-types-only", Boolean.FALSE }, };
80
81        DOMImplementation domImpl = null;
82        try {
83            domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
84        } catch (ParserConfigurationException parserConfigurationException) {
85            parserConfigurationException.printStackTrace();
86            Assert.fail(parserConfigurationException.toString());
87        }
88
89        DOMImplementationLS lsImpl = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
90
91        LSParser lsParser = lsImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
92
93        DOMConfiguration config = lsParser.getDomConfig();
94
95        for (int i = values.length; --i >= 0;) {
96            Object val = values[i][1];
97            String param = (String) values[i][0];
98            try {
99                config.setParameter(param, val);
100                Object returned = config.getParameter(param);
101                Assert.assertEquals(val, returned, "'" + param + "' is set to " + returned + ", but expected " + val);
102                System.out.println("set '" + param + "'" + " to '" + val + "'" + " and returned '" + returned + "'");
103            } catch (DOMException e) {
104                String settings = "setting '" + param + "' to " + val;
105                System.err.println(settings);
106                e.printStackTrace();
107                Assert.fail(settings + ", " + e.toString());
108            }
109        }
110    }
111}
112