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