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