Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating RegressionTest generateAnswers for when two classes are used for test .dat & answer files #821

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/test/java/emissary/test/core/junit5/RegressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ 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());
RegressionTestUtil.writeAnswerXml(resource, initialIbdo, finalIbdo, finalResults, finalLogEvents, getEncoders(),
super.answerFileClassRef);
}

@Override
Expand All @@ -325,7 +326,7 @@ protected List<IBaseDataObject> 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
Expand Down
29 changes: 21 additions & 8 deletions src/test/java/emissary/test/core/junit5/RegressionTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -171,12 +172,13 @@ public static List<SimplifiedLogEvent> 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<Class<?>> 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) {
Expand All @@ -190,17 +192,25 @@ 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<Class<?>> answerFileClassRef) {
final int datPos = resource.lastIndexOf(ResourceReader.DATA_SUFFIX);
if (datPos == -1) {
LOGGER.debug("Resource is not a DATA file {}", resource);
return null;
}

final String xmlPath = resource.substring(0, datPos) + ResourceReader.XML_SUFFIX;
String xmlPath;
if (answerFileClassRef == null) {
xmlPath = resource.substring(0, datPos) + ResourceReader.XML_SUFFIX;
} else {
String ansPath = answerFileClassRef.get().getName().replace(".", "/");
int testNamePos = resource.lastIndexOf("/");
xmlPath = ansPath + resource.substring(testNamePos, datPos) + ResourceReader.XML_SUFFIX;
}
return TEST_RESX.resolve(xmlPath);
}

Expand All @@ -212,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<IBaseDataObject> results, final List<SimplifiedLogEvent> logEvents, final ElementEncoders encoders) {
final List<IBaseDataObject> results, final List<SimplifiedLogEvent> logEvents, final ElementEncoders encoders,
@Nullable final AtomicReference<Class<?>> answerFileClassRef) {
final Element rootElement = IBaseDataObjectXmlHelper.xmlElementFromIbdo(finalIbdo, results, initialIbdo, encoders);
final Element answerElement = rootElement.getChild(ANSWERS);

Expand All @@ -237,17 +249,18 @@ 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);
writeXml(resource, xmlContent, answerFileClassRef);
}

/**
* Helper method to write XML for a given DAT file.
*
* @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<Class<?>> answerFileClassRef) {
final Path path = getXmlPath(resource, answerFileClassRef);
if (path == null) {
fail(String.format("Could not get path for resource = %s", resource));
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/emissary/test/core/junit5/UnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static Stream<? extends Arguments> 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) {
Expand Down