aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/junitparams/internal/ParameterisedFrameworkMethod.java
blob: f593f4f3ccbbb7e69385500f3d67239096b39066 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package junitparams.internal;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;

import org.junit.runner.Description;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.Filterable;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runners.model.FrameworkMethod;

/**
 * A {@link FrameworkMethod} that represents a parameterized method.
 *
 * <p>This contains a list of {@link InstanceFrameworkMethod} that represent the individual
 * instances of this method, one per parameter set.
 */
public class ParameterisedFrameworkMethod extends DescribableFrameworkMethod implements Filterable {

    /**
     * The base description, used as a template when creating {@link Description}.
     */
    private final Description baseDescription;

    /**
     * The list of {@link InstanceFrameworkMethod} that represent individual instances of this
     * method.
     */
    private List<InstanceFrameworkMethod> instanceMethods;

    /**
     * The {@link Description}, created lazily and updated after filtering.
     */
    private Description description;

    public ParameterisedFrameworkMethod(Method method, Description baseDescription,
            List<InstanceFrameworkMethod> instanceMethods) {
        super(method);
        this.baseDescription = baseDescription;
        this.instanceMethods = instanceMethods;
    }

    @Override
    public Description getDescription() {
        if (description == null) {
            description = baseDescription.childlessCopy();
            for (InstanceFrameworkMethod instanceMethod : instanceMethods) {
                description.addChild(instanceMethod.getInstanceDescription());
            }
        }

        return description;
    }

    public List<InstanceFrameworkMethod> getMethods() {
        return instanceMethods;
    }

    @Override
    public void filter(Filter filter) throws NoTestsRemainException {
        int count = instanceMethods.size();
        for (Iterator<InstanceFrameworkMethod> i = instanceMethods.iterator(); i.hasNext(); ) {
            InstanceFrameworkMethod instanceMethod = i.next();
            if (filter.shouldRun(instanceMethod.getInstanceDescription())) {
                try {
                    filter.apply(instanceMethod);
                } catch (NoTestsRemainException e) {
                    i.remove();
                }
            } else {
                i.remove();
            }
        }

        if (instanceMethods.size() != count) {
            // Some instance methods have been filtered out, so invalidate the description.
            description = null;
        }

        if (instanceMethods.isEmpty()) {
            throw new NoTestsRemainException();
        }
    }
}