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

Reviewed-by: phh
Backport-of: 7eb0372e55f23275b12470593adc97f1b79bc965
This commit is contained in:
Kazuhisa Takakuri 2025-05-21 08:11:19 +00:00
parent 6fd5ba5f62
commit 1daca5f46e

View File

@ -24,14 +24,14 @@
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.stream.Collectors; import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
@ -59,10 +59,11 @@ public class SurrogateTest {
public void toHTMLTest() throws Exception { public void toHTMLTest() throws Exception {
String out = "SurrogateTest1out.html"; String out = "SurrogateTest1out.html";
String expected = TEST_SRC + File.separator + "SurrogateTest1.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"; String xsl = TEST_SRC + File.separator + "SurrogateTest1.xsl";
try (FileInputStream tFis = new FileInputStream(xsl); try (FileInputStream tFis = new FileInputStream(xsl);
InputStream fis = this.getClass().getResourceAsStream("SurrogateTest1.xml"); InputStream fis = new FileInputStream(xml);
FileOutputStream fos = new FileOutputStream(out)) { FileOutputStream fos = new FileOutputStream(out)) {
Source tSrc = new StreamSource(tFis); Source tSrc = new StreamSource(tFis);
@ -74,7 +75,9 @@ public class SurrogateTest {
Result res = new StreamResult(fos); Result res = new StreamResult(fos);
t.transform(src, res); t.transform(src, res);
} }
compareWithGold(expected, out); if (!compareWithGold(expected, out)) {
throw new RuntimeException("toHTMLTest failed");
}
} }
public void handlerTest() throws Exception { public void handlerTest() throws Exception {
@ -84,15 +87,17 @@ public class SurrogateTest {
SAXParser sp = spf.newSAXParser(); SAXParser sp = spf.newSAXParser();
TestHandler th = new TestHandler(); TestHandler th = new TestHandler();
sp.parse(xmlFile, th); sp.parse(xmlFile, th);
compareStringWithGold(TEST_SRC + File.separator + "SurrogateTest2.txt", th.sb.toString()); if (!compareLinesWithGold(TEST_SRC + File.separator + "SurrogateTest2.txt", th.lines)) {
throw new RuntimeException("handlerTest failed");
}
} }
private static class TestHandler extends DefaultHandler { private static class TestHandler extends DefaultHandler {
private StringBuilder sb = new StringBuilder(); private List<String> lines = new ArrayList<>();
@Override @Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
sb.append( localName + "@attr:" + attributes.getValue("attr") + '\n'); lines.add( localName + "@attr:" + attributes.getValue("attr"));
} }
} }
@ -122,11 +127,9 @@ public class SurrogateTest {
return isSame; return isSame;
} }
// Compare contents of golden file with a test output string. // Compare contents of golden file with test output list line by line.
public static boolean compareStringWithGold(String goldfile, String string) public static boolean compareLinesWithGold(String goldfile, List<String> lines)
throws IOException { throws IOException {
return Files.readAllLines(Paths.get(goldfile)).stream().collect( return Files.readAllLines(Paths.get(goldfile)).equals(lines);
Collectors.joining(System.getProperty("line.separator")))
.equals(string);
} }
} }