-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
RequireThisCheckTest.java
532 lines (488 loc) · 25.7 KB
/
RequireThisCheckTest.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2021 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.checks.coding;
import static com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck.MSG_METHOD;
import static com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck.MSG_VARIABLE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.Optional;
import java.util.SortedSet;
import org.antlr.v4.runtime.CommonToken;
import org.junit.jupiter.api.Test;
import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.DetailAstImpl;
import com.puppycrawl.tools.checkstyle.JavaParser;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.api.Violation;
import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
public class RequireThisCheckTest extends AbstractModuleTestSupport {
@Override
protected String getPackageLocation() {
return "com/puppycrawl/tools/checkstyle/checks/coding/requirethis";
}
@Test
public void testIt() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = {
"20:9: " + getCheckMessage(MSG_VARIABLE, "i", ""),
"26:9: " + getCheckMessage(MSG_METHOD, "method1", ""),
"40:9: " + getCheckMessage(MSG_VARIABLE, "i", ""),
"58:13: " + getCheckMessage(MSG_VARIABLE, "z", ""),
"65:9: " + getCheckMessage(MSG_VARIABLE, "z", ""),
"122:9: " + getCheckMessage(MSG_VARIABLE, "i", ""),
"123:9: " + getCheckMessage(MSG_VARIABLE, "i", ""),
"124:9: " + getCheckMessage(MSG_METHOD, "instanceMethod", ""),
"130:13: " + getCheckMessage(MSG_METHOD, "instanceMethod", "Issue2240."),
"131:13: " + getCheckMessage(MSG_VARIABLE, "i", "Issue2240."),
"143:9: " + getCheckMessage(MSG_METHOD, "foo", ""),
"151:9: " + getCheckMessage(MSG_VARIABLE, "s", ""),
"177:16: " + getCheckMessage(MSG_VARIABLE, "a", ""),
"177:20: " + getCheckMessage(MSG_VARIABLE, "a", ""),
"177:24: " + getCheckMessage(MSG_VARIABLE, "a", ""),
"183:16: " + getCheckMessage(MSG_VARIABLE, "b", ""),
"183:20: " + getCheckMessage(MSG_VARIABLE, "b", ""),
"183:24: " + getCheckMessage(MSG_VARIABLE, "b", ""),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisEnumInnerClassesAndBugs.java"),
expected);
}
@Test
public void testMethodsOnly() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("checkFields", "false");
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = {
"25:9: " + getCheckMessage(MSG_METHOD, "method1", ""),
"124:9: " + getCheckMessage(MSG_METHOD, "instanceMethod", ""),
"130:13: " + getCheckMessage(MSG_METHOD, "instanceMethod", "Issue22402."),
"143:9: " + getCheckMessage(MSG_METHOD, "foo", ""),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisEnumInnerClassesAndBugs2.java"),
expected);
}
@Test
public void testFieldsOnly() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("checkMethods", "false");
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = {
"19:9: " + getCheckMessage(MSG_VARIABLE, "i", ""),
"39:9: " + getCheckMessage(MSG_VARIABLE, "i", ""),
"58:13: " + getCheckMessage(MSG_VARIABLE, "z", ""),
"65:9: " + getCheckMessage(MSG_VARIABLE, "z", ""),
"122:9: " + getCheckMessage(MSG_VARIABLE, "i", ""),
"123:9: " + getCheckMessage(MSG_VARIABLE, "i", ""),
"131:13: " + getCheckMessage(MSG_VARIABLE, "i", "Issue22403."),
"152:9: " + getCheckMessage(MSG_VARIABLE, "s", ""),
"179:16: " + getCheckMessage(MSG_VARIABLE, "a", ""),
"179:20: " + getCheckMessage(MSG_VARIABLE, "a", ""),
"179:24: " + getCheckMessage(MSG_VARIABLE, "a", ""),
"185:16: " + getCheckMessage(MSG_VARIABLE, "b", ""),
"185:20: " + getCheckMessage(MSG_VARIABLE, "b", ""),
"185:24: " + getCheckMessage(MSG_VARIABLE, "b", ""),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisEnumInnerClassesAndBugs3.java"),
expected);
}
@Test
public void testFieldsInExpressions() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("checkMethods", "false");
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = {
"18:28: " + getCheckMessage(MSG_VARIABLE, "id", ""),
"19:28: " + getCheckMessage(MSG_VARIABLE, "length", ""),
"20:28: " + getCheckMessage(MSG_VARIABLE, "length", ""),
"21:26: " + getCheckMessage(MSG_VARIABLE, "length", ""),
"22:26: " + getCheckMessage(MSG_VARIABLE, "length", ""),
"23:25: " + getCheckMessage(MSG_VARIABLE, "length", ""),
"24:25: " + getCheckMessage(MSG_VARIABLE, "length", ""),
"25:26: " + getCheckMessage(MSG_VARIABLE, "length", ""),
"26:26: " + getCheckMessage(MSG_VARIABLE, "length", ""),
"27:33: " + getCheckMessage(MSG_VARIABLE, "b", ""),
"28:36: " + getCheckMessage(MSG_VARIABLE, "b", ""),
"29:26: " + getCheckMessage(MSG_VARIABLE, "length", ""),
"30:26: " + getCheckMessage(MSG_VARIABLE, "length", ""),
"31:28: " + getCheckMessage(MSG_VARIABLE, "length", ""),
"32:26: " + getCheckMessage(MSG_VARIABLE, "length", ""),
"33:26: " + getCheckMessage(MSG_VARIABLE, "length", ""),
"34:26: " + getCheckMessage(MSG_VARIABLE, "length", ""),
"35:31: " + getCheckMessage(MSG_VARIABLE, "b", ""),
"36:32: " + getCheckMessage(MSG_VARIABLE, "b", ""),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisExpressions.java"),
expected);
}
@Test
public void testGenerics() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThis15Extensions.java"), expected);
}
@Test
public void testGithubIssue41() throws Exception {
final DefaultConfiguration checkConfig =
createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = {
"16:19: " + getCheckMessage(MSG_VARIABLE, "number", ""),
"17:16: " + getCheckMessage(MSG_METHOD, "other", ""),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisSimple.java"),
expected);
}
@Test
public void testTokensNotNull() {
final RequireThisCheck check = new RequireThisCheck();
assertNotNull(check.getAcceptableTokens(), "Acceptable tokens should not be null");
assertNotNull(check.getDefaultTokens(), "Acceptable tokens should not be null");
assertNotNull(check.getRequiredTokens(), "Acceptable tokens should not be null");
}
@Test
public void testWithAnonymousClass() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = {
"28:25: " + getCheckMessage(MSG_METHOD, "doSideEffect", ""),
"32:24: " + getCheckMessage(MSG_VARIABLE, "bar", "InputRequireThisAnonymousEmpty."),
"55:17: " + getCheckMessage(MSG_VARIABLE, "foobar", ""),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisAnonymousEmpty.java"),
expected);
}
@Test
public void testDefaultSwitch() {
final RequireThisCheck check = new RequireThisCheck();
final DetailAstImpl ast = new DetailAstImpl();
ast.initialize(new CommonToken(TokenTypes.ENUM, "ENUM"));
check.visitToken(ast);
final SortedSet<Violation> violations = check.getViolations();
assertEquals(0, violations.size(), "No exception violations expected");
}
@Test
public void testValidateOnlyOverlappingFalse() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = {
"29:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"30:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal1", ""),
"31:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal2", ""),
"32:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal3", ""),
"36:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal1", ""),
"37:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal2", ""),
"38:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal3", ""),
"42:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal1", ""),
"46:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal3", ""),
"50:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal1", ""),
"52:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"54:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal3", ""),
"58:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal2", ""),
"59:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal3", ""),
"69:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal1", ""),
"70:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal2", ""),
"89:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"128:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"137:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"141:9: " + getCheckMessage(MSG_METHOD, "method1", ""),
"177:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal1", ""),
"178:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal2", ""),
"179:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal3", ""),
"181:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"185:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal1", ""),
"186:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal2", ""),
"187:9: " + getCheckMessage(MSG_VARIABLE, "fieldFinal3", ""),
"189:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"194:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"198:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"219:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"226:29: " + getCheckMessage(MSG_VARIABLE, "booleanField", ""),
"237:21: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"247:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"262:9: " + getCheckMessage(MSG_VARIABLE, "booleanField", ""),
"271:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"279:18: " + getCheckMessage(MSG_METHOD, "addSuf2F", ""),
"284:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"284:18: " + getCheckMessage(MSG_METHOD, "addSuf2F", ""),
"310:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"349:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"383:25: " + getCheckMessage(MSG_METHOD, "getAction", ""),
"385:20: " + getCheckMessage(MSG_METHOD, "processAction", ""),
"393:16: " + getCheckMessage(MSG_METHOD, "processAction", ""),
"499:22: " + getCheckMessage(MSG_VARIABLE, "add", ""),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisValidateOnlyOverlappingFalse.java"), expected);
}
@Test
public void testValidateOnlyOverlappingTrue() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
final String[] expected = {
"29:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"52:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"89:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"128:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"181:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"189:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"247:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"262:9: " + getCheckMessage(MSG_VARIABLE, "booleanField", ""),
"271:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"284:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"310:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
"348:9: " + getCheckMessage(MSG_VARIABLE, "field1", ""),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisValidateOnlyOverlappingTrue.java"), expected);
}
@Test
public void testReceiverParameter() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisReceiver.java"), expected);
}
@Test
public void testBraceAlone() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisBraceAlone.java"), expected);
}
@Test
public void testStatic() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisStatic.java"), expected);
}
@Test
public void testMethodReferences() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
final String[] expected = {
"24:9: " + getCheckMessage(MSG_VARIABLE, "tags", ""),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisMethodReferences.java"), expected);
}
@Test
public void testAllowLocalVars() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
checkConfig.addProperty("checkMethods", "false");
final String[] expected = {
"18:9: " + getCheckMessage(MSG_VARIABLE, "s1", ""),
"26:9: " + getCheckMessage(MSG_VARIABLE, "s1", ""),
"39:9: " + getCheckMessage(MSG_VARIABLE, "s2", ""),
"44:9: " + getCheckMessage(MSG_VARIABLE, "s2", ""),
"50:9: " + getCheckMessage(MSG_VARIABLE, "s2", ""),
"51:16: " + getCheckMessage(MSG_VARIABLE, "s1", ""),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisAllowLocalVars.java"), expected);
}
@Test
public void testAllowLambdaParameters() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
checkConfig.addProperty("checkMethods", "false");
final String[] expected = {
"24:9: " + getCheckMessage(MSG_VARIABLE, "s1", ""),
"46:21: " + getCheckMessage(MSG_VARIABLE, "z", ""),
"71:29: " + getCheckMessage(MSG_VARIABLE, "a", ""),
"71:34: " + getCheckMessage(MSG_VARIABLE, "b", ""),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisAllowLambdaParameters.java"), expected);
}
@Test
public void testCatchVariables() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = {
"38:21: " + getCheckMessage(MSG_VARIABLE, "ex", ""),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisCatchVariables.java"), expected);
}
@Test
public void testEnumConstant() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisEnumConstant.java"), expected);
}
@Test
public void testAnnotationInterface() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisAnnotationInterface.java"), expected);
}
@Test
public void testFor() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = {
"22:13: " + getCheckMessage(MSG_VARIABLE, "bottom", ""),
"30:34: " + getCheckMessage(MSG_VARIABLE, "name", ""),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisFor.java"), expected);
}
@Test
public void testFinalInstanceVariable() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
final String[] expected = {
"18:9: " + getCheckMessage(MSG_VARIABLE, "y", ""),
"19:9: " + getCheckMessage(MSG_VARIABLE, "z", ""),
};
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisFinalInstanceVariable.java"), expected);
}
@Test
public void test() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisCaseGroup.java"), expected);
}
@Test
public void testExtendedMethod() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(checkConfig,
getPath("InputRequireThisExtendedMethod.java"), expected);
}
@Test
public void testRecordsAndCompactCtors() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("checkFields", "false");
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = {
"18:13: " + getCheckMessage(MSG_METHOD, "method1", ""),
"19:13: " + getCheckMessage(MSG_METHOD, "method2", ""),
"20:13: " + getCheckMessage(MSG_METHOD, "method3", ""),
"30:13: " + getCheckMessage(MSG_METHOD, "method1", ""),
"56:13: " + getCheckMessage(MSG_METHOD, "method1", ""),
"57:13: " + getCheckMessage(MSG_METHOD, "method2", ""),
"58:13: " + getCheckMessage(MSG_METHOD, "method3", ""),
"68:13: " + getCheckMessage(MSG_METHOD, "method1", ""),
};
verifyWithInlineConfigParser(checkConfig,
getNonCompilablePath("InputRequireThisRecordsAndCompactCtors.java"),
expected);
}
@Test
public void testRecordCompactCtors() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verifyWithInlineConfigParser(checkConfig,
getNonCompilablePath("InputRequireThisRecordCompactCtors.java"),
expected);
}
@Test
public void testRecordsAsTopLevel() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addProperty("checkFields", "true");
checkConfig.addProperty("validateOnlyOverlapping", "false");
final String[] expected = {
"17:9: " + getCheckMessage(MSG_METHOD, "method1", ""),
"18:9: " + getCheckMessage(MSG_METHOD, "method2", ""),
"19:9: " + getCheckMessage(MSG_METHOD, "method3", ""),
"26:9: " + getCheckMessage(MSG_METHOD, "method1", ""),
"30:21: " + getCheckMessage(MSG_VARIABLE, "x", ""),
"38:17: " + getCheckMessage(MSG_VARIABLE, "y", ""),
"45:9: " + getCheckMessage(MSG_METHOD, "method1", ""),
};
verifyWithInlineConfigParser(checkConfig,
getNonCompilablePath("InputRequireThisRecordAsTopLevel.java"),
expected);
}
@Test
public void testRecordsDefault() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
final String[] expected = {
"26:9: " + getCheckMessage(MSG_VARIABLE, "x", ""),
};
verifyWithInlineConfigParser(checkConfig,
getNonCompilablePath("InputRequireThisRecordDefault.java"),
expected);
}
@Test
public void testUnusedMethod() throws Exception {
final DetailAstImpl ident = new DetailAstImpl();
ident.setText("testName");
final Class<?> cls = Class.forName(RequireThisCheck.class.getName() + "$CatchFrame");
final Constructor<?> constructor = cls.getDeclaredConstructors()[0];
constructor.setAccessible(true);
final Object o = constructor.newInstance(null, ident);
final Object actual = TestUtil.getClassDeclaredMethod(cls, "getFrameNameIdent").invoke(o);
assertEquals(ident, actual, "expected ident token");
assertEquals("CATCH_FRAME",
TestUtil.getClassDeclaredMethod(cls, "getType").invoke(o).toString(),
"expected catch frame type");
}
/**
* We cannot reproduce situation when visitToken is called and leaveToken is not.
* So, we have to use reflection to be sure that even in such situation
* state of the field will be cleared.
*
* @throws Exception when code tested throws exception
*/
@Test
public void testClearState() throws Exception {
final RequireThisCheck check = new RequireThisCheck();
final DetailAST root = JavaParser.parseFile(
new File(getPath("InputRequireThisSimple.java")),
JavaParser.Options.WITHOUT_COMMENTS);
final Optional<DetailAST> classDef = TestUtil.findTokenInAstByPredicate(root,
ast -> ast.getType() == TokenTypes.CLASS_DEF);
assertTrue(classDef.isPresent(), "Ast should contain CLASS_DEF");
assertTrue(
TestUtil.isStatefulFieldClearedDuringBeginTree(check, classDef.get(),
"current", current -> ((Collection<?>) current).isEmpty()),
"State is not cleared on beginTree");
}
}