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