001package org.junit.experimental.categories;
002
003import java.util.HashSet;
004import java.util.List;
005import java.util.Set;
006
007import org.junit.experimental.categories.Categories.CategoryFilter;
008import org.junit.runner.manipulation.Filter;
009
010/**
011 * {@link org.junit.runner.FilterFactory} to exclude categories.
012 *
013 * The {@link Filter} that is created will filter out tests that are categorized with any of the
014 * given categories.
015 *
016 * Usage from command line:
017 * <code>
018 *     --filter=org.junit.experimental.categories.ExcludeCategories=pkg.of.Cat1,pkg.of.Cat2
019 * </code>
020 *
021 * Usage from API:
022 * <code>
023 *     new ExcludeCategories().createFilter(Cat1.class, Cat2.class);
024 * </code>
025 */
026public final class ExcludeCategories extends CategoryFilterFactory {
027    /**
028     * Creates a {@link Filter} which is only passed by tests that are
029     * not categorized with any of the specified categories.
030     *
031     * @param categories Category classes.
032     */
033    @Override
034    protected Filter createFilter(List<Class<?>> categories) {
035        return new ExcludesAny(categories);
036    }
037
038    private static class ExcludesAny extends CategoryFilter {
039        public ExcludesAny(List<Class<?>> categories) {
040            this(new HashSet<Class<?>>(categories));
041        }
042
043        public ExcludesAny(Set<Class<?>> categories) {
044            super(true, null, true, categories);
045        }
046
047        @Override
048        public String describe() {
049            return "excludes " + super.describe();
050        }
051    }
052}