2424import java .io .File ;
2525import java .io .FileInputStream ;
2626import java .io .FileOutputStream ;
27- import java .io .FileReader ;
2827import java .io .IOException ;
2928import java .io .InputStream ;
3029import java .nio .charset .Charset ;
3130import java .nio .charset .StandardCharsets ;
3231import java .nio .file .Files ;
3332import java .nio .file .Paths ;
34- import java .util .stream .Collectors ;
33+ import java .util .ArrayList ;
34+ import java .util .List ;
3535
3636import javax .xml .parsers .SAXParser ;
3737import javax .xml .parsers .SAXParserFactory ;
@@ -59,10 +59,11 @@ public class SurrogateTest {
5959 public void toHTMLTest () throws Exception {
6060 String out = "SurrogateTest1out.html" ;
6161 String expected = TEST_SRC + File .separator + "SurrogateTest1.html" ;
62+ String xml = TEST_SRC + File .separator + "SurrogateTest1.xml" ;
6263 String xsl = TEST_SRC + File .separator + "SurrogateTest1.xsl" ;
6364
6465 try (FileInputStream tFis = new FileInputStream (xsl );
65- InputStream fis = this . getClass (). getResourceAsStream ( "SurrogateTest1. xml" );
66+ InputStream fis = new FileInputStream ( xml );
6667 FileOutputStream fos = new FileOutputStream (out )) {
6768
6869 Source tSrc = new StreamSource (tFis );
@@ -74,7 +75,9 @@ public void toHTMLTest() throws Exception {
7475 Result res = new StreamResult (fos );
7576 t .transform (src , res );
7677 }
77- compareWithGold (expected , out );
78+ if (!compareWithGold (expected , out )) {
79+ throw new RuntimeException ("toHTMLTest failed" );
80+ }
7881 }
7982
8083 public void handlerTest () throws Exception {
@@ -84,15 +87,17 @@ public void handlerTest() throws Exception {
8487 SAXParser sp = spf .newSAXParser ();
8588 TestHandler th = new TestHandler ();
8689 sp .parse (xmlFile , th );
87- compareStringWithGold (TEST_SRC + File .separator + "SurrogateTest2.txt" , th .sb .toString ());
90+ if (!compareLinesWithGold (TEST_SRC + File .separator + "SurrogateTest2.txt" , th .lines )) {
91+ throw new RuntimeException ("handlerTest failed" );
92+ }
8893 }
8994
9095 private static class TestHandler extends DefaultHandler {
91- private StringBuilder sb = new StringBuilder ();
96+ private List < String > lines = new ArrayList <> ();
9297
9398 @ Override
9499 public void startElement (String uri , String localName , String qName , Attributes attributes ) throws SAXException {
95- sb . append ( localName + "@attr:" + attributes .getValue ("attr" ) + '\n' );
100+ lines . add ( localName + "@attr:" + attributes .getValue ("attr" ));
96101 }
97102 }
98103
@@ -122,11 +127,9 @@ public static boolean compareWithGold(String goldfile, String outputfile,
122127 return isSame ;
123128 }
124129
125- // Compare contents of golden file with a test output string .
126- public static boolean compareStringWithGold (String goldfile , String string )
130+ // Compare contents of golden file with test output list line by line .
131+ public static boolean compareLinesWithGold (String goldfile , List < String > lines )
127132 throws IOException {
128- return Files .readAllLines (Paths .get (goldfile )).stream ().collect (
129- Collectors .joining (System .getProperty ("line.separator" )))
130- .equals (string );
133+ return Files .readAllLines (Paths .get (goldfile )).equals (lines );
131134 }
132135}
0 commit comments