Skip to content

Commit

Permalink
adding ability to pull from different directories for test data & ans…
Browse files Browse the repository at this point in the history
…wer files
  • Loading branch information
sambish5 committed May 20, 2024
1 parent 85363e0 commit abf8cec
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/test/java/emissary/test/core/junit5/UnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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 @@ -23,6 +24,7 @@
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 @@ -39,6 +41,8 @@ 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 @@ -77,6 +81,11 @@ public void tearDown() throws Exception {
assertMaxNonSystemThreadCount(1);
}

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

/**
* Configure the test stuff
* <p>
Expand Down Expand Up @@ -132,6 +141,20 @@ public static Stream<? extends Arguments> getMyTestParameterFiles(Class<?> clz)
return rs.stream().map(Arguments::of);
}

/**
* Get test resources (*.dat) and test answers when they are in two different directories.
*
* @param answerClass class that provides the test answer files
* @param dataClass class that provides the test resource (*.dat) files
* @return the stream of test resource files to be used for JUnit Parameterized Tests
*/
public static Stream<? extends Arguments> getMyTestAnswerFiles(Class<?> answerClass, Class<?> dataClass) {
ResourceReader rr = new ResourceReader();
answerFiles = rr.findXmlResourcesFor(answerClass);
List<String> rs = rr.findDataResourcesFor(dataClass);
return rs.stream().map(Arguments::of);
}

/**
* Get all xml resources (*.xml) for this class
*/
Expand Down Expand Up @@ -199,7 +222,19 @@ protected Document getAnswerDocumentFor(String resource) {
return null;
}

String aname = resource.substring(0, datPos) + ResourceReader.XML_SUFFIX;
String aname = "";
if (answerFiles.isEmpty()) {
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
int testFileName = resource.lastIndexOf("/");
for (String answer : answerFiles) {
if (answer.contains(resource.substring(testFileName + 1, datPos))) {
aname = answer;
}
}
}

SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING);
Document answerDoc;
try (InputStream is = new ResourceReader().getResourceAsStream(aname)) {
Expand Down

0 comments on commit abf8cec

Please sign in to comment.