    001    package org.junit.experimental.categories;
    002
    003    import java.util.HashSet;
    004    import java.util.List;
    005    import java.util.Set;
    006
    007    import org.junit.experimental.categories.Categories.CategoryFilter;
    008    import org.junit.runner.manipulation.Filter;
    009
    010    /**
    011     * {@link org.junit.runner.FilterFactory} to include 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.IncludeCategories=pkg.of.Cat1,pkg.of.Cat2
    019     * </code>
    020     *
    021     * Usage from API:
    022     * <code>
    023     *     new IncludeCategories().createFilter(Cat1.class, Cat2.class);
    024     * </code>
    025     */
    026    public final class IncludeCategories extends CategoryFilterFactory {
    027        /**
    028         * Creates a {@link Filter} which is only passed by tests that are
    029         * 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 IncludesAny(categories);
    036        }
    037
    038        private static class IncludesAny extends CategoryFilter {
    039            public IncludesAny(List<Class<?>> categories) {
    040                this(new HashSet<Class<?>>(categories));
    041            }
    042
    043            public IncludesAny(Set<Class<?>> categories) {
    044                super(true, categories, true, null);
    045            }
    046
    047            @Override
    048            public String describe() {
    049                return "includes " + super.describe();
    050            }
    051        }
    052    }