1aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffinpackage org.junit.runner;
2aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
3aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffinimport org.junit.internal.Classes;
4aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffinimport org.junit.runner.FilterFactory.FilterNotCreatedException;
5aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffinimport org.junit.runner.manipulation.Filter;
6aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
7aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin/**
8aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin * Utility class whose methods create a {@link FilterFactory}.
9aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin */
10aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffinclass FilterFactories {
11aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    /**
12aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     * Creates a {@link Filter}.
13aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     *
14aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     * A filter specification is of the form "package.of.FilterFactory=args-to-filter-factory" or
15aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     * "package.of.FilterFactory".
16aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     *
17aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     * @param request the request that will be filtered
18aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     * @param filterSpec the filter specification
19aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     * @throws org.junit.runner.FilterFactory.FilterNotCreatedException
20aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     */
21aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    public static Filter createFilterFromFilterSpec(Request request, String filterSpec)
22aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            throws FilterFactory.FilterNotCreatedException {
23aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        Description topLevelDescription = request.getRunner().getDescription();
24aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        String[] tuple;
25aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
26aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        if (filterSpec.contains("=")) {
27aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            tuple = filterSpec.split("=", 2);
28aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        } else {
29aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            tuple = new String[]{ filterSpec, "" };
30aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        }
31aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
32aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        return createFilter(tuple[0], new FilterFactoryParams(topLevelDescription, tuple[1]));
33aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
34aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
35aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    /**
36aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     * Creates a {@link Filter}.
37aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     *
38aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     * @param filterFactoryFqcn The fully qualified class name of the {@link FilterFactory}
39aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     * @param params The arguments to the {@link FilterFactory}
40aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     */
41aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    public static Filter createFilter(String filterFactoryFqcn, FilterFactoryParams params)
42aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            throws FilterFactory.FilterNotCreatedException {
43aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        FilterFactory filterFactory = createFilterFactory(filterFactoryFqcn);
44aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
45aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        return filterFactory.createFilter(params);
46aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
47aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
48aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    /**
49aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     * Creates a {@link Filter}.
50aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     *
51aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     * @param filterFactoryClass The class of the {@link FilterFactory}
52aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     * @param params             The arguments to the {@link FilterFactory}
53aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     *
54aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin     */
55aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    public static Filter createFilter(Class<? extends FilterFactory> filterFactoryClass, FilterFactoryParams params)
56aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            throws FilterFactory.FilterNotCreatedException {
57aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        FilterFactory filterFactory = createFilterFactory(filterFactoryClass);
58aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
59aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        return filterFactory.createFilter(params);
60aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
61aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
62aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    static FilterFactory createFilterFactory(String filterFactoryFqcn) throws FilterNotCreatedException {
63aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        Class<? extends FilterFactory> filterFactoryClass;
64aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
65aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        try {
66aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            filterFactoryClass = Classes.getClass(filterFactoryFqcn).asSubclass(FilterFactory.class);
67aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        } catch (Exception e) {
68aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            throw new FilterNotCreatedException(e);
69aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        }
70aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
71aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        return createFilterFactory(filterFactoryClass);
72aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
73aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin
74aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    static FilterFactory createFilterFactory(Class<? extends FilterFactory> filterFactoryClass)
75aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            throws FilterNotCreatedException {
76aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        try {
77aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            return filterFactoryClass.getConstructor().newInstance();
78aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        } catch (Exception e) {
79aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin            throw new FilterNotCreatedException(e);
80aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin        }
81aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin    }
82aeb93fc33cae3aadbb9b46083350ad2dc9aea645Paul Duffin}
83