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 java.io.ByteArrayOutputStream; 27 import java.io.IOException; 28 import java.io.OutputStream; 29 import java.io.StringBufferInputStream; 30 import java.io.Writer; 31 32 import javax.xml.parsers.DocumentBuilder; 33 import javax.xml.parsers.DocumentBuilderFactory; 34 import javax.xml.parsers.ParserConfigurationException; 35 36 import org.testng.Assert; 37 import org.testng.annotations.AfterMethod; 38 import org.testng.annotations.BeforeMethod; 39 import org.testng.annotations.Listeners; 40 import org.testng.annotations.Test; 41 import org.w3c.dom.DOMError; 42 import org.w3c.dom.DOMErrorHandler; 43 import org.w3c.dom.DOMImplementation; 44 import org.w3c.dom.Document; 45 import org.w3c.dom.ls.DOMImplementationLS; 46 import org.w3c.dom.ls.LSInput; 47 import org.w3c.dom.ls.LSOutput; 48 import org.w3c.dom.ls.LSParser; 49 import org.w3c.dom.ls.LSSerializer; 50 import org.xml.sax.SAXException; 51 52 /* 53 * @test 54 * @bug 4973153 55 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 56 * @run testng/othervm -DrunSecMngr=true dom.ls.Bug4973153 57 * @run testng/othervm dom.ls.Bug4973153 58 * @summary Test LSSerialiser.setEncoding() raises 'unsupported-encoding' error if encoding is invalid. 59 */ 60 @Listeners({jaxp.library.BasePolicy.class}) 61 public class Bug4973153 { 62 63 DOMImplementationLS implLS = null; 64 public String xml1 = "<?xml version=\"1.0\"?><ROOT><ELEMENT1></ELEMENT1><ELEMENT2></ELEMENT2></ROOT>"; 65 66 @Test testOne()67 public void testOne() { 68 LSParser db = createLSParser(); 69 if (db == null) { 70 System.out.println("Unable to create LSParser !"); 71 return; 72 } 73 LSSerializer dw = createLSSerializer(); 74 if (dw == null) { 75 System.out.println("Unable to create LSSerializer!"); 76 return; 77 } 78 79 DOMErrorHandlerImpl eh = new DOMErrorHandlerImpl(); 80 dw.getDomConfig().setParameter("error-handler", eh); 81 Document doc = db.parse(getXml1Source()); 82 83 Output out = new Output(); 84 out.setByteStream(new ByteArrayOutputStream()); 85 out.setEncoding("WrOnG_EnCoDiNg"); 86 try { 87 if (dw.write(doc, out)) { 88 System.out.println("Expected result value - false"); 89 return; 90 } 91 } catch (Exception ex) { 92 // This is bad. 93 } 94 if (!eh.WrongEncodingErrorReceived) { 95 Assert.fail("'unsupported-encoding' error was expected "); 96 return; 97 } 98 System.out.println("OKAY"); 99 return; 100 } 101 102 @BeforeMethod setUp()103 public void setUp() { 104 Document doc = null; 105 DocumentBuilder parser = null; 106 try { 107 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 108 parser = factory.newDocumentBuilder(); 109 } catch (ParserConfigurationException e) { 110 e.printStackTrace(); 111 } 112 StringBufferInputStream is = new StringBufferInputStream(xml1); 113 try { 114 doc = parser.parse(is); 115 } catch (SAXException e) { 116 e.printStackTrace(); 117 } catch (IOException e) { 118 e.printStackTrace(); 119 } 120 DOMImplementation impl = doc.getImplementation(); 121 implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0"); 122 } 123 124 @AfterMethod tearDown()125 public void tearDown() { 126 implLS = null; 127 } 128 createLSParser()129 public LSParser createLSParser() { 130 return implLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, "http://www.w3.org/2001/XMLSchema"); 131 } 132 createLSSerializer()133 public LSSerializer createLSSerializer() { 134 return implLS.createLSSerializer(); 135 } 136 createLSInput()137 public LSInput createLSInput() { 138 return implLS.createLSInput(); 139 } 140 getXml1Source()141 public LSInput getXml1Source() { 142 LSInput src = createLSInput(); 143 src.setStringData(xml1); 144 return src; 145 } 146 } 147 148 class Output implements LSOutput { 149 OutputStream bs; 150 Writer cs; 151 String sId; 152 String enc; 153 Output()154 public Output() { 155 bs = null; 156 cs = null; 157 sId = null; 158 enc = "UTF-8"; 159 } 160 getByteStream()161 public OutputStream getByteStream() { 162 return bs; 163 } 164 setByteStream(OutputStream byteStream)165 public void setByteStream(OutputStream byteStream) { 166 bs = byteStream; 167 } 168 getCharacterStream()169 public Writer getCharacterStream() { 170 return cs; 171 } 172 setCharacterStream(Writer characterStream)173 public void setCharacterStream(Writer characterStream) { 174 cs = characterStream; 175 } 176 getSystemId()177 public String getSystemId() { 178 return sId; 179 } 180 setSystemId(String systemId)181 public void setSystemId(String systemId) { 182 sId = systemId; 183 } 184 getEncoding()185 public String getEncoding() { 186 return enc; 187 } 188 setEncoding(String encoding)189 public void setEncoding(String encoding) { 190 enc = encoding; 191 } 192 } 193 194 class DOMErrorHandlerImpl implements DOMErrorHandler { 195 boolean NoOutputSpecifiedErrorReceived = false; 196 boolean WrongEncodingErrorReceived = false; 197 handleError(DOMError error)198 public boolean handleError(DOMError error) { 199 if ("no-output-specified".equalsIgnoreCase(error.getType())) { 200 NoOutputSpecifiedErrorReceived = true; 201 } else if ("unsupported-encoding".equalsIgnoreCase(error.getType())) { 202 WrongEncodingErrorReceived = true; 203 } 204 return true; 205 } 206 } 207