Skip to content

Commit

Permalink
Introduce TestDescriptor.mayRegisterTests()
Browse files Browse the repository at this point in the history
Fixes #508.
  • Loading branch information
marcphilipp committed Jun 18, 2017
1 parent 936a919 commit 4752af0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
5 changes: 4 additions & 1 deletion documentation/src/docs/asciidoc/release-notes-5.0.0-M5.adoc
Expand Up @@ -65,7 +65,8 @@ is placed on the Java 9 module path.
- `junit-platform-commons`: `ReflectionUtils.findAllClassesInClasspathRoot(Path, Predicate, Predicate)` - `junit-platform-commons`: `ReflectionUtils.findAllClassesInClasspathRoot(Path, Predicate, Predicate)`
* The `isLeaf()` method of the `org.junit.platform.engine.support.hierarchical.Node` * The `isLeaf()` method of the `org.junit.platform.engine.support.hierarchical.Node`
interface has been removed. interface has been removed.
* The default methods `prune()` and `pruneTree()` have been removed from `TestDescriptor`. * The default methods `prune()`, `pruneTree()`, and `hasTests()` have been removed from
`TestDescriptor`.
* The `DefaultLauncher` no longer prunes trees of `TestDescriptors` it receives from test * The `DefaultLauncher` no longer prunes trees of `TestDescriptors` it receives from test
engines. It's the responsibility of test engines to only return `TestDescriptors` they engines. It's the responsibility of test engines to only return `TestDescriptors` they
wish to execute. wish to execute.
Expand All @@ -85,6 +86,8 @@ is placed on the Java 9 module path.
* When a `TestEngine` is discovered via Java's `ServiceLoader` mechanism, an attempt * When a `TestEngine` is discovered via Java's `ServiceLoader` mechanism, an attempt
will now be made to determine the location from which the engine class was loaded, will now be made to determine the location from which the engine class was loaded,
and if the location URL is available, it will be logged at configuration-level. and if the location URL is available, it will be logged at configuration-level.
* The `mayRegisterTests()` method may be used to signal that a `TestDescriptor` will
register dynamic tests during execution.




[[release-notes-5.0.0-m5-junit-jupiter]] [[release-notes-5.0.0-m5-junit-jupiter]]
Expand Down
Expand Up @@ -55,7 +55,7 @@ public Type getType() {
} }


@Override @Override
public boolean hasTests() { public boolean mayRegisterTests() {
return true; return true;
} }


Expand Down
Expand Up @@ -47,7 +47,7 @@ public Type getType() {
} }


@Override @Override
public boolean hasTests() { public boolean mayRegisterTests() {
return true; return true;
} }


Expand Down
Expand Up @@ -13,6 +13,7 @@
import static org.junit.platform.commons.meta.API.Usage.Experimental; import static org.junit.platform.commons.meta.API.Usage.Experimental;
import static org.junit.platform.commons.util.ReflectionUtils.findAllClassesInClasspathRoot; import static org.junit.platform.commons.util.ReflectionUtils.findAllClassesInClasspathRoot;
import static org.junit.platform.commons.util.ReflectionUtils.findAllClassesInPackage; import static org.junit.platform.commons.util.ReflectionUtils.findAllClassesInPackage;
import static org.junit.platform.engine.TestDescriptor.containsTests;
import static org.junit.platform.engine.support.filter.ClasspathScanningSupport.buildClassNamePredicate; import static org.junit.platform.engine.support.filter.ClasspathScanningSupport.buildClassNamePredicate;


import java.util.HashSet; import java.util.HashSet;
Expand Down Expand Up @@ -67,12 +68,11 @@ public void resolveSelectors(EngineDiscoveryRequest request, TestDescriptor engi
pruneTree(engineDescriptor); pruneTree(engineDescriptor);
} }


private void pruneTree(TestDescriptor engineDescriptor) { private void pruneTree(TestDescriptor rootDescriptor) {
engineDescriptor.accept(testDescriptor -> { rootDescriptor.accept(testDescriptor -> {
if (testDescriptor.isRoot() || testDescriptor.hasTests()) { if (!testDescriptor.isRoot() && !containsTests(testDescriptor)) {
return; testDescriptor.removeFromHierarchy();
} }
testDescriptor.removeFromHierarchy();
}); });
} }


Expand Down
Expand Up @@ -185,14 +185,22 @@ default boolean isTest() {
} }


/** /**
* Determine if this descriptor or any of its descendants describes a test. * Determine if this descriptor may register dynamic tests during execution.
* *
* <p>The default implementation returns {@code true} if {@link #isTest()} * <p>The default implementation assumes tests are usually known during
* returns {@code true} and otherwise recurses through this descriptor's * discovery and thus returns {@code false}.
* {@linkplain #getChildren() children} to determine if they have tests.
*/ */
default boolean hasTests() { default boolean mayRegisterTests() {
return isTest() || getChildren().stream().anyMatch(TestDescriptor::hasTests); return false;
}

/**
* Determine if the supplied descriptor or any of its descendants contains
* any tests.
*/
static boolean containsTests(TestDescriptor testDescriptor) {
return testDescriptor.isTest() || testDescriptor.mayRegisterTests()
|| testDescriptor.getChildren().stream().anyMatch(TestDescriptor::containsTests);
} }


/** /**
Expand Down
Expand Up @@ -47,7 +47,7 @@ public Type getType() {
} }


@Override @Override
public boolean hasTests() { public boolean mayRegisterTests() {
return true; return true;
} }


Expand Down

0 comments on commit 4752af0

Please sign in to comment.