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 transform;
  25 
  26 import java.io.ByteArrayInputStream;
  27 import java.io.ByteArrayOutputStream;
  28 
  29 import javax.xml.parsers.DocumentBuilderFactory;
  30 import javax.xml.transform.Transformer;
  31 import javax.xml.transform.TransformerFactory;
  32 import javax.xml.transform.dom.DOMSource;
  33 import javax.xml.transform.stream.StreamResult;
  34 
  35 import org.testng.Assert;
  36 import org.testng.annotations.Test;
  37 import org.w3c.dom.Document;
  38 import org.w3c.dom.Element;
  39 
  40 /*
  41  * @bug 6311448
  42  * @summary Test XML transformer can output Unicode surrorate pair.
  43  */
  44 public class Bug6311448 {
  45 
  46     @Test
  47     public void test01() {
  48         try {
  49             String attrKey = "key";
  50             String attrValue = "\ud800\udc00"; // 17-bit code point in UTF-16
  51 
  52             // Some obvious assertions for documentation purposes
  53             Assert.assertTrue(Character.isSurrogatePair('\ud800', '\udc00'));
  54             Assert.assertTrue(Character.toCodePoint('\ud800', '\udc00') == 65536);
  55             Assert.assertTrue(Character.charCount(Character.toCodePoint('\ud800', '\udc00')) == 2);
  56 
  57             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  58             Transformer t = TransformerFactory.newInstance().newTransformer();
  59 
  60             // Create a DOM with 'attrValue' in it
  61             Document doc = dbf.newDocumentBuilder().getDOMImplementation().createDocument(null, null, null);
  62             Element xmlRoot = doc.createElement("root");
  63             xmlRoot.setAttribute(attrKey, attrValue);
  64             doc.appendChild(xmlRoot);
  65 
  66             // Serialize DOM into a byte array
  67             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  68             t.setOutputProperty("encoding", "utf-8");
  69             t.transform(new DOMSource(doc), new StreamResult(baos));
  70 
  71             // Re-parse byte array back into a DOM
  72             ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  73             doc = dbf.newDocumentBuilder().parse(bais);
  74             String newValue = doc.getDocumentElement().getAttribute(attrKey);
  75             Assert.assertTrue(newValue.charAt(0) == '\ud800' && newValue.charAt(1) == '\udc00');
  76         } catch (Exception e) {
  77             Assert.fail(e.getMessage());
  78         }
  79     }
  80 
  81 }