aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/internal/runners/MethodValidator.java
blob: ba9c9d1546badd1ddcd7508b87f4589b3860c284 (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
package org.junit.internal.runners;

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

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runners.BlockJUnit4ClassRunner;

/**
 * @deprecated Included for backwards compatibility with JUnit 4.4. Will be
 *             removed in the next major release. Please use
 *             {@link BlockJUnit4ClassRunner} in place of {@link JUnit4ClassRunner}.
 */
@Deprecated
public class MethodValidator {

    private final List<Throwable> errors = new ArrayList<Throwable>();

    private TestClass testClass;

    public MethodValidator(TestClass testClass) {
        this.testClass = testClass;
    }

    public void validateInstanceMethods() {
        validateTestMethods(After.class, false);
        validateTestMethods(Before.class, false);
        validateTestMethods(Test.class, false);

        List<Method> methods = testClass.getAnnotatedMethods(Test.class);
        if (methods.size() == 0) {
            errors.add(new Exception("No runnable methods"));
        }
    }

    public void validateStaticMethods() {
        validateTestMethods(BeforeClass.class, true);
        validateTestMethods(AfterClass.class, true);
    }

    public List<Throwable> validateMethodsForDefaultRunner() {
        validateNoArgConstructor();
        validateStaticMethods();
        validateInstanceMethods();
        return errors;
    }

    public void assertValid() throws InitializationError {
        if (!errors.isEmpty()) {
            throw new InitializationError(errors);
        }
    }

    public void validateNoArgConstructor() {
        try {
            testClass.getConstructor();
        } catch (Exception e) {
            errors.add(new Exception("Test class should have public zero-argument constructor", e));
        }
    }

    private void validateTestMethods(Class<? extends Annotation> annotation,
            boolean isStatic) {
        List<Method> methods = testClass.getAnnotatedMethods(annotation);

        for (Method each : methods) {
            if (Modifier.isStatic(each.getModifiers()) != isStatic) {
                String state = isStatic ? "should" : "should not";
                errors.add(new Exception("Method " + each.getName() + "() "
						+ state + " be static"));
            }
            if (!Modifier.isPublic(each.getDeclaringClass().getModifiers())) {
                errors.add(new Exception("Class " + each.getDeclaringClass().getName()
						+ " should be public"));
            }
            if (!Modifier.isPublic(each.getModifiers())) {
                errors.add(new Exception("Method " + each.getName()
						+ " should be public"));
            }
            if (each.getReturnType() != Void.TYPE) {
                errors.add(new Exception("Method " + each.getName()
						+ " should be void"));
            }
            if (each.getParameterTypes().length != 0) {
                errors.add(new Exception("Method " + each.getName()
						+ " should have no parameters"));
            }
        }
    }
}