Skip to content

Commit

Permalink
update UnitTest so that alternative answer file location usage is thr…
Browse files Browse the repository at this point in the history
…ead-safe (#826)
  • Loading branch information
drivenflywheel authored Jun 18, 2024
1 parent bf00b70 commit ed4fa92
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Arguments> data() {
return getMyTestParameterFiles(HtmlEscapePlaceTest.class, TestingResourcesTest.class);
return getMyTestParameterFiles(DATA_FILE_CLASS);
}

public TestingResourcesTest() {
useAlternateAnswerFileSource(ANSWER_FILE_CLASS);
}

@Override
Expand Down
41 changes: 23 additions & 18 deletions src/test/java/emissary/test/core/junit5/UnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -42,7 +42,13 @@ public abstract class UnitTest {
// Runtime typed logger
protected Logger logger = LoggerFactory.getLogger(this.getClass());

protected static List<String> 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<String> answerFiles = new ArrayList<>();

protected final AtomicReference<Class<?>> answerFileClassRef = new AtomicReference<>(getClass());

@TempDir
public static File temporaryDirectory;
Expand Down Expand Up @@ -82,11 +88,6 @@ public void tearDown() throws Exception {
assertMaxNonSystemThreadCount(1);
}

@AfterAll
public static void clearAnswerFiles() {
answerFiles.clear();
}

/**
* Configure the test stuff
* <p>
Expand Down Expand Up @@ -137,24 +138,28 @@ protected List<String> getMyTestResources() {
* Get all test resources (*.dat) for this class in a format suitable for Junit Parameterized Tests
*/
public static Stream<? extends Arguments> getMyTestParameterFiles(Class<?> clz) {
return getMyTestParameterFiles(clz, clz);
ResourceReader rr = new ResourceReader();
List<String> 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<? extends Arguments> getMyTestParameterFiles(Class<?> dataClz, Class<?> ansClz) {
protected synchronized void useAlternateAnswerFileSource(Class<?> ansClz) {
answerFiles.clear();
answerFiles.addAll(getMyTestAnswerFiles(ansClz));
answerFileClassRef.set(ansClz);
}

private List<String> getMyTestAnswerFiles(Class<?> ansClz) {
ResourceReader rr = new ResourceReader();
List<String> rs = rr.findDataResourcesFor(dataClz);
answerFiles = getMyTestAnswerFiles(rr, ansClz);
return rs.stream().map(Arguments::of);
return getMyTestAnswerFiles(rr, ansClz);
}

private static List<String> getMyTestAnswerFiles(ResourceReader resourceReader, Class<?> ansClz) {
private List<String> getMyTestAnswerFiles(ResourceReader resourceReader, Class<?> ansClz) {
return resourceReader.findXmlResourcesFor(ansClz);
}

Expand Down

0 comments on commit ed4fa92

Please sign in to comment.