Skip to content

Commit

Permalink
Merge pull request #98 from jenkinsci/test-case-mapping
Browse files Browse the repository at this point in the history
Correctly merge test cases into the existing coverage tree
  • Loading branch information
uhafner authored Apr 22, 2024
2 parents b08c342 + fd0d307 commit 2aedb63
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
34 changes: 21 additions & 13 deletions src/main/java/edu/hm/hafner/coverage/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -629,26 +629,34 @@ public final Node copyNode() {
public abstract Node copy();

/**
* Maps the test cases in all test classes of the specified {@link Node} to the corresponding class nodes of this
* tree. The mapping is done by the name of the test class. If the name of the test class can't be mapped to a
* target class, then the tests of this test class are ignored.
* Merges the test cases of the specified test classes into the corresponding production classes of this coverage
* tree. The mapping is done by evaluating the name of the test class. If the name of the test class cannot be
* mapped to a target class, then the tests of this test class are ignored.
*
* @param testClassNodes
* the test classes containing the test cases
*
* @return the test classes that have not been merged into this coverage tree
*/
public void mapTests(final List<ClassNode> testClassNodes) {
testClassNodes.forEach(this::mapTestClass);
public Set<ClassNode> mergeTests(final Collection<ClassNode> testClassNodes) {
return testClassNodes.stream()
.map(this::mapTestClass)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
}

private void mapTestClass(final ClassNode testClassNode) {
findPackage(testClassNode.getPackageName())
.ifPresent(packageNode -> packageNode.getAllClassNodes().stream()
.filter(classNode -> isTargetOfTest(classNode, testClassNode))
.forEach(classNode -> classNode.addTestCases(testClassNode.getTestCases())));
}
private Optional<ClassNode> mapTestClass(final ClassNode testClassNode) {
Optional<ClassNode> targetClass = findPackage(testClassNode.getPackageName())
.map(Node::getAllClassNodes).stream()
.flatMap(Collection::stream)
.filter(classNode -> classNode.getName().endsWith(createTargetClassName(testClassNode)))
.findFirst();
if (targetClass.isPresent()) {
targetClass.get().addTestCases(testClassNode.getTestCases());

private boolean isTargetOfTest(final ClassNode classNode, final ClassNode testClassNode) {
return classNode.getName().endsWith(createTargetClassName(testClassNode));
return Optional.empty();
}
return Optional.of(testClassNode);
}

private String createTargetClassName(final ClassNode testClassNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ void shouldMapTestCasesToClasses() {
assertThat(tests.getTestCases()).hasSize(257);
assertThat(tests.getAllClassNodes()).extracting(Node::getName).containsExactly(TEST_CLASSES);

var testCases = tests.getTestCases();
coverage.mapTests(tests.getAllClassNodes());
var unmappedTests = coverage.mergeTests(tests.getAllClassNodes());
assertThat(unmappedTests).flatMap(Node::getTestCases).hasSize(10);
assertThat(coverage.getTestCases()).hasSize(247);

testCases.removeAll(coverage.getTestCases());
assertThat(testCases).hasSize(10)
.extracting(TestCase::getClassName)
assertThat(unmappedTests)
.extracting(Node::getName)
.containsOnly("ArchitectureTest", "PackageArchitectureTest");

assertThat(coverage.findFile("Metric.java")).hasValueSatisfying(
Expand Down

0 comments on commit 2aedb63

Please sign in to comment.