Skip to content

Commit

Permalink
Provide better names for the test plan specification
Browse files Browse the repository at this point in the history
- Renamed isAccepted() into included()
- Renamed isFiltered() into excluded()

Issue: #116
  • Loading branch information
bechte committed Jan 18, 2016
1 parent a90034d commit 82ff8d2
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 56 deletions.
Expand Up @@ -93,7 +93,7 @@ public boolean acceptDescriptor(TestDescriptor testDescriptor) {
// @formatter:off // @formatter:off
return this.getPostDiscoveryFilters().stream() return this.getPostDiscoveryFilters().stream()
.map(filter -> filter.filter(testDescriptor)) .map(filter -> filter.filter(testDescriptor))
.allMatch(FilterResult::isAccepted); .allMatch(FilterResult::included);
// @formatter:on // @formatter:on
} }


Expand Down
Expand Up @@ -10,8 +10,8 @@


package org.junit.gen5.engine; package org.junit.gen5.engine;


import static org.junit.gen5.engine.FilterResult.accepted; import static org.junit.gen5.engine.FilterResult.excluded;
import static org.junit.gen5.engine.FilterResult.filtered; import static org.junit.gen5.engine.FilterResult.included;


public class EngineIdFilter implements GenericFilter<String> { public class EngineIdFilter implements GenericFilter<String> {
private final String engineId; private final String engineId;
Expand All @@ -23,10 +23,10 @@ public EngineIdFilter(String engineId) {
@Override @Override
public FilterResult filter(String engineId) { public FilterResult filter(String engineId) {
if (this.engineId.equals(engineId)) { if (this.engineId.equals(engineId)) {
return accepted("EngineId matches"); return included("EngineId matches");

This comment has been minimized.

Copy link
@sbrannen

sbrannen Jan 18, 2016

Member

That should read "Engine ID matches".

} }
else { else {
return filtered("EngineId matches"); return excluded("EngineId matches");

This comment has been minimized.

Copy link
@sbrannen

sbrannen Jan 18, 2016

Member

That should read "Engine ID does not match".

} }
} }
} }
Expand Up @@ -21,61 +21,67 @@
*/ */
public class FilterResult { public class FilterResult {
/** /**
* Factory for creating <em>filtered</em> results. * Factory for creating <em>included</em> results.
* *
* @param reason the reason why the result was filtered * @param reason the reason why the result was included
* @return a filtered {@code FilterResult} with the given reason * @return an included {@code FilterResult} with the given reason
*/ */
public static FilterResult filtered(String reason) { public static FilterResult included(String reason) {
return new FilterResult(false, reason); return new FilterResult(true, reason);
} }


/** /**
* Factory for creating <em>active</em> results. * Factory for creating <em>excluded</em> results.
* *
* @param reason the reason why the result was filtered * @param reason the reason why the result was excluded
* @return an accepted {@code FilterResult} with the given reason * @return a excluded {@code FilterResult} with the given reason
*/ */
public static FilterResult accepted(String reason) { public static FilterResult excluded(String reason) {
return new FilterResult(true, reason); return new FilterResult(false, reason);
} }


/** /**
* Factory for creating filter results based on the condition given. * Factory for creating filter results based on the condition given.
* *
* @param isAccepted whether or not the returned {@code FilterResult} should be accepted * @param included whether or not the returned {@code FilterResult} should be included
* @return a valid {@code FilterResult} for the given condition * @return a valid {@code FilterResult} for the given condition
*/ */
public static FilterResult acceptedIf(boolean isAccepted) { public static FilterResult includedIf(boolean included) {
return acceptedIf(isAccepted, null); return includedIf(included, null);
} }


/** /**
* Factory for creating filter results based on the condition given. * Factory for creating filter results based on the condition given.
* *
* @param isAccepted whether or not the returned {@code FilterResult} should be accepted * @param included whether or not the returned {@code FilterResult} should be included
* @param reason the reason why the result was filtered * @param reason the reason for the result
* @return a valid {@code FilterResult} for the given condition * @return a valid {@code FilterResult} for the given condition
*/ */
public static FilterResult acceptedIf(boolean isAccepted, String reason) { public static FilterResult includedIf(boolean included, String reason) {
return isAccepted ? accepted(reason) : filtered(reason); return included ? included(reason) : excluded(reason);
} }


private final boolean accepted; private final boolean included;


private final Optional<String> reason; private final Optional<String> reason;


private FilterResult(boolean isAccepted, String reason) { private FilterResult(boolean included, String reason) {
this.accepted = isAccepted; this.included = included;
this.reason = Optional.ofNullable(reason); this.reason = Optional.ofNullable(reason);
} }


public boolean isAccepted() { /**
return accepted; * @return {@code true} if the filtered object should be included in the test plan
*/
public boolean included() {
return included;
} }


public boolean isDeclined() { /**
return !isAccepted(); * @return {@code true} if the filtered object should be excluded from the test plan
*/
public boolean excluded() {
return !included();
} }


public Optional<String> getReason() { public Optional<String> getReason() {
Expand All @@ -86,7 +92,7 @@ public Optional<String> getReason() {
public String toString() { public String toString() {
// @formatter:off // @formatter:off
return new ToStringBuilder(this) return new ToStringBuilder(this)
.append("accepted", this.accepted) .append("included", this.included)
.append("reason", this.reason) .append("reason", this.reason)
.toString(); .toString();
// @formatter:on // @formatter:on
Expand Down
Expand Up @@ -11,7 +11,7 @@
package org.junit.gen5.engine.specification; package org.junit.gen5.engine.specification;


import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.joining;
import static org.junit.gen5.engine.FilterResult.accepted; import static org.junit.gen5.engine.FilterResult.included;


import java.util.List; import java.util.List;


Expand All @@ -28,14 +28,14 @@ public AllClassFilters(List<ClassFilter> classFilters) {
@Override @Override
public FilterResult filter(Class<?> testClass) { public FilterResult filter(Class<?> testClass) {
if (classFilters == null) { if (classFilters == null) {
return accepted("No filters to be applied on test class"); return included("No filters to be applied on test class");
} }
else { else {
// @formatter:off // @formatter:off
return FilterResult.acceptedIf( return FilterResult.includedIf(
classFilters.stream() classFilters.stream()
.map(filter -> filter.filter(testClass)) .map(filter -> filter.filter(testClass))
.allMatch(FilterResult::isAccepted)); .allMatch(FilterResult::included));
// @formatter:on // @formatter:on
} }
} }
Expand Down
Expand Up @@ -10,8 +10,8 @@


package org.junit.gen5.engine.specification; package org.junit.gen5.engine.specification;


import static org.junit.gen5.engine.FilterResult.accepted; import static org.junit.gen5.engine.FilterResult.excluded;
import static org.junit.gen5.engine.FilterResult.filtered; import static org.junit.gen5.engine.FilterResult.included;


import java.util.regex.Pattern; import java.util.regex.Pattern;


Expand All @@ -28,10 +28,10 @@ public ClassNameFilter(String regex) {
@Override @Override
public FilterResult filter(Class<?> testClass) { public FilterResult filter(Class<?> testClass) {
if (pattern.matcher(testClass.getName()).matches()) { if (pattern.matcher(testClass.getName()).matches()) {
return accepted("TestClass matches name pattern"); return included("TestClass matches name pattern");
} }
else { else {
return filtered("TestClass does not match name pattern"); return excluded("TestClass does not match name pattern");
} }
} }


Expand Down
Expand Up @@ -10,8 +10,8 @@


package org.junit.gen5.engine.specification; package org.junit.gen5.engine.specification;


import static org.junit.gen5.engine.FilterResult.accepted; import static org.junit.gen5.engine.FilterResult.excluded;
import static org.junit.gen5.engine.FilterResult.filtered; import static org.junit.gen5.engine.FilterResult.included;


import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
Expand All @@ -36,10 +36,10 @@ public String toString() {
@Override @Override
public FilterResult filter(Class<?> testClass) { public FilterResult filter(Class<?> testClass) {
if (predicate.test(testClass)) { if (predicate.test(testClass)) {
return accepted("TestClass matches predicate"); return included("TestClass matches predicate");
} }
else { else {
return filtered("TestClass does not match predicate"); return excluded("TestClass does not match predicate");
} }
} }
} }
Expand Up @@ -11,10 +11,10 @@
package org.junit.gen5.engine.specification.dsl; package org.junit.gen5.engine.specification.dsl;


import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.junit.gen5.engine.FilterResult.acceptedIf;


import java.util.List; import java.util.List;


import org.junit.gen5.engine.FilterResult;
import org.junit.gen5.engine.PostDiscoveryFilter; import org.junit.gen5.engine.PostDiscoveryFilter;
import org.junit.gen5.engine.TestTag; import org.junit.gen5.engine.TestTag;


Expand All @@ -25,7 +25,7 @@ public static PostDiscoveryFilter includeTags(String... tagNames) {


public static PostDiscoveryFilter includeTags(List<String> includeTags) { public static PostDiscoveryFilter includeTags(List<String> includeTags) {
// @formatter:off // @formatter:off
return descriptor -> acceptedIf( return descriptor -> FilterResult.includedIf(
descriptor.getTags().stream() descriptor.getTags().stream()
.map(TestTag::getName) .map(TestTag::getName)
.anyMatch(includeTags::contains)); .anyMatch(includeTags::contains));
Expand All @@ -38,7 +38,7 @@ public static PostDiscoveryFilter excludeTags(String... tagNames) {


public static PostDiscoveryFilter excludeTags(List<String> excludeTags) { public static PostDiscoveryFilter excludeTags(List<String> excludeTags) {
// @formatter:off // @formatter:off
return descriptor -> acceptedIf( return descriptor -> FilterResult.includedIf(
descriptor.getTags().stream() descriptor.getTags().stream()
.map(TestTag::getName) .map(TestTag::getName)
.noneMatch(excludeTags::contains)); .noneMatch(excludeTags::contains));
Expand Down
Expand Up @@ -99,7 +99,7 @@ private RootTestDescriptor discoverRootDescriptor(DiscoveryRequest specification
RootTestDescriptor root = new RootTestDescriptor(); RootTestDescriptor root = new RootTestDescriptor();
for (TestEngine testEngine : testEngineRegistry.getTestEngines()) { for (TestEngine testEngine : testEngineRegistry.getTestEngines()) {
if (specification.getEngineIdFilters().stream().map( if (specification.getEngineIdFilters().stream().map(
engineIdFilter -> engineIdFilter.filter(testEngine.getId())).anyMatch(FilterResult::isDeclined)) { engineIdFilter -> engineIdFilter.filter(testEngine.getId())).anyMatch(FilterResult::excluded)) {
LOG.fine( LOG.fine(
() -> String.format("Test discovery for engine '%s' was skipped due to a filter in phase '%s'.", () -> String.format("Test discovery for engine '%s' was skipped due to a filter in phase '%s'.",
testEngine.getId(), phase)); testEngine.getId(), phase));
Expand Down
Expand Up @@ -27,17 +27,17 @@ void classNameMatches() {
ClassFilter filter = ClassFilters.classNameMatches(regex); ClassFilter filter = ClassFilters.classNameMatches(regex);


assertEquals("Filter class names with regular expression: " + regex, filter.toString()); assertEquals("Filter class names with regular expression: " + regex, filter.toString());
assertTrue(filter.filter(String.class).isAccepted()); assertTrue(filter.filter(String.class).included());
assertFalse(filter.filter(Collection.class).isAccepted()); assertFalse(filter.filter(Collection.class).included());
} }


@Test @Test
void anyClass() { void anyClass() {
ClassFilter filter = ClassFilters.anyClass(); ClassFilter filter = ClassFilters.anyClass();


assertEquals("Any class", filter.toString()); assertEquals("Any class", filter.toString());
assertTrue(filter.filter(String.class).isAccepted()); assertTrue(filter.filter(String.class).included());
assertTrue(filter.filter(this.getClass()).isAccepted()); assertTrue(filter.filter(this.getClass()).included());
} }


@Test @Test
Expand All @@ -47,8 +47,8 @@ void allOfWithoutFilter() {
ClassFilter filter = ClassFilters.allOf(noFilters); ClassFilter filter = ClassFilters.allOf(noFilters);


assertEquals("Any class", filter.toString()); assertEquals("Any class", filter.toString());
assertTrue(filter.filter(String.class).isAccepted()); assertTrue(filter.filter(String.class).included());
assertTrue(filter.filter(Object.class).isAccepted()); assertTrue(filter.filter(Object.class).included());
} }


@Test @Test
Expand All @@ -67,8 +67,8 @@ void allOfWithMultipleFiltersIsConjunction() {


ClassFilter filter = ClassFilters.allOf(firstFilter, secondFilter); ClassFilter filter = ClassFilters.allOf(firstFilter, secondFilter);


assertFalse(filter.filter(String.class).isAccepted()); assertFalse(filter.filter(String.class).included());
assertTrue(filter.filter(StringJoiner.class).isAccepted()); assertTrue(filter.filter(StringJoiner.class).included());
} }


@Test @Test
Expand Down
Expand Up @@ -63,7 +63,7 @@ private Set<TestClassRequest> filterAndConvertToTestClassRequests(DiscoveryReque
TestClassCollector collector) { TestClassCollector collector) {
// TODO #40 Log classes that are filtered out // TODO #40 Log classes that are filtered out
ClassFilter classFilter = new AllClassFilters(request.getFilterByType(ClassFilter.class)); ClassFilter classFilter = new AllClassFilters(request.getFilterByType(ClassFilter.class));
return collector.toRequests(testClass -> classFilter.filter(testClass).isAccepted()); return collector.toRequests(testClass -> classFilter.filter(testClass).included());
} }


private void populateEngineDescriptor(Set<TestClassRequest> requests) { private void populateEngineDescriptor(Set<TestClassRequest> requests) {
Expand Down
Expand Up @@ -58,7 +58,7 @@ private void applyEngineFilters(DiscoveryRequest specification, JUnit5EngineDesc
// @formatter:off // @formatter:off
if (classFilters.stream() if (classFilters.stream()
.map(filter -> filter.filter(testClass)) .map(filter -> filter.filter(testClass))
.anyMatch(FilterResult::isDeclined)) { .anyMatch(FilterResult::excluded)) {
remove.run(); remove.run();
} }
// @formatter:on // @formatter:on
Expand Down

0 comments on commit 82ff8d2

Please sign in to comment.