From 49b2c0863d6e29a3fa5f7d765d945720de4abed3 Mon Sep 17 00:00:00 2001 From: James Cover jdcove2 Date: Wed, 5 Feb 2025 21:04:04 +0000 Subject: [PATCH] Update RegressionTest to output Unix End-Of-Line instead of JDOM2's default DOS End-Of-Line. --- .../test/core/junit5/RegressionTestUtil.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java b/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java index 88c6b61705..29c3d36617 100644 --- a/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java +++ b/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java @@ -12,7 +12,6 @@ import emissary.test.core.junit5.LogbackTester.SimplifiedLogEvent; import emissary.util.PlaceComparisonHelper; import emissary.util.io.ResourceReader; -import emissary.util.xml.AbstractJDOMUtil; import ch.qos.logback.classic.Level; import org.apache.commons.compress.utils.SeekableInMemoryByteChannel; @@ -21,9 +20,13 @@ import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import org.jdom2.input.sax.XMLReaders; +import org.jdom2.output.Format; +import org.jdom2.output.LineSeparator; +import org.jdom2.output.XMLOutputter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URISyntaxException; import java.nio.channels.FileChannel; @@ -250,11 +253,24 @@ public static void writeAnswerXml(final String resource, final IBaseDataObject i } // Generate the full XML (setup & answers from before & after) - final String xmlContent = AbstractJDOMUtil.toString(new Document(rootElement)); + final byte[] xmlContent = bytesFromDocument(new Document(rootElement)); // Write out the XML to disk writeXml(resource, xmlContent, answerFileClassRef); } + @Nullable + private static byte[] bytesFromDocument(final Document jdom) { + final Format format = Format.getPrettyFormat().setLineSeparator(LineSeparator.UNIX); + final XMLOutputter outputter = new XMLOutputter(format); + + try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { + outputter.output(jdom, os); + return os.toByteArray(); + } catch (IOException iox) { + return null; + } + } + /** * Helper method to write XML for a given DAT file. * @@ -262,14 +278,14 @@ public static void writeAnswerXml(final String resource, final IBaseDataObject i * @param xmlContent to write to the XML answer file * @param answerFileClassRef answer file class (if different from data class) */ - public static void writeXml(final String resource, final String xmlContent, @Nullable final AtomicReference> answerFileClassRef) { + public static void writeXml(final String resource, final byte[] xmlContent, @Nullable final AtomicReference> answerFileClassRef) { final Path path = getXmlPath(resource, answerFileClassRef); if (path == null) { fail(String.format("Could not get path for resource = %s", resource)); } LOGGER.info("Writing answers file to path: {}", path); try (FileChannel fc = FileChannel.open(path, CREATE_WRITE_TRUNCATE); - SeekableInMemoryByteChannel simbc = new SeekableInMemoryByteChannel(xmlContent.getBytes())) { + SeekableInMemoryByteChannel simbc = new SeekableInMemoryByteChannel(xmlContent)) { fc.transferFrom(simbc, 0, simbc.size()); } catch (final IOException ioe) { fail(String.format("Couldn't write XML answer file for resource: %s", resource), ioe);