Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/xml/jaxp/transform/8207760/JDK8207760.java
38862 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayInputStream;24import java.io.InputStream;25import java.io.StringReader;26import java.io.StringWriter;27import java.nio.charset.StandardCharsets;28import javax.xml.transform.Transformer;29import javax.xml.transform.TransformerException;30import javax.xml.transform.TransformerFactory;31import javax.xml.transform.stream.StreamResult;32import javax.xml.transform.stream.StreamSource;3334import org.testng.Assert;35import org.testng.annotations.Listeners;36import org.testng.annotations.Test;37import java.util.Random;38import javax.xml.transform.OutputKeys;39import org.testng.annotations.DataProvider;4041/*42* @test43* @run testng/othervm JDK820776044* @summary Verifies that a surrogate pair at the edge of a buffer is properly handled45* @bug 820776046*/47public class JDK8207760 {48final String xsl8207760 =49"<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" +50" <xsl:output omit-xml-declaration=\"yes\" indent=\"no\" />\n" +51"\n" +52" <xsl:template match=\"node()|@*\">\n" +53" <xsl:copy>\n" +54" <xsl:apply-templates select=\"node()|@*\" />\n" +55" </xsl:copy>\n" +56" </xsl:template>\n" +57"</xsl:stylesheet>\n";5859final String xsl8207760_2 = "<xsl:stylesheet \n" +60" version=\"1.0\" \n" +61" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" +62"\n" +63" <xsl:output method=\"xml\" indent=\"no\" cdata-section-elements=\"source\"/>\n" +64"\n" +65" <xsl:template match=\"source\">\n" +66" <xsl:copy>\n" +67" <xsl:apply-templates select=\"node()\" />\n" +68" </xsl:copy>\n" +69" </xsl:template>\n" +70"\n" +71"</xsl:stylesheet>";7273final String xsl8207760_3 = "<xsl:stylesheet \n" +74" version=\"1.0\" \n" +75" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" +76"\n" +77" <xsl:output method=\"xml\" indent=\"no\" cdata-section-elements=\"source\"/>\n" +78"\n" +79" <xsl:template match=\"source\">\n" +80" <xsl:copy>\n" +81" <!-- Copy the attributes -->\n" +82" <xsl:apply-templates select=\"@*\"/>\n" +83" <!-- Convert the contained nodes (elements and text) into text -->\n" +84" <xsl:variable name=\"subElementsText\">\n" +85" <xsl:apply-templates select=\"node()\"/>\n" +86" </xsl:variable>\n" +87" <!-- Output the XML directive and the converted nodes -->\n" +88" <xsl:value-of select=\"$subElementsText\"/>\n" +89" </xsl:copy>\n" +90" </xsl:template>\n" +91"\n" +92"</xsl:stylesheet>";9394@DataProvider(name = "xsls")95public Object[][] getDataBug8207760_cdata() {96return new Object[][]{97{xsl8207760_2},98{xsl8207760_3},99};100}101102/*103* @bug 8207760104* Verifies that a surrogate pair at the edge of a buffer is properly handled105* when serializing into a Character section.106*/107@Test108public final void testBug8207760() throws Exception {109String[] xmls = prepareXML(false);110Transformer t = createTransformerFromInputstream(111new ByteArrayInputStream(xsl8207760.getBytes(StandardCharsets.UTF_8)));112t.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());113StringWriter sw = new StringWriter();114t.transform(new StreamSource(new StringReader(xmls[0])), new StreamResult(sw));115Assert.assertEquals(sw.toString().replaceAll(System.lineSeparator(), "\n"), xmls[1]);116}117118/*119* @bug 8207760120* Verifies that a surrogate pair at the edge of a buffer is properly handled121* when serializing into a CDATA section.122*/123@Test(dataProvider = "xsls")124public final void testBug8207760_cdata(String xsl) throws Exception {125String[] xmls = prepareXML(true);126Transformer t = createTransformerFromInputstream(127new ByteArrayInputStream(xsl.getBytes(StandardCharsets.UTF_8)));128t.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());129StringWriter sw = new StringWriter();130t.transform(new StreamSource(new StringReader(xmls[0])), new StreamResult(sw));131Assert.assertEquals(sw.toString().replaceAll(System.lineSeparator(), "\n"), xmls[1]);132}133134private String[] prepareXML(boolean cdata) {135String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><source>";136if (cdata) {137xml += "<![CDATA[";138}139String tail = "abc 123 </source>";140if (cdata) {141tail = "abc 123 ]]></source>";142}143String temp = generateString(1023);144xml = xml + temp + '\uD83C' + '\uDF42' + tail;145//xml = xml + temp + tail;146String expected = (!cdata) ? "<source>" + temp + "🍂" + tail147: xml;148149return new String[]{xml, expected};150}151152static final char[] CHARS = "abcdefghijklmnopqrstuvwxyz \n".toCharArray();153StringBuilder sb = new StringBuilder(1024 << 4);154Random random = new Random();155156private String generateString(int size) {157sb.setLength(0);158for (int i = 0; i < size; i++) {159char c = CHARS[random.nextInt(CHARS.length)];160sb.append(c);161}162163return sb.toString();164}165166private Transformer createTransformerFromInputstream(InputStream xslStream)167throws TransformerException {168return TransformerFactory.newInstance().newTransformer(new StreamSource(xslStream));169}170}171172173