Skip to content

Commit

Permalink
. t playing with xom library
Browse files Browse the repository at this point in the history
Co-Authored-By: Jay Bazuzi <[email protected]>
Co-Authored-By: Lars Eckart <[email protected]>
  • Loading branch information
3 people committed Oct 7, 2024
1 parent a1d5d4b commit 63a99dc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 55 deletions.
6 changes: 6 additions & 0 deletions approvaltests-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
<artifactId>approvaltests-util</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>xom</groupId>
<artifactId>xom</artifactId>
<version>1.3.8</version>
</dependency>

<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
Expand Down
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 &amp; 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>😸 &amp; 🐶</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();
}
}

}

0 comments on commit 63a99dc

Please sign in to comment.