From 5f0ea1a4ae8fac85381138462502c4b2a744cc44 Mon Sep 17 00:00:00 2001 From: drivenflywheel Date: Tue, 18 Jun 2024 16:54:55 +0000 Subject: [PATCH] Modifying UnitTest so that alternative answer file location usage is thread-safe --- .../core/junit5/TestingResourcesTest.java | 9 +++- .../emissary/test/core/junit5/UnitTest.java | 41 +++++++++++-------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/test/java/emissary/test/core/junit5/TestingResourcesTest.java b/src/test/java/emissary/test/core/junit5/TestingResourcesTest.java index 3a37c30ef9..d13a483d94 100644 --- a/src/test/java/emissary/test/core/junit5/TestingResourcesTest.java +++ b/src/test/java/emissary/test/core/junit5/TestingResourcesTest.java @@ -15,8 +15,15 @@ */ public class TestingResourcesTest extends ExtractionTest { + static final Class DATA_FILE_CLASS = HtmlEscapePlaceTest.class; + static final Class ANSWER_FILE_CLASS = TestingResourcesTest.class; + public static Stream data() { - return getMyTestParameterFiles(HtmlEscapePlaceTest.class, TestingResourcesTest.class); + return getMyTestParameterFiles(DATA_FILE_CLASS); + } + + public TestingResourcesTest() { + useAlternateAnswerFileSource(ANSWER_FILE_CLASS); } @Override diff --git a/src/test/java/emissary/test/core/junit5/UnitTest.java b/src/test/java/emissary/test/core/junit5/UnitTest.java index fdc5d5c0a5..563c188140 100644 --- a/src/test/java/emissary/test/core/junit5/UnitTest.java +++ b/src/test/java/emissary/test/core/junit5/UnitTest.java @@ -10,7 +10,6 @@ import org.jdom2.Document; import org.jdom2.input.SAXBuilder; import org.jdom2.input.sax.XMLReaders; -import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -27,6 +26,7 @@ import java.lang.management.ThreadInfo; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Stream; import javax.annotation.Nullable; @@ -42,7 +42,13 @@ public abstract class UnitTest { // Runtime typed logger protected Logger logger = LoggerFactory.getLogger(this.getClass()); - protected static List answerFiles = new ArrayList<>(); + /** + * Alternative answer files to use if loading answer files from a non-default location. This list should be populated + * using the {@link #useAlternateAnswerFileSource(Class)} method. + */ + private final List answerFiles = new ArrayList<>(); + + protected final AtomicReference> answerFileClassRef = new AtomicReference<>(getClass()); @TempDir public static File temporaryDirectory; @@ -82,11 +88,6 @@ public void tearDown() throws Exception { assertMaxNonSystemThreadCount(1); } - @AfterAll - public static void clearAnswerFiles() { - answerFiles.clear(); - } - /** * Configure the test stuff *

@@ -137,24 +138,28 @@ protected List getMyTestResources() { * Get all test resources (*.dat) for this class in a format suitable for Junit Parameterized Tests */ public static Stream getMyTestParameterFiles(Class clz) { - return getMyTestParameterFiles(clz, clz); + ResourceReader rr = new ResourceReader(); + List rs = rr.findDataResourcesFor(clz); + return rs.stream().map(Arguments::of); } /** - * Get test resources (*.dat) and test answers when they are in two different directories. - * - * @param dataClz class that provides the test resource (*.dat) files - * @param ansClz class that provides the test answer files - * @return the stream of test resource files to be used for JUnit Parameterized Tests + * Specifies a non-default source of test answer files + * + * @param ansClz Class that provides the test answer files. */ - public static Stream getMyTestParameterFiles(Class dataClz, Class ansClz) { + protected synchronized void useAlternateAnswerFileSource(Class ansClz) { + answerFiles.clear(); + answerFiles.addAll(getMyTestAnswerFiles(ansClz)); + answerFileClassRef.set(ansClz); + } + + private List getMyTestAnswerFiles(Class ansClz) { ResourceReader rr = new ResourceReader(); - List rs = rr.findDataResourcesFor(dataClz); - answerFiles = getMyTestAnswerFiles(rr, ansClz); - return rs.stream().map(Arguments::of); + return getMyTestAnswerFiles(rr, ansClz); } - private static List getMyTestAnswerFiles(ResourceReader resourceReader, Class ansClz) { + private List getMyTestAnswerFiles(ResourceReader resourceReader, Class ansClz) { return resourceReader.findXmlResourcesFor(ansClz); }