Skip to content

Commit

Permalink
Merge pull request #54 from jenkinsci/junit-autograding
Browse files Browse the repository at this point in the history
Copy test cases when copying class nodes
  • Loading branch information
uhafner authored Nov 22, 2023
2 parents 463bf2b + ac692ba commit 822b391
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 27 deletions.
2 changes: 1 addition & 1 deletion doc/dependency-graph.puml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ skinparam rectangle {
BackgroundColor<<runtime>> lightBlue
BackgroundColor<<provided>> lightGray
}
rectangle "coverage-model\n\n0.33.0" as edu_hm_hafner_coverage_model_jar
rectangle "coverage-model\n\n0.34.0-SNAPSHOT" as edu_hm_hafner_coverage_model_jar
rectangle "spotbugs-annotations\n\n4.7.3" as com_github_spotbugs_spotbugs_annotations_jar
rectangle "error_prone_annotations\n\n2.23.0" as com_google_errorprone_error_prone_annotations_jar
rectangle "streamex\n\n0.8.2" as one_util_streamex_jar
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/edu/hm/hafner/coverage/ClassNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public ClassNode(final String name) {

@Override
public ClassNode copy() {
return new ClassNode(getName());
var copy = new ClassNode(getName());
copy.testCases.addAll(testCases);
return copy;
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/edu/hm/hafner/coverage/FileNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,19 @@ private Object readResolve() {

@Override
public FileNode copy() {
var file = new FileNode(getName(), relativePath);
var copy = new FileNode(getName(), relativePath);

file.coveredPerLine.putAll(coveredPerLine);
file.missedPerLine.putAll(missedPerLine);
copy.coveredPerLine.putAll(coveredPerLine);
copy.missedPerLine.putAll(missedPerLine);

file.modifiedLines.addAll(modifiedLines);
copy.modifiedLines.addAll(modifiedLines);

file.mutations.addAll(mutations);
copy.mutations.addAll(mutations);

file.indirectCoverageChanges.putAll(indirectCoverageChanges);
file.coverageDelta.putAll(coverageDelta);
copy.indirectCoverageChanges.putAll(indirectCoverageChanges);
copy.coverageDelta.putAll(coverageDelta);

return file;
return copy;
}

@Override
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/edu/hm/hafner/coverage/TestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ public final class TestCase implements Serializable {

private final String testName;
private final String className;
private final TestResult status;
private final TestResult result;
private final String type;
private final String message;
private final String description;

private TestCase(final String testName, final String className, final TestResult status,
private TestCase(final String testName, final String className, final TestResult result,
final String type, final String message, final String description) {
this.testName = testName;
this.className = className.intern();
this.status = status;
this.result = result;
this.type = type;
this.message = message;
this.description = description;
Expand All @@ -41,8 +41,8 @@ public String getClassName() {
return className;
}

public TestResult getStatus() {
return status;
public TestResult getResult() {
return result;
}

public String getType() {
Expand Down Expand Up @@ -74,7 +74,7 @@ public boolean equals(final Object o) {
if (!className.equals(testCase.className)) {
return false;
}
if (status != testCase.status) {
if (result != testCase.result) {
return false;
}
if (!type.equals(testCase.type)) {
Expand All @@ -88,20 +88,20 @@ public boolean equals(final Object o) {

@Override @Generated
public String toString() {
return "TestCase{testName='" + testName + '\'' + ", className='" + className + '\'' + ", status=" + status
return "TestCase{testName='" + testName + '\'' + ", className='" + className + '\'' + ", status=" + result
+ ", type='" + type + '\'' + ", message='" + message + '\'' + ", description='" + description + '\''
+ '}';
}

@Override
public int hashCode() {
int result = testName.hashCode();
result = 31 * result + className.hashCode();
result = 31 * result + status.hashCode();
result = 31 * result + type.hashCode();
result = 31 * result + message.hashCode();
result = 31 * result + description.hashCode();
return result;
int value = testName.hashCode();
value = 31 * value + className.hashCode();
value = 31 * value + this.result.hashCode();
value = 31 * value + type.hashCode();
value = 31 * value + message.hashCode();
value = 31 * value + description.hashCode();
return value;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/edu/hm/hafner/coverage/ClassNodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.jupiter.api.Test;

import edu.hm.hafner.coverage.TestCase.TestCaseBuilder;

import static edu.hm.hafner.coverage.assertions.Assertions.*;

class ClassNodeTest extends AbstractNodeTest {
Expand Down Expand Up @@ -29,4 +31,14 @@ void shouldHandleUnexpectedNodes() {
.isNotPresent();
assertThat(classNode).isNotAggregation();
}

@Test
void shouldCopyTests() {
var original = new ClassNode("Copy Me");
var testCase = new TestCaseBuilder().withTestName("test").build();
original.addTestCase(testCase);

assertThat(original.getTestCases()).hasSize(1).contains(testCase);
assertThat(original.copy().getTestCases()).hasSize(1).contains(testCase);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void shouldReadArchUnitTests() {
assertThat(testClass.getTestCases()).hasSize(1);

var testCase = getFirstTest(tree);
assertThat(testCase).hasStatus(TestResult.FAILED)
assertThat(testCase).hasResult(TestResult.FAILED)
.hasClassName("Aufgabe3Test")
.hasTestName("shouldSplitToEmptyRight(int)[1]")
.hasType("org.opentest4j.AssertionFailedError");
Expand All @@ -73,7 +73,7 @@ void shouldReadWithNameOnly() {

assertThat(tree.aggregateValues()).contains(new TestCount(141));
assertThat(tree.getTestCases()).hasSize(141)
.filteredOn(test -> test.getStatus() == TestResult.SKIPPED).hasSize(19);
.filteredOn(test -> test.getResult() == TestResult.SKIPPED).hasSize(19);
}

@Test
Expand Down Expand Up @@ -206,7 +206,7 @@ private TestCase getFirstTest(final Node node) {
.map(ClassNode.class::cast)
.map(ClassNode::getTestCases)
.flatMap(Collection::stream)
.filter(test -> test.getStatus() == TestResult.FAILED)
.filter(test -> test.getResult() == TestResult.FAILED)
.findFirst()
.orElseThrow(() -> new NoSuchElementException("No failed test found"));
}
Expand Down

0 comments on commit 822b391

Please sign in to comment.