From fd0d3077ae40c6b43a1a251fb215350058a6fb61 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Mon, 22 Apr 2024 19:08:37 +0200 Subject: [PATCH] Correctly merge test cases into the existing 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. All unmapped test classes will be retained so that they can be visualized differently. --- .../java/edu/hm/hafner/coverage/Node.java | 34 ++++++++++++------- .../coverage/parser/TestCaseMappingTest.java | 10 +++--- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/main/java/edu/hm/hafner/coverage/Node.java b/src/main/java/edu/hm/hafner/coverage/Node.java index e567eeac..90e6621b 100644 --- a/src/main/java/edu/hm/hafner/coverage/Node.java +++ b/src/main/java/edu/hm/hafner/coverage/Node.java @@ -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 testClassNodes) { - testClassNodes.forEach(this::mapTestClass); + public Set mergeTests(final Collection 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 mapTestClass(final ClassNode testClassNode) { + Optional 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) { diff --git a/src/test/java/edu/hm/hafner/coverage/parser/TestCaseMappingTest.java b/src/test/java/edu/hm/hafner/coverage/parser/TestCaseMappingTest.java index 60b50a34..26563770 100644 --- a/src/test/java/edu/hm/hafner/coverage/parser/TestCaseMappingTest.java +++ b/src/test/java/edu/hm/hafner/coverage/parser/TestCaseMappingTest.java @@ -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(