1 /*
   2  * Copyright (c) 2014, 2015, 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 java.io.ByteArrayInputStream;
  27 import java.io.IOException;
  28 import java.io.StringBufferInputStream;
  29 import java.io.UnsupportedEncodingException;
  30 
  31 import javax.xml.parsers.DocumentBuilder;
  32 import javax.xml.parsers.DocumentBuilderFactory;
  33 import javax.xml.parsers.ParserConfigurationException;
  34 
  35 import org.testng.Assert;
  36 import org.testng.annotations.BeforeMethod;
  37 import org.testng.annotations.Test;
  38 import org.w3c.dom.DOMImplementation;
  39 import org.w3c.dom.Document;
  40 import org.w3c.dom.ls.DOMImplementationLS;
  41 import org.w3c.dom.ls.LSInput;
  42 import org.w3c.dom.ls.LSParser;
  43 import org.xml.sax.SAXException;
  44 
  45 /*
  46  * @bug 6355326
  47  * @summary Test DOM implementation encoding.
  48  */
  49 public class Bug6355326 {
  50 
  51     DOMImplementationLS implLS = null;
  52     String encodingXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><encodingXML/>";
  53 
  54     @BeforeMethod
  55     protected void setUp() {
  56         Document doc = null;
  57         DocumentBuilder parser = null;
  58         String xml1 = "<?xml version=\"1.0\"?><ROOT></ROOT>";
  59         try {
  60             parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  61         } catch (ParserConfigurationException e) {
  62             e.printStackTrace();
  63         }
  64         StringBufferInputStream is = new StringBufferInputStream(xml1);
  65         try {
  66             doc = parser.parse(is);
  67         } catch (SAXException e) {
  68             e.printStackTrace();
  69         } catch (IOException e) {
  70             e.printStackTrace();
  71         }
  72         DOMImplementation impl = doc.getImplementation();
  73         implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
  74     }
  75 
  76     @Test
  77     public void testExternalEncoding() {
  78 
  79         try {
  80             LSInput src = null;
  81             LSParser dp = null;
  82 
  83             src = createLSInputEncoding();
  84             dp = createLSParser();
  85 
  86             src.setEncoding("UTF-16");
  87             Document doc = dp.parse(src);
  88             Assert.assertTrue("encodingXML".equals(doc.getDocumentElement().getNodeName()), "XML document is not parsed correctly");
  89 
  90         } catch (Exception e) {
  91             e.printStackTrace();
  92             Assert.fail("Exception occured: " + e.getMessage());
  93         }
  94     }
  95 
  96     private LSInput createLSInputEncoding() {
  97         LSInput src = implLS.createLSInput();
  98         Assert.assertFalse(src == null, "Could not create LSInput from DOMImplementationLS");
  99 
 100         try {
 101             src.setByteStream(new ByteArrayInputStream(encodingXML.getBytes("UTF-16")));
 102         } catch (UnsupportedEncodingException e) {
 103             e.printStackTrace();
 104             Assert.fail("Exception occured: " + e.getMessage());
 105         }
 106         return src;
 107     }
 108 
 109     private LSParser createLSParser() {
 110         LSParser p = implLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/2001/XMLSchema");
 111         Assert.assertFalse(p == null, "Could not create Synchronous LSParser from DOMImplementationLS");
 112         return p;
 113     }
 114 }