Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8274606: Fix jaxp/javax/xml/jaxp/unittest/transform/SurrogateTest.java test #518

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one
Expand Down Expand Up @@ -1393,32 +1393,23 @@ else if (
}
}
}

// The next is kind of a hack to keep from escaping in the case
// of Shift_JIS and the like.

/*
else if ((ch < m_maxCharacter) && (m_maxCharacter == 0xFFFF)
&& (ch != 160))
{
writer.write(ch); // no escaping in this case
}
else
*/
String outputStringForChar = m_charInfo.getOutputStringForChar(ch);
if (null != outputStringForChar)
{
writer.write(outputStringForChar);
}
else if (escapingNotNeeded(ch))
{
writer.write(ch); // no escaping in this case
}
else
{
writer.write("&#");
writer.write(Integer.toString(ch));
writer.write(';');
String outputStringForChar = m_charInfo.getOutputStringForChar(ch);
if (null != outputStringForChar)
{
writer.write(outputStringForChar);
}
else if (escapingNotNeeded(ch))
{
writer.write(ch); // no escaping in this case
}
else
{
writer.write("&#");
writer.write(Integer.toString(ch));
writer.write(';');
}
}
}
cleanStart = i + 1;
Expand Down
135 changes: 135 additions & 0 deletions jdk/test/javax/xml/jaxp/transform/8268457/SurrogateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/*
* @test
* @bug 8268457
* @run main/othervm SurrogateTest
* @summary XML Transformer outputs Unicode supplementary character incorrectly to HTML
*/
public class SurrogateTest {

final static String TEST_SRC = System.getProperty("test.src", ".");

public void toHTMLTest() throws Exception {
String out = "SurrogateTest1out.html";
String expected = TEST_SRC + File.separator + "SurrogateTest1.html";
String xml = TEST_SRC + File.separator + "SurrogateTest1.xml";
String xsl = TEST_SRC + File.separator + "SurrogateTest1.xsl";

try (FileInputStream tFis = new FileInputStream(xsl);
InputStream fis = new FileInputStream(xml);
FileOutputStream fos = new FileOutputStream(out)) {

Source tSrc = new StreamSource(tFis);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer(tSrc);
t.setOutputProperty("method", "html");

Source src = new StreamSource(fis);
Result res = new StreamResult(fos);
t.transform(src, res);
}
if (!compareWithGold(expected, out)) {
throw new RuntimeException("toHTMLTest failed");
}
}

public void handlerTest() throws Exception {
File xmlFile = new File(TEST_SRC, "SurrogateTest2.xml");
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SAXParser sp = spf.newSAXParser();
TestHandler th = new TestHandler();
sp.parse(xmlFile, th);
if (!compareLinesWithGold(TEST_SRC + File.separator + "SurrogateTest2.txt", th.lines)) {
throw new RuntimeException("handlerTest failed");
}
}

private static class TestHandler extends DefaultHandler {
private List<String> lines = new ArrayList<>();

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
lines.add( localName + "@attr:" + attributes.getValue("attr"));
}
}

public static void main(String[] args) throws Exception {
SurrogateTest test = new SurrogateTest();
test.toHTMLTest();
test.handlerTest();
}

// Compare contents of golden file with test output file line by line.
public static boolean compareWithGold(String goldfile, String outputfile)
throws IOException {
return compareWithGold(goldfile, outputfile, StandardCharsets.UTF_8);
}

// Compare contents of golden file with test output file line by line.
public static boolean compareWithGold(String goldfile, String outputfile,
Charset cs) throws IOException {
boolean isSame = Files.readAllLines(Paths.get(goldfile)).
equals(Files.readAllLines(Paths.get(outputfile), cs));
if (!isSame) {
System.err.println("Golden file " + goldfile + " :");
Files.readAllLines(Paths.get(goldfile)).forEach(System.err::println);
System.err.println("Output file " + outputfile + " :");
Files.readAllLines(Paths.get(outputfile), cs).forEach(System.err::println);
}
return isSame;
}

// Compare contents of golden file with test output list line by line.
public static boolean compareLinesWithGold(String goldfile, List<String> lines)
throws IOException {
return Files.readAllLines(Paths.get(goldfile)).equals(lines);
}
}
12 changes: 12 additions & 0 deletions jdk/test/javax/xml/jaxp/transform/8268457/SurrogateTest1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form>
<input id="tag1" value="𠮟">
</form>
</body>
</html>
4 changes: 4 additions & 0 deletions jdk/test/javax/xml/jaxp/transform/8268457/SurrogateTest1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
<tag1>𠮟</tag1>
</root>
26 changes: 26 additions & 0 deletions jdk/test/javax/xml/jaxp/transform/8268457/SurrogateTest1.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
doctype-system="http://www.w3.org/TR/html4/loose.dtd"
encoding="UTF-8" indent="yes" method="html" omit-xml-declaration="yes"/>
<xsl:template match="/">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<xsl:for-each select="root">
<form>
<xsl:for-each select="tag1">
<input id="tag1">
<xsl:attribute name="value">
<xsl:value-of select="."/>
</xsl:attribute>
</input>
</xsl:for-each>
</form>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
4 changes: 4 additions & 0 deletions jdk/test/javax/xml/jaxp/transform/8268457/SurrogateTest2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root@attr:null
tag1@attr:𠮟
tag2@attr:𠀋
tag3@attr:𣱿
6 changes: 6 additions & 0 deletions jdk/test/javax/xml/jaxp/transform/8268457/SurrogateTest2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
<tag1 attr="𠮟"/>
<tag2 attr="𠀋"/>
<tag3 attr="𣱿"/>
</root>