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