/
JUnit4TestNotRun.java
218 lines (201 loc) · 8.52 KB
/
JUnit4TestNotRun.java
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Copyright 2013 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.matchers.JUnitMatchers.containsTestMethod;
import static com.google.errorprone.matchers.JUnitMatchers.isJUnit4TestClass;
import static com.google.errorprone.matchers.JUnitMatchers.isJunit3TestCase;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.hasModifier;
import static com.google.errorprone.matchers.Matchers.methodReturns;
import static com.google.errorprone.matchers.Matchers.not;
import static com.google.errorprone.suppliers.Suppliers.VOID_TYPE;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.getType;
import static com.google.errorprone.util.ASTHelpers.isSameType;
import static javax.lang.model.element.Modifier.PUBLIC;
import static javax.lang.model.element.Modifier.STATIC;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.JUnitMatchers;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.suppliers.Supplier;
import com.google.errorprone.suppliers.Suppliers;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.lang.model.element.Modifier;
/**
* @author eaftan@google.com (Eddie Aftandilian)
*/
@BugPattern(
summary =
"This looks like a test method but is not run; please add @Test and @Ignore, or, if this"
+ " is a helper method, reduce its visibility.",
severity = ERROR)
public class JUnit4TestNotRun extends BugChecker implements ClassTreeMatcher {
private static final String TEST_CLASS = "org.junit.Test";
private static final String IGNORE_CLASS = "org.junit.Ignore";
private static final String TEST_ANNOTATION = "@Test ";
private static final String IGNORE_ANNOTATION = "@Ignore ";
private final Matcher<MethodTree> possibleTestMethod =
allOf(
hasModifier(PUBLIC),
methodReturns(VOID_TYPE),
(t, s) ->
t.getParameters().stream()
.allMatch(
v ->
v.getModifiers().getAnnotations().stream()
.anyMatch(a -> isParameterAnnotation(a, s))),
not(JUnitMatchers::hasJUnitAnnotation));
private boolean isParameterAnnotation(AnnotationTree annotation, VisitorState state) {
Type annotationType = getType(annotation);
if (isSameType(annotationType, FROM_DATA_POINTS.get(state), state)) {
return true;
}
return false;
}
private static final Supplier<Type> FROM_DATA_POINTS =
Suppliers.typeFromString("org.junit.experimental.theories.FromDataPoints");
private static final Matcher<Tree> NOT_STATIC = not(hasModifier(STATIC));
@Override
public Description matchClass(ClassTree tree, VisitorState state) {
if (!isJUnit4TestClass.matches(tree, state)) {
return NO_MATCH;
}
Map<MethodSymbol, MethodTree> suspiciousMethods = new HashMap<>();
for (Tree member : tree.getMembers()) {
if (!(member instanceof MethodTree) || isSuppressed(member, state)) {
continue;
}
MethodTree methodTree = (MethodTree) member;
if (possibleTestMethod.matches(methodTree, state) && !isSuppressed(tree, state)) {
suspiciousMethods.put(getSymbol(methodTree), methodTree);
}
}
if (suspiciousMethods.isEmpty()) {
return NO_MATCH;
}
tree.accept(
new TreeScanner<Void, Void>() {
@Override
public Void visitMethodInvocation(
MethodInvocationTree methodInvocationTree, Void unused) {
suspiciousMethods.remove(getSymbol(methodInvocationTree));
return super.visitMethodInvocation(methodInvocationTree, null);
}
},
null);
for (MethodTree methodTree : suspiciousMethods.values()) {
handleMethod(methodTree, state).ifPresent(state::reportMatch);
}
return NO_MATCH;
}
/**
* Matches if:
*
* <ol>
* <li>The method is public, void, and has no parameters;
* <li>the method is not already annotated with {@code @Test}, {@code @Before}, {@code @After},
* {@code @BeforeClass}, or {@code @AfterClass};
* <li>and, the method appears to be a test method, that is:
* <ol type="a">
* <li>The method is named like a JUnit 3 test case,
* <li>or, the method body contains a method call with a name that contains "assert",
* "verify", "check", "fail", or "expect".
* </ol>
* </ol>
*
* Assumes that we have reason to believe we're in a test class (i.e. has a {@code RunWith}
* annotation; has other {@code @Test} methods, etc).
*/
private Optional<Description> handleMethod(MethodTree methodTree, VisitorState state) {
// Method appears to be a JUnit 3 test case (name prefixed with "test"), probably a test.
if (isJunit3TestCase.matches(methodTree, state)) {
return Optional.of(describeFixes(methodTree, state));
}
// Method is annotated, probably not a test.
List<? extends AnnotationTree> annotations = methodTree.getModifiers().getAnnotations();
if (annotations != null && !annotations.isEmpty()) {
return Optional.empty();
}
// Method non-static and contains call(s) to testing method, probably a test,
// unless it is called elsewhere in the class, in which case it is a helper method.
if (NOT_STATIC.matches(methodTree, state) && containsTestMethod(methodTree)) {
return Optional.of(describeFixes(methodTree, state));
}
return Optional.empty();
}
/**
* Returns a finding for the given method tree containing fixes:
*
* <ol>
* <li>Add @Test, remove static modifier if present.
* <li>Add @Test and @Ignore, remove static modifier if present.
* <li>Change visibility to private (for local helper methods).
* </ol>
*/
private Description describeFixes(MethodTree methodTree, VisitorState state) {
Optional<SuggestedFix> removeStatic =
SuggestedFixes.removeModifiers(methodTree, state, Modifier.STATIC);
SuggestedFix testFix =
SuggestedFix.builder()
.merge(removeStatic.orElse(null))
.addImport(TEST_CLASS)
.prefixWith(methodTree, TEST_ANNOTATION)
.build();
SuggestedFix ignoreFix =
SuggestedFix.builder()
.merge(testFix)
.addImport(IGNORE_CLASS)
.prefixWith(methodTree, IGNORE_ANNOTATION)
.build();
SuggestedFix visibilityFix =
SuggestedFix.builder()
.merge(SuggestedFixes.removeModifiers(methodTree, state, Modifier.PUBLIC).orElse(null))
.merge(SuggestedFixes.addModifiers(methodTree, state, Modifier.PRIVATE).orElse(null))
.build();
// Suggest @Ignore first if test method is named like a purposely disabled test.
String methodName = methodTree.getName().toString();
if (methodName.startsWith("disabl") || methodName.startsWith("ignor")) {
return buildDescription(methodTree)
.addFix(ignoreFix)
.addFix(testFix)
.addFix(visibilityFix)
.build();
}
return buildDescription(methodTree)
.addFix(testFix)
.addFix(ignoreFix)
.addFix(visibilityFix)
.build();
}
}