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