Skip to content

Commit

Permalink
Create new TestDescriptor.getAncestors() method
Browse files Browse the repository at this point in the history
  • Loading branch information
juliette-derancourt committed Jun 26, 2023
1 parent 368a1de commit 1381a3d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,26 @@ default String getLegacyReportingName() {
*/
Set<? extends TestDescriptor> getChildren();

/**
* Get the immutable set of all <em>ancestors</em> of this descriptor.
*
* <p>An <em>ancestors</em> is the parent of this descriptor or the parent
* of one of its parents, recursively.
*
* @see #getParent()
*/
@API(status = STABLE, since = "5.10")
default Set<? extends TestDescriptor> getAncestors() {
if (!getParent().isPresent()) {
return Collections.emptySet();
}
TestDescriptor parent = getParent().get();
Set<TestDescriptor> ancestors = new LinkedHashSet<>();
ancestors.add(parent);
ancestors.addAll(parent.getAncestors());
return Collections.unmodifiableSet(ancestors);
}

/**
* Get the immutable set of all <em>descendants</em> of this descriptor.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@

package org.junit.platform.launcher.core;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.platform.commons.util.ExceptionUtils;
Expand Down Expand Up @@ -49,9 +47,10 @@ public void executionFinished(TestDescriptor testDescriptor, TestExecutionResult
}

private static List<String> getTestClassNames(TestDescriptor testDescriptor) {
return findAllParentDescriptors(testDescriptor) //
return testDescriptor.getAncestors() //
.stream() //
.map(TestDescriptor::getSource).filter(Optional::isPresent) //
.map(TestDescriptor::getSource) //
.filter(Optional::isPresent) //
.map(Optional::get) //
.map(source -> {
if (source instanceof ClassSource) {
Expand All @@ -68,18 +67,4 @@ else if (source instanceof MethodSource) {
.collect(Collectors.toList());
}

private static Set<TestDescriptor> findAllParentDescriptors(TestDescriptor testDescriptor) {
return findParentDescriptors(testDescriptor, new HashSet<>());
}

private static Set<TestDescriptor> findParentDescriptors(TestDescriptor testDescriptor,
Set<TestDescriptor> visitedDescriptors) {
if (testDescriptor.isRoot()) {
return visitedDescriptors;
}
TestDescriptor parent = testDescriptor.getParent().get();
visitedDescriptors.add(parent);
return findParentDescriptors(parent, visitedDescriptors);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package org.junit.platform.engine.support.descriptor;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
Expand All @@ -34,23 +35,27 @@
class AbstractTestDescriptorTests {

private EngineDescriptor engineDescriptor;
private GroupDescriptor group1;
private GroupDescriptor group11;
private LeafDescriptor leaf111;

@BeforeEach
void initTree() {
engineDescriptor = new EngineDescriptor(UniqueId.forEngine("testEngine"), "testEngine");
var group1 = new GroupDescriptor(UniqueId.root("group", "group1"));
group1 = new GroupDescriptor(UniqueId.root("group", "group1"));
engineDescriptor.addChild(group1);
var group2 = new GroupDescriptor(UniqueId.root("group", "group2"));
engineDescriptor.addChild(group2);
var group11 = new GroupDescriptor(UniqueId.root("group", "group1-1"));
group11 = new GroupDescriptor(UniqueId.root("group", "group1-1"));
group1.addChild(group11);

group1.addChild(new LeafDescriptor(UniqueId.root("leaf", "leaf1-1")));
group1.addChild(new LeafDescriptor(UniqueId.root("leaf", "leaf1-2")));

group2.addChild(new LeafDescriptor(UniqueId.root("leaf", "leaf2-1")));

group11.addChild(new LeafDescriptor(UniqueId.root("leaf", "leaf11-1")));
leaf111 = new LeafDescriptor(UniqueId.root("leaf", "leaf11-1"));
group11.addChild(leaf111);
}

@Test
Expand Down Expand Up @@ -133,6 +138,27 @@ void pruneGroup() {
assertFalse(visited.contains(UniqueId.root("group", "group1")));
}

@Test
void getAncestors() {
assertThat(getAncestorsUniqueIds(engineDescriptor)).isEmpty();

assertThat(getAncestorsUniqueIds(group1)).containsExactly( //
UniqueId.forEngine("testEngine"));

assertThat(getAncestorsUniqueIds(group11)).containsExactly( //
UniqueId.root("group", "group1"), //
UniqueId.forEngine("testEngine"));

assertThat(getAncestorsUniqueIds(leaf111)).containsExactly( //
UniqueId.root("group", "group1-1"), //
UniqueId.root("group", "group1"), //
UniqueId.forEngine("testEngine"));
}

private List<UniqueId> getAncestorsUniqueIds(TestDescriptor descriptor) {
return descriptor.getAncestors().stream().map(TestDescriptor::getUniqueId).toList();
}

}

class GroupDescriptor extends AbstractTestDescriptor {
Expand Down

0 comments on commit 1381a3d

Please sign in to comment.