From 2221ee651e15468fc8bee9fddc4ade28c2c37e2f Mon Sep 17 00:00:00 2001 From: Sam Bishop Date: Mon, 17 Jun 2024 10:58:14 -0400 Subject: [PATCH 1/4] updating RegressionTest generateAnswers for when two classes are provided in getMyTestParameterFiles --- .../test/core/junit5/RegressionTest.java | 2 +- .../test/core/junit5/RegressionTestUtil.java | 20 ++++++++++++++++++- .../emissary/test/core/junit5/UnitTest.java | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/test/java/emissary/test/core/junit5/RegressionTest.java b/src/test/java/emissary/test/core/junit5/RegressionTest.java index 7351eddcd8..e29670165d 100644 --- a/src/test/java/emissary/test/core/junit5/RegressionTest.java +++ b/src/test/java/emissary/test/core/junit5/RegressionTest.java @@ -60,7 +60,7 @@ public abstract class RegressionTest extends ExtractionTest { * automatically */ @ForOverride - protected boolean generateAnswers() { + protected static boolean generateAnswers() { return Boolean.getBoolean("generateAnswers"); } diff --git a/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java b/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java index 137b01d61b..a03b631bb3 100644 --- a/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java +++ b/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java @@ -95,6 +95,13 @@ public final class RegressionTestUtil { */ private static final Path TEST_RESX = getTestResx(); + /** + * Used for RegressionTest generateAnswer when {@link UnitTest#getMyTestParameterFiles(Class, Class)} has two different + * classes, and we need to generate answer files in the ansClass directory + */ + @Nullable + private static String ansClass = null; + private RegressionTestUtil() {} /** @@ -200,7 +207,14 @@ public static Path getXmlPath(final String resource) { return null; } - final String xmlPath = resource.substring(0, datPos) + ResourceReader.XML_SUFFIX; + String xmlPath; + if (ansClass == null) { + xmlPath = resource.substring(0, datPos) + ResourceReader.XML_SUFFIX; + } else { + String ansPath = ansClass.replace(".", "/"); + int testNamePos = resource.lastIndexOf("/"); + xmlPath = ansPath + resource.substring(testNamePos, datPos) + ResourceReader.XML_SUFFIX; + } return TEST_RESX.resolve(xmlPath); } @@ -350,4 +364,8 @@ public static void tweakFinalIbdoWithFormInFilename(final String resource, final fail("Couldn't get path for resource: " + resource, e); } } + + public static void setAnsClass(String ansClass) { + RegressionTestUtil.ansClass = ansClass; + } } diff --git a/src/test/java/emissary/test/core/junit5/UnitTest.java b/src/test/java/emissary/test/core/junit5/UnitTest.java index 563c188140..cc71c54a5a 100644 --- a/src/test/java/emissary/test/core/junit5/UnitTest.java +++ b/src/test/java/emissary/test/core/junit5/UnitTest.java @@ -145,7 +145,7 @@ public static Stream getMyTestParameterFiles(Class clz) /** * Specifies a non-default source of test answer files - * + * * @param ansClz Class that provides the test answer files. */ protected synchronized void useAlternateAnswerFileSource(Class ansClz) { From 56c7606663e6d3185db15e85694028845bc6a13a Mon Sep 17 00:00:00 2001 From: Sam Bishop Date: Tue, 25 Jun 2024 15:41:32 -0400 Subject: [PATCH 2/4] updating with new thread-safe UnitTest --- .../test/core/junit5/RegressionTest.java | 10 ++++-- .../test/core/junit5/RegressionTestUtil.java | 33 ++++++++++++------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/test/java/emissary/test/core/junit5/RegressionTest.java b/src/test/java/emissary/test/core/junit5/RegressionTest.java index e29670165d..95cab25200 100644 --- a/src/test/java/emissary/test/core/junit5/RegressionTest.java +++ b/src/test/java/emissary/test/core/junit5/RegressionTest.java @@ -60,7 +60,7 @@ public abstract class RegressionTest extends ExtractionTest { * automatically */ @ForOverride - protected static boolean generateAnswers() { + protected boolean generateAnswers() { return Boolean.getBoolean("generateAnswers"); } @@ -301,7 +301,13 @@ protected void generateAnswerFiles(final String resource) throws Exception { tweakFinalLogEventsBeforeSerialisation(resource, finalLogEvents); // Generate the full XML (setup & answers from before & after) - RegressionTestUtil.writeAnswerXml(resource, initialIbdo, finalIbdo, finalResults, finalLogEvents, getEncoders()); + if (super.answerFileClassRef == null) { + RegressionTestUtil.writeAnswerXml(resource, initialIbdo, finalIbdo, finalResults, finalLogEvents, getEncoders()); + } else { + RegressionTestUtil.writeAnswerXml(resource, initialIbdo, finalIbdo, finalResults, finalLogEvents, getEncoders(), + super.answerFileClassRef); + } + // RegressionTestUtil.writeAnswerXml(resource, initialIbdo, finalIbdo, finalResults, finalLogEvents, getEncoders()); } @Override diff --git a/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java b/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java index a03b631bb3..f8dc1c4607 100644 --- a/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java +++ b/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java @@ -35,6 +35,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; import javax.annotation.Nullable; import static emissary.core.constants.IbdoXmlElementNames.ANSWERS; @@ -95,12 +96,7 @@ public final class RegressionTestUtil { */ private static final Path TEST_RESX = getTestResx(); - /** - * Used for RegressionTest generateAnswer when {@link UnitTest#getMyTestParameterFiles(Class, Class)} has two different - * classes, and we need to generate answer files in the ansClass directory - */ - @Nullable - private static String ansClass = null; + private static AtomicReference> answerFileClassRef = null; private RegressionTestUtil() {} @@ -208,10 +204,10 @@ public static Path getXmlPath(final String resource) { } String xmlPath; - if (ansClass == null) { + if (answerFileClassRef == null) { xmlPath = resource.substring(0, datPos) + ResourceReader.XML_SUFFIX; } else { - String ansPath = ansClass.replace(".", "/"); + String ansPath = answerFileClassRef.get().getName().replace(".", "/"); int testNamePos = resource.lastIndexOf("/"); xmlPath = ansPath + resource.substring(testNamePos, datPos) + ResourceReader.XML_SUFFIX; } @@ -254,6 +250,23 @@ public static void writeAnswerXml(final String resource, final IBaseDataObject i writeXml(resource, xmlContent); } + /** + * Generate the relevant XML and write to disk. Used when answer file class and data file class are not the same. + * + * @param resource referencing the DAT file + * @param initialIbdo for 'setup' section + * @param finalIbdo for 'answers' section + * @param encoders for encoding ibdo into XML + * @param results for 'answers' section + * @param answerFileClassRef test class for answer files + */ + public static void writeAnswerXml(final String resource, final IBaseDataObject initialIbdo, final IBaseDataObject finalIbdo, + final List results, final List logEvents, final ElementEncoders encoders, + final AtomicReference> answerFileClassRef) { + RegressionTestUtil.answerFileClassRef = answerFileClassRef; + writeAnswerXml(resource, initialIbdo, finalIbdo, results, logEvents, encoders); + } + /** * Helper method to write XML for a given DAT file. * @@ -364,8 +377,4 @@ public static void tweakFinalIbdoWithFormInFilename(final String resource, final fail("Couldn't get path for resource: " + resource, e); } } - - public static void setAnsClass(String ansClass) { - RegressionTestUtil.ansClass = ansClass; - } } From abaa765314505e3a73f59dc9b1c5fe7c84bc06df Mon Sep 17 00:00:00 2001 From: Sam Bishop Date: Tue, 25 Jun 2024 15:42:32 -0400 Subject: [PATCH 3/4] further updates --- src/test/java/emissary/test/core/junit5/RegressionTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/emissary/test/core/junit5/RegressionTest.java b/src/test/java/emissary/test/core/junit5/RegressionTest.java index 95cab25200..5acdb24761 100644 --- a/src/test/java/emissary/test/core/junit5/RegressionTest.java +++ b/src/test/java/emissary/test/core/junit5/RegressionTest.java @@ -307,7 +307,6 @@ protected void generateAnswerFiles(final String resource) throws Exception { RegressionTestUtil.writeAnswerXml(resource, initialIbdo, finalIbdo, finalResults, finalLogEvents, getEncoders(), super.answerFileClassRef); } - // RegressionTestUtil.writeAnswerXml(resource, initialIbdo, finalIbdo, finalResults, finalLogEvents, getEncoders()); } @Override From 506b6e97930c6b83cbb3f5912858d3ded0bf2832 Mon Sep 17 00:00:00 2001 From: Sam Bishop Date: Wed, 26 Jun 2024 10:06:21 -0400 Subject: [PATCH 4/4] updating RegressionTestUtil to not store answerFileClassRef --- .../test/core/junit5/RegressionTest.java | 10 ++--- .../test/core/junit5/RegressionTestUtil.java | 38 ++++++------------- 2 files changed, 15 insertions(+), 33 deletions(-) diff --git a/src/test/java/emissary/test/core/junit5/RegressionTest.java b/src/test/java/emissary/test/core/junit5/RegressionTest.java index 5acdb24761..5dd04d3129 100644 --- a/src/test/java/emissary/test/core/junit5/RegressionTest.java +++ b/src/test/java/emissary/test/core/junit5/RegressionTest.java @@ -301,12 +301,8 @@ protected void generateAnswerFiles(final String resource) throws Exception { tweakFinalLogEventsBeforeSerialisation(resource, finalLogEvents); // Generate the full XML (setup & answers from before & after) - if (super.answerFileClassRef == null) { - RegressionTestUtil.writeAnswerXml(resource, initialIbdo, finalIbdo, finalResults, finalLogEvents, getEncoders()); - } else { - RegressionTestUtil.writeAnswerXml(resource, initialIbdo, finalIbdo, finalResults, finalLogEvents, getEncoders(), - super.answerFileClassRef); - } + RegressionTestUtil.writeAnswerXml(resource, initialIbdo, finalIbdo, finalResults, finalLogEvents, getEncoders(), + super.answerFileClassRef); } @Override @@ -330,7 +326,7 @@ protected List processHeavyDutyHook(IServiceProviderPlace place @Override protected Document getAnswerDocumentFor(final String resource) { // If generating answers, get the src version, otherwise get the normal XML file - return generateAnswers() ? RegressionTestUtil.getAnswerDocumentFor(resource) : super.getAnswerDocumentFor(resource); + return generateAnswers() ? RegressionTestUtil.getAnswerDocumentFor(resource, super.answerFileClassRef) : super.getAnswerDocumentFor(resource); } @Override diff --git a/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java b/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java index f8dc1c4607..c9dea7054e 100644 --- a/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java +++ b/src/test/java/emissary/test/core/junit5/RegressionTestUtil.java @@ -96,8 +96,6 @@ public final class RegressionTestUtil { */ private static final Path TEST_RESX = getTestResx(); - private static AtomicReference> answerFileClassRef = null; - private RegressionTestUtil() {} /** @@ -174,12 +172,13 @@ public static List getSimplifiedLogEvents(final Element answ * This method returns the XML file from that location. * * @param resource to get the answer file for + * @param answerFileClassRef answer file class (if different from data class) * @return the XML file in the src directory (not target) */ @Nullable - public static Document getAnswerDocumentFor(final String resource) { + public static Document getAnswerDocumentFor(final String resource, @Nullable final AtomicReference> answerFileClassRef) { try { - final Path path = RegressionTestUtil.getXmlPath(resource); + final Path path = RegressionTestUtil.getXmlPath(resource, answerFileClassRef); return path == null ? null : XML_BUILDER.build(path.toFile()); } catch (final JDOMException | IOException e) { @@ -193,10 +192,11 @@ public static Document getAnswerDocumentFor(final String resource) { * Gets the XML filename/path for the given resource (a .dat file) * * @param resource path to the .dat file + * @param answerFileClassRef answer file class (if different from data class) * @return path to the corresponding .xml file */ @Nullable - public static Path getXmlPath(final String resource) { + public static Path getXmlPath(final String resource, @Nullable final AtomicReference> answerFileClassRef) { final int datPos = resource.lastIndexOf(ResourceReader.DATA_SUFFIX); if (datPos == -1) { LOGGER.debug("Resource is not a DATA file {}", resource); @@ -222,9 +222,11 @@ public static Path getXmlPath(final String resource) { * @param finalIbdo for 'answers' section * @param encoders for encoding ibdo into XML * @param results for 'answers' section + * @param answerFileClassRef answer file class (if different from data class) */ public static void writeAnswerXml(final String resource, final IBaseDataObject initialIbdo, final IBaseDataObject finalIbdo, - final List results, final List logEvents, final ElementEncoders encoders) { + final List results, final List logEvents, final ElementEncoders encoders, + @Nullable final AtomicReference> answerFileClassRef) { final Element rootElement = IBaseDataObjectXmlHelper.xmlElementFromIbdo(finalIbdo, results, initialIbdo, encoders); final Element answerElement = rootElement.getChild(ANSWERS); @@ -247,24 +249,7 @@ 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)); // Write out the XML to disk - writeXml(resource, xmlContent); - } - - /** - * Generate the relevant XML and write to disk. Used when answer file class and data file class are not the same. - * - * @param resource referencing the DAT file - * @param initialIbdo for 'setup' section - * @param finalIbdo for 'answers' section - * @param encoders for encoding ibdo into XML - * @param results for 'answers' section - * @param answerFileClassRef test class for answer files - */ - public static void writeAnswerXml(final String resource, final IBaseDataObject initialIbdo, final IBaseDataObject finalIbdo, - final List results, final List logEvents, final ElementEncoders encoders, - final AtomicReference> answerFileClassRef) { - RegressionTestUtil.answerFileClassRef = answerFileClassRef; - writeAnswerXml(resource, initialIbdo, finalIbdo, results, logEvents, encoders); + writeXml(resource, xmlContent, answerFileClassRef); } /** @@ -272,9 +257,10 @@ public static void writeAnswerXml(final String resource, final IBaseDataObject i * * @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) { - final Path path = getXmlPath(resource); + public static void writeXml(final String resource, final String 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)); }