Skip to content

Commit

Permalink
Clean up filter logic in JUnit5 Runner
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Jan 14, 2016
1 parent 7cca048 commit 001747b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
Expand Up @@ -201,22 +201,23 @@ private <A extends Annotation, V> V getValueFromAnnotation(Class<A> annotationCl


@Override @Override
public void filter(Filter filter) throws NoTestsRemainException { public void filter(Filter filter) throws NoTestsRemainException {
Set<TestIdentifier> identifiers = this.testTree.getByDescription(filter); Set<TestIdentifier> filteredIdentifiers = testTree.getFilteredLeaves(filter);
System.out.println("original identifiers: "); if (filteredIdentifiers.isEmpty()) {
identifiers.forEach(System.out::println);
if (!identifiers.isEmpty()) {
identifiers.removeIf(testIdentifier -> identifiers.stream().anyMatch(
acceptedIdentifier -> testTree.getTestPlan().getDescendants(testIdentifier).contains(
acceptedIdentifier)));
System.out.println("filtered identifiers: ");
identifiers.forEach(System.out::println);
List<TestPlanSpecificationElement> elements = identifiers.stream().map(TestIdentifier::getUniqueId).map(
Object::toString).map(TestPlanSpecification::forUniqueId).collect(toList());
this.specification = TestPlanSpecification.build(elements);
this.testTree = generateTestTree(testClass);
}
else
throw new NoTestsRemainException(); throw new NoTestsRemainException();
}
this.specification = createTestPlanSpecificationForUniqueIds(filteredIdentifiers);
this.testTree = generateTestTree(testClass);
}

private TestPlanSpecification createTestPlanSpecificationForUniqueIds(Set<TestIdentifier> testIdentifiers) {
// @formatter:off
List<TestPlanSpecificationElement> elements = testIdentifiers.stream()
.map(TestIdentifier::getUniqueId)
.map(Object::toString)
.map(TestPlanSpecification::forUniqueId)
.collect(toList());
// @formatter:on
return TestPlanSpecification.build(elements);
} }


} }
Expand Up @@ -16,6 +16,7 @@
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate;


import org.junit.gen5.launcher.TestIdentifier; import org.junit.gen5.launcher.TestIdentifier;
import org.junit.gen5.launcher.TestPlan; import org.junit.gen5.launcher.TestPlan;
Expand All @@ -36,10 +37,6 @@ class JUnit5TestTree {
this.suiteDescription = generateDescription(plan, testClass); this.suiteDescription = generateDescription(plan, testClass);
} }


public TestPlan getTestPlan() {
return plan;
}

Description getSuiteDescription() { Description getSuiteDescription() {
return this.suiteDescription; return this.suiteDescription;
} }
Expand Down Expand Up @@ -77,9 +74,30 @@ private Description createJUnit4Description(TestIdentifier identifier, TestPlan
} }
} }


Set<TestIdentifier> getByDescription(Filter filter) { Set<TestIdentifier> getFilteredLeaves(Filter filter) {
return descriptions.entrySet().stream().filter(entry -> filter.shouldRun(entry.getValue())).map( Set<TestIdentifier> identifiers = applyFilterToDescriptions(filter);
Entry::getKey).collect(toSet()); return removeNonLeafIdentifiers(identifiers);
}

private Set<TestIdentifier> removeNonLeafIdentifiers(Set<TestIdentifier> identifiers) {
return identifiers.stream().filter(isALeaf(identifiers)).collect(toSet());
}

private Predicate<? super TestIdentifier> isALeaf(Set<TestIdentifier> identifiers) {
return testIdentifier -> {
Set<TestIdentifier> descendants = plan.getDescendants(testIdentifier);
return identifiers.stream().noneMatch(descendants::contains);
};
}

private Set<TestIdentifier> applyFilterToDescriptions(Filter filter) {
// @formatter:off
return descriptions.entrySet()
.stream()
.filter(entry -> filter.shouldRun(entry.getValue()))
.map(Entry::getKey)
.collect(toSet());
// @formatter:on
} }


} }

0 comments on commit 001747b

Please sign in to comment.