aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/junitparams/internal/TestMethod.java
blob: d0188efdfc00dec7397f2eecc28eeafc1857a54b (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package junitparams.internal;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.TestClass;

import junitparams.internal.annotation.FrameworkMethodAnnotations;
import junitparams.internal.parameters.ParametersReader;
import junitparams.naming.MacroSubstitutionNamingStrategy;
import junitparams.naming.TestCaseNamingStrategy;

/**
 * A wrapper for a test method
 *
 * @author Pawel Lipinski
 */
public class TestMethod {
    private FrameworkMethod frameworkMethod;
    private FrameworkMethodAnnotations frameworkMethodAnnotations;
    private Class<?> testClass;
    private ParametersReader parametersReader;
    private Object[] cachedParameters;
    private TestCaseNamingStrategy namingStrategy;
    private DescribableFrameworkMethod describableFrameworkMethod;

    public TestMethod(FrameworkMethod method, TestClass testClass) {
        this.frameworkMethod = method;
        this.testClass = testClass.getJavaClass();
        frameworkMethodAnnotations = new FrameworkMethodAnnotations(method);
        parametersReader = new ParametersReader(testClass(), frameworkMethod);

        namingStrategy = new MacroSubstitutionNamingStrategy(this);
    }

    public String name() {
        return frameworkMethod.getName();
    }

    public static List<FrameworkMethod> listFrom(TestClass testClass) {
        List<FrameworkMethod> methods = new ArrayList<FrameworkMethod>();

        for (FrameworkMethod frameworkMethod : testClass.getAnnotatedMethods(Test.class)) {
            TestMethod testMethod = new TestMethod(frameworkMethod, testClass);
            methods.add(testMethod.describableFrameworkMethod());
        }

        return methods;
    }

    @Override
    public int hashCode() {
        return frameworkMethod.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return (obj instanceof TestMethod)
                && hasTheSameNameAsFrameworkMethod((TestMethod) obj)
                && hasTheSameParameterTypesAsFrameworkMethod((TestMethod) obj);
    }

    private boolean hasTheSameNameAsFrameworkMethod(TestMethod testMethod) {
        return frameworkMethod.getName().equals(testMethod.frameworkMethod.getName());
    }

    private boolean hasTheSameParameterTypesAsFrameworkMethod(TestMethod testMethod) {
        Class<?>[] frameworkMethodParameterTypes = frameworkMethod.getMethod().getParameterTypes();
        Class<?>[] testMethodParameterTypes = testMethod.frameworkMethod.getMethod().getParameterTypes();
        return Arrays.equals(frameworkMethodParameterTypes, testMethodParameterTypes);
    }

    private Class<?> testClass() {
        return testClass;
    }

    private boolean isIgnored() {
        return hasIgnoredAnnotation() || hasNoParameters();
    }

    private boolean hasIgnoredAnnotation() {
        return frameworkMethodAnnotations.hasAnnotation(Ignore.class);
    }

    private boolean hasNoParameters() {
       return isParameterised() && parametersSets().length == 0;
    }

    private boolean isNotIgnored() {
        return !isIgnored();
    }

    public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
        return frameworkMethodAnnotations.getAnnotation(annotationType);
    }

    private Description getDescription(Object[] params, int i) {
        Object paramSet = params[i];
        String name = namingStrategy.getTestCaseName(i, paramSet);
        String uniqueMethodId = Utils.uniqueMethodId(i, paramSet, name());

        return Description.createTestDescription(testClass().getName(), name, uniqueMethodId);
    }

    DescribableFrameworkMethod describableFrameworkMethod() {
        if (describableFrameworkMethod == null) {
            Description baseDescription = Description.createTestDescription(
                    testClass, name(), frameworkMethodAnnotations.allAnnotations());
            Method method = frameworkMethod.getMethod();
            try {
                describableFrameworkMethod =
                        createDescribableFrameworkMethod(method, baseDescription);
            } catch (IllegalStateException e) {
                // Defer error until running.
                describableFrameworkMethod =
                        new DeferredErrorFrameworkMethod(method, baseDescription, e);
            }
        }

        return describableFrameworkMethod;
    }

    private DescribableFrameworkMethod createDescribableFrameworkMethod(Method method, Description baseDescription) {
        if (isParameterised()) {
            if (isNotIgnored()) {
                Object[] parametersSets = parametersSets();
                List<InstanceFrameworkMethod> methods
                        = new ArrayList<InstanceFrameworkMethod>();
                for (int i = 0; i < parametersSets.length; i++) {
                    Object parametersSet = parametersSets[i];
                    Description description = getDescription(parametersSets, i);
                    methods.add(new InstanceFrameworkMethod(
                            method, baseDescription.childlessCopy(),
                            description, parametersSet));
                }

                return new ParameterisedFrameworkMethod(method, baseDescription, methods);
            }

            warnIfNoParamsGiven();
        } else {
            verifyMethodCanBeRunByStandardRunner(frameworkMethod);
        }

        // The method to use if it was ignored or was parameterized but had no parameters.
        return new NonParameterisedFrameworkMethod(method, baseDescription, isIgnored());
    }

    private void verifyMethodCanBeRunByStandardRunner(FrameworkMethod method) {
        List<Throwable> errors = new ArrayList<Throwable>();
        method.validatePublicVoidNoArg(false, errors);
        if (!errors.isEmpty()) {
            throw new RuntimeException(errors.get(0));
        }
    }

    public Object[] parametersSets() {
        if (cachedParameters == null) {
            cachedParameters = parametersReader.read();
        }
        return cachedParameters;
    }

    private void warnIfNoParamsGiven() {
        if (isNotIgnored() && isParameterised() && parametersSets().length == 0)
            System.err.println("Method " + name() + " gets empty list of parameters, so it's being ignored!");
    }

    public FrameworkMethod frameworkMethod() {
        return frameworkMethod;
    }

    boolean isParameterised() {
        return frameworkMethodAnnotations.isParametrised();
    }
}