Bug6355326.java revision 968:874082a9b565
155682Smarkm/* 2233294Sstas * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. 3233294Sstas * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4233294Sstas * 555682Smarkm * This code is free software; you can redistribute it and/or modify it 6233294Sstas * under the terms of the GNU General Public License version 2 only, as 755682Smarkm * published by the Free Software Foundation. 8233294Sstas * 9233294Sstas * This code is distributed in the hope that it will be useful, but WITHOUT 10233294Sstas * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1155682Smarkm * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12233294Sstas * version 2 for more details (a copy is included in the LICENSE file that 13233294Sstas * accompanied this code). 1455682Smarkm * 15233294Sstas * You should have received a copy of the GNU General Public License version 16233294Sstas * 2 along with this work; if not, write to the Free Software Foundation, 17233294Sstas * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1855682Smarkm * 19233294Sstas * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20233294Sstas * or visit www.oracle.com if you need additional information or have any 21233294Sstas * questions. 22233294Sstas */ 23233294Sstas 24233294Sstaspackage dom; 25233294Sstas 26233294Sstasimport java.io.ByteArrayInputStream; 27233294Sstasimport java.io.IOException; 28233294Sstasimport java.io.StringBufferInputStream; 29233294Sstasimport java.io.UnsupportedEncodingException; 30233294Sstas 31233294Sstasimport javax.xml.parsers.DocumentBuilder; 32233294Sstasimport javax.xml.parsers.DocumentBuilderFactory; 33233294Sstasimport javax.xml.parsers.ParserConfigurationException; 3455682Smarkm 3555682Smarkmimport org.testng.Assert; 3655682Smarkmimport org.testng.annotations.BeforeMethod; 3755682Smarkmimport org.testng.annotations.Listeners; 38178825Sdfrimport org.testng.annotations.Test; 39178825Sdfrimport org.w3c.dom.DOMImplementation; 40178825Sdfrimport org.w3c.dom.Document; 41178825Sdfrimport org.w3c.dom.ls.DOMImplementationLS; 42178825Sdfrimport org.w3c.dom.ls.LSInput; 43178825Sdfrimport org.w3c.dom.ls.LSParser; 44233294Sstasimport org.xml.sax.SAXException; 45178825Sdfr 46178825Sdfr/* 47178825Sdfr * @test 48178825Sdfr * @bug 6355326 49178825Sdfr * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 50178825Sdfr * @run testng/othervm -DrunSecMngr=true dom.Bug6355326 51178825Sdfr * @run testng/othervm dom.Bug6355326 52233294Sstas * @summary Test DOM implementation encoding. 53233294Sstas */ 54233294Sstas@Listeners({jaxp.library.BasePolicy.class}) 55178825Sdfrpublic class Bug6355326 { 56233294Sstas 57233294Sstas DOMImplementationLS implLS = null; 58233294Sstas String encodingXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><encodingXML/>"; 59178825Sdfr 60178825Sdfr @BeforeMethod 61178825Sdfr public void setUp() { 62178825Sdfr Document doc = null; 63233294Sstas DocumentBuilder parser = null; 64233294Sstas String xml1 = "<?xml version=\"1.0\"?><ROOT></ROOT>"; 65233294Sstas try { 66233294Sstas parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 67233294Sstas } catch (ParserConfigurationException e) { 68233294Sstas e.printStackTrace(); 69233294Sstas } 70233294Sstas StringBufferInputStream is = new StringBufferInputStream(xml1); 71233294Sstas try { 72233294Sstas doc = parser.parse(is); 73233294Sstas } catch (SAXException e) { 74233294Sstas e.printStackTrace(); 75233294Sstas } catch (IOException e) { 76233294Sstas e.printStackTrace(); 77233294Sstas } 78233294Sstas DOMImplementation impl = doc.getImplementation(); 79233294Sstas implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0"); 80178825Sdfr } 81178825Sdfr 82233294Sstas @Test 83233294Sstas public void testExternalEncoding() { 84233294Sstas 85233294Sstas try { 86233294Sstas LSInput src = null; 87233294Sstas LSParser dp = null; 88233294Sstas 89233294Sstas src = createLSInputEncoding(); 90233294Sstas dp = createLSParser(); 91233294Sstas 92233294Sstas src.setEncoding("UTF-16"); 93233294Sstas Document doc = dp.parse(src); 94233294Sstas Assert.assertTrue("encodingXML".equals(doc.getDocumentElement().getNodeName()), "XML document is not parsed correctly"); 95233294Sstas 96233294Sstas } catch (Exception e) { 97233294Sstas e.printStackTrace(); 98233294Sstas Assert.fail("Exception occured: " + e.getMessage()); 99178825Sdfr } 100178825Sdfr } 101178825Sdfr 102178825Sdfr private LSInput createLSInputEncoding() { 103178825Sdfr LSInput src = implLS.createLSInput(); 104178825Sdfr Assert.assertFalse(src == null, "Could not create LSInput from DOMImplementationLS"); 105178825Sdfr 106178825Sdfr try { 107233294Sstas src.setByteStream(new ByteArrayInputStream(encodingXML.getBytes("UTF-16"))); 108233294Sstas } catch (UnsupportedEncodingException e) { 109178825Sdfr e.printStackTrace(); 110178825Sdfr Assert.fail("Exception occured: " + e.getMessage()); 111178825Sdfr } 112178825Sdfr return src; 113178825Sdfr } 114178825Sdfr 115233294Sstas private LSParser createLSParser() { 116178825Sdfr LSParser p = implLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/2001/XMLSchema"); 117178825Sdfr Assert.assertFalse(p == null, "Could not create Synchronous LSParser from DOMImplementationLS"); 118178825Sdfr return p; 119178825Sdfr } 120178825Sdfr} 121178825Sdfr 122178825Sdfr