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;
  25 
  26 import javax.xml.parsers.DocumentBuilder;
  27 import javax.xml.parsers.DocumentBuilderFactory;
  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.DOMImplementation;
  34 import org.w3c.dom.DOMStringList;
  35 import org.w3c.dom.ls.DOMImplementationLS;
  36 import org.w3c.dom.ls.LSParser;
  37 import org.w3c.dom.ls.LSSerializer;
  38 
  39 /*
  40  * @test
  41  * @bug 6339023
  42  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  43  * @run testng/othervm -DrunSecMngr=true dom.Bug6339023
  44  * @run testng/othervm dom.Bug6339023
  45  * @summary Test normalize-characters.
  46  */
  47 @Listeners({jaxp.library.BasePolicy.class})
  48 public class Bug6339023 {
  49 
  50     /*
  51      * This test checks DOMConfiguration for DOM Level3 Load and Save
  52      * implementation.
  53      */
  54     @Test
  55     public void testLSSerializer() {
  56         try {
  57             DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  58             DOMImplementation impln = parser.getDOMImplementation();
  59             DOMImplementationLS lsImpln = (DOMImplementationLS) impln.getFeature("LS", "3.0");
  60             LSSerializer serializer = lsImpln.createLSSerializer();
  61             DOMConfiguration domConfig = serializer.getDomConfig();
  62             System.out.println("DOMConfig: " + domConfig.toString());
  63             Assert.assertTrue(domConfig.getParameter("normalize-characters") == null);
  64             System.out.println("value: " + domConfig.getParameter("normalize-characters"));
  65 
  66             DOMStringList list = domConfig.getParameterNames();
  67             for (int i = 0; i < list.getLength(); i++) {
  68                 System.out.println("Param Name: " + list.item(i));
  69                 Assert.assertFalse(list.item(i).equals("normalize-characters"));
  70             }
  71 
  72             Assert.assertFalse(domConfig.canSetParameter("normalize-characters", Boolean.FALSE));
  73             Assert.assertFalse(domConfig.canSetParameter("normalize-characters", Boolean.TRUE));
  74 
  75             try {
  76                 domConfig.setParameter("normalize-characters", Boolean.TRUE);
  77                 Assert.fail("Exception expected as 'normalize-characters' is not supported");
  78             } catch (Exception e) {
  79             }
  80 
  81             try {
  82                 domConfig.setParameter("normalize-characters", Boolean.FALSE);
  83                 Assert.fail("Exception expected as 'normalize-characters' is not supported");
  84             } catch (Exception e) {
  85             }
  86 
  87         } catch (Exception e) {
  88             e.printStackTrace();
  89             Assert.fail("Exception: " + e.getMessage());
  90         }
  91     }
  92 
  93     /*
  94      * This test checks DOMConfiguration for DOM Level3 Core implementation.
  95      */
  96     @Test
  97     public void testLSParser() {
  98         try {
  99             DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 100             DOMImplementation impln = parser.getDOMImplementation();
 101             DOMImplementationLS lsImpln = (DOMImplementationLS) impln.getFeature("Core", "3.0");
 102             LSParser lsparser = lsImpln.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/2001/XMLSchema");
 103             DOMConfiguration domConfig = lsparser.getDomConfig();
 104             System.out.println("DOMConfig: " + domConfig.toString());
 105             Assert.assertTrue(domConfig.getParameter("normalize-characters").toString().equalsIgnoreCase("false"));
 106             System.out.println("value: " + domConfig.getParameter("normalize-characters"));
 107 
 108             DOMStringList list = domConfig.getParameterNames();
 109             boolean flag = false;
 110             for (int i = 0; i < list.getLength(); i++) {
 111                 System.out.println("Param Name: " + list.item(i));
 112                 if (list.item(i).equals("normalize-characters")) {
 113                     flag = true;
 114                     break;
 115                 }
 116             }
 117             Assert.assertTrue(flag, "'normalize-characters' doesnot exist in the list returned by domConfig.getParameterNames()");
 118 
 119             Assert.assertTrue(domConfig.canSetParameter("normalize-characters", Boolean.FALSE));
 120             Assert.assertFalse(domConfig.canSetParameter("normalize-characters", Boolean.TRUE));
 121 
 122             try {
 123                 domConfig.setParameter("normalize-characters", Boolean.TRUE);
 124                 Assert.fail("Exception expected as 'normalize-characters' is not supported");
 125             } catch (Exception e) {
 126             }
 127 
 128             try {
 129                 domConfig.setParameter("normalize-characters", Boolean.FALSE);
 130             } catch (Exception e) {
 131                 e.printStackTrace();
 132                 Assert.fail("Exception expected as 'normalize-characters' is not supported");
 133             }
 134 
 135         } catch (Exception e) {
 136             e.printStackTrace();
 137             Assert.fail("Exception: " + e.getMessage());
 138         }
 139     }
 140 
 141 }