Skip to content

Support for Multiple Include/Exclude Categories #340

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

Closed
wants to merge 5 commits into from
Closed
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
65 changes: 45 additions & 20 deletions src/main/java/org/junit/experimental/categories/Categories.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,47 @@ public class Categories extends Suite {

@Retention(RetentionPolicy.RUNTIME)
public @interface IncludeCategory {
public Class<?> value();
public Class<?>[] value();
}

@Retention(RetentionPolicy.RUNTIME)
public @interface ExcludeCategory {
public Class<?> value();
public Class<?>[] value();
}

public static class CategoryFilter extends Filter {
public static CategoryFilter include(Class<?> categoryType) {
return new CategoryFilter(categoryType, null);
public static CategoryFilter include(Class<?>... categoryTypes) {
return new CategoryFilter(categoryTypes, null);
}

public static CategoryFilter exclude(Class<?>... categoryTypes) {
return new CategoryFilter(null, categoryTypes);
}

private final Class<?> fIncluded;

private final Class<?> fExcluded;
private final Class<?>[] fIncluded;

public CategoryFilter(Class<?> includedCategory,
Class<?> excludedCategory) {
fIncluded= includedCategory;
fExcluded= excludedCategory;
private final Class<?>[] fExcluded;

public CategoryFilter(Class<?>[] includedCategories, Class<?>[] excludedCategories) {
fIncluded= includedCategories;
fExcluded= excludedCategories;
}

@Override
public String describe() {
return "category " + fIncluded;
return ((fIncluded == null || fIncluded.length == 1) ? "category ":"categories ") + join(", ", fIncluded);
}

private String join(String seperator, Class<?>... values) {
if (values == null || values.length == 0)
return "";
StringBuilder sb= new StringBuilder();
for(Class<?> each : values)
sb.append(each.toString()).append(seperator);
return sb.substring(0, sb.length() - seperator.length());
}


@Override
public boolean shouldRun(Description description) {
if (hasCorrectCategoryAnnotation(description))
Expand All @@ -111,11 +124,23 @@ private boolean hasCorrectCategoryAnnotation(Description description) {
List<Class<?>> categories= categories(description);
if (categories.isEmpty())
return fIncluded == null;
for (Class<?> each : categories)
if (fExcluded != null && fExcluded.isAssignableFrom(each))
return categoryListPassesFilters(categories);
}

private boolean categoryListPassesFilters(List<Class<?>> categories) {
boolean hasIncludeCategory= false;
for (Class<?> each: categories) {
if ((fExcluded != null) && classIsAssignableFromClassInArray(each, fExcluded))
return false;
for (Class<?> each : categories)
if (fIncluded == null || fIncluded.isAssignableFrom(each))
hasIncludeCategory= (fIncluded == null) || hasIncludeCategory ||
classIsAssignableFromClassInArray(each, fIncluded);
}
return hasIncludeCategory;
}

private boolean classIsAssignableFromClassInArray(Class<?> clazz, Class<?>[] classes) {
for(Class<?> each: classes)
if (each.isAssignableFrom(clazz))
return true;
return false;
}
Expand Down Expand Up @@ -148,20 +173,20 @@ public Categories(Class<?> klass, RunnerBuilder builder)
throws InitializationError {
super(klass, builder);
try {
filter(new CategoryFilter(getIncludedCategory(klass),
getExcludedCategory(klass)));
filter(new CategoryFilter(getIncludedCategories(klass),
getExcludedCategories(klass)));
} catch (NoTestsRemainException e) {
throw new InitializationError(e);
}
assertNoCategorizedDescendentsOfUncategorizeableParents(getDescription());
}

private Class<?> getIncludedCategory(Class<?> klass) {
private Class<?>[] getIncludedCategories(Class<?> klass) {
IncludeCategory annotation= klass.getAnnotation(IncludeCategory.class);
return annotation == null ? null : annotation.value();
}

private Class<?> getExcludedCategory(Class<?> klass) {
private Class<?>[] getExcludedCategories(Class<?> klass) {
ExcludeCategory annotation= klass.getAnnotation(ExcludeCategory.class);
return annotation == null ? null : annotation.value();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public interface FastTests {
public interface SlowTests {
// category marker
}

public interface ReallySlowTests {
Copy link
Member

Choose a reason for hiding this comment

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

Can you write tests that exercise the multiple-category functionality?

Copy link
Author

Choose a reason for hiding this comment

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

There was already a test that exercised multi-category exclude. I renamed it to make this more evident and also added another that tests multi-category includes.

see testCountWithMultipleExcludeFilter and testCountWithMultipleIncludeFilter

// category marker
}

public static class A {
@Test
Expand Down Expand Up @@ -133,6 +137,24 @@ public void testCountWithExplicitFilter() throws Throwable {
assertTrue(result.wasSuccessful());
assertEquals(2, result.getRunCount());
}

@Test
public void testCountWithMultipleExcludeFilter() throws Throwable {
CategoryFilter exclude = CategoryFilter.exclude(SlowTests.class, FastTests.class);
Request baseRequest= Request.aClass(OneOfEach.class);
Result result= new JUnitCore().run(baseRequest.filterWith(exclude));
assertTrue(result.wasSuccessful());
assertEquals(1, result.getRunCount());
}

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

@Test
public void categoryFilterLeavesOnlyMatchingMethods()
Expand All @@ -143,7 +165,7 @@ public void categoryFilterLeavesOnlyMatchingMethods()
assertEquals(1, runner.testCount());
}

public static class OneFastOneSlow {
public static class OneOfEach {
@Category(FastTests.class)
@Test
public void a() {
Expand All @@ -155,14 +177,20 @@ public void a() {
public void b() {

}

@Category(ReallySlowTests.class)
@Test
public void c() {

}
}

@Test
public void categoryFilterRejectsIncompatibleCategory()
throws InitializationError, NoTestsRemainException {
CategoryFilter filter= CategoryFilter.include(SlowTests.class);
BlockJUnit4ClassRunner runner= new BlockJUnit4ClassRunner(
OneFastOneSlow.class);
OneOfEach.class);
filter.apply(runner);
assertEquals(1, runner.testCount());
}
Expand Down Expand Up @@ -194,6 +222,12 @@ public void describeACategoryFilter() {
assertEquals("category " + SlowTests.class, filter.describe());
}

@Test
public void describeAMultiCategoryFilter() {
CategoryFilter filter= CategoryFilter.include(SlowTests.class, FastTests.class);
assertEquals("categories " + SlowTests.class + ", " + FastTests.class, filter.describe());
}

public static class OneThatIsBothFastAndSlow {
@Category({FastTests.class, SlowTests.class})
@Test
Expand Down