-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: Jay Bazuzi <[email protected]> Co-Authored-By: Lars Eckart <[email protected]>
- Loading branch information
1 parent
a1d5d4b
commit 63a99dc
Showing
2 changed files
with
49 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 43 additions & 55 deletions
98
approvaltests-tests/src/test/java/org/approvaltests/XmlFormattingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,51 @@ | ||
package org.approvaltests; | ||
|
||
import nu.xom.*; | ||
import org.approvaltests.core.Options; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.xml.transform.OutputKeys; | ||
import javax.xml.transform.Source; | ||
import javax.xml.transform.Transformer; | ||
import javax.xml.transform.TransformerException; | ||
import javax.xml.transform.TransformerFactory; | ||
import javax.xml.transform.stream.StreamResult; | ||
import javax.xml.transform.stream.StreamSource; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
|
||
class XmlFormattingTest | ||
{ | ||
@Disabled("SPIKE for #466 - continue next time") | ||
@Test | ||
void xmlWithEmojiesAndAmpersands() | ||
{ | ||
var expected = """ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<a> | ||
<b>Tom & Jerry</b> | ||
<emoji>😸</emoji> | ||
</a> | ||
"""; | ||
String minimizedXml = expected.replaceAll("\n", "").replace(" ", ""); | ||
verifyXml(minimizedXml, expected); | ||
} | ||
|
||
private static void verifyXml(String minimizedXml, String expected) { | ||
Options options = new Options().inline(expected); | ||
final String formattedXml = prettyPrintXml(minimizedXml); | ||
Approvals.verify(formattedXml, options.forFile().withExtension(".xml")); | ||
} | ||
|
||
private static String prettyPrintXml(String minimizedXml) { | ||
try | ||
{ | ||
Source xmlInput = new StreamSource(new StringReader(minimizedXml)); | ||
StringWriter stringWriter = new StringWriter(); | ||
StreamResult xmlOutput = new StreamResult(stringWriter); | ||
TransformerFactory transformerFactory = TransformerFactory.newInstance(); | ||
transformerFactory.setAttribute("indent-number", 2); | ||
Transformer transformer = transformerFactory.newTransformer(); | ||
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); | ||
transformer.transform(xmlInput, xmlOutput); | ||
try (Writer writer = xmlOutput.getWriter()) | ||
{ | ||
return writer.toString(); | ||
} | ||
import java.io.*; | ||
|
||
class XmlFormattingTest { | ||
// @Disabled("SPIKE for #466 - continue next time") | ||
@Test | ||
void xmlWithEmojiesAndAmpersands() { | ||
var expected = """ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<a> | ||
<b>😸 & 🐶</b> | ||
</a> | ||
"""; | ||
String minimizedXml = expected.replaceAll("\n", " ").replace(" ", ""); | ||
verifyXml(minimizedXml, expected); | ||
} | ||
catch (TransformerException | IOException e) | ||
{ | ||
return minimizedXml; | ||
|
||
private static void verifyXml(String minimizedXml, String expected) { | ||
Options options = new Options().inline(expected); | ||
final String formattedXml = prettyPrintXml(minimizedXml, 2); | ||
Approvals.verify(formattedXml, options.forFile().withExtension(".xml")); | ||
} | ||
} | ||
|
||
|
||
private static String prettyPrintXml(String minimizedXml, int indent) { | ||
try { | ||
Builder builder = new Builder(); | ||
Document doc = builder.build(new StringReader(minimizedXml)); | ||
|
||
// create a string output stream | ||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
Serializer serializer = new Serializer(byteArrayOutputStream, "UTF-8"); | ||
serializer.setIndent(indent); // Increase indentation to make XML more readable | ||
serializer.setLineSeparator("\n"); // Ensure new lines are added | ||
serializer.setMaxLength(0); // Prevent line wrapping | ||
|
||
serializer.write(doc); | ||
|
||
return byteArrayOutputStream.toString("UTF-8"); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return minimizedXml + "\n\nXML Parsing Error:\n" + e.getMessage(); | ||
} | ||
} | ||
|
||
} |