Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

IncludeCategory and ExcludeCategory should have plural... (#336)

This enables you to specify even more than one category on a suite.
IMHO the Javadoc on Categories is not in conflict with this feature.
  • Loading branch information...
commit 278afcbd875a67823f8f3298da527656d094f149 1 parent 33e3c15
@Tibor17 authored
View
20 src/main/java/org/junit/experimental/categories/Categories.java
@@ -97,12 +97,12 @@
@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 {
@@ -113,15 +113,13 @@ public static CategoryFilter include(Class<?> categoryType) {
}
/**
- * TODO useful method for maven-surefire-plugin
- * public static createCategoryFilter(Set<Class<?>> includes, Set<Class<?>> excludes):CategoryFilter
- * TODO performance improvement includes.removeAll(excludes) unless any null, and use them both anyway
+ * Useful method for e.g. maven-surefire-plugin.
* See the issue #336
* https://github.com/KentBeck/junit/issues/336
- * public static CategoryFilter createCategoryFilter(Set<Class<?>> includes, Set<Class<?>> excludes) {
- * return new CategoryFilter(includes, excludes);
- * }
* */
+ public static CategoryFilter createCategoryFilter(Set<Class<?>> includes, Set<Class<?>> excludes) {
+ return new CategoryFilter(includes, excludes);
+ }
private final Set<Class<?>> fIncluded,//NULL represents 'All' categories without limitation to Included.
fExcluded;//Cannot be null. Empty Set does not exclude categories.
@@ -230,14 +228,12 @@ public Categories(Class<?> klass, RunnerBuilder builder) throws InitializationEr
}
private static Set<Class<?>> getIncludedCategory(Class<?> klass) throws ClassNotFoundException {
- IncludeCategory annotation = klass.getAnnotation(IncludeCategory.class);//FIX for issue #336: IncludeCategory[] annotations
- // see https://github.com/KentBeck/junit/issues/336
+ IncludeCategory annotation = klass.getAnnotation(IncludeCategory.class);
return intersectWithSystemPropertyInclusions(annotation == null ? null : createSet(annotation.value()));
}
private static Set<Class<?>> getExcludedCategory(Class<?> klass) throws ClassNotFoundException {
- ExcludeCategory annotation = klass.getAnnotation(ExcludeCategory.class);//FIX for issue #336: ExcludeCategory[] annotations
- // see https://github.com/KentBeck/junit/issues/336
+ ExcludeCategory annotation = klass.getAnnotation(ExcludeCategory.class);
return unionWithSystemPropertyExclusions(annotation == null ? createSet() : createSet(annotation.value()));
}
View
2  src/test/java/org/junit/tests/AllTests.java
@@ -20,6 +20,7 @@
import org.junit.tests.experimental.categories.CategoriesAndParameterizedTest;
import org.junit.tests.experimental.categories.CategoriesConfiguredBySystemPropertiesTest;
import org.junit.tests.experimental.categories.CategoryTest;
+import org.junit.tests.experimental.categories.MultiCategoryTest;
import org.junit.tests.experimental.max.JUnit38SortingTest;
import org.junit.tests.experimental.max.MaxStarterTest;
import org.junit.tests.experimental.parallel.ParallelClassTest;
@@ -154,6 +155,7 @@
CategoryTest.class,
CategoriesAndParameterizedTest.class,
CategoriesConfiguredBySystemPropertiesTest.class,
+ MultiCategoryTest.class,
ParentRunnerFilteringTest.class,
RuleChainTest.class,
BlockJUnit4ClassRunnerTest.class,
View
74 src/test/java/org/junit/tests/experimental/categories/MultiCategoryTest.java
@@ -0,0 +1,74 @@
+package org.junit.tests.experimental.categories;
+
+import org.junit.Test;
+import org.junit.experimental.categories.Categories;
+import org.junit.experimental.categories.Category;
+import org.junit.experimental.results.PrintableResult;
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+import static org.junit.experimental.results.PrintableResult.testResult;
+import static org.junit.experimental.results.ResultMatchers.isSuccessful;
+import static org.junit.matchers.JUnitMatchers.hasItems;
+
+public final class MultiCategoryTest {
+ public interface A {}
+ public interface B {}
+ public interface C {}
+
+ static enum TestCategory { A, B, C }
+
+ @Test
+ public void runSuite() { shouldRun(MultiCategorySuite.class, TestCategory.A, TestCategory.B); }
+
+ private static void shouldRun(final Class<?> junitTestType, final TestCategory... expectedPlatforms) {
+
+ // Targeting Test:
+ final PrintableResult testResult = testResult(junitTestType);
+
+ final Set<TestCategory> passedTestCases = CategoriesTest.passedTestCases;
+
+ assertThat("unexpected size", passedTestCases.size(), is(equalTo(expectedPlatforms.length)));
+ assertThat(passedTestCases, hasItems(expectedPlatforms));
+
+ passedTestCases.clear();
+
+ assertThat("wrong test modifications, and broken collection of expectations", testResult, isSuccessful());
+ }
+
+ @RunWith(Categories.class)
+ @Categories.IncludeCategory({A.class, B.class})
+ @Categories.ExcludeCategory(C.class)
+ @Suite.SuiteClasses({CategoriesTest.class})
+ public static final class MultiCategorySuite {}
+
+ public static final class CategoriesTest {
+ static final Set<TestCategory> passedTestCases = new HashSet<TestCategory>();
+
+ @Test
+ @Category(A.class)
+ public void a() { passedTestCases.add(TestCategory.A); }
+
+ @Test
+ @Category(B.class)
+ public void b() { passedTestCases.add(TestCategory.B); }
+
+ @Test
+ @Category(C.class)
+ public void c() {
+ fail();
+ }
+
+ @Test
+ public void anything() {
+ fail();
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.