Skip to content

Commit

Permalink
alternative fix for thread-safe UnitTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sambish5 committed Jun 18, 2024
1 parent 7e39ba0 commit 000c896
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
public class TestingResourcesTest extends ExtractionTest {

public static Stream<? extends Arguments> data() {
return getMyTestParameterFiles(HtmlEscapePlaceTest.class, TestingResourcesTest.class);
return getMyTestParameterFiles(HtmlEscapePlaceTest.class);
}

@Override
public IServiceProviderPlace createPlace() throws IOException {
return new HtmlEscapePlace();
}


}
38 changes: 10 additions & 28 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 @@ -25,7 +24,6 @@
import java.io.File;
import java.io.InputStream;
import java.lang.management.ThreadInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import javax.annotation.Nullable;
Expand All @@ -42,8 +40,6 @@ public abstract class UnitTest {
// Runtime typed logger
protected Logger logger = LoggerFactory.getLogger(this.getClass());

protected static List<String> answerFiles = new ArrayList<>();

@TempDir
public static File temporaryDirectory;
protected static String TMPDIR = "/tmp";
Expand Down Expand Up @@ -82,10 +78,6 @@ public void tearDown() throws Exception {
assertMaxNonSystemThreadCount(1);
}

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

/**
* Configure the test stuff
Expand Down Expand Up @@ -137,31 +129,15 @@ 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);
}

/**
* 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
*/
public static Stream<? extends Arguments> getMyTestParameterFiles(Class<?> dataClz, Class<?> ansClz) {
ResourceReader rr = new ResourceReader();
List<String> rs = rr.findDataResourcesFor(dataClz);
answerFiles = getMyTestAnswerFiles(rr, ansClz);
List<String> rs = rr.findDataResourcesFor(clz);
return rs.stream().map(Arguments::of);
}

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

/**
* Get all xml resources (*.xml) for this class
*/
protected List<String> getMyXmlResources() {
protected List<String> getMyTestXmlResources() {
ResourceReader rr = new ResourceReader();
return rr.findXmlResourcesFor(this.getClass());
}
Expand Down Expand Up @@ -228,13 +204,19 @@ protected Document getAnswerDocumentFor(String resource) {
return null;
}

// get String of resource's test location, to see if data files are in different directory than answer files
int resourceDir = resource.lastIndexOf("/");
String cutResource = resource.substring(0, resourceDir);
int classDir = cutResource.lastIndexOf("/") + 1;
String datClass = cutResource.substring(classDir);

String aname = "";
if (answerFiles.isEmpty()) {
if (datClass.equals(this.getClass().getSimpleName())) {
aname = resource.substring(0, datPos) + ResourceReader.XML_SUFFIX;
} else {
// if answer files are in different directory than data files, this will be used to find matching answer to data pair
String testFileName = FilenameUtils.getBaseName(resource);
for (String answer : answerFiles) {
for (String answer : getMyTestXmlResources()) {
if (FilenameUtils.getBaseName(answer).equals(testFileName)) {
aname = answer;
break;
Expand Down

0 comments on commit 000c896

Please sign in to comment.