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

Programming exercises: Fix test case parsing of nested test suite elements #9490

Merged
merged 2 commits into from
Oct 20, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,8 @@ public static void processTestResultFile(String testResultFileString, List<Build
testResultFileString = testResultFileString.replaceAll(INVALID_XML_CHARS, "");
TestSuite testSuite = mapper.readValue(testResultFileString, TestSuite.class);

// If the xml file is only one test suite, parse it directly
if (!testSuite.testCases().isEmpty()) {
processTestSuite(testSuite, failedTests, successfulTests);
}
else {
// Else, check if the file contains an outer <testsuites> element
// And parse the inner test suites
TestSuites suites = mapper.readValue(testResultFileString, TestSuites.class);
if (suites.testsuites() == null) {
return;
}

for (TestSuite suite : suites.testsuites()) {
processTestSuite(suite, failedTests, successfulTests);
}
}
// A toplevel <testsuites> element is parsed like a <testsuite>
processTestSuite(testSuite, failedTests, successfulTests);
}

private static void processTestSuite(TestSuite testSuite, List<BuildResult.LocalCITestJobDTO> failedTests, List<BuildResult.LocalCITestJobDTO> successfulTests) {
Expand All @@ -64,17 +50,19 @@ private static void processTestSuite(TestSuite testSuite, List<BuildResult.Local
successfulTests.add(new BuildResult.LocalCITestJobDTO(testCase.name(), List.of()));
}
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
record TestSuites(@JacksonXmlElementWrapper(useWrapping = false) @JacksonXmlProperty(localName = "testsuite") List<TestSuite> testsuites) {
for (TestSuite suite : testSuite.testSuites()) {
processTestSuite(suite, failedTests, successfulTests);
}
magaupp marked this conversation as resolved.
Show resolved Hide resolved
}

@JsonIgnoreProperties(ignoreUnknown = true)
record TestSuite(@JacksonXmlElementWrapper(useWrapping = false) @JacksonXmlProperty(localName = "testcase") List<TestCase> testCases) {
record TestSuite(@JacksonXmlElementWrapper(useWrapping = false) @JacksonXmlProperty(localName = "testcase") List<TestCase> testCases,
@JacksonXmlElementWrapper(useWrapping = false) @JacksonXmlProperty(localName = "testsuite") List<TestSuite> testSuites) {

TestSuite {
testCases = Objects.requireNonNullElse(testCases, Collections.emptyList());
testSuites = Objects.requireNonNullElse(testSuites, Collections.emptyList());
magaupp marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,42 @@ void testEmptyTestMessage() throws IOException {
assertThat(test.getName()).isEqualTo("mwe-name");
assertThat(test.getTestMessages()).hasSize(1).contains("");
}

@Test
void testNestedTestsuite() throws IOException {
String input = """
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<testsuites errors="0" failures="0" tests="12" time="0.013">
<testsuite name="Tests" tests="12">
<testsuite name="Properties" tests="9">
<testsuite name="Checked by SmallCheck" tests="6">
<testcase classname="Tests.Properties.Checked by SmallCheck" name="Testing filtering in A" time="0.004"/>
<testcase classname="Tests.Properties.Checked by SmallCheck" name="Testing mapping in A" time="0.000"/>
<testcase classname="Tests.Properties.Checked by SmallCheck" name="Testing filtering in B" time="0.003"/>
<testcase classname="Tests.Properties.Checked by SmallCheck" name="Testing mapping in B" time="0.000"/>
<testcase classname="Tests.Properties.Checked by SmallCheck" name="Testing filtering in C" time="0.001"/>
<testcase classname="Tests.Properties.Checked by SmallCheck" name="Testing mapping in C" time="0.000"/>
</testsuite>
<testsuite name="Checked by QuickCheck" tests="3">
<testcase classname="Tests.Properties.Checked by QuickCheck" name="Testing A against sample solution" time="0.001"/>
<testcase classname="Tests.Properties.Checked by QuickCheck" name="Testing B against sample solution" time="0.001"/>
<testcase classname="Tests.Properties.Checked by QuickCheck" name="Testing C against sample solution" time="0.001"/>
</testsuite>
</testsuite>
<testsuite name="Unit Tests" tests="3">
<testcase classname="Tests.Unit Tests" name="Testing selectAndReflectA (0,0) []" time="0.000"/>
<testcase classname="Tests.Unit Tests" name="Testing selectAndReflectB (0,1) [(0,0)]" time="0.000"/>
<testcase classname="Tests.Unit Tests" name="Testing selectAndReflectC (0,1) [(-1,-1)]" time="0.000"/>
</testsuite>
</testsuite>
</testsuites>
""";

TestResultXmlParser.processTestResultFile(input, failedTests, successfulTests);
assertThat(successfulTests).hasSize(12).extracting(BuildResult.LocalCITestJobDTO::getName).containsExactlyInAnyOrder("Testing filtering in A", "Testing mapping in A",
"Testing filtering in B", "Testing mapping in B", "Testing filtering in C", "Testing mapping in C", "Testing A against sample solution",
"Testing B against sample solution", "Testing C against sample solution", "Testing selectAndReflectA (0,0) []", "Testing selectAndReflectB (0,1) [(0,0)]",
"Testing selectAndReflectC (0,1) [(-1,-1)]");
assertThat(failedTests).isEmpty();
}
magaupp marked this conversation as resolved.
Show resolved Hide resolved
}
Loading