Skip to content

Add public CategoryFilter constructor that was removed in JUnit 4.12 #1403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 44 additions & 25 deletions src/main/java/org/junit/experimental/categories/Categories.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import org.junit.runner.Description;
Expand Down Expand Up @@ -117,10 +119,7 @@ public static class CategoryFilter extends Filter {
private final boolean excludedAny;

public static CategoryFilter include(boolean matchAny, Class<?>... categories) {
if (hasNull(categories)) {
throw new NullPointerException("has null category");
}
return categoryFilter(matchAny, createSet(categories), true, null);
return new CategoryFilter(matchAny, categories, true, null);
}

public static CategoryFilter include(Class<?> category) {
Expand All @@ -132,10 +131,7 @@ public static CategoryFilter include(Class<?>... categories) {
}

public static CategoryFilter exclude(boolean matchAny, Class<?>... categories) {
if (hasNull(categories)) {
throw new NullPointerException("has null category");
}
return categoryFilter(true, null, matchAny, createSet(categories));
return new CategoryFilter(true, null, matchAny, categories);
}

public static CategoryFilter exclude(Class<?> category) {
Expand All @@ -151,14 +147,30 @@ public static CategoryFilter categoryFilter(boolean matchAnyInclusions, Set<Clas
return new CategoryFilter(matchAnyInclusions, inclusions, matchAnyExclusions, exclusions);
}

@Deprecated
public CategoryFilter(Class<?> includedCategory, Class<?> excludedCategory) {
includedAny = true;
excludedAny = true;
included = nullableClassToSet(includedCategory);
excluded = nullableClassToSet(excludedCategory);
}

protected CategoryFilter(boolean matchAnyIncludes, Set<Class<?>> includes,
boolean matchAnyExcludes, Set<Class<?>> excludes) {
boolean matchAnyExcludes, Set<Class<?>> excludes) {
includedAny = matchAnyIncludes;
excludedAny = matchAnyExcludes;
included = copyAndRefine(includes);
excluded = copyAndRefine(excludes);
}

private CategoryFilter(boolean matchAnyIncludes, Class<?>[] inclusions,
boolean matchAnyExcludes, Class<?>[] exclusions) {
includedAny = matchAnyIncludes;
excludedAny = matchAnyExcludes;
included = createSet(inclusions);
excluded = createSet(exclusions);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to remove the duplication in these constructors by having only a single private one that does the assignments?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think doing that would sacrifice readability

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

/**
* @see #toString()
*/
Expand Down Expand Up @@ -284,23 +296,13 @@ private static Class<?>[] directCategories(Description description) {
}

private static Set<Class<?>> copyAndRefine(Set<Class<?>> classes) {
Set<Class<?>> c= new HashSet<Class<?>>();
Set<Class<?>> c= new LinkedHashSet<Class<?>>();
if (classes != null) {
c.addAll(classes);
}
c.remove(null);
return c;
}

private static boolean hasNull(Class<?>... classes) {
if (classes == null) return false;
for (Class<?> clazz : classes) {
if (clazz == null) {
return true;
}
}
return false;
}
}

public Categories(Class<?> klass, RunnerBuilder builder) throws InitializationError {
Expand Down Expand Up @@ -346,11 +348,28 @@ private static boolean hasAssignableTo(Set<Class<?>> assigns, Class<?> to) {
return false;
}

private static Set<Class<?>> createSet(Class<?>... t) {
final Set<Class<?>> set= new HashSet<Class<?>>();
if (t != null) {
Collections.addAll(set, t);
private static Set<Class<?>> createSet(Class<?>[] classes) {
// Not throwing a NPE if t is null is a bad idea, but it's the behavior from JUnit 4.12
// for include(boolean, Class<?>...) and exclude(boolean, Class<?>...)
if (classes == null || classes.length == 0) {
return Collections.emptySet();
}
for (Class<?> category : classes) {
if (category == null) {
throw new NullPointerException("has null category");
}
}
return set;

return classes.length == 1
? Collections.<Class<?>>singleton(classes[0])
: new LinkedHashSet<Class<?>>(Arrays.asList(classes));
}

private static Set<Class<?>> nullableClassToSet(Class<?> nullableClass) {
// Not throwing a NPE if t is null is a bad idea, but it's the behavior from JUnit 4.11
// for CategoryFilter(Class<?> includedCategory, Class<?> excludedCategory)
return nullableClass == null
? Collections.<Class<?>>emptySet()
: Collections.<Class<?>>singleton(nullableClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,32 @@ public static class TestSuiteWithNoCategories {
}

@Test
public void testCountWithExplicitFilter() throws Throwable {
public void testCountWithExplicitIncludeFilter() throws Throwable {
CategoryFilter include = CategoryFilter.include(SlowTests.class);
Request baseRequest = Request.aClass(TestSuiteWithNoCategories.class);
Result result = new JUnitCore().run(baseRequest.filterWith(include));
assertTrue(result.wasSuccessful());
assertEquals(2, result.getRunCount());
}

@Test
public void testCountWithExplicitExcludeFilter() throws Throwable {
CategoryFilter include = CategoryFilter.exclude(SlowTests.class);
Request baseRequest = Request.aClass(TestSuiteWithNoCategories.class);
Result result = new JUnitCore().run(baseRequest.filterWith(include));
assertEquals(2, result.getFailureCount());
assertEquals(2, result.getRunCount());
}

@Test
public void testCountWithExplicitExcludeFilter_usingConstructor() throws Throwable {
CategoryFilter include = new CategoryFilter(null, SlowTests.class);
Request baseRequest = Request.aClass(TestSuiteWithNoCategories.class);
Result result = new JUnitCore().run(baseRequest.filterWith(include));
assertEquals(2, result.getFailureCount());
assertEquals(2, result.getRunCount());
}

@Test
public void categoryFilterLeavesOnlyMatchingMethods()
throws InitializationError, NoTestsRemainException {
Expand All @@ -196,6 +214,15 @@ public void categoryFilterLeavesOnlyMatchingMethods()
assertEquals(1, runner.testCount());
}

@Test
public void categoryFilterLeavesOnlyMatchingMethods_usingConstructor()
throws InitializationError, NoTestsRemainException {
CategoryFilter filter = new CategoryFilter(SlowTests.class, null);
BlockJUnit4ClassRunner runner = new BlockJUnit4ClassRunner(A.class);
filter.apply(runner);
assertEquals(1, runner.testCount());
}

public static class OneFastOneSlow {
@Category(FastTests.class)
@Test
Expand Down