Skip to content

Commit 20d2298

Browse files
committed
Add back public CategoryFilter constructor that was removed in JUnit 4.12.
1 parent 6f68556 commit 20d2298

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/main/java/org/junit/experimental/categories/Categories.java

+22-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.lang.annotation.Retention;
44
import java.lang.annotation.RetentionPolicy;
5+
import java.util.Arrays;
56
import java.util.Collections;
67
import java.util.HashSet;
78
import java.util.Set;
@@ -120,7 +121,7 @@ public static CategoryFilter include(boolean matchAny, Class<?>... categories) {
120121
if (hasNull(categories)) {
121122
throw new NullPointerException("has null category");
122123
}
123-
return categoryFilter(matchAny, createSet(categories), true, null);
124+
return new CategoryFilter(matchAny, categories, true, null);
124125
}
125126

126127
public static CategoryFilter include(Class<?> category) {
@@ -135,7 +136,7 @@ public static CategoryFilter exclude(boolean matchAny, Class<?>... categories) {
135136
if (hasNull(categories)) {
136137
throw new NullPointerException("has null category");
137138
}
138-
return categoryFilter(true, null, matchAny, createSet(categories));
139+
return new CategoryFilter(true, null, matchAny, categories);
139140
}
140141

141142
public static CategoryFilter exclude(Class<?> category) {
@@ -151,14 +152,27 @@ public static CategoryFilter categoryFilter(boolean matchAnyInclusions, Set<Clas
151152
return new CategoryFilter(matchAnyInclusions, inclusions, matchAnyExclusions, exclusions);
152153
}
153154

155+
@Deprecated
156+
public CategoryFilter(Class<?> includedCategory, Class<?> excludedCategory) {
157+
this(true, createSet(includedCategory), true, createSet(excludedCategory));
158+
}
159+
154160
protected CategoryFilter(boolean matchAnyIncludes, Set<Class<?>> includes,
155-
boolean matchAnyExcludes, Set<Class<?>> excludes) {
161+
boolean matchAnyExcludes, Set<Class<?>> excludes) {
156162
includedAny = matchAnyIncludes;
157163
excludedAny = matchAnyExcludes;
158164
included = copyAndRefine(includes);
159165
excluded = copyAndRefine(excludes);
160166
}
161167

168+
private CategoryFilter(boolean matchAnyIncludes, Class<?>[] inclusions,
169+
boolean matchAnyExcludes, Class<?>[] exclusions) {
170+
includedAny = matchAnyIncludes;
171+
excludedAny = matchAnyExcludes;
172+
included = createSet(inclusions);
173+
excluded = createSet(exclusions);
174+
}
175+
162176
/**
163177
* @see #toString()
164178
*/
@@ -347,10 +361,11 @@ private static boolean hasAssignableTo(Set<Class<?>> assigns, Class<?> to) {
347361
}
348362

349363
private static Set<Class<?>> createSet(Class<?>... t) {
350-
final Set<Class<?>> set= new HashSet<Class<?>>();
351-
if (t != null) {
352-
Collections.addAll(set, t);
364+
if (t == null || t.length == 0) {
365+
return Collections.emptySet();
366+
} else if (t.length == 1) {
367+
return Collections.<Class<?>>singleton(t[0]);
353368
}
354-
return set;
369+
return new HashSet<Class<?>>(Arrays.asList(t));
355370
}
356371
}

src/test/java/org/junit/experimental/categories/CategoryTest.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,32 @@ public static class TestSuiteWithNoCategories {
179179
}
180180

181181
@Test
182-
public void testCountWithExplicitFilter() throws Throwable {
182+
public void testCountWithExplicitIncludeFilter() throws Throwable {
183183
CategoryFilter include = CategoryFilter.include(SlowTests.class);
184184
Request baseRequest = Request.aClass(TestSuiteWithNoCategories.class);
185185
Result result = new JUnitCore().run(baseRequest.filterWith(include));
186186
assertTrue(result.wasSuccessful());
187187
assertEquals(2, result.getRunCount());
188188
}
189189

190+
@Test
191+
public void testCountWithExplicitExcludeFilter() throws Throwable {
192+
CategoryFilter include = CategoryFilter.exclude(SlowTests.class);
193+
Request baseRequest = Request.aClass(TestSuiteWithNoCategories.class);
194+
Result result = new JUnitCore().run(baseRequest.filterWith(include));
195+
assertEquals(2, result.getFailureCount());
196+
assertEquals(2, result.getRunCount());
197+
}
198+
199+
@Test
200+
public void testCountWithExplicitExcludeFilter_usingConstructor() throws Throwable {
201+
CategoryFilter include = new CategoryFilter(null, SlowTests.class);
202+
Request baseRequest = Request.aClass(TestSuiteWithNoCategories.class);
203+
Result result = new JUnitCore().run(baseRequest.filterWith(include));
204+
assertEquals(2, result.getFailureCount());
205+
assertEquals(2, result.getRunCount());
206+
}
207+
190208
@Test
191209
public void categoryFilterLeavesOnlyMatchingMethods()
192210
throws InitializationError, NoTestsRemainException {
@@ -196,6 +214,15 @@ public void categoryFilterLeavesOnlyMatchingMethods()
196214
assertEquals(1, runner.testCount());
197215
}
198216

217+
@Test
218+
public void categoryFilterLeavesOnlyMatchingMethods_usingConstructor()
219+
throws InitializationError, NoTestsRemainException {
220+
CategoryFilter filter = new CategoryFilter(SlowTests.class, null);
221+
BlockJUnit4ClassRunner runner = new BlockJUnit4ClassRunner(A.class);
222+
filter.apply(runner);
223+
assertEquals(1, runner.testCount());
224+
}
225+
199226
public static class OneFastOneSlow {
200227
@Category(FastTests.class)
201228
@Test

0 commit comments

Comments
 (0)