Skip to content

Commit

Permalink
Update RegressionTest to output Unix End-Of-Line instead of JDOM2's d…
Browse files Browse the repository at this point in the history
…efault DOS End-Of-Line.
  • Loading branch information
James Cover jdcove2 committed Feb 5, 2025
1 parent 67cdcce commit 49b2c08
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/test/java/emissary/test/core/junit5/RegressionTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -250,26 +253,39 @@ 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.
*
* @param resource referencing the DAT file
* @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<Class<?>> answerFileClassRef) {
public static void writeXml(final String resource, final byte[] xmlContent, @Nullable final AtomicReference<Class<?>> 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);
Expand Down

0 comments on commit 49b2c08

Please sign in to comment.