-
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.
*** t spiking xml formatting/transformation
Co-authored-by: Jay Bazuzi <[email protected]> Co-authored-by: Llewellyn Falco <[email protected]>
- Loading branch information
1 parent
257151a
commit a2e1b31
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.approvaltests; | ||
|
||
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 - continue next time") | ||
@Test | ||
void xmlWithEmojiesAndAmpersands() | ||
{ | ||
var expected = """ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<a> | ||
<b>Tom & Jerry</b> | ||
<emoji>😸</emoji> | ||
</a> | ||
"""; | ||
String input = expected.replaceAll("\n", "").replace(" ", ""); | ||
Approvals.verify(prettyPrint(input, 2), new Options().inline(expected)); | ||
} | ||
private static String prettyPrint(String expected, int tabSize) | ||
{ | ||
try | ||
{ | ||
Source xmlInput = new StreamSource(new StringReader(expected)); | ||
StringWriter stringWriter = new StringWriter(); | ||
StreamResult xmlOutput = new StreamResult(stringWriter); | ||
TransformerFactory transformerFactory = TransformerFactory.newInstance(); | ||
transformerFactory.setAttribute("indent-number", tabSize); | ||
Transformer transformer = transformerFactory.newTransformer(); | ||
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); | ||
transformer.transform(xmlInput, xmlOutput); | ||
try (Writer writer = xmlOutput.getWriter()) | ||
{ | ||
return writer.toString(); | ||
} | ||
} | ||
catch (TransformerException | IOException e) | ||
{ | ||
return expected; | ||
} | ||
} | ||
} |