-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
XpathRegressionTest.java
312 lines (283 loc) · 12.3 KB
/
XpathRegressionTest.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2020 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.internal;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck;
import com.puppycrawl.tools.checkstyle.internal.utils.CheckUtil;
public class XpathRegressionTest extends AbstractModuleTestSupport {
// Checks that not compatible with SuppressionXpathFilter
// till https://github.com/checkstyle/checkstyle/issues/5777
public static final Set<String> INCOMPATIBLE_CHECK_NAMES =
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
"AnnotationLocation",
"AnnotationOnSameLine",
"AnnotationUseStyle",
"ArrayTrailingComma",
"AvoidEscapedUnicodeCharacters",
"AvoidStarImport",
"AvoidStaticImport",
"CommentsIndentation",
"CustomImportOrder",
"EmptyCatchBlock",
"EmptyLineSeparator",
"FinalClass",
"IllegalCatch",
"ImportOrder",
"Indentation",
"InterfaceIsType",
"InterfaceMemberImpliedModifier",
"InvalidJavadocPosition",
"JavadocContentLocation",
"JavadocMethod",
"JavadocStyle",
"JavadocType",
"LambdaParameterName",
"MethodCount",
"MissingCtor",
"MissingJavadocMethod",
"MissingJavadocPackage",
"MissingJavadocType",
"MissingOverride",
"MissingSwitchDefault",
"NeedBraces",
"NoClone",
"NoFinalizer",
"NoLineWrap",
"OneTopLevelClass",
"OuterTypeFilename",
"OverloadMethodsDeclarationOrder",
"PackageAnnotation",
"PackageDeclaration",
"Regexp",
"RegexpSinglelineJava",
"SuppressWarningsHolder",
"TodoComment",
"TrailingComment",
"UncommentedMain",
"UnnecessaryParentheses",
"VariableDeclarationUsageDistance",
"WriteTag"
)));
// Javadoc checks are not compatible with SuppressionXpathFilter
// till https://github.com/checkstyle/checkstyle/issues/5770
// then all of them should be added to the list of incompatible checks
// and this field should be removed
public static final Set<String> INCOMPATIBLE_JAVADOC_CHECK_NAMES =
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
"AtclauseOrder",
"JavadocBlockTagLocation",
"JavadocMissingWhitespaceAfterAsterisk",
"JavadocParagraph",
"JavadocTagContinuationIndentation",
"MissingDeprecated",
"NonEmptyAtclauseDescription",
"SingleLineJavadoc",
"SummaryJavadoc"
)));
// Checks that allowed to have no XPath IT Regression Testing
// till https://github.com/checkstyle/checkstyle/issues/6207
private static final Set<String> MISSING_CHECK_NAMES = new HashSet<>(Arrays.asList(
"BooleanExpressionComplexity",
"CatchParameterName",
"ClassDataAbstractionCoupling",
"ClassFanOutComplexity",
"ClassTypeParameterName",
"ConstantName",
"CovariantEquals",
"DescendantToken",
"DesignForExtension",
"EmptyBlock",
"EmptyStatement",
"EqualsAvoidNull",
"EqualsHashCode",
"ExecutableStatementCount",
"FinalLocalVariable",
"FinalParameters",
"HideUtilityClassConstructor",
"IllegalInstantiation",
"IllegalToken",
"IllegalTokenText",
"IllegalType",
"InnerAssignment",
"InnerTypeLast",
"InterfaceTypeParameterName",
"JavaNCSS",
"IllegalImport",
"LocalFinalVariableName",
"LocalVariableName",
"MagicNumber",
"MemberName",
"MethodLength",
"MethodName",
"MethodTypeParameterName",
"ModifiedControlVariable",
"ModifierOrder",
"MultipleStringLiterals",
"MutableException",
"OperatorWrap",
"PackageName",
"ParameterAssignment",
"ParameterName",
"ParameterNumber",
"RedundantImport",
"RedundantModifier",
"ReturnCount",
"SeparatorWrap",
"SimplifyBooleanExpression",
"SimplifyBooleanReturn",
"StaticVariableName",
"StringLiteralEquality",
"SuperClone",
"SuperFinalize",
"SuppressWarnings",
"ThrowsCount",
"TypeName",
"VisibilityModifier"
));
private static Set<String> simpleCheckNames;
private static Map<String, String> allowedDirectoryAndChecks;
private Path javaDir;
private Path inputDir;
@BeforeAll
public static void setUpBeforeClass() throws IOException {
simpleCheckNames = CheckUtil.getSimpleNames(CheckUtil.getCheckstyleChecks());
allowedDirectoryAndChecks = simpleCheckNames
.stream()
.collect(Collectors.toMap(id -> id.toLowerCase(Locale.ENGLISH), id -> id));
}
@BeforeEach
public void setUp() throws Exception {
javaDir = Paths.get("src/it/java/" + getPackageLocation());
inputDir = Paths.get(getPath(""));
}
@Override
protected String getPackageLocation() {
return "org/checkstyle/suppressionxpathfilter";
}
@Override
protected String getResourceLocation() {
return "it";
}
@Test
public void validateIncompatibleJavadocCheckNames() throws IOException {
final Set<Class<?>> abstractJavadocCheckNames = CheckUtil.getCheckstyleChecks()
.stream()
.filter(AbstractJavadocCheck.class::isAssignableFrom)
.collect(Collectors.toSet());
assertWithMessage("INCOMPATIBLE_JAVADOC_CHECK_NAMES should contains all descendants "
+ "of AbstractJavadocCheck")
.that(CheckUtil.getSimpleNames(abstractJavadocCheckNames))
.isEqualTo(INCOMPATIBLE_JAVADOC_CHECK_NAMES);
}
@Test
public void validateIntegrationTestClassNames() throws Exception {
final Set<String> compatibleChecks = new HashSet<>();
final Pattern pattern = Pattern.compile("^XpathRegression(.+)Test\\.java$");
try (DirectoryStream<Path> javaPaths = Files.newDirectoryStream(javaDir)) {
for (Path path : javaPaths) {
assertTrue(Files.isRegularFile(path), path + " is not a regular file");
final String filename = path.toFile().getName();
if (filename.startsWith("Abstract")) {
continue;
}
final Matcher matcher = pattern.matcher(filename);
assertTrue(matcher.matches(),
"Invalid test file: " + filename + ", expected pattern: " + pattern);
final String check = matcher.group(1);
assertTrue(simpleCheckNames.contains(check),
"Unknown check '" + check + "' in test file: " + filename);
assertFalse(MISSING_CHECK_NAMES.contains(check),
"Check '" + check + "' is now tested. Please update the todo list in"
+ " XpathRegressionTest.MISSING_CHECK_NAMES");
assertFalse(INCOMPATIBLE_CHECK_NAMES.contains(check),
"Check '" + check + "' is now compatible with SuppressionXpathFilter."
+ " Please update the todo list in"
+ " XpathRegressionTest.INCOMPATIBLE_CHECK_NAMES");
compatibleChecks.add(check);
}
}
// Ensure that all lists are up to date
final Set<String> allChecks = new HashSet<>(simpleCheckNames);
allChecks.removeAll(INCOMPATIBLE_JAVADOC_CHECK_NAMES);
allChecks.removeAll(INCOMPATIBLE_CHECK_NAMES);
allChecks.removeAll(MISSING_CHECK_NAMES);
allChecks.removeAll(compatibleChecks);
assertTrue(allChecks.isEmpty(), "XpathRegressionTest is missing for ["
+ String.join(", ", allChecks)
+ "]. Please add them to src/it/java/org/checkstyle/suppressionxpathfilter");
}
@Test
public void validateInputFiles() throws Exception {
try (DirectoryStream<Path> dirs = Files.newDirectoryStream(inputDir)) {
for (Path dir : dirs) {
// input directory must be named in lower case
assertTrue(Files.isDirectory(dir), dir + " is not a directory");
final String dirName = dir.toFile().getName();
assertTrue(allowedDirectoryAndChecks.containsKey(dirName),
"Invalid directory name: " + dirName);
// input directory must be connected to an existing test
final String check = allowedDirectoryAndChecks.get(dirName);
final Path javaPath = javaDir.resolve("XpathRegression" + check + "Test.java");
assertTrue(Files.exists(javaPath),
"Input directory '" + dir + "' is not connected to Java test case: "
+ javaPath);
// input files should named correctly
validateInputDirectory(dir);
}
}
}
private static void validateInputDirectory(Path checkDir) throws IOException {
final Pattern pattern = Pattern.compile("^SuppressionXpathRegression(.+)\\.java$");
final String check = allowedDirectoryAndChecks.get(checkDir.toFile().getName());
try (DirectoryStream<Path> inputPaths = Files.newDirectoryStream(checkDir)) {
for (Path inputPath : inputPaths) {
final String filename = inputPath.toFile().getName();
if (filename.endsWith("java")) {
final Matcher matcher = pattern.matcher(filename);
assertTrue(matcher.matches(),
"Invalid input file '" + inputPath + "', expected pattern:" + pattern);
final String remaining = matcher.group(1);
assertTrue(remaining.startsWith(check),
"Check name '" + check + "' should be included in input file: "
+ inputPath);
}
}
}
}
}