1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.indentation;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck.MSG_CHILD_ERROR;
24 import static com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck.MSG_CHILD_ERROR_MULTI;
25 import static com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck.MSG_ERROR;
26 import static com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck.MSG_ERROR_MULTI;
27
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35 import java.util.Locale;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38
39 import org.junit.jupiter.api.Test;
40
41 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
42 import com.puppycrawl.tools.checkstyle.Checker;
43 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
44 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
45 import com.puppycrawl.tools.checkstyle.api.AuditListener;
46 import com.puppycrawl.tools.checkstyle.api.Configuration;
47 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
48
49
50
51
52 public class IndentationCheckTest extends AbstractModuleTestSupport {
53
54 private static final Pattern LINE_WITH_COMMENT_REGEX = Pattern.compile(
55 ".*?//(?:below )?indent:(\\d+)(?:"
56 + " ioffset:(\\d+))? exp:(>=)?(\\d+(?:,\\d+)*?)( warn)?$");
57
58 private static final IndentComment[] EMPTY_INDENT_COMMENT_ARRAY = new IndentComment[0];
59
60 private static IndentComment[] getLinesWithWarnAndCheckComments(String aFileName,
61 final int tabWidth)
62 throws IOException {
63 final List<IndentComment> result = new ArrayList<>();
64 try (BufferedReader br = Files.newBufferedReader(Path.of(aFileName))) {
65 int lineNumber = 1;
66 String line = br.readLine();
67 IndentComment pendingBelowComment = null;
68
69 while (line != null) {
70 final Matcher match = LINE_WITH_COMMENT_REGEX.matcher(line);
71 if (pendingBelowComment != null) {
72 final int actualIndent = getLineStart(line, tabWidth);
73
74 processPendingBelowComment(pendingBelowComment, actualIndent,
75 lineNumber, result);
76
77 pendingBelowComment = null;
78 }
79 else if (match.matches()) {
80 final boolean isBelow = line.contains("//below indent:");
81 final IndentComment warn = new IndentComment(match, lineNumber);
82
83 if (isBelow) {
84 pendingBelowComment = warn;
85 }
86 else {
87 final int actualIndent = getLineStart(line, tabWidth);
88 processInlineComment(warn, actualIndent, lineNumber, aFileName, result);
89 }
90 }
91 else if (!line.isEmpty()) {
92 throw new IllegalStateException(String.format(Locale.ROOT,
93 "File \"%1$s\" has no indentation comment or its format "
94 + "malformed. Error on line: %2$d",
95 aFileName,
96 lineNumber));
97 }
98
99 line = br.readLine();
100 lineNumber++;
101 }
102 }
103
104 return result.toArray(EMPTY_INDENT_COMMENT_ARRAY);
105 }
106
107 private static void processPendingBelowComment(IndentComment pendingBelowComment,
108 int actualIndent, int lineNumber, List<IndentComment> result) {
109 if (actualIndent != pendingBelowComment.getIndent()) {
110 throw new IllegalStateException(String.format(Locale.ROOT,
111 "Incorrect indentation in 'below' comment. "
112 + "Line %1$d (from line %2$d): comment:%3$d, actual:%4$d.",
113 lineNumber,
114 pendingBelowComment.getLineNumber(),
115 pendingBelowComment.getIndent(),
116 actualIndent));
117 }
118
119 if (!isCommentConsistent(pendingBelowComment)) {
120 throw new IllegalStateException(String.format(Locale.ROOT,
121 "Inconsistent 'below' comment on line %1$d",
122 pendingBelowComment.getLineNumber()));
123 }
124
125 if (pendingBelowComment.isWarning()) {
126 result.add(new IndentComment(pendingBelowComment, lineNumber));
127 }
128 }
129
130 private static void processInlineComment(IndentComment warn, int actualIndent,
131 int lineNumber, String fileName, List<IndentComment> result) {
132
133 if (actualIndent != warn.getIndent()) {
134 throw new IllegalStateException(String.format(Locale.ROOT,
135 "File \"%1$s\" has incorrect indentation in comment. "
136 + "Line %2$d: comment:%3$d, actual:%4$d.",
137 fileName,
138 lineNumber,
139 warn.getIndent(),
140 actualIndent));
141 }
142
143 if (!isCommentConsistent(warn)) {
144 throw new IllegalStateException(String.format(Locale.ROOT,
145 "File \"%1$s\" has inconsistent comment on line %2$d",
146 fileName,
147 lineNumber));
148 }
149
150 if (warn.isWarning()) {
151 result.add(warn);
152 }
153 }
154
155 private static boolean isCommentConsistent(IndentComment comment) {
156 final String[] levels = comment.getExpectedWarning().split(", ");
157 final int indent = comment.getIndent() + comment.getIndentOffset();
158 final boolean result;
159 if (levels.length > 1) {
160
161 final boolean containsActualLevel =
162 Arrays.asList(levels).contains(String.valueOf(indent));
163
164 result = containsActualLevel != comment.isWarning();
165 }
166 else {
167 final int expectedWarning = Integer.parseInt(comment.getExpectedWarning());
168
169 final boolean test;
170 if (comment.isExpectedNonStrict()) {
171
172 test = indent >= expectedWarning;
173 }
174 else {
175
176 test = expectedWarning == indent;
177 }
178 result = test != comment.isWarning();
179
180 }
181 return result;
182 }
183
184 private static int getLineStart(String line, final int tabWidth) {
185 int lineStart = 0;
186 for (int index = 0; index < line.length(); ++index) {
187 if (!Character.isWhitespace(line.charAt(index))) {
188 lineStart = CommonUtil.lengthExpandedTabs(line, index, tabWidth);
189 break;
190 }
191 }
192 return lineStart;
193 }
194
195 private void verifyWarns(Configuration config, String filePath,
196 String... expected)
197 throws Exception {
198 final int tabWidth = Integer.parseInt(config.getProperty("tabWidth"));
199 final IndentComment[] linesWithWarn =
200 getLinesWithWarnAndCheckComments(filePath, tabWidth);
201 verify(config, filePath, expected, linesWithWarn);
202 assertWithMessage("Expected warning count in UT does not match warn comment count "
203 + "in input file")
204 .that(expected.length)
205 .isEqualTo(linesWithWarn.length);
206 }
207
208 private void verify(Configuration config, String filePath, String[] expected,
209 final IndentComment... linesWithWarn) throws Exception {
210 final Checker checker = createChecker(config);
211 checker.addListener(new IndentAudit(linesWithWarn));
212 verify(checker, filePath, expected);
213 }
214
215 @Override
216 protected String getPackageLocation() {
217 return "com/puppycrawl/tools/checkstyle/checks/indentation/indentation";
218 }
219
220 @Test
221 public void testGetRequiredTokens() {
222 final IndentationCheck checkObj = new IndentationCheck();
223 final int[] requiredTokens = checkObj.getRequiredTokens();
224 final HandlerFactory handlerFactory = new HandlerFactory();
225 final int[] expected = handlerFactory.getHandledTypes();
226 Arrays.sort(expected);
227 Arrays.sort(requiredTokens);
228 assertWithMessage("Default required tokens are invalid")
229 .that(requiredTokens)
230 .isEqualTo(expected);
231 }
232
233 @Test
234 public void testGetAcceptableTokens() {
235 final IndentationCheck checkObj = new IndentationCheck();
236 final int[] acceptableTokens = checkObj.getAcceptableTokens();
237 final HandlerFactory handlerFactory = new HandlerFactory();
238 final int[] expected = handlerFactory.getHandledTypes();
239 Arrays.sort(expected);
240 Arrays.sort(acceptableTokens);
241 assertWithMessage("Default acceptable tokens are invalid")
242 .that(acceptableTokens)
243 .isEqualTo(expected);
244 }
245
246 @Test
247 public void testThrowsIndentProperty() {
248 final IndentationCheck indentationCheck = new IndentationCheck();
249
250 indentationCheck.setThrowsIndent(1);
251
252 assertWithMessage("Invalid throws indent")
253 .that(indentationCheck.getThrowsIndent())
254 .isEqualTo(1);
255 }
256
257 @Test
258 public void testStrictCondition() throws Exception {
259 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
260 checkConfig.addProperty("arrayInitIndent", "4");
261 checkConfig.addProperty("basicOffset", "4");
262 checkConfig.addProperty("braceAdjustment", "4");
263 checkConfig.addProperty("caseIndent", "4");
264 checkConfig.addProperty("forceStrictCondition", "true");
265 checkConfig.addProperty("lineWrappingIndentation", "8");
266 checkConfig.addProperty("tabWidth", "4");
267 checkConfig.addProperty("throwsIndent", "8");
268 final String[] expected = {
269 "10:29: " + getCheckMessage(MSG_ERROR_MULTI, "method def rcurly", 28, "16, 20, 24"),
270 "13:9: " + getCheckMessage(MSG_ERROR, "method def rcurly", 8, 4),
271 "14:5: " + getCheckMessage(MSG_ERROR, "class def rcurly", 4, 0),
272 };
273 verifyWarns(checkConfig, getPath("InputIndentationStrictCondition.java"), expected);
274 }
275
276 @Test
277 public void forbidOldStyle() throws Exception {
278 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
279 checkConfig.addProperty("arrayInitIndent", "4");
280 checkConfig.addProperty("basicOffset", "4");
281 checkConfig.addProperty("braceAdjustment", "0");
282 checkConfig.addProperty("caseIndent", "4");
283 checkConfig.addProperty("forceStrictCondition", "true");
284 checkConfig.addProperty("lineWrappingIndentation", "8");
285 checkConfig.addProperty("tabWidth", "4");
286 checkConfig.addProperty("throwsIndent", "8");
287 final String[] expected = {
288 "20:30: " + getCheckMessage(MSG_ERROR, "int", 29, 12),
289 "21:30: " + getCheckMessage(MSG_ERROR, "int", 29, 12),
290 };
291 verifyWarns(checkConfig, getPath("InputIndentationMethodCStyle.java"), expected);
292 }
293
294 @Test
295 public void testZeroCaseLevel() throws Exception {
296 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
297 checkConfig.addProperty("arrayInitIndent", "4");
298 checkConfig.addProperty("basicOffset", "4");
299 checkConfig.addProperty("braceAdjustment", "0");
300 checkConfig.addProperty("caseIndent", "0");
301 checkConfig.addProperty("forceStrictCondition", "false");
302 checkConfig.addProperty("lineWrappingIndentation", "4");
303 checkConfig.addProperty("tabWidth", "4");
304 checkConfig.addProperty("throwsIndent", "4");
305 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
306 verifyWarns(checkConfig, getPath("InputIndentationZeroCaseLevel.java"), expected);
307 }
308
309 @Test
310 public void testAndroidStyle() throws Exception {
311 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
312 checkConfig.addProperty("arrayInitIndent", "4");
313 checkConfig.addProperty("basicOffset", "4");
314 checkConfig.addProperty("braceAdjustment", "0");
315 checkConfig.addProperty("caseIndent", "4");
316 checkConfig.addProperty("forceStrictCondition", "false");
317 checkConfig.addProperty("lineWrappingIndentation", "8");
318 checkConfig.addProperty("tabWidth", "4");
319 checkConfig.addProperty("throwsIndent", "8");
320 final String[] expected = {
321 "42:4: " + getCheckMessage(MSG_ERROR, "extends", 3, 8),
322 "44:4: " + getCheckMessage(MSG_ERROR, "member def type", 3, 4),
323 "47:9: " + getCheckMessage(MSG_ERROR, "foo", 8, 12),
324 "50:9: " + getCheckMessage(MSG_ERROR, "int", 8, 12),
325 "53:14: " + getCheckMessage(MSG_ERROR, "true", 13, 16),
326 "56:17: " + getCheckMessage(MSG_ERROR, "+", 16, 20),
327 "57:9: " + getCheckMessage(MSG_ERROR, "if", 8, 12),
328 "60:12: " + getCheckMessage(MSG_ERROR, "if rcurly", 11, 12),
329 "62:8: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 7, 8),
330 };
331 verifyWarns(checkConfig, getPath("InputIndentationAndroidStyle.java"), expected);
332 }
333
334 @Test
335 public void testMethodCallLineWrap() throws Exception {
336 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
337
338 checkConfig.addProperty("arrayInitIndent", "4");
339 checkConfig.addProperty("basicOffset", "4");
340 checkConfig.addProperty("braceAdjustment", "0");
341 checkConfig.addProperty("caseIndent", "4");
342 checkConfig.addProperty("forceStrictCondition", "false");
343 checkConfig.addProperty("lineWrappingIndentation", "4");
344 checkConfig.addProperty("tabWidth", "4");
345 checkConfig.addProperty("throwsIndent", "4");
346 final String[] expected = {
347 "53:19: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 18, 20),
348 "54:15: " + getCheckMessage(MSG_ERROR, "method call rparen", 14, 16),
349 "75:13: " + getCheckMessage(MSG_ERROR, "lambda arguments", 12, 16),
350 };
351 verifyWarns(checkConfig, getPath("InputIndentationMethodCallLineWrap.java"), expected);
352 }
353
354 @Test
355 public void testDifficultAnnotations() throws Exception {
356 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
357
358 checkConfig.addProperty("arrayInitIndent", "4");
359 checkConfig.addProperty("basicOffset", "4");
360 checkConfig.addProperty("braceAdjustment", "0");
361 checkConfig.addProperty("caseIndent", "4");
362 checkConfig.addProperty("forceStrictCondition", "false");
363 checkConfig.addProperty("lineWrappingIndentation", "4");
364 checkConfig.addProperty("tabWidth", "4");
365 checkConfig.addProperty("throwsIndent", "4");
366 final String[] expected = {
367 "40:1: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
368 "annotation array initialization", 0, "4, 23, 25"),
369 "41:1: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
370 "annotation array initialization", 0, "4, 23, 25"),
371 "50:7: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
372 "annotation array initialization", 6, "8, 27, 29"),
373 };
374 verifyWarns(checkConfig, getPath("InputIndentationDifficultAnnotations.java"), expected);
375 }
376
377 @Test
378 public void testAnnotationClosingParenthesisEndsInSameIndentationAsOpening() throws Exception {
379 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
380
381 checkConfig.addProperty("basicOffset", "4");
382 checkConfig.addProperty("forceStrictCondition", "true");
383 checkConfig.addProperty("tabWidth", "4");
384
385 final String[] expected = {
386 "34:17: " + getCheckMessage(MSG_ERROR, ")", 16, 0),
387 "36:17: " + getCheckMessage(MSG_ERROR, ")", 16, 0),
388 "40:9: " + getCheckMessage(MSG_ERROR, ")", 8, 4),
389 "42:9: " + getCheckMessage(MSG_ERROR, ")", 8, 4),
390 "46:9: " + getCheckMessage(MSG_ERROR, ")", 8, 4),
391 };
392
393 verifyWarns(checkConfig,
394 getPath("InputIndentation"
395 + "AnnotationClosingParenthesisEndsInSameIndentationAsOpen.java"),
396 expected);
397 }
398
399 @Test
400 public void testAnnotationsFromGuava() throws Exception {
401 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
402
403 checkConfig.addProperty("arrayInitIndent", "4");
404 checkConfig.addProperty("basicOffset", "2");
405 checkConfig.addProperty("braceAdjustment", "0");
406 checkConfig.addProperty("caseIndent", "4");
407 checkConfig.addProperty("forceStrictCondition", "false");
408 checkConfig.addProperty("lineWrappingIndentation", "4");
409 checkConfig.addProperty("tabWidth", "4");
410 checkConfig.addProperty("throwsIndent", "4");
411 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
412 verifyWarns(checkConfig, getPath("InputIndentationFromGuava.java"), expected);
413 }
414
415 @Test
416 public void testAnnotationsFromGuava1() throws Exception {
417 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
418
419 checkConfig.addProperty("arrayInitIndent", "4");
420 checkConfig.addProperty("basicOffset", "2");
421 checkConfig.addProperty("braceAdjustment", "0");
422 checkConfig.addProperty("caseIndent", "4");
423 checkConfig.addProperty("forceStrictCondition", "false");
424 checkConfig.addProperty("lineWrappingIndentation", "4");
425 checkConfig.addProperty("tabWidth", "4");
426 checkConfig.addProperty("throwsIndent", "4");
427 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
428 verifyWarns(checkConfig, getPath("InputIndentationFromGuava1.java"), expected);
429 }
430
431 @Test
432 public void testAnnotationsFromGuava2() throws Exception {
433 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
434
435 checkConfig.addProperty("arrayInitIndent", "4");
436 checkConfig.addProperty("basicOffset", "2");
437 checkConfig.addProperty("braceAdjustment", "0");
438 checkConfig.addProperty("caseIndent", "4");
439 checkConfig.addProperty("forceStrictCondition", "false");
440 checkConfig.addProperty("lineWrappingIndentation", "4");
441 checkConfig.addProperty("tabWidth", "4");
442 checkConfig.addProperty("throwsIndent", "4");
443 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
444 verifyWarns(checkConfig, getPath("InputIndentationFromGuava2.java"), expected);
445 }
446
447 @Test
448 public void testAnnotationsFromGuava3() throws Exception {
449 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
450
451 checkConfig.addProperty("arrayInitIndent", "4");
452 checkConfig.addProperty("basicOffset", "2");
453 checkConfig.addProperty("braceAdjustment", "0");
454 checkConfig.addProperty("caseIndent", "4");
455 checkConfig.addProperty("forceStrictCondition", "false");
456 checkConfig.addProperty("lineWrappingIndentation", "4");
457 checkConfig.addProperty("tabWidth", "4");
458 checkConfig.addProperty("throwsIndent", "4");
459 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
460 verifyWarns(checkConfig, getPath("InputIndentationFromGuava3.java"), expected);
461 }
462
463 @Test
464 public void testAnnotationsFromGuava4() throws Exception {
465 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
466
467 checkConfig.addProperty("arrayInitIndent", "4");
468 checkConfig.addProperty("basicOffset", "2");
469 checkConfig.addProperty("braceAdjustment", "0");
470 checkConfig.addProperty("caseIndent", "4");
471 checkConfig.addProperty("forceStrictCondition", "false");
472 checkConfig.addProperty("lineWrappingIndentation", "4");
473 checkConfig.addProperty("tabWidth", "4");
474 checkConfig.addProperty("throwsIndent", "4");
475 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
476 verifyWarns(checkConfig, getPath("InputIndentationFromGuava4.java"), expected);
477 }
478
479 @Test
480 public void testCorrectIfAndParameters() throws Exception {
481 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
482
483 checkConfig.addProperty("arrayInitIndent", "4");
484 checkConfig.addProperty("basicOffset", "2");
485 checkConfig.addProperty("braceAdjustment", "0");
486 checkConfig.addProperty("caseIndent", "4");
487 checkConfig.addProperty("forceStrictCondition", "false");
488 checkConfig.addProperty("lineWrappingIndentation", "4");
489 checkConfig.addProperty("tabWidth", "4");
490 checkConfig.addProperty("throwsIndent", "4");
491 final String[] expected = {
492 "66:15: " + getCheckMessage(MSG_ERROR_MULTI, "new", 14, "16, 18"),
493 "75:19: " + getCheckMessage(MSG_ERROR_MULTI, "new", 18, "40, 42"),
494 "96:17: " + getCheckMessage(MSG_ERROR_MULTI, "new", 16, "18, 20"),
495 "100:11: " + getCheckMessage(MSG_ERROR, "+", 10, 12),
496 "104:31: " + getCheckMessage(MSG_ERROR_MULTI, "new", 30, "38, 40"),
497 "106:17: " + getCheckMessage(MSG_CHILD_ERROR, "new", 16, 44),
498 };
499 verifyWarns(checkConfig, getPath("InputIndentationIfAndParameter.java"), expected);
500 }
501
502 @Test
503 public void testCorrectIfAndParameters1() throws Exception {
504 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
505
506 checkConfig.addProperty("arrayInitIndent", "4");
507 checkConfig.addProperty("basicOffset", "2");
508 checkConfig.addProperty("braceAdjustment", "0");
509 checkConfig.addProperty("caseIndent", "4");
510 checkConfig.addProperty("forceStrictCondition", "false");
511 checkConfig.addProperty("lineWrappingIndentation", "4");
512 checkConfig.addProperty("tabWidth", "4");
513 checkConfig.addProperty("throwsIndent", "4");
514 final String[] expected = {
515 "38:13: " + getCheckMessage(MSG_ERROR_MULTI, "new", 12, "14, 16"),
516 "42:17: " + getCheckMessage(MSG_CHILD_ERROR, "new", 16, 28),
517 "45:25: " + getCheckMessage(MSG_ERROR_MULTI, "new", 24, "42, 44"),
518 };
519 verifyWarns(checkConfig, getPath("InputIndentationCorrectIfAndParameter1.java"), expected);
520 }
521
522 @Test
523 public void testAnonymousClasses() throws Exception {
524 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
525
526 checkConfig.addProperty("arrayInitIndent", "4");
527 checkConfig.addProperty("basicOffset", "2");
528 checkConfig.addProperty("braceAdjustment", "0");
529 checkConfig.addProperty("caseIndent", "4");
530 checkConfig.addProperty("forceStrictCondition", "false");
531 checkConfig.addProperty("lineWrappingIndentation", "4");
532 checkConfig.addProperty("tabWidth", "4");
533 checkConfig.addProperty("throwsIndent", "4");
534 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
535 verifyWarns(checkConfig, getPath("InputIndentationAnonymousClasses.java"), expected);
536 }
537
538 @Test
539 public void testArrays() throws Exception {
540 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
541
542 checkConfig.addProperty("arrayInitIndent", "2");
543 checkConfig.addProperty("basicOffset", "2");
544 checkConfig.addProperty("braceAdjustment", "0");
545 checkConfig.addProperty("caseIndent", "4");
546 checkConfig.addProperty("forceStrictCondition", "false");
547 checkConfig.addProperty("lineWrappingIndentation", "4");
548 checkConfig.addProperty("tabWidth", "4");
549 checkConfig.addProperty("throwsIndent", "4");
550 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
551 verifyWarns(checkConfig, getPath("InputIndentationArrays.java"), expected);
552 }
553
554 @Test
555 public void testLabels() throws Exception {
556 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
557
558 checkConfig.addProperty("arrayInitIndent", "4");
559 checkConfig.addProperty("basicOffset", "2");
560 checkConfig.addProperty("braceAdjustment", "0");
561 checkConfig.addProperty("caseIndent", "4");
562 checkConfig.addProperty("forceStrictCondition", "false");
563 checkConfig.addProperty("lineWrappingIndentation", "4");
564 checkConfig.addProperty("tabWidth", "4");
565 checkConfig.addProperty("throwsIndent", "4");
566 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
567 verifyWarns(checkConfig, getPath("InputIndentationLabels.java"), expected);
568 }
569
570 @Test
571 public void testLabels1() throws Exception {
572 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
573
574 checkConfig.addProperty("arrayInitIndent", "4");
575 checkConfig.addProperty("basicOffset", "2");
576 checkConfig.addProperty("braceAdjustment", "0");
577 checkConfig.addProperty("caseIndent", "4");
578 checkConfig.addProperty("forceStrictCondition", "false");
579 checkConfig.addProperty("lineWrappingIndentation", "4");
580 checkConfig.addProperty("tabWidth", "4");
581 checkConfig.addProperty("throwsIndent", "4");
582 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
583 verifyWarns(checkConfig, getPath("InputIndentationLabels1.java"), expected);
584 }
585
586 @Test
587 public void testClassesAndMethods() throws Exception {
588 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
589
590 checkConfig.addProperty("arrayInitIndent", "4");
591 checkConfig.addProperty("basicOffset", "2");
592 checkConfig.addProperty("braceAdjustment", "0");
593 checkConfig.addProperty("caseIndent", "4");
594 checkConfig.addProperty("forceStrictCondition", "false");
595 checkConfig.addProperty("lineWrappingIndentation", "4");
596 checkConfig.addProperty("tabWidth", "4");
597 checkConfig.addProperty("throwsIndent", "4");
598 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
599 verifyWarns(checkConfig, getPath("InputIndentationClassesMethods.java"), expected);
600 }
601
602 @Test
603 public void testCtorCall() throws Exception {
604 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
605
606 checkConfig.addProperty("basicOffset", "2");
607 checkConfig.addProperty("braceAdjustment", "0");
608 checkConfig.addProperty("lineWrappingIndentation", "4");
609 checkConfig.addProperty("tabWidth", "4");
610 final String[] expected = {
611 "29:5: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 4, 6),
612 "30:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 6),
613 "31:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 6),
614 "35:5: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 4, 6),
615 "36:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 6),
616 "40:5: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 4, 6),
617 "41:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 8),
618 "42:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 8),
619 "46:5: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 4, 6),
620 "47:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 8),
621 "51:5: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 4, 6),
622 "52:5: " + getCheckMessage(MSG_ERROR, "(", 4, 8),
623 "53:5: " + getCheckMessage(MSG_ERROR, "x", 4, 8),
624 "57:5: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 4, 6),
625 "58:5: " + getCheckMessage(MSG_ERROR, "method call lparen", 4, 6),
626 "63:5: " + getCheckMessage(MSG_ERROR, ".", 4, 10),
627 "64:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 8),
628 "69:5: " + getCheckMessage(MSG_ERROR, "super", 4, 10),
629 "70:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 8),
630 "76:11: " + getCheckMessage(MSG_ERROR_MULTI, "lambda arguments", 10, "12, 14"),
631 };
632 verifyWarns(checkConfig, getPath("InputIndentationCtorCall.java"), expected);
633 }
634
635 @Test
636 public void testCtorCall1() throws Exception {
637 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
638
639 checkConfig.addProperty("basicOffset", "2");
640 checkConfig.addProperty("braceAdjustment", "0");
641 checkConfig.addProperty("lineWrappingIndentation", "4");
642 checkConfig.addProperty("tabWidth", "4");
643 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
644 verifyWarns(checkConfig, getPath("InputIndentationCtorCall1.java"), expected);
645 }
646
647 @Test
648 public void testMembers() throws Exception {
649 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
650
651 checkConfig.addProperty("arrayInitIndent", "4");
652 checkConfig.addProperty("basicOffset", "2");
653 checkConfig.addProperty("braceAdjustment", "0");
654 checkConfig.addProperty("caseIndent", "4");
655 checkConfig.addProperty("forceStrictCondition", "false");
656 checkConfig.addProperty("lineWrappingIndentation", "4");
657 checkConfig.addProperty("tabWidth", "4");
658 checkConfig.addProperty("throwsIndent", "4");
659 final String[] expected = {
660 "22:6: " + getCheckMessage(MSG_ERROR, "=", 5, 6),
661 "57:4: " + getCheckMessage(MSG_ERROR, "class def rcurly", 3, 2),
662 };
663
664 verifyWarns(checkConfig, getPath("InputIndentationMembers.java"), expected);
665 }
666
667 @Test
668 public void testAnnotationArrayInit() throws Exception {
669 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
670
671 checkConfig.addProperty("arrayInitIndent", "6");
672 checkConfig.addProperty("basicOffset", "2");
673 checkConfig.addProperty("braceAdjustment", "0");
674 checkConfig.addProperty("caseIndent", "4");
675 checkConfig.addProperty("forceStrictCondition", "false");
676 checkConfig.addProperty("lineWrappingIndentation", "4");
677 checkConfig.addProperty("tabWidth", "8");
678 checkConfig.addProperty("throwsIndent", "4");
679 final String[] expected = {
680
681 "17:1: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization", 0,
682 "4, 6, 34, 36"),
683 "22:14: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
684 13, "4, 6, 34, 36"),
685 "23:3: " + getCheckMessage(MSG_ERROR_MULTI,
686 "annotation array initialization rcurly", 2, "0, 4"),
687 "35:7: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization", 6,
688 "8, 10, 31, 33"),
689 "36:3: " + getCheckMessage(MSG_ERROR_MULTI,
690 "annotation array initialization rcurly", 2, "4, 8"),
691
692 "52:6: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
693 "annotation array initialization", 5, "6, 8, 10"),
694 "54:6: " + getCheckMessage(MSG_ERROR_MULTI,
695 "annotation array initialization rcurly", 5, "2, 6"),
696 };
697 final String fileName = getPath("InputIndentationAnnArrInit.java");
698 verifyWarns(checkConfig, fileName, expected);
699 }
700
701 @Test
702 public void testAnnotationArrayInitTwo() throws Exception {
703 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
704
705 checkConfig.addProperty("arrayInitIndent", "0");
706 checkConfig.addProperty("basicOffset", "2");
707 checkConfig.addProperty("braceAdjustment", "0");
708 checkConfig.addProperty("caseIndent", "4");
709 checkConfig.addProperty("forceStrictCondition", "false");
710 checkConfig.addProperty("lineWrappingIndentation", "0");
711 checkConfig.addProperty("tabWidth", "8");
712 checkConfig.addProperty("throwsIndent", "4");
713 final String[] expected = {
714
715 "17:5: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
716 "annotation array initialization", 4, "0, 33, 35"),
717 "30:9: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
718 "annotation array initialization", 8, "4, 29, 31"),
719 "32:3: " + getCheckMessage(MSG_ERROR,
720 "annotation array initialization rcurly", 2, 4),
721 "47:7: " + getCheckMessage(MSG_ERROR,
722 "annotation array initialization lcurly", 6, 2),
723 "49:5: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
724 "annotation array initialization", 4, "2, 6, 8"),
725 };
726 final String fileName = getPath("InputIndentationAnnArrInit2.java");
727 verifyWarns(checkConfig, fileName, expected);
728 }
729
730 @Test
731 public void testAnnotationArrayInitWithEmoji() throws Exception {
732 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
733
734 checkConfig.addProperty("arrayInitIndent", "0");
735 checkConfig.addProperty("basicOffset", "2");
736 checkConfig.addProperty("braceAdjustment", "0");
737 checkConfig.addProperty("caseIndent", "4");
738 checkConfig.addProperty("forceStrictCondition", "false");
739 checkConfig.addProperty("lineWrappingIndentation", "0");
740 checkConfig.addProperty("tabWidth", "8");
741 checkConfig.addProperty("throwsIndent", "4");
742 final String[] expected = {
743 "17:5: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
744 "annotation array initialization", 4, "0, 41, 43"),
745 "30:9: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
746 "annotation array initialization", 8, "4, 29, 31"),
747 "32:3: " + getCheckMessage(MSG_ERROR,
748 "annotation array initialization rcurly", 2, 4),
749 "42:7: " + getCheckMessage(MSG_ERROR,
750 "member def type", 6, "4"),
751 "47:7: " + getCheckMessage(MSG_ERROR,
752 "annotation array initialization lcurly", 6, "2"),
753 "48:11: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
754 "annotation array initialization", 10, "2, 6, 8"),
755 "49:13: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
756 "annotation array initialization", 12, "2, 6, 8"),
757 "50:21: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
758 "annotation array initialization", 20, "2, 6, 8"),
759 "52:5: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
760 "annotation array initialization", 4, "2, 6, 8"),
761 };
762 final String fileName = getPath("InputIndentationAnnArrInitWithEmoji.java");
763 verifyWarns(checkConfig, fileName, expected);
764
765 }
766
767 @Test
768 public void testOddAnnotations()
769 throws Exception {
770 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
771
772 checkConfig.addProperty("arrayInitIndent", "3");
773 checkConfig.addProperty("basicOffset", "4");
774 checkConfig.addProperty("braceAdjustment", "0");
775 checkConfig.addProperty("caseIndent", "4");
776
777 checkConfig.addProperty("forceStrictCondition", "false");
778 checkConfig.addProperty("lineWrappingIndentation", "9");
779 checkConfig.addProperty("tabWidth", "4");
780 checkConfig.addProperty("throwsIndent", "4");
781 final String fileName = getPath("InputIndentationOddLineWrappingAndArrayInit.java");
782 final String[] expected = {
783 "25:17: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
784 16, "11, 17, 47, 54"),
785 };
786 verifyWarns(checkConfig, fileName, expected);
787 }
788
789 @Test
790 public void testAnnotationOddStyles() throws Exception {
791 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
792
793 checkConfig.addProperty("tabWidth", "8");
794
795 final String fileName = getPath("InputIndentationAnnotationArrayInitOldStyle.java");
796
797 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
798
799 verifyWarns(checkConfig, fileName, expected);
800 }
801
802 @Test
803 public void testZeroAnnotationArrayInit()
804 throws Exception {
805 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
806
807 checkConfig.addProperty("arrayInitIndent", "0");
808 checkConfig.addProperty("basicOffset", "4");
809 checkConfig.addProperty("braceAdjustment", "0");
810 checkConfig.addProperty("caseIndent", "4");
811 checkConfig.addProperty("forceStrictCondition", "false");
812 checkConfig.addProperty("lineWrappingIndentation", "4");
813 checkConfig.addProperty("tabWidth", "4");
814 checkConfig.addProperty("throwsIndent", "4");
815 final String fileName = getPath("InputIndentationZeroArrayInit.java");
816
817 final String[] expected = {
818 "22:12: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
819 11, "8, 12, 35, 37"),
820 };
821 verifyWarns(checkConfig, fileName, expected);
822 }
823
824 @Test
825 public void testAnnotationArrayInitGoodCase()
826 throws Exception {
827 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
828
829 checkConfig.addProperty("arrayInitIndent", "4");
830 checkConfig.addProperty("basicOffset", "4");
831 checkConfig.addProperty("braceAdjustment", "0");
832 checkConfig.addProperty("caseIndent", "4");
833 checkConfig.addProperty("forceStrictCondition", "false");
834 checkConfig.addProperty("lineWrappingIndentation", "4");
835 checkConfig.addProperty("tabWidth", "4");
836 checkConfig.addProperty("throwsIndent", "4");
837 final String fileName = getPath("InputIndentationAnnotationArrayInitGood.java");
838 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
839 verifyWarns(checkConfig, fileName, expected);
840 }
841
842 @Test
843 public void testAnnotationArrayInitGoodCaseTwo()
844 throws Exception {
845 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
846
847 checkConfig.addProperty("arrayInitIndent", "4");
848 checkConfig.addProperty("basicOffset", "4");
849 checkConfig.addProperty("braceAdjustment", "2");
850 checkConfig.addProperty("caseIndent", "4");
851 checkConfig.addProperty("forceStrictCondition", "false");
852 checkConfig.addProperty("lineWrappingIndentation", "4");
853 checkConfig.addProperty("tabWidth", "4");
854 checkConfig.addProperty("throwsIndent", "4");
855 final String fileName = getPath("InputIndentationAnnotationArrayInitGood.java");
856 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
857 verifyWarns(checkConfig, fileName, expected);
858 }
859
860 @Test
861 public void testInvalidLabel() throws Exception {
862 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
863
864 checkConfig.addProperty("arrayInitIndent", "4");
865 checkConfig.addProperty("basicOffset", "4");
866 checkConfig.addProperty("braceAdjustment", "0");
867 checkConfig.addProperty("caseIndent", "4");
868 checkConfig.addProperty("forceStrictCondition", "false");
869 checkConfig.addProperty("lineWrappingIndentation", "4");
870 checkConfig.addProperty("tabWidth", "4");
871 checkConfig.addProperty("throwsIndent", "4");
872 final String[] expected = {
873 "24:11: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "label", 10, "8, 12"),
874 "33:3: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "label", 2, "4, 8"),
875 "36:19: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "label", 18, "8, 12"),
876 "37:19: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 18, 8),
877 "39:7: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "label", 6, "8, 12"),
878 "41:7: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "label", 6, "8, 12"),
879 };
880 verifyWarns(checkConfig, getPath("InputIndentationInvalidLabelIndent.java"), expected);
881 }
882
883 @Test
884 public void testInvalidLabelWithWhileLoop() throws Exception {
885 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
886
887 checkConfig.addProperty("arrayInitIndent", "4");
888 checkConfig.addProperty("basicOffset", "4");
889 checkConfig.addProperty("braceAdjustment", "0");
890 checkConfig.addProperty("caseIndent", "4");
891 checkConfig.addProperty("forceStrictCondition", "false");
892 checkConfig.addProperty("lineWrappingIndentation", "4");
893 checkConfig.addProperty("tabWidth", "4");
894 checkConfig.addProperty("throwsIndent", "4");
895 final String[] expected = {
896 "18:10: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "label", 9, "4, 8"),
897 "19:10: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "label", 9, "8, 12"),
898 };
899 verifyWarns(checkConfig, getPath("InputIndentationInvalidLabelWithWhileLoopIndent.java"),
900 expected);
901 }
902
903 @Test
904 public void testValidLabel() throws Exception {
905 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
906
907 checkConfig.addProperty("arrayInitIndent", "4");
908 checkConfig.addProperty("basicOffset", "4");
909 checkConfig.addProperty("braceAdjustment", "0");
910 checkConfig.addProperty("caseIndent", "4");
911 checkConfig.addProperty("forceStrictCondition", "false");
912 checkConfig.addProperty("lineWrappingIndentation", "4");
913 checkConfig.addProperty("tabWidth", "4");
914 checkConfig.addProperty("throwsIndent", "4");
915 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
916 verifyWarns(checkConfig, getPath("InputIndentationValidLabelIndent.java"), expected);
917 }
918
919 @Test
920 public void testValidIfWithChecker() throws Exception {
921 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
922
923 checkConfig.addProperty("arrayInitIndent", "4");
924 checkConfig.addProperty("basicOffset", "4");
925 checkConfig.addProperty("braceAdjustment", "0");
926 checkConfig.addProperty("caseIndent", "4");
927 checkConfig.addProperty("forceStrictCondition", "false");
928 checkConfig.addProperty("lineWrappingIndentation", "4");
929 checkConfig.addProperty("tabWidth", "4");
930 checkConfig.addProperty("throwsIndent", "4");
931 final String fileName = getPath("InputIndentationValidIfIndent.java");
932 final String[] expected = {
933 "95:9: " + getCheckMessage(MSG_ERROR, "(", 8, 12),
934 };
935 verifyWarns(checkConfig, fileName, expected);
936 }
937
938 @Test
939 public void testValidIfWithChecker1() throws Exception {
940 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
941
942 checkConfig.addProperty("arrayInitIndent", "4");
943 checkConfig.addProperty("basicOffset", "4");
944 checkConfig.addProperty("braceAdjustment", "0");
945 checkConfig.addProperty("caseIndent", "4");
946 checkConfig.addProperty("forceStrictCondition", "false");
947 checkConfig.addProperty("lineWrappingIndentation", "4");
948 checkConfig.addProperty("tabWidth", "4");
949 checkConfig.addProperty("throwsIndent", "4");
950 final String fileName = getPath("InputIndentationValidIfIndent1.java");
951 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
952 verifyWarns(checkConfig, fileName, expected);
953 }
954
955 @Test
956 public void testValidIfWithChecker2() throws Exception {
957 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
958
959 checkConfig.addProperty("arrayInitIndent", "4");
960 checkConfig.addProperty("basicOffset", "4");
961 checkConfig.addProperty("braceAdjustment", "0");
962 checkConfig.addProperty("caseIndent", "4");
963 checkConfig.addProperty("forceStrictCondition", "false");
964 checkConfig.addProperty("lineWrappingIndentation", "4");
965 checkConfig.addProperty("tabWidth", "4");
966 checkConfig.addProperty("throwsIndent", "4");
967 final String fileName = getPath("InputIndentationValidIfIndent2.java");
968 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
969 verifyWarns(checkConfig, fileName, expected);
970 }
971
972 @Test
973 public void testValidDotWithChecker()
974 throws Exception {
975 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
976
977 checkConfig.addProperty("arrayInitIndent", "4");
978 checkConfig.addProperty("basicOffset", "4");
979 checkConfig.addProperty("braceAdjustment", "0");
980 checkConfig.addProperty("caseIndent", "4");
981 checkConfig.addProperty("forceStrictCondition", "false");
982 checkConfig.addProperty("lineWrappingIndentation", "4");
983 checkConfig.addProperty("tabWidth", "4");
984 checkConfig.addProperty("throwsIndent", "4");
985 final String fileName = getPath("InputIndentationValidDotIndent.java");
986 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
987 verifyWarns(checkConfig, fileName, expected);
988 }
989
990 @Test
991 public void testValidMethodWithChecker()
992 throws Exception {
993 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
994
995 checkConfig.addProperty("arrayInitIndent", "4");
996 checkConfig.addProperty("basicOffset", "4");
997 checkConfig.addProperty("braceAdjustment", "0");
998 checkConfig.addProperty("caseIndent", "4");
999 checkConfig.addProperty("forceStrictCondition", "false");
1000 checkConfig.addProperty("lineWrappingIndentation", "4");
1001 checkConfig.addProperty("tabWidth", "4");
1002 checkConfig.addProperty("throwsIndent", "4");
1003 final String fileName = getPath("InputIndentationValidMethodIndent.java");
1004 final String[] expected = {
1005 "76:5: " + getCheckMessage(MSG_ERROR, "void", 4, 8),
1006 "77:5: " + getCheckMessage(MSG_ERROR, "method5", 4, 8),
1007 };
1008 verifyWarns(checkConfig, fileName, expected);
1009 }
1010
1011 @Test
1012 public void testValidMethodWithChecker1()
1013 throws Exception {
1014 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1015
1016 checkConfig.addProperty("arrayInitIndent", "4");
1017 checkConfig.addProperty("basicOffset", "4");
1018 checkConfig.addProperty("braceAdjustment", "0");
1019 checkConfig.addProperty("caseIndent", "4");
1020 checkConfig.addProperty("forceStrictCondition", "false");
1021 checkConfig.addProperty("lineWrappingIndentation", "4");
1022 checkConfig.addProperty("tabWidth", "4");
1023 checkConfig.addProperty("throwsIndent", "4");
1024 final String fileName = getPath("InputIndentationValidMethodIndent1.java");
1025 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1026 verifyWarns(checkConfig, fileName, expected);
1027 }
1028
1029 @Test
1030 public void testInvalidMethodWithChecker1()
1031 throws Exception {
1032 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1033
1034 checkConfig.addProperty("arrayInitIndent", "4");
1035 checkConfig.addProperty("basicOffset", "4");
1036 checkConfig.addProperty("braceAdjustment", "0");
1037 checkConfig.addProperty("caseIndent", "4");
1038 checkConfig.addProperty("forceStrictCondition", "false");
1039 checkConfig.addProperty("lineWrappingIndentation", "4");
1040 checkConfig.addProperty("tabWidth", "4");
1041 checkConfig.addProperty("throwsIndent", "4");
1042 final String fileName = getPath("InputIndentationInvalidMethodIndent1.java");
1043 final String[] expected = {
1044 "23:7: " + getCheckMessage(MSG_ERROR, "ctor def rcurly", 6, 4),
1045 "26:7: " + getCheckMessage(MSG_ERROR, "ctor def modifier", 6, 4),
1046 "27:3: " + getCheckMessage(MSG_ERROR, "ctor def lcurly", 2, 4),
1047 "28:7: " + getCheckMessage(MSG_ERROR, "ctor def rcurly", 6, 4),
1048 "31:3: " + getCheckMessage(MSG_ERROR, "method def modifier", 2, 4),
1049 "32:7: " + getCheckMessage(MSG_ERROR, "method def rcurly", 6, 4),
1050 "69:6: " + getCheckMessage(MSG_ERROR, "method def modifier", 5, 4),
1051 "70:6: " + getCheckMessage(MSG_ERROR, "final", 5, 9),
1052 "71:6: " + getCheckMessage(MSG_ERROR, "void", 5, 9),
1053 "72:5: " + getCheckMessage(MSG_ERROR, "method5", 4, 9),
1054 "86:11: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 10, 12),
1055 "89:11: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 10, 12),
1056 "99:7: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 6, 12),
1057 };
1058 verifyWarns(checkConfig, fileName, expected);
1059 }
1060
1061 @Test
1062 public void testInvalidMethodWithChecker2()
1063 throws Exception {
1064 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1065
1066 checkConfig.addProperty("arrayInitIndent", "4");
1067 checkConfig.addProperty("basicOffset", "4");
1068 checkConfig.addProperty("braceAdjustment", "0");
1069 checkConfig.addProperty("caseIndent", "4");
1070 checkConfig.addProperty("forceStrictCondition", "false");
1071 checkConfig.addProperty("lineWrappingIndentation", "4");
1072 checkConfig.addProperty("tabWidth", "4");
1073 checkConfig.addProperty("throwsIndent", "4");
1074 final String fileName = getPath("InputIndentationInvalidMethodIndent2.java");
1075 final String[] expected = {
1076 "23:5: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 4, 8),
1077 "26:4: " + getCheckMessage(MSG_ERROR, "method def modifier", 3, 4),
1078 "27:4: " + getCheckMessage(MSG_ERROR, "final", 3, 7),
1079 "28:4: " + getCheckMessage(MSG_ERROR, "void", 3, 7),
1080 "29:6: " + getCheckMessage(MSG_ERROR, "method6", 5, 7),
1081 "39:7: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 6, 8),
1082 "40:7: " + getCheckMessage(MSG_ERROR, "if", 6, 8),
1083 "41:11: " + getCheckMessage(MSG_CHILD_ERROR, "if", 10, 12),
1084 "42:7: " + getCheckMessage(MSG_ERROR, "if rcurly", 6, 8),
1085 "45:11: " + getCheckMessage(MSG_ERROR, "Arrays", 10, 12),
1086 "51:15: " + getCheckMessage(MSG_ERROR, "new", 14, 16),
1087 "54:11: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 10, 12),
1088 "59:15: " + getCheckMessage(MSG_ERROR, "new", 14, 16),
1089 "63:11: " + getCheckMessage(MSG_ERROR, "new", 10, 12),
1090 "67:11: " + getCheckMessage(MSG_ERROR, "new", 10, 12),
1091 "68:7: " + getCheckMessage(MSG_ERROR, ")", 6, 8),
1092 "72:7: " + getCheckMessage(MSG_ERROR, "method call rparen", 6, 8),
1093 "86:5: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 4, 8),
1094 "91:5: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 4, 8),
1095 "95:1: " + getCheckMessage(MSG_ERROR, "int", 0, 8),
1096 "96:5: " + getCheckMessage(MSG_ERROR, "method9", 4, 8),
1097 "106:13: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 12, 8),
1098 };
1099 verifyWarns(checkConfig, fileName, expected);
1100 }
1101
1102 @Test
1103 public void testAlternativeGoogleStyleSwitchCaseAndEnums()
1104 throws Exception {
1105 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1106
1107 checkConfig.addProperty("arrayInitIndent", "4");
1108 checkConfig.addProperty("basicOffset", "2");
1109 checkConfig.addProperty("braceAdjustment", "2");
1110 checkConfig.addProperty("caseIndent", "2");
1111 checkConfig.addProperty("forceStrictCondition", "false");
1112 checkConfig.addProperty("lineWrappingIndentation", "4");
1113 checkConfig.addProperty("tabWidth", "4");
1114 checkConfig.addProperty("throwsIndent", "4");
1115 final String fileName = getPath("InputIndentationSwitchCasesAndEnums.java");
1116 final String[] expected = {
1117 "18:7: " + getCheckMessage(MSG_CHILD_ERROR, "block", 6, 4),
1118 "35:7: " + getCheckMessage(MSG_ERROR, "block lcurly", 6, 8),
1119 "38:11: " + getCheckMessage(MSG_ERROR, "block rcurly", 10, 8),
1120 "54:5: " + getCheckMessage(MSG_ERROR, "block lcurly", 4, 2),
1121 "55:3: " + getCheckMessage(MSG_CHILD_ERROR, "block", 2, 4),
1122 };
1123 verifyWarns(checkConfig, fileName, expected);
1124 }
1125
1126 @Test
1127 public void testInvalidSwitchWithChecker()
1128 throws Exception {
1129 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1130
1131 checkConfig.addProperty("arrayInitIndent", "4");
1132 checkConfig.addProperty("basicOffset", "4");
1133 checkConfig.addProperty("braceAdjustment", "0");
1134 checkConfig.addProperty("caseIndent", "4");
1135 checkConfig.addProperty("forceStrictCondition", "false");
1136 checkConfig.addProperty("lineWrappingIndentation", "4");
1137 checkConfig.addProperty("tabWidth", "4");
1138 checkConfig.addProperty("throwsIndent", "4");
1139 final String fileName = getPath("InputIndentationInvalidSwitchIndent.java");
1140 final String[] expected = {
1141 "30:7: " + getCheckMessage(MSG_ERROR, "switch", 6, 8),
1142 "32:11: " + getCheckMessage(MSG_CHILD_ERROR, "case", 10, 12),
1143 "33:15: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
1144 "37:15: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
1145 "39:15: " + getCheckMessage(MSG_CHILD_ERROR, "case", 14, 12),
1146 "40:11: " + getCheckMessage(MSG_CHILD_ERROR, "case", 10, 12),
1147 "43:11: " + getCheckMessage(MSG_CHILD_ERROR, "case", 10, 12),
1148 "44:15: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
1149 "45:15: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
1150 "53:15: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
1151 "54:19: " + getCheckMessage(MSG_CHILD_ERROR, "block", 18, 16),
1152 "55:11: " + getCheckMessage(MSG_ERROR, "block rcurly", 10, 12),
1153 "59:11: " + getCheckMessage(MSG_ERROR, "block lcurly", 10, 12),
1154 "62:15: " + getCheckMessage(MSG_ERROR, "block rcurly", 14, 12),
1155 "66:15: " + getCheckMessage(MSG_ERROR, "block lcurly", 14, 12),
1156 "69:11: " + getCheckMessage(MSG_ERROR, "block rcurly", 10, 12),
1157 "76:15: " + getCheckMessage(MSG_CHILD_ERROR, "case", 14, 16),
1158 "81:15: " + getCheckMessage(MSG_CHILD_ERROR, "case", 14, 16),
1159 "89:7: " + getCheckMessage(MSG_ERROR, "switch rcurly", 6, 8),
1160 "92:7: " + getCheckMessage(MSG_ERROR, "switch lcurly", 6, 8),
1161 "93:11: " + getCheckMessage(MSG_ERROR, "switch rcurly", 10, 8),
1162 "95:11: " + getCheckMessage(MSG_ERROR, "switch lcurly", 10, 8),
1163 "96:7: " + getCheckMessage(MSG_ERROR, "switch rcurly", 6, 8),
1164 "99:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 12),
1165 "100:13: " + getCheckMessage(MSG_ERROR, "if", 12, 16),
1166 "101:17: " + getCheckMessage(MSG_CHILD_ERROR, "if", 16, 20),
1167 "102:13: " + getCheckMessage(MSG_ERROR, "else", 12, 16),
1168 "103:17: " + getCheckMessage(MSG_CHILD_ERROR, "else", 16, 20),
1169 "106:17: " + getCheckMessage(MSG_CHILD_ERROR, "case", 4, 12),
1170 };
1171 verifyWarns(checkConfig, fileName, expected);
1172 }
1173
1174 @Test
1175 public void testIfElseWithNoCurly()
1176 throws Exception {
1177 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1178
1179 checkConfig.addProperty("arrayInitIndent", "4");
1180 checkConfig.addProperty("basicOffset", "4");
1181 checkConfig.addProperty("braceAdjustment", "0");
1182 checkConfig.addProperty("caseIndent", "4");
1183 checkConfig.addProperty("forceStrictCondition", "false");
1184 checkConfig.addProperty("lineWrappingIndentation", "4");
1185 checkConfig.addProperty("tabWidth", "4");
1186 checkConfig.addProperty("throwsIndent", "4");
1187 final String fileName = getPath("InputIndentationIfElseWithNoCurly.java");
1188 final String[] expected = {
1189 "20:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
1190 "25:5: " + getCheckMessage(MSG_ERROR, "if", 4, 8),
1191 "26:9: " + getCheckMessage(MSG_CHILD_ERROR, "if", 8, 12),
1192 "37:13: " + getCheckMessage(MSG_ERROR, "else", 12, 8),
1193 "39:9: " + getCheckMessage(MSG_ERROR, "if", 8, 12),
1194 "43:9: " + getCheckMessage(MSG_CHILD_ERROR, "if", 8, 16),
1195 };
1196 verifyWarns(checkConfig, fileName, expected);
1197 }
1198
1199 @Test
1200 public void testWhileWithNoCurly()
1201 throws Exception {
1202 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1203
1204 checkConfig.addProperty("arrayInitIndent", "4");
1205 checkConfig.addProperty("basicOffset", "4");
1206 checkConfig.addProperty("braceAdjustment", "0");
1207 checkConfig.addProperty("caseIndent", "4");
1208 checkConfig.addProperty("forceStrictCondition", "false");
1209 checkConfig.addProperty("lineWrappingIndentation", "4");
1210 checkConfig.addProperty("tabWidth", "4");
1211 checkConfig.addProperty("throwsIndent", "4");
1212 final String fileName = getPath("InputIndentationWhileNoCurly.java");
1213 final String[] expected = {
1214 "21:1: " + getCheckMessage(MSG_CHILD_ERROR, "while", 0, 12),
1215 "26:5: " + getCheckMessage(MSG_ERROR, "while", 4, 8),
1216 "27:9: " + getCheckMessage(MSG_CHILD_ERROR, "while", 8, 12),
1217 "32:9: " + getCheckMessage(MSG_ERROR, "while", 8, 12),
1218 "36:9: " + getCheckMessage(MSG_CHILD_ERROR, "while", 8, 16),
1219 };
1220 verifyWarns(checkConfig, fileName, expected);
1221 }
1222
1223 @Test
1224 public void testForWithNoCurly()
1225 throws Exception {
1226 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1227
1228 checkConfig.addProperty("arrayInitIndent", "4");
1229 checkConfig.addProperty("basicOffset", "4");
1230 checkConfig.addProperty("braceAdjustment", "0");
1231 checkConfig.addProperty("caseIndent", "4");
1232 checkConfig.addProperty("forceStrictCondition", "false");
1233 checkConfig.addProperty("lineWrappingIndentation", "4");
1234 checkConfig.addProperty("tabWidth", "4");
1235 checkConfig.addProperty("throwsIndent", "4");
1236 final String fileName = getPath("InputIndentationForWithoutCurly.java");
1237 final String[] expected = {
1238 "21:1: " + getCheckMessage(MSG_CHILD_ERROR, "for", 0, 12),
1239 "26:5: " + getCheckMessage(MSG_ERROR, "for", 4, 8),
1240 "27:9: " + getCheckMessage(MSG_CHILD_ERROR, "for", 8, 12),
1241 "32:9: " + getCheckMessage(MSG_ERROR, "for", 8, 12),
1242 "33:9: " + getCheckMessage(MSG_CHILD_ERROR, "for", 8, 16),
1243 "37:9: " + getCheckMessage(MSG_CHILD_ERROR, "for", 8, 16),
1244
1245 };
1246 verifyWarns(checkConfig, fileName, expected);
1247 }
1248
1249 @Test
1250 public void testDoWhileWithoutCurly()
1251 throws Exception {
1252 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1253
1254 checkConfig.addProperty("arrayInitIndent", "4");
1255 checkConfig.addProperty("basicOffset", "4");
1256 checkConfig.addProperty("braceAdjustment", "0");
1257 checkConfig.addProperty("caseIndent", "4");
1258 checkConfig.addProperty("forceStrictCondition", "false");
1259 checkConfig.addProperty("lineWrappingIndentation", "4");
1260 checkConfig.addProperty("tabWidth", "4");
1261 checkConfig.addProperty("throwsIndent", "4");
1262 final String fileName = getPath("InputIndentationDoWhile.java");
1263 final String[] expected = {
1264 "23:9: " + getCheckMessage(MSG_CHILD_ERROR, "do..while", 8, 12),
1265 "30:5: " + getCheckMessage(MSG_ERROR, "do..while while", 4, 8),
1266 "33:13: " + getCheckMessage(MSG_ERROR, "do..while while", 12, 8),
1267 };
1268 verifyWarns(checkConfig, fileName, expected);
1269 }
1270
1271 @Test
1272 public void testValidSwitchWithChecker()
1273 throws Exception {
1274 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1275
1276 checkConfig.addProperty("arrayInitIndent", "4");
1277 checkConfig.addProperty("basicOffset", "4");
1278 checkConfig.addProperty("braceAdjustment", "0");
1279 checkConfig.addProperty("caseIndent", "4");
1280 checkConfig.addProperty("forceStrictCondition", "false");
1281 checkConfig.addProperty("lineWrappingIndentation", "4");
1282 checkConfig.addProperty("tabWidth", "4");
1283 checkConfig.addProperty("throwsIndent", "4");
1284 final String fileName = getPath("InputIndentationValidSwitchIndent.java");
1285 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1286 verifyWarns(checkConfig, fileName, expected);
1287 }
1288
1289 @Test
1290 public void testNewKeyword() throws Exception {
1291 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1292
1293 checkConfig.addProperty("basicOffset", "4");
1294 checkConfig.addProperty("forceStrictCondition", "false");
1295 checkConfig.addProperty("lineWrappingIndentation", "8");
1296 checkConfig.addProperty("tabWidth", "4");
1297 checkConfig.addProperty("throwsIndent", "8");
1298 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1299 verifyWarns(checkConfig, getPath("InputIndentationNew.java"), expected);
1300 }
1301
1302 @Test
1303 public void testNewKeywordChildren() throws Exception {
1304 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1305
1306 checkConfig.addProperty("arrayInitIndent", "2");
1307 checkConfig.addProperty("basicOffset", "2");
1308 checkConfig.addProperty("braceAdjustment", "2");
1309 checkConfig.addProperty("caseIndent", "2");
1310 checkConfig.addProperty("forceStrictCondition", "false");
1311 checkConfig.addProperty("lineWrappingIndentation", "4");
1312 checkConfig.addProperty("tabWidth", "4");
1313 checkConfig.addProperty("throwsIndent", "4");
1314 final String[] expected = {
1315 "27:1: " + getCheckMessage(MSG_ERROR_MULTI, "new", 0, "14, 16"),
1316 "28:1: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "new", 0, "18, 20"),
1317 "36:1: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "new", 0, "18, 20"),
1318 "42:9: " + getCheckMessage(MSG_ERROR, "new", 8, 12),
1319 "43:11: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "object def", 10, "14, 16, 18"),
1320 "51:1: " + getCheckMessage(MSG_CHILD_ERROR, "new", 0, 16),
1321 "56:5: " + getCheckMessage(MSG_CHILD_ERROR, "new", 4, 8),
1322 "57:5: " + getCheckMessage(MSG_CHILD_ERROR, "new", 4, 8),
1323 "63:5: " + getCheckMessage(MSG_ERROR_MULTI, "lambda arguments", 4, "10, 12"),
1324 "64:1: " + getCheckMessage(MSG_CHILD_ERROR, "new", 0, 8),
1325 };
1326 verifyWarns(checkConfig, getPath("InputIndentationNewChildren.java"), expected);
1327 }
1328
1329 @Test
1330 public void testNewKeywordChildrenSevntuConfig() throws Exception {
1331 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1332
1333 checkConfig.addProperty("arrayInitIndent", "4");
1334 checkConfig.addProperty("basicOffset", "4");
1335 checkConfig.addProperty("braceAdjustment", "0");
1336 checkConfig.addProperty("caseIndent", "4");
1337 checkConfig.addProperty("forceStrictCondition", "false");
1338 checkConfig.addProperty("lineWrappingIndentation", "8");
1339 checkConfig.addProperty("tabWidth", "4");
1340 checkConfig.addProperty("throwsIndent", "8");
1341 final String[] expected = {
1342 "43:29: " + getCheckMessage(MSG_CHILD_ERROR, "new", 28, 36),
1343 };
1344 verifyWarns(checkConfig,
1345 getPath("InputIndentationNewChildrenSevntuConfig.java"), expected);
1346 }
1347
1348 @Test
1349 public void testNewKeyword2() throws Exception {
1350 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1351
1352 checkConfig.addProperty("basicOffset", "4");
1353 checkConfig.addProperty("forceStrictCondition", "true");
1354 checkConfig.addProperty("lineWrappingIndentation", "8");
1355 checkConfig.addProperty("tabWidth", "4");
1356 checkConfig.addProperty("throwsIndent", "8");
1357 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1358 verifyWarns(checkConfig, getPath("InputIndentationNew.java"), expected);
1359 }
1360
1361
1362 @Test
1363 public void testTextBlockLiteral() throws Exception {
1364 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1365
1366 checkConfig.addProperty("lineWrappingIndentation", "4");
1367 checkConfig.addProperty("forceStrictCondition", "true");
1368 checkConfig.addProperty("tabWidth", "4");
1369 final String[] expected = {
1370 "18:1: " + getCheckMessage(MSG_ERROR, "\"\"\"", 0, 8),
1371 "29:17: " + getCheckMessage(MSG_ERROR, "\"\"\"", 16, 12),
1372 "46:1: " + getCheckMessage(MSG_ERROR, "\"\"\"", 0, 12),
1373 "52:1: " + getCheckMessage(MSG_ERROR, "\"\"\"", 0, 12),
1374 "59:9: " + getCheckMessage(MSG_ERROR, "\"\"\"", 8, 12),
1375 "78:15: " + getCheckMessage(MSG_ERROR, "\"\"\"", 14, 12),
1376 };
1377 verifyWarns(checkConfig, getPath("InputIndentationTextBlock.java"),
1378 expected);
1379 }
1380
1381 @Test
1382 public void testValidNewKeywordWithForceStrictCondition() throws Exception {
1383 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1384
1385 checkConfig.addProperty("basicOffset", "4");
1386 checkConfig.addProperty("forceStrictCondition", "true");
1387 checkConfig.addProperty("lineWrappingIndentation", "8");
1388 checkConfig.addProperty("tabWidth", "4");
1389 checkConfig.addProperty("throwsIndent", "8");
1390 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1391 verifyWarns(checkConfig, getPath("InputIndentationNew.java"), expected);
1392 }
1393
1394 @Test
1395 public void testInvalidNewKeywordWithForceStrictCondition() throws Exception {
1396 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1397
1398 checkConfig.addProperty("basicOffset", "4");
1399 checkConfig.addProperty("forceStrictCondition", "true");
1400 checkConfig.addProperty("lineWrappingIndentation", "8");
1401 checkConfig.addProperty("tabWidth", "4");
1402 checkConfig.addProperty("throwsIndent", "8");
1403 final String[] expected = {
1404 "21:12: " + getCheckMessage(MSG_ERROR, "]", 11, 12),
1405 "25:5: " + getCheckMessage(MSG_ERROR, "[", 4, 12),
1406 "32:17: " + getCheckMessage(MSG_ERROR, "new", 16, 24),
1407 "33:21: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "object def", 20, "28, 32, 36"),
1408 "34:17: " + getCheckMessage(MSG_ERROR_MULTI, "object def rcurly", 16, "24, 28, 32"),
1409 "37:36: " + getCheckMessage(MSG_ERROR, "+", 35, 16),
1410 "41:35: " + getCheckMessage(MSG_ERROR, "]", 34, 16),
1411 "45:36: " + getCheckMessage(MSG_ERROR, "42", 35, 16),
1412 "49:36: " + getCheckMessage(MSG_ERROR, "+", 35, 16),
1413 "50:36: " + getCheckMessage(MSG_ERROR, "+", 35, 16),
1414 "55:21: " + getCheckMessage(MSG_ERROR, "1", 20, 16),
1415 "59:13: " + getCheckMessage(MSG_ERROR, "fun2", 12, 16),
1416 "78:11: " + getCheckMessage(MSG_ERROR, "Object", 10, 12),
1417 "82:16: " + getCheckMessage(MSG_ERROR, "]", 15, 12),
1418 };
1419 verifyWarns(checkConfig,
1420 getPath("InputIndentationNewWithForceStrictCondition.java"), expected);
1421 }
1422
1423 @Test
1424 public void testValidArrayInitDefaultIndentWithChecker()
1425 throws Exception {
1426 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1427
1428 checkConfig.addProperty("arrayInitIndent", "4");
1429 checkConfig.addProperty("basicOffset", "4");
1430 checkConfig.addProperty("braceAdjustment", "0");
1431 checkConfig.addProperty("caseIndent", "4");
1432 checkConfig.addProperty("forceStrictCondition", "false");
1433 checkConfig.addProperty("lineWrappingIndentation", "4");
1434 checkConfig.addProperty("tabWidth", "4");
1435 checkConfig.addProperty("throwsIndent", "4");
1436 final String fileName = getPath("InputIndentationValidArrayInitDefaultIndent.java");
1437 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1438 verifyWarns(checkConfig, fileName, expected);
1439 }
1440
1441 @Test
1442 public void testValidArrayInitWithChecker()
1443 throws Exception {
1444 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1445
1446 checkConfig.addProperty("arrayInitIndent", "8");
1447 checkConfig.addProperty("basicOffset", "4");
1448 checkConfig.addProperty("braceAdjustment", "0");
1449 checkConfig.addProperty("caseIndent", "4");
1450 checkConfig.addProperty("forceStrictCondition", "false");
1451 checkConfig.addProperty("lineWrappingIndentation", "4");
1452 checkConfig.addProperty("tabWidth", "4");
1453 checkConfig.addProperty("throwsIndent", "4");
1454 final String fileName = getPath("InputIndentationValidArrayInitIndent.java");
1455 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1456 verifyWarns(checkConfig, fileName, expected);
1457 }
1458
1459 @Test
1460 public void testValidArrayInitTwoDimensional() throws Exception {
1461 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1462
1463 checkConfig.addProperty("arrayInitIndent", "2");
1464 checkConfig.addProperty("basicOffset", "4");
1465 checkConfig.addProperty("braceAdjustment", "4");
1466 checkConfig.addProperty("caseIndent", "4");
1467 checkConfig.addProperty("forceStrictCondition", "false");
1468 checkConfig.addProperty("lineWrappingIndentation", "4");
1469 checkConfig.addProperty("tabWidth", "4");
1470 checkConfig.addProperty("throwsIndent", "4");
1471 final String fileName = getPath("InputIndentationValidArrayInitIndentTwoDimensional.java");
1472 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1473 verifyWarns(checkConfig, fileName, expected);
1474 }
1475
1476 @Test
1477 public void testInvalidArrayInitTwoDimensional() throws Exception {
1478 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1479
1480 checkConfig.addProperty("arrayInitIndent", "2");
1481 checkConfig.addProperty("basicOffset", "4");
1482 checkConfig.addProperty("braceAdjustment", "4");
1483 checkConfig.addProperty("caseIndent", "4");
1484 checkConfig.addProperty("forceStrictCondition", "false");
1485 checkConfig.addProperty("lineWrappingIndentation", "4");
1486 checkConfig.addProperty("tabWidth", "4");
1487 checkConfig.addProperty("throwsIndent", "4");
1488 final String fileName =
1489 getPath("InputIndentationInvalidArrayInitIndentTwoDimensional.java");
1490 final String[] expected = {
1491 "18:5: " + getCheckMessage(MSG_ERROR_MULTI,
1492 "array initialization lcurly", 4, "6, 8, 18, 20, 24"),
1493 "23:10: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
1494 "array initialization", 9, "8, 10, 12, 20, 22, 24"),
1495 "26:7: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
1496 "array initialization", 6, "8, 10, 12, 20, 22, 24"),
1497 "28:5: " + getCheckMessage(MSG_ERROR_MULTI,
1498 "array initialization lcurly", 4, "6, 8, 18, 20, 24"),
1499 "30:5: " + getCheckMessage(MSG_ERROR_MULTI,
1500 "array initialization rcurly", 4, "6, 8, 18, 20, 24"),
1501
1502 };
1503 verifyWarns(checkConfig, fileName, expected);
1504 }
1505
1506 @Test
1507 public void testValidArrayInit()
1508 throws Exception {
1509 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1510
1511 checkConfig.addProperty("arrayInitIndent", "2");
1512 checkConfig.addProperty("basicOffset", "2");
1513 checkConfig.addProperty("braceAdjustment", "2");
1514 checkConfig.addProperty("caseIndent", "2");
1515 checkConfig.addProperty("forceStrictCondition", "false");
1516 checkConfig.addProperty("lineWrappingIndentation", "4");
1517 checkConfig.addProperty("tabWidth", "4");
1518 checkConfig.addProperty("throwsIndent", "4");
1519 final String fileName = getPath("InputIndentationValidArrayInitIndentTwo.java");
1520 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1521 verifyWarns(checkConfig, fileName, expected);
1522 }
1523
1524 @Test
1525 public void testArrayInitWithEmoji() throws Exception {
1526 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1527
1528 checkConfig.addProperty("arrayInitIndent", "2");
1529 checkConfig.addProperty("basicOffset", "2");
1530 checkConfig.addProperty("braceAdjustment", "2");
1531 checkConfig.addProperty("caseIndent", "2");
1532 checkConfig.addProperty("forceStrictCondition", "false");
1533 checkConfig.addProperty("lineWrappingIndentation", "4");
1534 checkConfig.addProperty("tabWidth", "4");
1535 checkConfig.addProperty("throwsIndent", "4");
1536 final String fileName = getPath("InputIndentationArrayInitIndentWithEmoji.java");
1537 final String[] expected = {
1538 "19:6: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1539 5, "4, 6, 52, 54"),
1540 "24:9: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1541 8, "4, 6, 35, 37"),
1542 "25:11: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1543 10, "4, 6, 35, 37"),
1544 "30:11: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly",
1545 10, "4, 6, 19, 21, 25"),
1546 };
1547 verifyWarns(checkConfig, fileName, expected);
1548 }
1549
1550 @Test
1551 public void testYieldKeywordWithForceStrictCondition() throws Exception {
1552 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1553 checkConfig.addProperty("forceStrictCondition", "true");
1554 checkConfig.addProperty("tabWidth", "4");
1555 final String[] expected = {
1556 "15:13: " + getCheckMessage(MSG_CHILD_ERROR, "block", 12, 16),
1557 "16:13: " + getCheckMessage(MSG_ERROR, "yield", 12, 16),
1558 "44:13: " + getCheckMessage(MSG_CHILD_ERROR, "block", 12, 16),
1559 "45:13: " + getCheckMessage(MSG_ERROR, "yield", 12, 16),
1560 "50:5: " + getCheckMessage(MSG_ERROR, "yield", 4, 16),
1561 "71:15: " + getCheckMessage(MSG_ERROR, "yield", 14, 16),
1562 "74:20: " + getCheckMessage(MSG_ERROR, "yield", 19, 16),
1563 "77:9: " + getCheckMessage(MSG_ERROR, "yield", 8, 16),
1564 };
1565 verifyWarns(checkConfig,
1566 getPath("InputIndentationYieldForceStrict.java"), expected);
1567 }
1568
1569 @Test
1570 public void testChainedMethodCalling() throws Exception {
1571 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1572
1573 checkConfig.addProperty("arrayInitIndent", "2");
1574 checkConfig.addProperty("basicOffset", "2");
1575 checkConfig.addProperty("braceAdjustment", "2");
1576 checkConfig.addProperty("caseIndent", "2");
1577 checkConfig.addProperty("forceStrictCondition", "false");
1578 checkConfig.addProperty("lineWrappingIndentation", "4");
1579 checkConfig.addProperty("tabWidth", "4");
1580 checkConfig.addProperty("throwsIndent", "4");
1581 final String fileName = getPath("InputIndentationChainedMethodCalls.java");
1582 final String[] expected = {
1583 "33:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 8),
1584 "38:5: " + getCheckMessage(MSG_ERROR, ".", 4, 8),
1585 "39:5: " + getCheckMessage(MSG_ERROR, ".", 4, 8),
1586 "42:5: " + getCheckMessage(MSG_ERROR, "new", 4, 8),
1587 };
1588 verifyWarns(checkConfig, fileName, expected);
1589 }
1590
1591 @Test
1592 public void testInvalidArrayInitWithTrueStrictCondition()
1593 throws Exception {
1594 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1595
1596 checkConfig.addProperty("arrayInitIndent", "4");
1597 checkConfig.addProperty("basicOffset", "4");
1598 checkConfig.addProperty("braceAdjustment", "0");
1599 checkConfig.addProperty("caseIndent", "4");
1600 checkConfig.addProperty("forceStrictCondition", "true");
1601 checkConfig.addProperty("lineWrappingIndentation", "4");
1602 checkConfig.addProperty("tabWidth", "4");
1603 checkConfig.addProperty("throwsIndent", "4");
1604 final String fileName = getPath("InputIndentationInvalidArrayInitIndent.java");
1605 final String[] expected = {
1606 "21:3: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4),
1607 "22:7: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4),
1608 "24:3: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4),
1609 "28:7: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4),
1610 "29:9: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 8,
1611 "10, 34, 36"),
1612 "30:5: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 4, "6, 10"),
1613 "33:10: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 9,
1614 "8, 31, 33"),
1615 "34:8: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 7,
1616 "8, 31, 33"),
1617 "35:10: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 9,
1618 "8, 31, 33"),
1619 "40:3: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 2, "4, 8"),
1620 "44:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "4, 8"),
1621 "48:3: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 2, "4, 8"),
1622 "52:21: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 20,
1623 "8, 31, 33"),
1624 "53:5: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1625 4, "8, 31, 33"),
1626 "58:7: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1627 6, "8, 31, 33"),
1628 "63:3: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4),
1629 "65:7: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4),
1630 "66:3: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 2, "6, 10"),
1631 "69:7: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1632 6, "8, 36, 38"),
1633 "76:11: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1634 10, "12, 24, 26"),
1635 };
1636
1637 verifyWarns(checkConfig, fileName, expected);
1638 }
1639
1640 @Test
1641 public void testInvalidArrayInitWithTrueStrictCondition1()
1642 throws Exception {
1643 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1644
1645 checkConfig.addProperty("arrayInitIndent", "4");
1646 checkConfig.addProperty("basicOffset", "4");
1647 checkConfig.addProperty("braceAdjustment", "0");
1648 checkConfig.addProperty("caseIndent", "4");
1649 checkConfig.addProperty("forceStrictCondition", "true");
1650 checkConfig.addProperty("lineWrappingIndentation", "4");
1651 checkConfig.addProperty("tabWidth", "4");
1652 checkConfig.addProperty("throwsIndent", "4");
1653 final String fileName = getPath("InputIndentationInvalidArrayInitIndent1.java");
1654 final String[] expected = {
1655 "28:9: " + getCheckMessage(MSG_ERROR, "1", 8, 12),
1656 "39:11: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 10,
1657 "12, 30, 32"),
1658 "40:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 14,
1659 "12, 30, 32"),
1660 "43:11: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 10,
1661 "12, 31, 32"),
1662 "44:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 14,
1663 "12, 31, 32"),
1664 "45:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "8, 12"),
1665 "48:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 6, "8, 12"),
1666 "49:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 14,
1667 "8, 10, 12"),
1668 "51:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "8, 12"),
1669
1670 "59:13: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1671 12, "16, 46, 48"),
1672 "67:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1673 14, "16, 28, 30"),
1674 "68:9: " + getCheckMessage(MSG_ERROR_MULTI, "annotation array initialization rcurly",
1675 8, "12, 16"),
1676 "70:13: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1677 12, "16, 29, 31"),
1678 };
1679
1680 verifyWarns(checkConfig, fileName, expected);
1681 }
1682
1683 @Test
1684 public void testInvalidArrayInitWithFalseStrictCondition()
1685 throws Exception {
1686 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1687
1688 checkConfig.addProperty("arrayInitIndent", "4");
1689 checkConfig.addProperty("basicOffset", "4");
1690 checkConfig.addProperty("braceAdjustment", "0");
1691 checkConfig.addProperty("caseIndent", "4");
1692 checkConfig.addProperty("forceStrictCondition", "false");
1693 checkConfig.addProperty("lineWrappingIndentation", "4");
1694 checkConfig.addProperty("tabWidth", "4");
1695 checkConfig.addProperty("throwsIndent", "4");
1696 final String fileName = getPath("InputIndentationInvalidArrayInitIndent.java");
1697 final String[] expected = {
1698 "21:3: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4),
1699 "22:7: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4),
1700 "24:3: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4),
1701 "28:7: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4),
1702 "29:9: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 8,
1703 "10, 34, 36"),
1704 "30:5: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 4, "6, 10"),
1705 "33:10: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 9,
1706 "8, 31, 33"),
1707 "34:8: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 7,
1708 "8, 31, 33"),
1709 "35:10: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 9,
1710 "8, 31, 33"),
1711 "40:3: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 2, "4, 8"),
1712 "44:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "4, 8"),
1713 "48:3: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 2, "4, 8"),
1714 "52:21: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 20,
1715 "8, 31, 33"),
1716 "53:5: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1717 4, "8, 31, 33"),
1718 "58:7: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1719 6, "8, 31, 33"),
1720 "63:3: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4),
1721 "65:7: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4),
1722 "66:3: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 2, "6, 10"),
1723 "69:7: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1724 6, "8, 36, 38"),
1725 "76:11: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1726 10, "12, 24, 26"),
1727 };
1728
1729 verifyWarns(checkConfig, fileName, expected);
1730 }
1731
1732 @Test
1733 public void testInvalidArrayInitWithFalseStrictCondition1()
1734 throws Exception {
1735 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1736
1737 checkConfig.addProperty("arrayInitIndent", "4");
1738 checkConfig.addProperty("basicOffset", "4");
1739 checkConfig.addProperty("braceAdjustment", "0");
1740 checkConfig.addProperty("caseIndent", "4");
1741 checkConfig.addProperty("forceStrictCondition", "false");
1742 checkConfig.addProperty("lineWrappingIndentation", "4");
1743 checkConfig.addProperty("tabWidth", "4");
1744 checkConfig.addProperty("throwsIndent", "4");
1745 final String fileName = getPath("InputIndentationInvalidArrayInitIndent1.java");
1746 final String[] expected = {
1747 "28:9: " + getCheckMessage(MSG_ERROR, "1", 8, 12),
1748 "39:11: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 10,
1749 "12, 30, 32"),
1750 "40:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 14,
1751 "12, 30, 32"),
1752 "43:11: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 10,
1753 "12, 31, 32"),
1754 "44:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 14,
1755 "12, 31, 32"),
1756 "45:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "8, 12"),
1757 "48:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 6, "8, 12"),
1758 "49:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 14,
1759 "8, 10, 12"),
1760 "51:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "8, 12"),
1761
1762 "59:13: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1763 12, "16, 46, 48"),
1764 "67:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1765 14, "16, 28, 30"),
1766 "68:9: " + getCheckMessage(MSG_ERROR_MULTI, "annotation array initialization rcurly",
1767 8, "12, 16"),
1768 "70:13: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1769 12, "16, 29, 31"),
1770 };
1771
1772 verifyWarns(checkConfig, fileName, expected);
1773 }
1774
1775
1776 @Test
1777 public void testInvalidArrayInitIndentNoCommentsTrueStrictCondition()
1778 throws Exception {
1779 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1780
1781 checkConfig.addProperty("arrayInitIndent", "4");
1782 checkConfig.addProperty("basicOffset", "4");
1783 checkConfig.addProperty("braceAdjustment", "0");
1784 checkConfig.addProperty("caseIndent", "4");
1785 checkConfig.addProperty("forceStrictCondition", "true");
1786 checkConfig.addProperty("lineWrappingIndentation", "4");
1787 checkConfig.addProperty("tabWidth", "4");
1788 checkConfig.addProperty("throwsIndent", "4");
1789 final String fileName = getPath(
1790 "InputIndentationInvalidArrInitIndentNoTrailingComments.java");
1791 final String[] expected = {
1792 "31:13: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1793 12, "16, 46, 48"),
1794 "37:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1795 14, "12, 16"),
1796 "41:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1797 14, "16, 28, 30"),
1798 "42:9: " + getCheckMessage(MSG_ERROR_MULTI, "annotation array initialization rcurly",
1799 8, "12, 16"),
1800 "45:13: " + getCheckMessage(MSG_CHILD_ERROR, "annotation array initialization",
1801 12, 16),
1802 };
1803 verifyWarns(checkConfig, fileName, expected);
1804 }
1805
1806
1807 @Test
1808 public void testInvalidArrayInitIndentNoCommentsFalseStrictCondition()
1809 throws Exception {
1810 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1811
1812 checkConfig.addProperty("arrayInitIndent", "4");
1813 checkConfig.addProperty("basicOffset", "4");
1814 checkConfig.addProperty("braceAdjustment", "0");
1815 checkConfig.addProperty("caseIndent", "4");
1816 checkConfig.addProperty("forceStrictCondition", "false");
1817 checkConfig.addProperty("lineWrappingIndentation", "4");
1818 checkConfig.addProperty("tabWidth", "4");
1819 checkConfig.addProperty("throwsIndent", "4");
1820 final String fileName = getPath(
1821 "InputIndentationInvalidArrInitIndentNoTrailingComments.java");
1822 final String[] expected = {
1823 "31:13: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1824 12, "16, 46, 48"),
1825 "37:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1826 14, "12, 16"),
1827 "41:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1828 14, "16, 28, 30"),
1829 "42:9: " + getCheckMessage(MSG_ERROR_MULTI, "annotation array initialization rcurly",
1830 8, "12, 16"),
1831 "45:13: " + getCheckMessage(MSG_CHILD_ERROR, "annotation array initialization",
1832 12, 16),
1833 };
1834 verifyWarns(checkConfig, fileName, expected);
1835 }
1836
1837 @Test
1838 public void testValidTryWithChecker()
1839 throws Exception {
1840 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1841
1842 checkConfig.addProperty("arrayInitIndent", "4");
1843 checkConfig.addProperty("basicOffset", "4");
1844 checkConfig.addProperty("braceAdjustment", "0");
1845 checkConfig.addProperty("caseIndent", "4");
1846 checkConfig.addProperty("forceStrictCondition", "false");
1847 checkConfig.addProperty("lineWrappingIndentation", "4");
1848 checkConfig.addProperty("tabWidth", "4");
1849 checkConfig.addProperty("throwsIndent", "4");
1850 final String fileName = getPath("InputIndentationValidTryIndent.java");
1851 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1852 verifyWarns(checkConfig, fileName, expected);
1853 }
1854
1855 @Test
1856 public void testInvalidTryWithChecker()
1857 throws Exception {
1858 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1859
1860 checkConfig.addProperty("arrayInitIndent", "4");
1861 checkConfig.addProperty("basicOffset", "4");
1862 checkConfig.addProperty("braceAdjustment", "0");
1863 checkConfig.addProperty("caseIndent", "4");
1864 checkConfig.addProperty("forceStrictCondition", "false");
1865 checkConfig.addProperty("lineWrappingIndentation", "4");
1866 checkConfig.addProperty("tabWidth", "4");
1867 checkConfig.addProperty("throwsIndent", "4");
1868 final String fileName = getPath("InputIndentationInvalidTryIndent.java");
1869 final String[] expected = {
1870 "25:10: " + getCheckMessage(MSG_ERROR, "try", 9, 8),
1871 "26:8: " + getCheckMessage(MSG_ERROR, "try rcurly", 7, 8),
1872 "28:8: " + getCheckMessage(MSG_ERROR, "catch rcurly", 7, 8),
1873 "30:5: " + getCheckMessage(MSG_ERROR, "try", 4, 8),
1874 "31:9: " + getCheckMessage(MSG_CHILD_ERROR, "try", 8, 12),
1875 "32:5: " + getCheckMessage(MSG_ERROR, "try rcurly", 4, 8),
1876 "33:9: " + getCheckMessage(MSG_CHILD_ERROR, "finally", 8, 12),
1877 "38:9: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 8, 12),
1878 "43:11: " + getCheckMessage(MSG_ERROR, "try rcurly", 10, 8),
1879 "45:7: " + getCheckMessage(MSG_ERROR, "catch rcurly", 6, 8),
1880 "52:6: " + getCheckMessage(MSG_ERROR, "catch rcurly", 5, 8),
1881 "59:11: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 10, 12),
1882 "60:15: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 14, 12),
1883 "61:11: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 10, 12),
1884 "63:7: " + getCheckMessage(MSG_ERROR, "catch", 6, 8),
1885 "70:11: " + getCheckMessage(MSG_ERROR, "try lcurly", 10, 8),
1886 "72:11: " + getCheckMessage(MSG_ERROR, "try rcurly", 10, 8),
1887 "74:7: " + getCheckMessage(MSG_ERROR, "catch lcurly", 6, 8),
1888 "77:11: " + getCheckMessage(MSG_ERROR, "catch rcurly", 10, 8),
1889 "80:11: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 10, 12),
1890 "86:1: " + getCheckMessage(MSG_ERROR, "try", 0, 8),
1891 "87:1: " + getCheckMessage(MSG_ERROR, "try rcurly", 0, 8),
1892 "88:1: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 0, 12),
1893 "89:1: " + getCheckMessage(MSG_ERROR, "catch rcurly", 0, 8),
1894 "91:1: " + getCheckMessage(MSG_ERROR, "try", 0, 8),
1895 "92:1: " + getCheckMessage(MSG_ERROR, "try rcurly", 0, 8),
1896 "93:1: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 0, 12),
1897 "94:1: " + getCheckMessage(MSG_ERROR, "catch rcurly", 0, 8),
1898 };
1899 verifyWarns(checkConfig, fileName, expected);
1900 }
1901
1902 @Test
1903 public void testCatchParametersOnNewLine()
1904 throws Exception {
1905 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1906
1907 checkConfig.addProperty("arrayInitIndent", "2");
1908 checkConfig.addProperty("basicOffset", "2");
1909 checkConfig.addProperty("braceAdjustment", "2");
1910 checkConfig.addProperty("caseIndent", "2");
1911 checkConfig.addProperty("forceStrictCondition", "false");
1912 checkConfig.addProperty("lineWrappingIndentation", "4");
1913 checkConfig.addProperty("tabWidth", "4");
1914 checkConfig.addProperty("throwsIndent", "4");
1915 final String fileName =
1916 getPath("InputIndentationCatchParametersOnNewLine.java");
1917
1918 final String[] expected = {
1919 "22:1: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 0, 8),
1920 "31:5: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 4, 8),
1921 "38:5: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 4, 8),
1922 "48:13: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 12, 8),
1923 "64:9: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 8, 12),
1924 };
1925 verifyWarns(checkConfig, fileName, expected);
1926 }
1927
1928 @Test
1929 public void testMultiLineStatements()
1930 throws Exception {
1931 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1932
1933 checkConfig.addProperty("arrayInitIndent", "2");
1934 checkConfig.addProperty("basicOffset", "2");
1935 checkConfig.addProperty("braceAdjustment", "2");
1936 checkConfig.addProperty("caseIndent", "2");
1937 checkConfig.addProperty("forceStrictCondition", "false");
1938 checkConfig.addProperty("lineWrappingIndentation", "4");
1939 checkConfig.addProperty("tabWidth", "4");
1940 checkConfig.addProperty("throwsIndent", "4");
1941 final String fileName =
1942 getPath("InputIndentationMultilineStatements.java");
1943
1944 final String[] expected = {
1945 "23:7: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 6, 8),
1946 "39:7: " + getCheckMessage(MSG_ERROR, 0, 6, 8),
1947 "40:7: " + getCheckMessage(MSG_ERROR, 1, 6, 8),
1948 "65:7: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 6, 8),
1949 };
1950 verifyWarns(checkConfig, fileName, expected);
1951 }
1952
1953 @Test
1954 public void testInvalidClassDefWithChecker()
1955 throws Exception {
1956 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1957
1958 checkConfig.addProperty("arrayInitIndent", "4");
1959 checkConfig.addProperty("basicOffset", "4");
1960 checkConfig.addProperty("braceAdjustment", "0");
1961 checkConfig.addProperty("caseIndent", "4");
1962 checkConfig.addProperty("forceStrictCondition", "false");
1963 checkConfig.addProperty("lineWrappingIndentation", "4");
1964 checkConfig.addProperty("tabWidth", "4");
1965 checkConfig.addProperty("throwsIndent", "4");
1966 final String fileName = getPath("InputIndentationInvalidClassDefIndent.java");
1967 final String[] expected = {
1968 "18:3: " + getCheckMessage(MSG_ERROR, "class def modifier", 2, 0),
1969 "24:3: " + getCheckMessage(MSG_ERROR, "class def lcurly", 2, 0),
1970 "27:3: " + getCheckMessage(MSG_ERROR, "class def rcurly", 2, 0),
1971 "30:9: " + getCheckMessage(MSG_ERROR, "class def ident", 2, 0),
1972 "34:3: " + getCheckMessage(MSG_ERROR, "class def rcurly", 2, 0),
1973 "39:3: " + getCheckMessage(MSG_ERROR, "extends", 2, 4),
1974 "40:3: " + getCheckMessage(MSG_ERROR, "implements", 2, 4),
1975 "46:3: " + getCheckMessage(MSG_ERROR, "extends", 2, 4),
1976 "54:3: " + getCheckMessage(MSG_ERROR, "implements", 2, 4),
1977 "55:3: " + getCheckMessage(MSG_ERROR, "java", 2, 4),
1978 "60:3: " + getCheckMessage(MSG_ERROR, "class def modifier", 2, 0),
1979 "61:3: " + getCheckMessage(MSG_ERROR, "class def lcurly", 2, 0),
1980 "69:3: " + getCheckMessage(MSG_ERROR, "class def rcurly", 2, 0),
1981 "73:3: " + getCheckMessage(MSG_ERROR, "extends", 2, 4),
1982 "81:1: " + getCheckMessage(MSG_ERROR, "class", 0, 4),
1983 };
1984 verifyWarns(checkConfig, fileName, expected);
1985 }
1986
1987 @Test
1988 public void testInvalidClassDefWithChecker1()
1989 throws Exception {
1990 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1991
1992 checkConfig.addProperty("arrayInitIndent", "4");
1993 checkConfig.addProperty("basicOffset", "4");
1994 checkConfig.addProperty("braceAdjustment", "0");
1995 checkConfig.addProperty("caseIndent", "4");
1996 checkConfig.addProperty("forceStrictCondition", "false");
1997 checkConfig.addProperty("lineWrappingIndentation", "4");
1998 checkConfig.addProperty("tabWidth", "4");
1999 checkConfig.addProperty("throwsIndent", "4");
2000 final String fileName = getPath("InputIndentationInvalidClassDefIndent1.java");
2001 final String[] expected = {
2002 "23:3: " + getCheckMessage(MSG_ERROR, "class def modifier", 2, 0),
2003 "28:9: " + getCheckMessage(MSG_ERROR, "class def ident", 2, 4),
2004 "30:13: " + getCheckMessage(MSG_ERROR, "class def ident", 6, 4),
2005 "32:9: " + getCheckMessage(MSG_ERROR, "class def ident", 2, 4),
2006 "35:7: " + getCheckMessage(MSG_ERROR, "member def modifier", 6, 8),
2007 "41:11: " + getCheckMessage(MSG_ERROR, "int", 10, 12),
2008 "45:7: " + getCheckMessage(MSG_ERROR, "member def modifier", 6, 8),
2009 "50:7: " + getCheckMessage(MSG_ERROR, "class def rcurly", 6, 4),
2010 "52:13: " + getCheckMessage(MSG_ERROR, "class def ident", 6, 4),
2011 "57:13: " + getCheckMessage(MSG_ERROR, "class def ident", 6, 8),
2012 "60:17: " + getCheckMessage(MSG_ERROR, "class def ident", 10, 8),
2013 "62:11: " + getCheckMessage(MSG_ERROR, "class def rcurly", 10, 8),
2014 "65:11: " + getCheckMessage(MSG_ERROR, "member def type", 10, 12),
2015 "70:11: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 10, 8),
2016 "71:9: " + getCheckMessage(MSG_ERROR_MULTI, "object def lcurly", 8, "10, 14"),
2017 "75:9: " + getCheckMessage(MSG_ERROR_MULTI, "object def rcurly", 8, "10, 14"),
2018 "78:7: " + getCheckMessage(MSG_ERROR_MULTI, "object def lcurly", 6, "8, 12"),
2019 "82:7: " + getCheckMessage(MSG_ERROR_MULTI, "object def rcurly", 6, "8, 12"),
2020 "86:11: " + getCheckMessage(MSG_ERROR, "method def modifier", 10, 12),
2021 "88:11: " + getCheckMessage(MSG_ERROR, "method def rcurly", 10, 12),
2022 };
2023 verifyWarns(checkConfig, fileName, expected);
2024 }
2025
2026 @Test
2027 public void testInvalidBlockWithChecker()
2028 throws Exception {
2029 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2030
2031 checkConfig.addProperty("arrayInitIndent", "4");
2032 checkConfig.addProperty("basicOffset", "4");
2033 checkConfig.addProperty("braceAdjustment", "0");
2034 checkConfig.addProperty("caseIndent", "4");
2035 checkConfig.addProperty("forceStrictCondition", "false");
2036 checkConfig.addProperty("lineWrappingIndentation", "4");
2037 checkConfig.addProperty("tabWidth", "4");
2038 checkConfig.addProperty("throwsIndent", "4");
2039 final String fileName = getPath("InputIndentationInvalidBlockIndent.java");
2040 final String[] expected = {
2041 "26:8: " + getCheckMessage(MSG_ERROR, "block lcurly", 7, 8),
2042 "27:10: " + getCheckMessage(MSG_ERROR, "block lcurly", 9, 8),
2043 "29:10: " + getCheckMessage(MSG_ERROR, "block lcurly", 9, 8),
2044 "30:8: " + getCheckMessage(MSG_ERROR, "block rcurly", 7, 8),
2045 "32:7: " + getCheckMessage(MSG_ERROR, "block lcurly", 6, 8),
2046 "34:7: " + getCheckMessage(MSG_ERROR, "block rcurly", 6, 8),
2047 "35:7: " + getCheckMessage(MSG_ERROR, "block lcurly", 6, 8),
2048 "38:10: " + getCheckMessage(MSG_ERROR, "block lcurly", 9, 8),
2049 "39:14: " + getCheckMessage(MSG_CHILD_ERROR, "block", 13, 12),
2050 "41:14: " + getCheckMessage(MSG_CHILD_ERROR, "block", 13, 12),
2051 "42:10: " + getCheckMessage(MSG_ERROR, "block rcurly", 9, 8),
2052 "45:7: " + getCheckMessage(MSG_ERROR, "block lcurly", 6, 8),
2053 "46:11: " + getCheckMessage(MSG_CHILD_ERROR, "block", 10, 12),
2054 "48:11: " + getCheckMessage(MSG_CHILD_ERROR, "block", 10, 12),
2055 "49:7: " + getCheckMessage(MSG_ERROR, "block rcurly", 6, 8),
2056 "52:7: " + getCheckMessage(MSG_ERROR, "block lcurly", 6, 8),
2057 "55:11: " + getCheckMessage(MSG_CHILD_ERROR, "block", 10, 12),
2058 "59:11: " + getCheckMessage(MSG_ERROR, "block lcurly", 10, 12),
2059 "63:11: " + getCheckMessage(MSG_ERROR, "block rcurly", 10, 12),
2060 "68:11: " + getCheckMessage(MSG_CHILD_ERROR, "block", 10, 12),
2061 "70:11: " + getCheckMessage(MSG_ERROR, "block lcurly", 10, 12),
2062 "71:15: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
2063 "86:11: " + getCheckMessage(MSG_ERROR, "block rcurly", 10, 12),
2064 };
2065 verifyWarns(checkConfig, fileName, expected);
2066 }
2067
2068 @Test
2069 public void testInvalidBlockWithChecker1()
2070 throws Exception {
2071 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2072
2073 checkConfig.addProperty("arrayInitIndent", "4");
2074 checkConfig.addProperty("basicOffset", "4");
2075 checkConfig.addProperty("braceAdjustment", "0");
2076 checkConfig.addProperty("caseIndent", "4");
2077 checkConfig.addProperty("forceStrictCondition", "false");
2078 checkConfig.addProperty("lineWrappingIndentation", "4");
2079 checkConfig.addProperty("tabWidth", "4");
2080 checkConfig.addProperty("throwsIndent", "4");
2081 final String fileName = getPath("InputIndentationInvalidBlockIndent1.java");
2082 final String[] expected = {
2083 "27:3: " + getCheckMessage(MSG_ERROR, "static initialization", 2, 4),
2084 "28:7: " + getCheckMessage(MSG_ERROR, "static initialization", 6, 4),
2085 "32:8: " + getCheckMessage(MSG_CHILD_ERROR, "static initialization", 7, 8),
2086 "35:7: " + getCheckMessage(MSG_ERROR, "static initialization", 6, 4),
2087 "37:3: " + getCheckMessage(MSG_ERROR, "static initialization rcurly", 2, 4),
2088 "39:3: " + getCheckMessage(MSG_ERROR, "static initialization", 2, 4),
2089 "41:7: " + getCheckMessage(MSG_ERROR, "static initialization rcurly", 6, 4),
2090 "43:3: " + getCheckMessage(MSG_ERROR, "static initialization", 2, 4),
2091 "45:7: " + getCheckMessage(MSG_CHILD_ERROR, "static initialization", 6, 8),
2092 "48:3: " + getCheckMessage(MSG_ERROR, "static initialization lcurly", 2, 4),
2093 "49:7: " + getCheckMessage(MSG_CHILD_ERROR, "static initialization", 6, 8),
2094 "50:7: " + getCheckMessage(MSG_ERROR, "static initialization rcurly", 6, 4),
2095 "55:7: " + getCheckMessage(MSG_CHILD_ERROR, "static initialization", 6, 8),
2096 "60:5: " + getCheckMessage(MSG_CHILD_ERROR, "static initialization", 4, 8),
2097 "61:3: " + getCheckMessage(MSG_ERROR, "static initialization rcurly", 2, 4),
2098 "66:7: " + getCheckMessage(MSG_ERROR, "static initialization rcurly", 6, 4),
2099 "69:3: " + getCheckMessage(MSG_ERROR, "block lcurly", 2, 4),
2100 "70:7: " + getCheckMessage(MSG_ERROR, "block lcurly", 6, 4),
2101 "73:3: " + getCheckMessage(MSG_ERROR, "block lcurly", 2, 4),
2102 "75:7: " + getCheckMessage(MSG_ERROR, "block rcurly", 6, 4),
2103 "77:7: " + getCheckMessage(MSG_ERROR, "block lcurly", 6, 4),
2104 "79:3: " + getCheckMessage(MSG_ERROR, "block rcurly", 2, 4),
2105 "82:7: " + getCheckMessage(MSG_CHILD_ERROR, "block", 6, 8),
2106 };
2107 verifyWarns(checkConfig, fileName, expected);
2108 }
2109
2110 @Test
2111 public void testInvalidIfWithChecker()
2112 throws Exception {
2113 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2114
2115 checkConfig.addProperty("arrayInitIndent", "4");
2116 checkConfig.addProperty("basicOffset", "4");
2117 checkConfig.addProperty("braceAdjustment", "0");
2118 checkConfig.addProperty("caseIndent", "4");
2119 checkConfig.addProperty("forceStrictCondition", "false");
2120 checkConfig.addProperty("lineWrappingIndentation", "4");
2121 checkConfig.addProperty("tabWidth", "4");
2122 checkConfig.addProperty("throwsIndent", "4");
2123 final String fileName = getPath("InputIndentationInvalidIfIndent.java");
2124 final String[] expected = {
2125 "54:2: " + getCheckMessage(MSG_ERROR, "if", 1, 8),
2126 "60:10: " + getCheckMessage(MSG_ERROR, "if", 9, 8),
2127 "61:10: " + getCheckMessage(MSG_ERROR, "if lcurly", 9, 8),
2128 "62:8: " + getCheckMessage(MSG_ERROR, "if rcurly", 7, 8),
2129 "64:7: " + getCheckMessage(MSG_ERROR, "if", 6, 8),
2130 "65:6: " + getCheckMessage(MSG_ERROR, "if lcurly", 5, 8),
2131 "66:6: " + getCheckMessage(MSG_ERROR, "if rcurly", 5, 8),
2132 "71:11: " + getCheckMessage(MSG_ERROR, "if rcurly", 10, 8),
2133 "72:8: " + getCheckMessage(MSG_ERROR, "else rcurly", 7, 8),
2134 "75:10: " + getCheckMessage(MSG_ERROR, "if", 9, 8),
2135
2136 "76:8: " + getCheckMessage(MSG_ERROR, "if lcurly", 7, 8),
2137 "78:10: " + getCheckMessage(MSG_ERROR, "else", 9, 8),
2138 "80:10: " + getCheckMessage(MSG_ERROR, "else rcurly", 9, 8),
2139 "83:11: " + getCheckMessage(MSG_ERROR, "if", 10, 8),
2140 "84:8: " + getCheckMessage(MSG_ERROR, "if rcurly", 7, 8),
2141 "85:10: " + getCheckMessage(MSG_ERROR, "else", 9, 8),
2142 "86:8: " + getCheckMessage(MSG_ERROR, "else lcurly", 7, 8),
2143 "87:10: " + getCheckMessage(MSG_ERROR, "else rcurly", 9, 8),
2144
2145 "91:10: " + getCheckMessage(MSG_ERROR, "if", 9, 8),
2146 "92:10: " + getCheckMessage(MSG_ERROR, "if lcurly", 9, 8),
2147 "93:10: " + getCheckMessage(MSG_ERROR, "if rcurly", 9, 8),
2148 "94:8: " + getCheckMessage(MSG_ERROR, "else lcurly", 7, 8),
2149 "95:11: " + getCheckMessage(MSG_ERROR, "else rcurly", 10, 8),
2150 "98:7: " + getCheckMessage(MSG_ERROR, "if", 6, 8),
2151 "99:11: " + getCheckMessage(MSG_ERROR, "if lcurly", 10, 8),
2152 "100:11: " + getCheckMessage(MSG_ERROR, "if rcurly", 10, 8),
2153 "101:8: " + getCheckMessage(MSG_ERROR, "else rcurly", 7, 8),
2154 "104:6: " + getCheckMessage(MSG_ERROR, "if", 5, 8),
2155 "105:12: " + getCheckMessage(MSG_ERROR, "if rcurly", 11, 8),
2156 "106:6: " + getCheckMessage(MSG_ERROR, "else", 5, 8),
2157 "107:12: " + getCheckMessage(MSG_ERROR, "else rcurly", 11, 8),
2158 };
2159 verifyWarns(checkConfig, fileName, expected);
2160 }
2161
2162 @Test
2163 public void testInvalidIfWithChecker1()
2164 throws Exception {
2165 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2166
2167 checkConfig.addProperty("arrayInitIndent", "4");
2168 checkConfig.addProperty("basicOffset", "4");
2169 checkConfig.addProperty("braceAdjustment", "0");
2170 checkConfig.addProperty("caseIndent", "4");
2171 checkConfig.addProperty("forceStrictCondition", "false");
2172 checkConfig.addProperty("lineWrappingIndentation", "4");
2173 checkConfig.addProperty("tabWidth", "4");
2174 checkConfig.addProperty("throwsIndent", "4");
2175 final String fileName = getPath("InputIndentationInvalidIfIndent1.java");
2176 final String[] expected = {
2177 "37:15: " + getCheckMessage(MSG_CHILD_ERROR, "if", 14, 12),
2178 "42:11: " + getCheckMessage(MSG_ERROR, "if lcurly", 10, 8),
2179 "43:11: " + getCheckMessage(MSG_CHILD_ERROR, "if", 10, 12),
2180 "48:15: " + getCheckMessage(MSG_CHILD_ERROR, "if", 14, 12),
2181 "49:11: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 10, 12),
2182 "51:11: " + getCheckMessage(MSG_CHILD_ERROR, "else", 10, 12),
2183 "52:9: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 8, 12),
2184
2185 "59:17: " + getCheckMessage(MSG_CHILD_ERROR, "if", 16, 12),
2186 "60:10: " + getCheckMessage(MSG_ERROR, "if rcurly", 9, 8),
2187 "63:17: " + getCheckMessage(MSG_CHILD_ERROR, "else", 16, 12),
2188 "69:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
2189 "73:41: " + getCheckMessage(MSG_CHILD_ERROR, "else", 40, 12),
2190 "80:15: " + getCheckMessage(MSG_CHILD_ERROR, "if", 14, 12),
2191
2192 "83:15: " + getCheckMessage(MSG_CHILD_ERROR, "else", 14, 12),
2193 "89:11: " + getCheckMessage(MSG_CHILD_ERROR, "if", 10, 12),
2194 "91:11: " + getCheckMessage(MSG_CHILD_ERROR, "else", 10, 12),
2195 "95:11: " + getCheckMessage(MSG_ERROR, "if", 10, 8),
2196 "96:15: " + getCheckMessage(MSG_CHILD_ERROR, "if", 14, 12),
2197 "97:11: " + getCheckMessage(MSG_ERROR, "if rcurly", 10, 8),
2198 "98:11: " + getCheckMessage(MSG_ERROR, "else", 10, 8),
2199 "99:15: " + getCheckMessage(MSG_CHILD_ERROR, "else", 14, 12),
2200 "100:11: " + getCheckMessage(MSG_ERROR, "else rcurly", 10, 8),
2201 };
2202 verifyWarns(checkConfig, fileName, expected);
2203 }
2204
2205 @Test
2206 public void testInvalidIfWithChecker2()
2207 throws Exception {
2208 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2209
2210 checkConfig.addProperty("arrayInitIndent", "4");
2211 checkConfig.addProperty("basicOffset", "4");
2212 checkConfig.addProperty("braceAdjustment", "0");
2213 checkConfig.addProperty("caseIndent", "4");
2214 checkConfig.addProperty("forceStrictCondition", "false");
2215 checkConfig.addProperty("lineWrappingIndentation", "4");
2216 checkConfig.addProperty("tabWidth", "4");
2217 checkConfig.addProperty("throwsIndent", "4");
2218 final String fileName = getPath("InputIndentationInvalidIfIndent2.java");
2219 final String[] expected = {
2220 "26:10: " + getCheckMessage(MSG_CHILD_ERROR, "if", 9, 12),
2221 "27:12: " + getCheckMessage(MSG_CHILD_ERROR, "if", 11, 12),
2222 "31:11: " + getCheckMessage(MSG_CHILD_ERROR, "if", 10, 12),
2223 "34:8: " + getCheckMessage(MSG_ERROR, "if rcurly", 7, 8),
2224 "41:11: " + getCheckMessage(MSG_CHILD_ERROR, "if", 10, 12),
2225 "43:11: " + getCheckMessage(MSG_CHILD_ERROR, "if", 10, 12),
2226
2227 "50:11: " + getCheckMessage(MSG_CHILD_ERROR, "if", 10, 12),
2228 "59:11: " + getCheckMessage(MSG_ERROR, "if", 10, 12),
2229 "63:19: " + getCheckMessage(MSG_CHILD_ERROR, "if", 18, 20),
2230 "74:11: " + getCheckMessage(MSG_ERROR, "if rparen", 10, 8),
2231 "79:7: " + getCheckMessage(MSG_ERROR, "if rparen", 6, 8),
2232 "85:7: " + getCheckMessage(MSG_ERROR, "if lparen", 6, 8),
2233 "87:7: " + getCheckMessage(MSG_ERROR, "if rparen", 6, 8),
2234 "90:1: " + getCheckMessage(MSG_ERROR, "if", 0, 8),
2235 "91:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
2236 "92:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
2237 "93:1: " + getCheckMessage(MSG_ERROR, "if rcurly", 0, 8),
2238 "94:1: " + getCheckMessage(MSG_ERROR, "if", 0, 8),
2239 "95:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
2240 "96:1: " + getCheckMessage(MSG_ERROR, "else", 0, 8),
2241 "97:1: " + getCheckMessage(MSG_CHILD_ERROR, "else", 0, 12),
2242 };
2243 verifyWarns(checkConfig, fileName, expected);
2244 }
2245
2246 @Test
2247 public void testInvalidWhileWithChecker()
2248 throws Exception {
2249 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2250
2251 checkConfig.addProperty("arrayInitIndent", "4");
2252 checkConfig.addProperty("basicOffset", "4");
2253 checkConfig.addProperty("braceAdjustment", "0");
2254 checkConfig.addProperty("caseIndent", "4");
2255 checkConfig.addProperty("forceStrictCondition", "false");
2256 checkConfig.addProperty("lineWrappingIndentation", "4");
2257 checkConfig.addProperty("tabWidth", "4");
2258 checkConfig.addProperty("throwsIndent", "4");
2259 final String fileName = getPath("InputIndentationInvalidWhileIndent.java");
2260 final String[] expected = {
2261 "25:10: " + getCheckMessage(MSG_ERROR, "while", 9, 8),
2262 "26:8: " + getCheckMessage(MSG_ERROR, "while rcurly", 7, 8),
2263 "28:8: " + getCheckMessage(MSG_ERROR, "while", 7, 8),
2264 "29:10: " + getCheckMessage(MSG_ERROR, "while lcurly", 9, 8),
2265 "30:10: " + getCheckMessage(MSG_ERROR, "while rcurly", 9, 8),
2266
2267 "32:10: " + getCheckMessage(MSG_ERROR, "while", 9, 8),
2268 "33:7: " + getCheckMessage(MSG_ERROR, "while lcurly", 6, 8),
2269 "34:15: " + getCheckMessage(MSG_CHILD_ERROR, "while", 14, 12),
2270 "35:7: " + getCheckMessage(MSG_ERROR, "while rcurly", 6, 8),
2271
2272 "37:11: " + getCheckMessage(MSG_ERROR, "while", 10, 8),
2273 "39:11: " + getCheckMessage(MSG_ERROR, "while rcurly", 10, 8),
2274 "41:11: " + getCheckMessage(MSG_ERROR, "while", 10, 8),
2275 "44:11: " + getCheckMessage(MSG_ERROR, "while rcurly", 10, 8),
2276
2277 "46:7: " + getCheckMessage(MSG_ERROR, "while", 6, 8),
2278 "47:11: " + getCheckMessage(MSG_ERROR, "while lcurly", 10, 8),
2279 "50:7: " + getCheckMessage(MSG_ERROR, "while rcurly", 6, 8),
2280 "53:15: " + getCheckMessage(MSG_ERROR, "if", 14, 12),
2281 "54:19: " + getCheckMessage(MSG_CHILD_ERROR, "if", 18, 16),
2282 "55:15: " + getCheckMessage(MSG_ERROR, "if rcurly", 14, 12),
2283 "56:15: " + getCheckMessage(MSG_CHILD_ERROR, "while", 14, 12),
2284 "57:11: " + getCheckMessage(MSG_ERROR, "while rcurly", 10, 8),
2285
2286 "60:11: " + getCheckMessage(MSG_CHILD_ERROR, "while", 10, 12),
2287 "66:11: " + getCheckMessage(MSG_CHILD_ERROR, "while", 10, 12),
2288 "71:11: " + getCheckMessage(MSG_CHILD_ERROR, "while", 10, 12),
2289 "78:6: " + getCheckMessage(MSG_ERROR, "while rparen", 5, 8),
2290 "85:11: " + getCheckMessage(MSG_ERROR, "while rparen", 10, 8),
2291 "92:11: " + getCheckMessage(MSG_ERROR, "while rparen", 10, 8),
2292 "99:9: " + getCheckMessage(MSG_CHILD_ERROR, "while", 8, 12),
2293 };
2294 verifyWarns(checkConfig, fileName, expected);
2295 }
2296
2297 @Test
2298 public void testInvalidInvalidAnonymousClass() throws Exception {
2299 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2300
2301 checkConfig.addProperty("arrayInitIndent", "4");
2302 checkConfig.addProperty("basicOffset", "4");
2303 checkConfig.addProperty("braceAdjustment", "0");
2304 checkConfig.addProperty("caseIndent", "4");
2305 checkConfig.addProperty("forceStrictCondition", "false");
2306 checkConfig.addProperty("lineWrappingIndentation", "4");
2307 checkConfig.addProperty("tabWidth", "4");
2308 checkConfig.addProperty("throwsIndent", "4");
2309 final String fileName = getPath("InputIndentationInvalidAnonymousClassIndent.java");
2310 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2311 verifyWarns(checkConfig, fileName, expected);
2312 }
2313
2314 @Test
2315 public void testInvalidForWithChecker()
2316 throws Exception {
2317 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2318
2319 checkConfig.addProperty("arrayInitIndent", "4");
2320 checkConfig.addProperty("basicOffset", "4");
2321 checkConfig.addProperty("braceAdjustment", "0");
2322 checkConfig.addProperty("caseIndent", "4");
2323 checkConfig.addProperty("forceStrictCondition", "false");
2324 checkConfig.addProperty("lineWrappingIndentation", "4");
2325 checkConfig.addProperty("tabWidth", "4");
2326 checkConfig.addProperty("throwsIndent", "4");
2327 final String fileName = getPath("InputIndentationInvalidForIndent.java");
2328 final String[] expected = {
2329 "27:7: " + getCheckMessage(MSG_ERROR, "for", 6, 8),
2330 "28:11: " + getCheckMessage(MSG_ERROR, "for rcurly", 10, 8),
2331 "30:10: " + getCheckMessage(MSG_ERROR, "for", 9, 8),
2332 "31:7: " + getCheckMessage(MSG_ERROR, "for lcurly", 6, 8),
2333 "32:7: " + getCheckMessage(MSG_ERROR, "for rcurly", 6, 8),
2334 "36:11: " + getCheckMessage(MSG_CHILD_ERROR, "for", 10, 12),
2335
2336 "37:11: " + getCheckMessage(MSG_ERROR, "for rcurly", 10, 8),
2337 "40:11: " + getCheckMessage(MSG_ERROR, "for lcurly", 10, 8),
2338 "41:11: " + getCheckMessage(MSG_CHILD_ERROR, "for", 10, 12),
2339 "49:11: " + getCheckMessage(MSG_CHILD_ERROR, "for", 10, 12),
2340 "55:8: " + getCheckMessage(MSG_ERROR, "for", 7, 8),
2341
2342 "56:11: " + getCheckMessage(MSG_CHILD_ERROR, "for", 10, 12),
2343 "65:8: " + getCheckMessage(MSG_CHILD_ERROR, "for", 7, 12),
2344
2345 "70:7: " + getCheckMessage(MSG_ERROR, "for", 6, 8),
2346 "71:11: " + getCheckMessage(MSG_CHILD_ERROR, "for", 10, 12),
2347 "72:15: " + getCheckMessage(MSG_CHILD_ERROR, "for", 14, 16),
2348 "73:11: " + getCheckMessage(MSG_CHILD_ERROR, "for", 10, 12),
2349 "82:13: " + getCheckMessage(MSG_ERROR, "for rparen", 12, 8),
2350 "87:3: " + getCheckMessage(MSG_ERROR, "method def modifier", 2, 4),
2351 "88:5: " + getCheckMessage(MSG_ERROR, "for", 4, 8),
2352 "89:9: " + getCheckMessage(MSG_CHILD_ERROR, "for", 8, 12),
2353 "90:7: " + getCheckMessage(MSG_CHILD_ERROR, "for", 6, 12),
2354 "91:9: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 8, 16),
2355 "93:1: " + getCheckMessage(MSG_ERROR, "for", 0, 8),
2356 "94:1: " + getCheckMessage(MSG_ERROR, "for lparen", 0, 8),
2357 "95:1: " + getCheckMessage(MSG_CHILD_ERROR, "for", 0, 12),
2358 "96:1: " + getCheckMessage(MSG_ERROR, ";", 0, 4),
2359 "97:1: " + getCheckMessage(MSG_CHILD_ERROR, "for", 0, 12),
2360 "98:1: " + getCheckMessage(MSG_ERROR, ";", 0, 4),
2361 "99:1: " + getCheckMessage(MSG_CHILD_ERROR, "for", 0, 12),
2362 };
2363 verifyWarns(checkConfig, fileName, expected);
2364 }
2365
2366 @Test
2367 public void testValidForWithChecker()
2368 throws Exception {
2369 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2370
2371 checkConfig.addProperty("arrayInitIndent", "4");
2372 checkConfig.addProperty("basicOffset", "4");
2373 checkConfig.addProperty("braceAdjustment", "0");
2374 checkConfig.addProperty("caseIndent", "4");
2375 checkConfig.addProperty("forceStrictCondition", "false");
2376 checkConfig.addProperty("lineWrappingIndentation", "4");
2377 checkConfig.addProperty("tabWidth", "4");
2378 checkConfig.addProperty("throwsIndent", "4");
2379 final String fileName = getPath("InputIndentationValidForIndent.java");
2380 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2381 verifyWarns(checkConfig, fileName, expected);
2382 }
2383
2384 @Test
2385 public void testValidDoWhileWithChecker()
2386 throws Exception {
2387 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2388
2389 checkConfig.addProperty("arrayInitIndent", "4");
2390 checkConfig.addProperty("basicOffset", "4");
2391 checkConfig.addProperty("braceAdjustment", "0");
2392 checkConfig.addProperty("caseIndent", "4");
2393 checkConfig.addProperty("forceStrictCondition", "false");
2394 checkConfig.addProperty("lineWrappingIndentation", "4");
2395 checkConfig.addProperty("tabWidth", "4");
2396 checkConfig.addProperty("throwsIndent", "4");
2397 final String fileName = getPath("InputIndentationValidDoWhileIndent.java");
2398 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2399 verifyWarns(checkConfig, fileName, expected);
2400 }
2401
2402 @Test
2403 public void testInvalidDoWhileWithChecker()
2404 throws Exception {
2405 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2406
2407 checkConfig.addProperty("arrayInitIndent", "4");
2408 checkConfig.addProperty("basicOffset", "4");
2409 checkConfig.addProperty("braceAdjustment", "0");
2410 checkConfig.addProperty("caseIndent", "4");
2411 checkConfig.addProperty("forceStrictCondition", "false");
2412 checkConfig.addProperty("lineWrappingIndentation", "4");
2413 checkConfig.addProperty("tabWidth", "4");
2414 checkConfig.addProperty("throwsIndent", "4");
2415 final String fileName = getPath("InputIndentationInvalidDoWhileIndent.java");
2416 final String[] expected = {
2417 "7:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
2418 "8:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
2419 "9:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
2420 "10:1: " + getCheckMessage(MSG_ERROR, "do..while rcurly", 0, 8),
2421 "11:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
2422 "12:1: " + getCheckMessage(MSG_ERROR, "do..while while", 0, 8),
2423 "13:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
2424 "14:1: " + getCheckMessage(MSG_ERROR, "do..while lcurly", 0, 8),
2425 "15:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
2426 "16:1: " + getCheckMessage(MSG_ERROR, "do..while while", 0, 8),
2427 "17:1: " + getCheckMessage(MSG_ERROR, "do..while lparen", 0, 8),
2428 "18:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
2429 "19:1: " + getCheckMessage(MSG_ERROR, "do..while lparen", 0, 8),
2430 "20:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
2431 "21:1: " + getCheckMessage(MSG_ERROR, "do..while lparen", 0, 8),
2432 "22:1: " + getCheckMessage(MSG_CHILD_ERROR, "do..while", 0, 8),
2433 "23:1: " + getCheckMessage(MSG_ERROR, "do..while rparen", 0, 8),
2434 };
2435 verifyWarns(checkConfig, fileName, expected);
2436 }
2437
2438 @Test
2439 public void testValidBlockWithChecker()
2440 throws Exception {
2441 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2442
2443 checkConfig.addProperty("arrayInitIndent", "4");
2444 checkConfig.addProperty("basicOffset", "4");
2445 checkConfig.addProperty("braceAdjustment", "0");
2446 checkConfig.addProperty("caseIndent", "4");
2447 checkConfig.addProperty("forceStrictCondition", "false");
2448 checkConfig.addProperty("lineWrappingIndentation", "4");
2449 checkConfig.addProperty("tabWidth", "4");
2450 checkConfig.addProperty("throwsIndent", "4");
2451 final String fileName = getPath("InputIndentationValidBlockIndent.java");
2452 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2453 verifyWarns(checkConfig, fileName, expected);
2454 }
2455
2456 @Test
2457 public void testValidBlockWithChecker1()
2458 throws Exception {
2459 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2460
2461 checkConfig.addProperty("arrayInitIndent", "4");
2462 checkConfig.addProperty("basicOffset", "4");
2463 checkConfig.addProperty("braceAdjustment", "0");
2464 checkConfig.addProperty("caseIndent", "4");
2465 checkConfig.addProperty("forceStrictCondition", "false");
2466 checkConfig.addProperty("lineWrappingIndentation", "4");
2467 checkConfig.addProperty("tabWidth", "4");
2468 checkConfig.addProperty("throwsIndent", "4");
2469 final String fileName = getPath("InputIndentationValidBlockIndent1.java");
2470 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2471 verifyWarns(checkConfig, fileName, expected);
2472 }
2473
2474 @Test
2475 public void testValidWhileWithChecker()
2476 throws Exception {
2477 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2478
2479 checkConfig.addProperty("arrayInitIndent", "4");
2480 checkConfig.addProperty("basicOffset", "4");
2481 checkConfig.addProperty("braceAdjustment", "0");
2482 checkConfig.addProperty("caseIndent", "4");
2483 checkConfig.addProperty("forceStrictCondition", "false");
2484 checkConfig.addProperty("lineWrappingIndentation", "4");
2485 checkConfig.addProperty("tabWidth", "4");
2486 checkConfig.addProperty("throwsIndent", "4");
2487 final String fileName = getPath("InputIndentationValidWhileIndent.java");
2488 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2489 verifyWarns(checkConfig, fileName, expected);
2490 }
2491
2492 @Test
2493 public void testValidClassDefWithChecker()
2494 throws Exception {
2495 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2496
2497 checkConfig.addProperty("arrayInitIndent", "4");
2498 checkConfig.addProperty("basicOffset", "4");
2499 checkConfig.addProperty("braceAdjustment", "0");
2500 checkConfig.addProperty("caseIndent", "4");
2501 checkConfig.addProperty("forceStrictCondition", "false");
2502 checkConfig.addProperty("lineWrappingIndentation", "4");
2503 checkConfig.addProperty("tabWidth", "4");
2504 checkConfig.addProperty("throwsIndent", "4");
2505 final String fileName = getPath("InputIndentationValidClassDefIndent.java");
2506 final String[] expected = {
2507 "38:9: " + getCheckMessage(MSG_ERROR, "int", 8, 12),
2508 };
2509 verifyWarns(checkConfig, fileName, expected);
2510 }
2511
2512 @Test
2513 public void testValidClassDefWithChecker1()
2514 throws Exception {
2515 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2516
2517 checkConfig.addProperty("arrayInitIndent", "4");
2518 checkConfig.addProperty("basicOffset", "4");
2519 checkConfig.addProperty("braceAdjustment", "0");
2520 checkConfig.addProperty("caseIndent", "4");
2521 checkConfig.addProperty("forceStrictCondition", "false");
2522 checkConfig.addProperty("lineWrappingIndentation", "4");
2523 checkConfig.addProperty("tabWidth", "4");
2524 checkConfig.addProperty("throwsIndent", "4");
2525 final String fileName = getPath("InputIndentationValidClassDefIndent1.java");
2526 final String[] expected = {
2527 "43:1: " + getCheckMessage(MSG_ERROR, "class", 0, 4),
2528 };
2529 verifyWarns(checkConfig, fileName, expected);
2530 }
2531
2532 @Test
2533 public void testValidInterfaceDefWithChecker()
2534 throws Exception {
2535 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2536
2537 checkConfig.addProperty("arrayInitIndent", "4");
2538 checkConfig.addProperty("basicOffset", "4");
2539 checkConfig.addProperty("braceAdjustment", "0");
2540 checkConfig.addProperty("caseIndent", "4");
2541 checkConfig.addProperty("forceStrictCondition", "false");
2542 checkConfig.addProperty("lineWrappingIndentation", "4");
2543 checkConfig.addProperty("tabWidth", "4");
2544 checkConfig.addProperty("throwsIndent", "4");
2545 final String fileName = getPath("InputIndentationValidInterfaceDefIndent.java");
2546 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2547 verifyWarns(checkConfig, fileName, expected);
2548 }
2549
2550 @Test
2551 public void testValidCommaWithChecker()
2552 throws Exception {
2553 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2554
2555 checkConfig.addProperty("arrayInitIndent", "4");
2556 checkConfig.addProperty("basicOffset", "4");
2557 checkConfig.addProperty("braceAdjustment", "0");
2558 checkConfig.addProperty("caseIndent", "4");
2559 checkConfig.addProperty("forceStrictCondition", "false");
2560 checkConfig.addProperty("lineWrappingIndentation", "4");
2561 checkConfig.addProperty("tabWidth", "4");
2562 checkConfig.addProperty("throwsIndent", "4");
2563 final String fileName = getPath("InputIndentationValidCommaIndent.java");
2564 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2565 verifyWarns(checkConfig, fileName, expected);
2566 }
2567
2568 @Test
2569 public void testTabs() throws Exception {
2570 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2571
2572 checkConfig.addProperty("arrayInitIndent", "4");
2573 checkConfig.addProperty("basicOffset", "4");
2574 checkConfig.addProperty("braceAdjustment", "0");
2575 checkConfig.addProperty("caseIndent", "4");
2576 checkConfig.addProperty("forceStrictCondition", "false");
2577 checkConfig.addProperty("lineWrappingIndentation", "4");
2578 checkConfig.addProperty("tabWidth", "4");
2579 checkConfig.addProperty("throwsIndent", "4");
2580 final String[] expected = {
2581 "29:10: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 9, 8),
2582 };
2583 verifyWarns(checkConfig, getPath("InputIndentationUseTabs.java"), expected);
2584 }
2585
2586 @Test
2587 public void testIndentationLevel() throws Exception {
2588 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2589
2590 checkConfig.addProperty("arrayInitIndent", "4");
2591 checkConfig.addProperty("basicOffset", "2");
2592 checkConfig.addProperty("braceAdjustment", "0");
2593 checkConfig.addProperty("caseIndent", "4");
2594 checkConfig.addProperty("forceStrictCondition", "false");
2595 checkConfig.addProperty("lineWrappingIndentation", "2");
2596 checkConfig.addProperty("tabWidth", "4");
2597 checkConfig.addProperty("throwsIndent", "4");
2598 final String[] expected = {
2599 "29:6: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 5, 4),
2600 };
2601 verifyWarns(checkConfig, getPath("InputIndentationUseTwoSpaces.java"), expected);
2602 }
2603
2604 @Test
2605 public void testThrowsIndentationLevel() throws Exception {
2606 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2607
2608 checkConfig.addProperty("arrayInitIndent", "4");
2609 checkConfig.addProperty("basicOffset", "4");
2610 checkConfig.addProperty("braceAdjustment", "0");
2611 checkConfig.addProperty("caseIndent", "4");
2612 checkConfig.addProperty("forceStrictCondition", "false");
2613 checkConfig.addProperty("lineWrappingIndentation", "4");
2614 checkConfig.addProperty("tabWidth", "4");
2615 checkConfig.addProperty("throwsIndent", "8");
2616 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2617 verifyWarns(checkConfig, getPath("InputIndentationInvalidThrowsIndent.java"), expected);
2618 }
2619
2620 @Test
2621 public void testThrowsIndentationLevel2() throws Exception {
2622 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2623
2624 checkConfig.addProperty("basicOffset", "1");
2625 checkConfig.addProperty("forceStrictCondition", "true");
2626 checkConfig.addProperty("lineWrappingIndentation", "3");
2627 checkConfig.addProperty("tabWidth", "4");
2628 checkConfig.addProperty("throwsIndent", "5");
2629 final String[] expected = {
2630 "7:1: " + getCheckMessage(MSG_ERROR, "Exception", 0, 6),
2631 "10:1: " + getCheckMessage(MSG_ERROR, "NullPointerException", 0, 6),
2632 "13:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 6),
2633 "16:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 6),
2634 "18:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 6),
2635 "19:1: " + getCheckMessage(MSG_ERROR, "Exception", 0, 6),
2636 "22:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 6),
2637 "23:1: " + getCheckMessage(MSG_ERROR, "Exception", 0, 6),
2638 "24:1: " + getCheckMessage(MSG_ERROR, "NullPointerException", 0, 6),
2639 "27:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 6),
2640 "28:1: " + getCheckMessage(MSG_ERROR, "Exception", 0, 6),
2641 "31:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 6),
2642 "37:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 6),
2643 };
2644 verifyWarns(checkConfig, getPath("InputIndentationInvalidThrowsIndent2.java"), expected);
2645 }
2646
2647 @Test
2648 public void testCaseLevel() throws Exception {
2649 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2650
2651 checkConfig.addProperty("arrayInitIndent", "4");
2652 checkConfig.addProperty("basicOffset", "4");
2653 checkConfig.addProperty("braceAdjustment", "0");
2654 checkConfig.addProperty("caseIndent", "0");
2655 checkConfig.addProperty("forceStrictCondition", "false");
2656 checkConfig.addProperty("lineWrappingIndentation", "4");
2657 checkConfig.addProperty("tabWidth", "4");
2658 checkConfig.addProperty("throwsIndent", "4");
2659 final String[] expected = {
2660 "27:11: " + getCheckMessage(MSG_CHILD_ERROR, "case", 10, 8),
2661 };
2662 verifyWarns(checkConfig, getPath("InputIndentationCaseLevel.java"), expected);
2663 }
2664
2665 @Test
2666 public void testBraceAdjustment() throws Exception {
2667 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2668
2669 checkConfig.addProperty("arrayInitIndent", "4");
2670 checkConfig.addProperty("basicOffset", "4");
2671 checkConfig.addProperty("braceAdjustment", "2");
2672 checkConfig.addProperty("caseIndent", "4");
2673 checkConfig.addProperty("forceStrictCondition", "false");
2674 checkConfig.addProperty("lineWrappingIndentation", "4");
2675 checkConfig.addProperty("tabWidth", "4");
2676 checkConfig.addProperty("throwsIndent", "4");
2677 final String[] expected = {
2678 "25:9: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 8, 10),
2679 "26:9: " + getCheckMessage(MSG_ERROR, "if", 8, 10),
2680 "27:11: " + getCheckMessage(MSG_ERROR, "if lcurly", 10, 12),
2681 "28:13: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "if", 12, "14, 16"),
2682 "29:9: " + getCheckMessage(MSG_ERROR, "if rcurly", 8, 12),
2683 };
2684 verifyWarns(checkConfig, getPath("InputIndentationBraceAdjustment.java"), expected);
2685 }
2686
2687 @Test
2688 public void testInvalidAssignWithChecker() throws Exception {
2689 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2690
2691 checkConfig.addProperty("arrayInitIndent", "4");
2692 checkConfig.addProperty("basicOffset", "4");
2693 checkConfig.addProperty("braceAdjustment", "0");
2694 checkConfig.addProperty("caseIndent", "4");
2695 checkConfig.addProperty("forceStrictCondition", "false");
2696 checkConfig.addProperty("lineWrappingIndentation", "4");
2697 checkConfig.addProperty("tabWidth", "4");
2698 checkConfig.addProperty("throwsIndent", "4");
2699 final String[] expected = {
2700 "22:11: " + getCheckMessage(MSG_ERROR, "getLineNo", 10, 12),
2701 "24:11: " + getCheckMessage(MSG_ERROR, "getLine", 10, 12),
2702 "28:10: " + getCheckMessage(MSG_ERROR, "=", 9, 12),
2703 "29:11: " + getCheckMessage(MSG_ERROR, "1", 10, 12),
2704 };
2705 verifyWarns(checkConfig, getPath("InputIndentationInvalidAssignIndent.java"), expected);
2706 }
2707
2708 @Test
2709 public void testInvalidImportIndent() throws Exception {
2710 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2711 checkConfig.addProperty("basicOffset", "8");
2712 checkConfig.addProperty("tabWidth", "4");
2713 final String[] expected = {
2714 "4:3: " + getCheckMessage(MSG_ERROR, ".", 2, 4),
2715 "5:2: " + getCheckMessage(MSG_ERROR, "import", 1, 0),
2716 };
2717 verifyWarns(checkConfig, getPath("InputIndentationInvalidImportIndent.java"), expected);
2718 }
2719
2720 @Test
2721 public void testValidAssignWithChecker() throws Exception {
2722 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2723
2724 checkConfig.addProperty("arrayInitIndent", "4");
2725 checkConfig.addProperty("basicOffset", "4");
2726 checkConfig.addProperty("braceAdjustment", "0");
2727 checkConfig.addProperty("caseIndent", "4");
2728 checkConfig.addProperty("forceStrictCondition", "false");
2729 checkConfig.addProperty("lineWrappingIndentation", "4");
2730 checkConfig.addProperty("tabWidth", "4");
2731 checkConfig.addProperty("throwsIndent", "4");
2732 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2733 verifyWarns(checkConfig, getPath("InputIndentationValidAssignIndent.java"), expected);
2734 }
2735
2736 @Test
2737 public void test15Extensions() throws Exception {
2738 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2739
2740 checkConfig.addProperty("arrayInitIndent", "4");
2741 checkConfig.addProperty("basicOffset", "4");
2742 checkConfig.addProperty("braceAdjustment", "0");
2743 checkConfig.addProperty("caseIndent", "4");
2744 checkConfig.addProperty("forceStrictCondition", "false");
2745 checkConfig.addProperty("lineWrappingIndentation", "4");
2746 checkConfig.addProperty("tabWidth", "4");
2747 checkConfig.addProperty("throwsIndent", "4");
2748 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2749 verifyWarns(checkConfig, getPath("InputIndentation15Extensions.java"), expected);
2750 }
2751
2752 @Test
2753 public void testTryResources() throws Exception {
2754 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2755
2756 checkConfig.addProperty("arrayInitIndent", "4");
2757 checkConfig.addProperty("basicOffset", "4");
2758 checkConfig.addProperty("braceAdjustment", "0");
2759 checkConfig.addProperty("caseIndent", "4");
2760 checkConfig.addProperty("forceStrictCondition", "false");
2761 checkConfig.addProperty("lineWrappingIndentation", "4");
2762 checkConfig.addProperty("tabWidth", "4");
2763 checkConfig.addProperty("throwsIndent", "4");
2764 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2765 verifyWarns(checkConfig, getPath("InputIndentationValidTryResourcesIndent.java"),
2766 expected);
2767 }
2768
2769 @Test
2770 public void testSwitchCustom() throws Exception {
2771 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2772
2773 checkConfig.addProperty("arrayInitIndent", "4");
2774 checkConfig.addProperty("basicOffset", "4");
2775 checkConfig.addProperty("braceAdjustment", "0");
2776 checkConfig.addProperty("caseIndent", "4");
2777 checkConfig.addProperty("forceStrictCondition", "false");
2778 checkConfig.addProperty("lineWrappingIndentation", "8");
2779 checkConfig.addProperty("tabWidth", "4");
2780 checkConfig.addProperty("throwsIndent", "8");
2781 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2782 verifyWarns(checkConfig, getPath("InputIndentationSwitchCustom.java"),
2783 expected);
2784 }
2785
2786 @Test
2787 public void testSynchronizedStatement() throws Exception {
2788 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2789 checkConfig.addProperty("arrayInitIndent", "4");
2790 checkConfig.addProperty("basicOffset", "4");
2791 checkConfig.addProperty("braceAdjustment", "0");
2792 checkConfig.addProperty("caseIndent", "4");
2793 checkConfig.addProperty("forceStrictCondition", "false");
2794 checkConfig.addProperty("lineWrappingIndentation", "8");
2795 checkConfig.addProperty("tabWidth", "4");
2796 checkConfig.addProperty("throwsIndent", "8");
2797 final String[] expected = {
2798 "27:1: " + getCheckMessage(MSG_CHILD_ERROR, "synchronized", 0, 12),
2799 "30:13: " + getCheckMessage(MSG_ERROR, "synchronized lparen", 12, 8),
2800 };
2801 verifyWarns(checkConfig, getPath("InputIndentationSynchronizedStatement.java"), expected);
2802 }
2803
2804 @Test
2805 public void testSynchronizedMethod() throws Exception {
2806 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2807 checkConfig.addProperty("arrayInitIndent", "4");
2808 checkConfig.addProperty("basicOffset", "4");
2809 checkConfig.addProperty("braceAdjustment", "0");
2810 checkConfig.addProperty("caseIndent", "4");
2811 checkConfig.addProperty("forceStrictCondition", "false");
2812 checkConfig.addProperty("lineWrappingIndentation", "8");
2813 checkConfig.addProperty("tabWidth", "4");
2814 checkConfig.addProperty("throwsIndent", "8");
2815 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2816 verifyWarns(checkConfig, getPath("InputIndentationSynchronizedMethod.java"), expected);
2817 }
2818
2819 @Test
2820 public void testAnonymousClassInMethod() throws Exception {
2821 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2822 checkConfig.addProperty("tabWidth", "8");
2823 checkConfig.addProperty("basicOffset", "2");
2824 checkConfig.addProperty("braceAdjustment", "0");
2825 checkConfig.addProperty("caseIndent", "2");
2826 checkConfig.addProperty("lineWrappingIndentation", "4");
2827 checkConfig.addProperty("throwsIndent", "4");
2828 checkConfig.addProperty("arrayInitIndent", "2");
2829 final String[] expected = {
2830 "19:9: " + getCheckMessage(MSG_ERROR, "method def modifier", 8, 2),
2831 "20:17: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 16, 4),
2832 "21:25: " + getCheckMessage(MSG_ERROR_MULTI, "method def modifier", 24, "18, 20, 22"),
2833 "23:33: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "method def", 32, "20, 22, 24"),
2834 "24:25: " + getCheckMessage(MSG_ERROR_MULTI, "method def rcurly", 24, "18, 20, 22"),
2835 "26:9: " + getCheckMessage(MSG_ERROR, "method def rcurly", 8, 2),
2836 };
2837 verifyWarns(checkConfig, getPath("InputIndentationAnonymousClassInMethod.java"), expected);
2838 }
2839
2840 @Test
2841 public void testAnonymousClassInMethodWithCurlyOnNewLine() throws Exception {
2842 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2843 checkConfig.addProperty("tabWidth", "4");
2844 checkConfig.addProperty("basicOffset", "4");
2845 checkConfig.addProperty("braceAdjustment", "0");
2846 checkConfig.addProperty("caseIndent", "4");
2847 checkConfig.addProperty("lineWrappingIndentation", "8");
2848 checkConfig.addProperty("throwsIndent", "4");
2849 checkConfig.addProperty("arrayInitIndent", "4");
2850 final String[] expected = {
2851 "40:19: " + getCheckMessage(MSG_ERROR_MULTI, "object def rcurly", 18, "16, 20, 24"),
2852 "42:15: " + getCheckMessage(MSG_ERROR, "new", 14, 16),
2853 "48:15: " + getCheckMessage(MSG_ERROR_MULTI, "object def rcurly", 14, "16, 20, 24"),
2854 "60:19: " + getCheckMessage(MSG_ERROR_MULTI, "object def lcurly", 18, "16, 20, 24"),
2855 "66:19: " + getCheckMessage(MSG_ERROR_MULTI, "object def rcurly", 18, "16, 20, 24"),
2856 "69:15: " + getCheckMessage(MSG_ERROR_MULTI, "object def lcurly", 14, "16, 20, 24"),
2857 "75:15: " + getCheckMessage(MSG_ERROR_MULTI, "object def rcurly", 14, "16, 20, 24"),
2858 };
2859 verifyWarns(checkConfig,
2860 getPath("InputIndentationAnonymousClassInMethodCurlyOnNewLine.java"), expected);
2861 }
2862
2863 @Test
2864 public void testAnnotationDefinition() throws Exception {
2865 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2866 checkConfig.addProperty("tabWidth", "4");
2867 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2868 verifyWarns(checkConfig, getPath("InputIndentationAnnotationDefinition.java"), expected);
2869 }
2870
2871 @Test
2872 public void testPackageDeclaration() throws Exception {
2873 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2874 checkConfig.addProperty("tabWidth", "4");
2875 final String[] expected = {
2876 "1:2: " + getCheckMessage(MSG_ERROR, "package def", 1, 0),
2877 };
2878 verifyWarns(checkConfig, getPath("InputIndentationPackageDeclaration.java"), expected);
2879 }
2880
2881 @Test
2882 public void testPackageDeclaration2() throws Exception {
2883 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2884 checkConfig.addProperty("tabWidth", "4");
2885 final String[] expected = {
2886 "2:2: " + getCheckMessage(MSG_ERROR, "package def", 1, 0),
2887 };
2888 verifyWarns(checkConfig,
2889 getPath("package-info.java"), expected);
2890 }
2891
2892 @Test
2893 public void testPackageDeclaration3() throws Exception {
2894 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2895 checkConfig.addProperty("tabWidth", "4");
2896 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2897 verifyWarns(checkConfig, getPath("InputIndentationPackageDeclaration3.java"), expected);
2898 }
2899
2900 @Test
2901 public void testPackageDeclaration4() throws Exception {
2902 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2903 checkConfig.addProperty("tabWidth", "4");
2904 final String[] expected = {
2905 "2:1: " + getCheckMessage(MSG_ERROR, "com", 0, 4),
2906 "3:1: " + getCheckMessage(MSG_ERROR, "checks", 0, 4),
2907 };
2908 verifyWarns(checkConfig, getPath("InputIndentationPackageDeclaration4.java"), expected);
2909 }
2910
2911 @Test
2912 public void testLambda() throws Exception {
2913 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2914 checkConfig.addProperty("tabWidth", "2");
2915 checkConfig.addProperty("basicOffset", "2");
2916 checkConfig.addProperty("lineWrappingIndentation", "4");
2917 final String[] expected = {
2918 "37:6: " + getCheckMessage(MSG_ERROR_MULTI, "block lcurly", 5, "4, 8"),
2919 "38:6: " + getCheckMessage(MSG_ERROR_MULTI, "block rcurly", 5, "4, 8"),
2920 "42:12: " + getCheckMessage(MSG_ERROR, "lambda", 11, 12),
2921 "43:10: " + getCheckMessage(MSG_ERROR, "block lcurly", 9, 8),
2922 "55:8: " + getCheckMessage(MSG_CHILD_ERROR, "block", 7, 6),
2923 "56:6: " + getCheckMessage(MSG_ERROR, "block rcurly", 5, 4),
2924 };
2925 verifyWarns(checkConfig, getPath("InputIndentationLambda.java"), expected);
2926 }
2927
2928 @Test
2929 public void testLambda1() throws Exception {
2930 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2931 checkConfig.addProperty("tabWidth", "2");
2932 checkConfig.addProperty("basicOffset", "2");
2933 checkConfig.addProperty("lineWrappingIndentation", "4");
2934 final String[] expected = {
2935 "69:10: " + getCheckMessage(MSG_CHILD_ERROR, "block", 9, 10),
2936 "70:12: " + getCheckMessage(MSG_CHILD_ERROR, "block", 11, 10),
2937 "75:8: " + getCheckMessage(MSG_ERROR, "block rcurly", 7, 8),
2938 };
2939 verifyWarns(checkConfig, getPath("InputIndentationLambda1.java"), expected);
2940 }
2941
2942 @Test
2943 public void testLambda2() throws Exception {
2944 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2945 checkConfig.addProperty("tabWidth", "4");
2946 checkConfig.addProperty("basicOffset", "4");
2947 checkConfig.addProperty("lineWrappingIndentation", "8");
2948 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2949 verifyWarns(checkConfig, getPath("InputIndentationLambda2.java"), expected);
2950 }
2951
2952 @Test
2953 public void testLambda3() throws Exception {
2954 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2955 checkConfig.addProperty("tabWidth", "4");
2956 checkConfig.addProperty("basicOffset", "4");
2957 checkConfig.addProperty("lineWrappingIndentation", "8");
2958 final String[] expected = {
2959 "15:13: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 12, 8),
2960 "29:13: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 12, 8),
2961 "30:13: " + getCheckMessage(MSG_CHILD_ERROR, "block", 12, 16),
2962 "31:9: " + getCheckMessage(MSG_ERROR, "block rcurly", 8, 12),
2963 "65:13: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 12, 8),
2964 "87:13: " + getCheckMessage(MSG_ERROR, "method def rcurly", 12, 8),
2965 };
2966 verifyWarns(checkConfig, getPath("InputIndentationLambda3.java"), expected);
2967 }
2968
2969 @Test
2970 public void testLambda4() throws Exception {
2971 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2972 checkConfig.addProperty("tabWidth", "4");
2973 checkConfig.addProperty("basicOffset", "4");
2974 checkConfig.addProperty("lineWrappingIndentation", "8");
2975 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2976 verifyWarns(checkConfig, getPath("InputIndentationLambda4.java"), expected);
2977 }
2978
2979 @Test
2980 public void testLambda5() throws Exception {
2981 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2982 checkConfig.addProperty("tabWidth", "3");
2983 checkConfig.addProperty("basicOffset", "3");
2984 checkConfig.addProperty("caseIndent", "0");
2985 checkConfig.addProperty("lineWrappingIndentation", "6");
2986 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2987 verifyWarns(checkConfig, getPath("InputIndentationLambda5.java"), expected);
2988 }
2989
2990 @Test
2991 public void testLambdaFalseForceStrictCondition() throws Exception {
2992 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2993 checkConfig.addProperty("tabWidth", "4");
2994 checkConfig.addProperty("basicOffset", "4");
2995 checkConfig.addProperty("braceAdjustment", "0");
2996 checkConfig.addProperty("forceStrictCondition", "false");
2997 checkConfig.addProperty("lineWrappingIndentation", "0");
2998 final String[] expected = {
2999 "34:5: " + getCheckMessage(MSG_ERROR, "(", 4, 8),
3000 "35:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 12),
3001 "36:5: " + getCheckMessage(MSG_ERROR, "(", 4, 8),
3002 "47:5: " + getCheckMessage(MSG_ERROR_MULTI, "block rcurly", 4, "8, 16"),
3003 "73:5: " + getCheckMessage(MSG_ERROR, "->", 4, 8),
3004 };
3005
3006 verifyWarns(checkConfig, getPath("InputIndentationLambda6.java"), expected);
3007 }
3008
3009 @Test
3010 public void testLambdaTrueForceStrictCondition() throws Exception {
3011 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3012 checkConfig.addProperty("tabWidth", "4");
3013 checkConfig.addProperty("basicOffset", "4");
3014 checkConfig.addProperty("braceAdjustment", "0");
3015 checkConfig.addProperty("forceStrictCondition", "true");
3016 checkConfig.addProperty("lineWrappingIndentation", "4");
3017 final String[] expected = {
3018 "23:17: " + getCheckMessage(MSG_ERROR, "(", 16, 12),
3019 "24:17: " + getCheckMessage(MSG_ERROR, "->", 16, 12),
3020 "26:27: " + getCheckMessage(MSG_ERROR, "\"SECOND_ARG\"", 26, 12),
3021 "27:26: " + getCheckMessage(MSG_ERROR, "(", 25, 12),
3022 "30:17: " + getCheckMessage(MSG_ERROR, "(", 16, 12),
3023 "31:21: " + getCheckMessage(MSG_ERROR, "if", 20, 16),
3024 "32:25: " + getCheckMessage(MSG_CHILD_ERROR, "if", 24, 20),
3025 "33:21: " + getCheckMessage(MSG_ERROR, "if rcurly", 20, 16),
3026 "34:25: " + getCheckMessage(MSG_CHILD_ERROR, "else", 24, 20),
3027 "35:21: " + getCheckMessage(MSG_ERROR, "else rcurly", 20, 16),
3028 "36:17: " + getCheckMessage(MSG_ERROR, "block rcurly", 16, 12),
3029 "39:17: " + getCheckMessage(MSG_ERROR, "(", 16, 12),
3030 "40:17: " + getCheckMessage(MSG_ERROR, "->", 16, 12),
3031 "41:21: " + getCheckMessage(MSG_ERROR, "if", 20, 16),
3032 "44:1: " + getCheckMessage(MSG_ERROR, "block rcurly", 0, 12),
3033 };
3034
3035 verifyWarns(checkConfig, getPath("InputIndentationLambda7.java"), expected);
3036 }
3037
3038 @Test
3039 public void testLambdaOddConditions() throws Exception {
3040 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3041 checkConfig.addProperty("tabWidth", "4");
3042 checkConfig.addProperty("basicOffset", "3");
3043 checkConfig.addProperty("braceAdjustment", "0");
3044 checkConfig.addProperty("forceStrictCondition", "false");
3045 checkConfig.addProperty("lineWrappingIndentation", "7");
3046 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3047
3048 verifyWarns(checkConfig, getPath("InputIndentationLambda8.java"), expected);
3049 }
3050
3051 @Test
3052 public void testSeparatedStatements() throws Exception {
3053 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3054 checkConfig.addProperty("tabWidth", "4");
3055 final String fileName = getPath("InputIndentationSeparatedStatements.java");
3056 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3057 verifyWarns(checkConfig, fileName, expected);
3058 }
3059
3060 @Test
3061 public void testSeparatedLineWithJustSpaces() throws Exception {
3062 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3063 checkConfig.addProperty("tabWidth", "4");
3064 final String fileName = getPath("InputIndentationSeparatedStatementWithSpaces.java");
3065 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3066 verifyWarns(checkConfig, fileName, expected);
3067 }
3068
3069 @Test
3070 public void testTwoStatementsPerLine() throws Exception {
3071 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3072 checkConfig.addProperty("tabWidth", "4");
3073 checkConfig.addProperty("basicOffset", "4");
3074 final String fileName = getPath("InputIndentationTwoStatementsPerLine.java");
3075 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3076 verifyWarns(checkConfig, fileName, expected);
3077 }
3078
3079 @Test
3080 public void testMethodChaining() throws Exception {
3081 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3082 checkConfig.addProperty("tabWidth", "4");
3083 checkConfig.addProperty("basicOffset", "4");
3084 final String fileName = getPath("InputIndentationChainedMethods.java");
3085 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3086 verifyWarns(checkConfig, fileName, expected);
3087 }
3088
3089 @Test
3090 public void testMultipleAnnotationsWithWrappedLines() throws Exception {
3091 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3092 checkConfig.addProperty("tabWidth", "4");
3093 checkConfig.addProperty("basicOffset", "4");
3094 checkConfig.addProperty("forceStrictCondition", "true");
3095 final String fileName =
3096 getPath("InputIndentationCorrectMultipleAnnotationsWithWrappedLines.java");
3097 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3098 verifyWarns(checkConfig, fileName, expected);
3099 }
3100
3101 @Test
3102 public void testMultipleAnnotationsWithWrappedLines1() throws Exception {
3103 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3104 checkConfig.addProperty("tabWidth", "4");
3105 checkConfig.addProperty("basicOffset", "4");
3106 checkConfig.addProperty("forceStrictCondition", "true");
3107 final String fileName =
3108 getPath("InputIndentationCorrectMultipleAnnotationsWithWrappedLines1.java");
3109 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3110 verifyWarns(checkConfig, fileName, expected);
3111 }
3112
3113 @Test
3114 public void testMethodPrecedeByAnnotationsWithParameterOnSeparateLine() throws Exception {
3115 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3116 checkConfig.addProperty("tabWidth", "4");
3117 checkConfig.addProperty("basicOffset", "2");
3118 checkConfig.addProperty("braceAdjustment", "0");
3119 checkConfig.addProperty("caseIndent", "2");
3120 checkConfig.addProperty("throwsIndent", "4");
3121 checkConfig.addProperty("lineWrappingIndentation", "4");
3122 checkConfig.addProperty("arrayInitIndent", "2");
3123 final String fileName =
3124 getPath("InputIndentationMethodPrecededByAnnotationWithParameterOnSeparateLine.java");
3125 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3126 verifyWarns(checkConfig, fileName, expected);
3127 }
3128
3129 @Test
3130 public void testAnnotationIncorrect() throws Exception {
3131 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3132 checkConfig.addProperty("tabWidth", "4");
3133 checkConfig.addProperty("basicOffset", "4");
3134 checkConfig.addProperty("braceAdjustment", "0");
3135 checkConfig.addProperty("lineWrappingIndentation", "4");
3136 final String fileName =
3137 getPath("InputIndentationAnnotationIncorrect.java");
3138 final String[] expected = {
3139 "11:5: " + getCheckMessage(MSG_ERROR, "(", 4, 8),
3140 "14:9: " + getCheckMessage(MSG_ERROR, "(", 8, 12),
3141 "19:5: " + getCheckMessage(MSG_ERROR, "(", 4, 8),
3142 };
3143 verifyWarns(checkConfig, fileName, expected);
3144 }
3145
3146 @Test
3147 public void testInputAnnotationScopeIndentationCheck() throws Exception {
3148 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3149 checkConfig.addProperty("tabWidth", "4");
3150 checkConfig.addProperty("basicOffset", "4");
3151 checkConfig.addProperty("forceStrictCondition", "true");
3152 final String fileName = getPath("InputIndentationAnnotationScopeIndentationCheck.java");
3153 final String[] expected = {
3154 "9:9: " + getCheckMessage(MSG_ERROR_MULTI,
3155 "annotation array initialization rcurly", 8, "0, 4"),
3156 };
3157 verifyWarns(checkConfig, fileName, expected);
3158 }
3159
3160 @Test
3161 public void testInputAnnotationDefIndentationCheck() throws Exception {
3162 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3163 checkConfig.addProperty("tabWidth", "4");
3164 checkConfig.addProperty("arrayInitIndent", "4");
3165 checkConfig.addProperty("basicOffset", "4");
3166 checkConfig.addProperty("braceAdjustment", "0");
3167 checkConfig.addProperty("lineWrappingIndentation", "4");
3168 checkConfig.addProperty("forceStrictCondition", "true");
3169 final String fileName = getPath("InputIndentationCustomAnnotation.java");
3170 final String[] expected = {
3171 "14:6: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 5, 0),
3172 "16:6: " + getCheckMessage(MSG_ERROR, "annotation def rcurly", 5, 0),
3173 "17:6: " + getCheckMessage(MSG_ERROR, "@", 5, 0),
3174 "18:1: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 0, 4),
3175 "19:6: " + getCheckMessage(MSG_ERROR, "annotation def rcurly", 5, 0),
3176 "21:4: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 3, 0),
3177 "23:1: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 0, 4),
3178 "24:6: " + getCheckMessage(MSG_ERROR, "annotation def rcurly", 5, 0),
3179 "26:6: " + getCheckMessage(MSG_ERROR, "@", 5, 0),
3180 "27:6: " + getCheckMessage(MSG_ERROR, "AnnotationWithLineWrap", 5, 0),
3181 "31:6: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 5, 0),
3182 "32:4: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 3, 0),
3183 "35:6: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 5, 4),
3184 "36:4: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 3, 4),
3185 "37:1: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 0, 4),
3186 "38:1: " + getCheckMessage(MSG_ERROR, "@", 0, 4),
3187 "39:9: " + getCheckMessage(MSG_ERROR, "AnnotationInnerLineWrap", 8, 4),
3188 "42:8: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 7, 8),
3189 "60:5: " + getCheckMessage(MSG_ERROR, "AnnotationInnerLineWrap2", 4, 0),
3190 "61:4: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 3, 4),
3191 "62:8: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 7, 4),
3192 "64:5: " + getCheckMessage(MSG_ERROR, "annotation def rcurly", 4, 0),
3193 "75:4: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 3, 4),
3194 "91:29: " + getCheckMessage(MSG_ERROR_MULTI, "new", 28, "20, 24"),
3195 };
3196 verifyWarns(checkConfig, fileName, expected);
3197 }
3198
3199 @Test
3200 public void testInputAnnotationDefIndentationCheck1() throws Exception {
3201 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3202 checkConfig.addProperty("tabWidth", "4");
3203 checkConfig.addProperty("arrayInitIndent", "4");
3204 checkConfig.addProperty("basicOffset", "4");
3205 checkConfig.addProperty("braceAdjustment", "0");
3206 checkConfig.addProperty("lineWrappingIndentation", "4");
3207 checkConfig.addProperty("forceStrictCondition", "true");
3208 final String fileName = getPath("InputIndentationCustomAnnotation1.java");
3209 final String[] expected = {
3210 "36:6: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 5, 4),
3211 "47:2: " + getCheckMessage(MSG_ERROR, "interface", 1, 0),
3212 "53:12: " + getCheckMessage(MSG_ERROR, "@", 11, 0),
3213 "56:17: " + getCheckMessage(MSG_ERROR, "@", 16, 0),
3214 "63:13: " + getCheckMessage(MSG_ERROR, "@", 12, 4),
3215 "67:23: " + getCheckMessage(MSG_ERROR, "class def ident", 16, 0),
3216 };
3217 verifyWarns(checkConfig, fileName, expected);
3218 }
3219
3220 @Test
3221 public void testTryResourcesStrict() throws Exception {
3222 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3223 checkConfig.addProperty("tabWidth", "4");
3224 checkConfig.addProperty("forceStrictCondition", "true");
3225 checkConfig.addProperty("braceAdjustment", "0");
3226 checkConfig.addProperty("lineWrappingIndentation", "4");
3227 final String fileName = getPath("InputIndentationTryWithResourcesStrict.java");
3228 final String[] expected = {
3229 "26:1: " + getCheckMessage(MSG_ERROR, "try resource", 0, 12),
3230 "28:14: " + getCheckMessage(MSG_ERROR_MULTI, "try rparen", 13, "8, 12"),
3231 "33:1: " + getCheckMessage(MSG_ERROR, "newBufferedWriter", 0, 16),
3232 "39:1: " + getCheckMessage(MSG_ERROR, "newBufferedWriter", 0, 12),
3233 "59:21: " + getCheckMessage(MSG_ERROR, "try resource", 20, 16),
3234 "79:14: " + getCheckMessage(MSG_ERROR, ".", 13, 12),
3235 "85:12: " + getCheckMessage(MSG_ERROR, ".", 11, 12),
3236 };
3237 verifyWarns(checkConfig, fileName, expected);
3238 }
3239
3240 @Test
3241 public void testTryResourcesStrict1() throws Exception {
3242 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3243 checkConfig.addProperty("tabWidth", "4");
3244 checkConfig.addProperty("forceStrictCondition", "true");
3245 checkConfig.addProperty("braceAdjustment", "0");
3246 checkConfig.addProperty("lineWrappingIndentation", "4");
3247 final String fileName = getPath("InputIndentationTryWithResourcesStrict1.java");
3248 final String[] expected = {
3249 "31:20: " + getCheckMessage(MSG_ERROR, "writ", 19, 12),
3250 "38:20: " + getCheckMessage(MSG_ERROR, "writ", 19, 16),
3251 "45:22: " + getCheckMessage(MSG_ERROR, "writ", 21, 16),
3252 "60:18: " + getCheckMessage(MSG_ERROR, "zipFileName", 17, 16),
3253 "67:16: " + getCheckMessage(MSG_ERROR, "zipFileName", 15, 16),
3254 "77:8: " + getCheckMessage(MSG_ERROR, "try", 7, 8),
3255 "82:16: " + getCheckMessage(MSG_CHILD_ERROR, "try", 15, 12),
3256 "88:12: " + getCheckMessage(MSG_ERROR, "try resource", 11, 12),
3257 "89:10: " + getCheckMessage(MSG_CHILD_ERROR, "try", 9, 12),
3258 "93:12: " + getCheckMessage(MSG_ERROR, "try resource", 11, 12),
3259 "94:12: " + getCheckMessage(MSG_ERROR, "newBufferedWriter", 11, 16),
3260 "95:14: " + getCheckMessage(MSG_CHILD_ERROR, "try", 13, 12),
3261 "97:8: " + getCheckMessage(MSG_ERROR, "try", 7, 8),
3262 "98:8: " + getCheckMessage(MSG_ERROR_MULTI, "try rparen", 7, "8, 12"),
3263 "102:10: " + getCheckMessage(MSG_ERROR, "try", 9, 8),
3264 };
3265 verifyWarns(checkConfig, fileName, expected);
3266 }
3267
3268 @Test
3269 public void testTryResourcesNotStrict() throws Exception {
3270 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3271 checkConfig.addProperty("tabWidth", "4");
3272 checkConfig.addProperty("braceAdjustment", "0");
3273 checkConfig.addProperty("lineWrappingIndentation", "4");
3274 final String fileName = getPath("InputIndentationTryResourcesNotStrict.java");
3275 final String[] expected = {
3276 "26:1: " + getCheckMessage(MSG_ERROR, "try resource", 0, 12),
3277 "32:1: " + getCheckMessage(MSG_ERROR, "newBufferedWriter", 0, 16),
3278 "38:1: " + getCheckMessage(MSG_ERROR, "newBufferedWriter", 0, 12),
3279 };
3280 verifyWarns(checkConfig, fileName, expected);
3281 }
3282
3283 @Test
3284 public void testTryResourcesNotStrict1() throws Exception {
3285 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3286 checkConfig.addProperty("tabWidth", "4");
3287 checkConfig.addProperty("braceAdjustment", "0");
3288 checkConfig.addProperty("lineWrappingIndentation", "4");
3289 final String fileName = getPath("InputIndentationTryResourcesNotStrict1.java");
3290 final String[] expected = {
3291 "44:16: " + getCheckMessage(MSG_ERROR, "zipFileName", 15, 16),
3292 "54:8: " + getCheckMessage(MSG_ERROR, "try", 7, 8),
3293 "59:16: " + getCheckMessage(MSG_CHILD_ERROR, "try", 15, 12),
3294 "65:12: " + getCheckMessage(MSG_ERROR, "try resource", 11, 12),
3295 "66:10: " + getCheckMessage(MSG_CHILD_ERROR, "try", 9, 12),
3296 "70:12: " + getCheckMessage(MSG_ERROR, "try resource", 11, 12),
3297 "71:12: " + getCheckMessage(MSG_ERROR, "newBufferedWriter", 11, 16),
3298 "72:14: " + getCheckMessage(MSG_CHILD_ERROR, "try", 13, 12),
3299 "74:8: " + getCheckMessage(MSG_ERROR, "try", 7, 8),
3300 "75:8: " + getCheckMessage(MSG_ERROR_MULTI, "try rparen", 7, "8, 12"),
3301 "88:9: " + getCheckMessage(MSG_ERROR, ".", 8, 12),
3302 "96:12: " + getCheckMessage(MSG_ERROR, "new", 11, 12),
3303 };
3304 verifyWarns(checkConfig, fileName, expected);
3305 }
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319 @Test
3320 public void testArgumentOrderOfErrorMessages() {
3321 final Object[] arguments = {"##0##", "##1##", "##2##"};
3322 final String[] messages = {
3323 getCheckMessage(MSG_ERROR, arguments),
3324 getCheckMessage(MSG_CHILD_ERROR, arguments),
3325 getCheckMessage(MSG_ERROR_MULTI, arguments),
3326 getCheckMessage(MSG_CHILD_ERROR_MULTI, arguments),
3327 };
3328 final boolean isInOrder = Arrays.stream(messages).allMatch(msg -> {
3329 final int indexOfArgumentZero = msg.indexOf((String) arguments[0]);
3330 return Arrays.stream(arguments)
3331 .map(String.class::cast)
3332 .mapToInt(msg::indexOf)
3333 .allMatch(index -> index >= indexOfArgumentZero);
3334 });
3335 assertWithMessage(
3336 "the argument 0 of error messages (indentation.error, indentation.child.error,"
3337 + " indentation.error.multi, indentation.child.error.multi)"
3338 + " is required to be the first argument of them")
3339 .that(isInOrder)
3340 .isTrue();
3341 }
3342
3343 @Test
3344 public void testEmptyArray() throws Exception {
3345 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3346 checkConfig.addProperty("tabWidth", "4");
3347 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3348 verifyWarns(checkConfig, getPath("InputIndentationEmptyArray.java"), expected);
3349 }
3350
3351 @Test
3352 public void testNewHandler() throws Exception {
3353 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3354 checkConfig.addProperty("tabWidth", "4");
3355 final String[] expected = {
3356 "10:1: " + getCheckMessage(MSG_ERROR, "Object", 0, 12),
3357 "12:1: " + getCheckMessage(MSG_ERROR, "(", 0, 12),
3358 "15:1: " + getCheckMessage(MSG_CHILD_ERROR, "new", 0, 8),
3359 "17:1: " + getCheckMessage(MSG_ERROR, "new lparen", 0, 8),
3360 "25:1: " + getCheckMessage(MSG_ERROR, "=", 0, 8),
3361 };
3362 verifyWarns(checkConfig, getPath("InputIndentationNewHandler.java"), expected);
3363 }
3364
3365 @Test
3366 public void testTryHandler() throws Exception {
3367 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3368 checkConfig.addProperty("tabWidth", "4");
3369 checkConfig.addProperty("braceAdjustment", "0");
3370 checkConfig.addProperty("lineWrappingIndentation", "8");
3371 checkConfig.addProperty("forceStrictCondition", "true");
3372 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3373 verifyWarns(checkConfig, getPath("InputIndentationTryBlockWithResources.java"), expected);
3374 }
3375
3376 @Test
3377 public void testTryHandler2() throws Exception {
3378 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3379 checkConfig.addProperty("tabWidth", "4");
3380 checkConfig.addProperty("braceAdjustment", "0");
3381 checkConfig.addProperty("lineWrappingIndentation", "8");
3382 checkConfig.addProperty("forceStrictCondition", "true");
3383 final String[] expected = {
3384 "25:17: " + getCheckMessage(MSG_ERROR, "new", 16, 20),
3385 "27:13: " + getCheckMessage(MSG_ERROR, "new", 12, 20),
3386 };
3387 verifyWarns(checkConfig, getPath("InputIndentationTryBlock.java"), expected);
3388 }
3389
3390 @Test
3391 public void testChainedMethodWithBracketOnNewLine() throws Exception {
3392 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3393
3394 checkConfig.addProperty("arrayInitIndent", "2");
3395 checkConfig.addProperty("basicOffset", "2");
3396 checkConfig.addProperty("braceAdjustment", "0");
3397 checkConfig.addProperty("caseIndent", "2");
3398 checkConfig.addProperty("forceStrictCondition", "false");
3399 checkConfig.addProperty("lineWrappingIndentation", "4");
3400 checkConfig.addProperty("tabWidth", "2");
3401 checkConfig.addProperty("throwsIndent", "2");
3402 final String[] expected = {
3403 "44:7: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 6, 8),
3404 "45:9: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 8, 10),
3405 "47:7: " + getCheckMessage(MSG_ERROR, "method call rparen", 6, 8),
3406 "61:6: " + getCheckMessage(MSG_ERROR, "foo", 5, 8),
3407 "82:5: " + getCheckMessage(MSG_ERROR, "if rcurly", 4, 6),
3408 "84:3: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 2, 4),
3409 };
3410 final String fileName = "InputIndentationChainedMethodWithBracketOnNewLine.java";
3411 verifyWarns(checkConfig, getPath(fileName), expected);
3412 }
3413
3414 @Test
3415 public void testIndentationSwitchExpression() throws Exception {
3416 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3417 checkConfig.addProperty("tabWidth", "4");
3418 final String[] expected = {
3419 "17:1: " + getCheckMessage(MSG_CHILD_ERROR, "case", 0, 12),
3420 "18:9: " + getCheckMessage(MSG_CHILD_ERROR, "block", 8, 16),
3421 "21:25: " + getCheckMessage(MSG_CHILD_ERROR, "case", 24, 12),
3422 "22:9: " + getCheckMessage(MSG_CHILD_ERROR, "block", 8, 16),
3423 "27:9: " + getCheckMessage(MSG_CHILD_ERROR, "block", 8, 20),
3424 "29:1: " + getCheckMessage(MSG_CHILD_ERROR, "block", 0, 16),
3425 "30:1: " + getCheckMessage(MSG_ERROR, "yield", 0, 16),
3426 "34:5: " + getCheckMessage(MSG_CHILD_ERROR, "block", 4, 20),
3427 "44:1: " + getCheckMessage(MSG_CHILD_ERROR, "block", 0, 16),
3428 "46:21: " + getCheckMessage(MSG_CHILD_ERROR, "case", 20, 12),
3429 "47:1: " + getCheckMessage(MSG_CHILD_ERROR, "block", 0, 16),
3430 "51:9: " + getCheckMessage(MSG_CHILD_ERROR, "block", 8, 20),
3431 "56:33: " + getCheckMessage(MSG_CHILD_ERROR, "block", 32, 20),
3432 };
3433
3434 verifyWarns(checkConfig,
3435 getPath("InputIndentationCheckSwitchExpression.java"),
3436 expected);
3437 }
3438
3439 @Test
3440 public void testIndentationYieldStatement() throws Exception {
3441 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3442 checkConfig.addProperty("tabWidth", "4");
3443 final String[] expected = {
3444 "23:13: " + getCheckMessage(MSG_ERROR, "yield", 12, 16),
3445 "28:9: " + getCheckMessage(MSG_CHILD_ERROR, "yield", 8, 16),
3446 "40:5: " + getCheckMessage(MSG_ERROR, "yield", 4, 16),
3447 "41:9: " + getCheckMessage(MSG_CHILD_ERROR, "yield", 8, 16),
3448 "71:1: " + getCheckMessage(MSG_ERROR, "yield", 0, 16),
3449 "74:37: " + getCheckMessage(MSG_ERROR, "yield", 36, 16),
3450 };
3451
3452 verifyWarns(checkConfig,
3453 getPath("InputIndentationYieldStatement.java"),
3454 expected);
3455 }
3456
3457 @Test
3458 public void testIndentationSwitchExpressionCorrect() throws Exception {
3459 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3460 checkConfig.addProperty("tabWidth", "4");
3461 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3462 verifyWarns(checkConfig,
3463 getPath("InputIndentationCheckSwitchExpressionCorrect.java"),
3464 expected);
3465 }
3466
3467 @Test
3468 public void testIndentationSwitchExpressionDeclaration() throws Exception {
3469 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3470 checkConfig.addProperty("tabWidth", "4");
3471 checkConfig.addProperty("caseIndent", "4");
3472 checkConfig.addProperty("lineWrappingIndentation", "8");
3473 final String[] expected = {
3474 "33:17: " + getCheckMessage(MSG_CHILD_ERROR, "case", 16, 12),
3475 "34:17: " + getCheckMessage(MSG_CHILD_ERROR, "case", 16, 12),
3476 "41:17: " + getCheckMessage(MSG_CHILD_ERROR, "case", 16, 12),
3477 "42:17: " + getCheckMessage(MSG_CHILD_ERROR, "case", 16, 12),
3478 "49:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 12),
3479 "50:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 12),
3480 "57:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 12),
3481 "58:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 12),
3482 };
3483 verifyWarns(checkConfig,
3484 getPath("InputIndentationCheckSwitchExpressionDeclaration.java"),
3485 expected);
3486 }
3487
3488 @Test
3489 public void testIndentationSwitchExpressionDeclarationLeftCurlyNewLine() throws Exception {
3490 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3491 checkConfig.addProperty("tabWidth", "4");
3492 final String[] expected = {
3493 "34:5: " + getCheckMessage(MSG_ERROR, "switch lcurly", 4, 8),
3494 "42:5: " + getCheckMessage(MSG_ERROR, "switch lcurly", 4, 8),
3495 "50:13: " + getCheckMessage(MSG_ERROR, "switch lcurly", 12, 8),
3496 "58:13: " + getCheckMessage(MSG_ERROR, "switch lcurly", 12, 8),
3497 };
3498 verifyWarns(checkConfig,
3499 getPath(
3500 "InputIndentationCheckSwitchExpressionDeclarationLCurlyNewLine.java"),
3501 expected);
3502 }
3503
3504 @Test
3505 public void testIndentationRecords() throws Exception {
3506 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3507 checkConfig.addProperty("tabWidth", "4");
3508 checkConfig.addProperty("basicOffset", "4");
3509 checkConfig.addProperty("braceAdjustment", "0");
3510 checkConfig.addProperty("caseIndent", "4");
3511 checkConfig.addProperty("throwsIndent", "4");
3512 checkConfig.addProperty("arrayInitIndent", "4");
3513 checkConfig.addProperty("lineWrappingIndentation", "4");
3514 checkConfig.addProperty("forceStrictCondition", "false");
3515
3516 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3517
3518 verifyWarns(checkConfig,
3519 getPath("InputIndentationRecords.java"),
3520 expected);
3521 }
3522
3523 @Test
3524 public void testIndentationRecordsAndCompactCtors() throws Exception {
3525 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3526 checkConfig.addProperty("tabWidth", "4");
3527 final String[] expected = {
3528 "13:1: " + getCheckMessage(MSG_ERROR, "(", 0, 8),
3529 "25:1: " + getCheckMessage(MSG_ERROR, "String", 0, 12),
3530 "38:1: " + getCheckMessage(MSG_CHILD_ERROR, "compact ctor def", 0, 12),
3531 "48:8: " + getCheckMessage(MSG_ERROR, "record def ident", 0, 4),
3532 "53:1: " + getCheckMessage(MSG_ERROR, "compact ctor def rcurly", 0, 8),
3533 "61:1: " + getCheckMessage(MSG_ERROR, "ctor def rcurly", 0, 8),
3534 };
3535
3536 verifyWarns(checkConfig,
3537 getPath("InputIndentationRecordsAndCompactCtors.java"),
3538 expected);
3539 }
3540
3541 @Test
3542 public void testIndentationSwitchExpressionNewLine() throws Exception {
3543 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3544 checkConfig.addProperty("tabWidth", "4");
3545 final String[] expected = {
3546 "30:13: " + getCheckMessage(MSG_ERROR, "lambda", 12, 16),
3547 "32:13: " + getCheckMessage(MSG_ERROR, "lambda", 12, 16),
3548 };
3549
3550 verifyWarns(checkConfig,
3551 getPath("InputIndentationCheckSwitchExpressionNewLine.java"),
3552 expected);
3553 }
3554
3555 @Test
3556 public void testIndentationMethodParenthesisOnNewLine() throws Exception {
3557 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3558 checkConfig.addProperty("tabWidth", "4");
3559 final String[] expected = {
3560 "13:9: " + getCheckMessage(MSG_ERROR, "method def rparen", 8, 4),
3561 "18:9: " + getCheckMessage(MSG_ERROR, "method def rparen", 8, 4),
3562 };
3563
3564 verifyWarns(checkConfig,
3565 getPath("InputIndentationCheckMethodParenOnNewLine.java"),
3566 expected);
3567 }
3568
3569 @Test
3570 public void testIndentationMethodParenthesisOnNewLine1() throws Exception {
3571 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3572 checkConfig.addProperty("tabWidth", "4");
3573 final String[] expected = {
3574 "11:10: " + getCheckMessage(MSG_ERROR, "2", 9, 12),
3575 "17:8: " + getCheckMessage(MSG_ERROR, "int", 7, 8),
3576 "18:9: " + getCheckMessage(MSG_ERROR, "method def rparen", 8, 4),
3577 };
3578
3579 verifyWarns(checkConfig,
3580 getPath("InputIndentationCheckMethodParenOnNewLine1.java"),
3581 expected);
3582 }
3583
3584 @Test
3585 public void testIndentationLineWrappedRecordDeclaration() throws Exception {
3586 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3587 checkConfig.addProperty("tabWidth", "4");
3588 checkConfig.addProperty("basicOffset", "4");
3589 checkConfig.addProperty("braceAdjustment", "0");
3590 checkConfig.addProperty("caseIndent", "4");
3591 checkConfig.addProperty("throwsIndent", "4");
3592 checkConfig.addProperty("arrayInitIndent", "4");
3593 checkConfig.addProperty("lineWrappingIndentation", "4");
3594
3595 final String[] expected = {
3596 "33:1: " + getCheckMessage(MSG_ERROR, ")", 0, 4),
3597 "55:11: " + getCheckMessage(MSG_ERROR, "interface def ident", 0, 4),
3598 "56:1: " + getCheckMessage(MSG_ERROR, "method def modifier", 0, 8),
3599 "57:1: " + getCheckMessage(MSG_ERROR, "void", 0, 4),
3600 "58:1: " + getCheckMessage(MSG_ERROR, "method", 0, 4),
3601 "59:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 4),
3602 "60:1: " + getCheckMessage(MSG_ERROR, "IOException", 0, 4),
3603 "61:1: " + getCheckMessage(MSG_ERROR, "method def rcurly", 0, 8),
3604 "62:1: " + getCheckMessage(MSG_ERROR, "interface def rcurly", 0, 4),
3605 "75:8: " + getCheckMessage(MSG_ERROR, "record def ident", 0, 4),
3606 "76:1: " + getCheckMessage(MSG_ERROR, "record def rparen", 0, 4),
3607 "77:1: " + getCheckMessage(MSG_ERROR, "implements", 0, 4),
3608 "78:1: " + getCheckMessage(MSG_ERROR, "SimpleInterface2", 0, 4),
3609 "79:8: " + getCheckMessage(MSG_ERROR, "record def ident", 0, 8),
3610 "80:1: " + getCheckMessage(MSG_ERROR, "(", 0, 4),
3611 "81:1: " + getCheckMessage(MSG_ERROR, "record def rparen", 0, 8),
3612 "82:1: " + getCheckMessage(MSG_ERROR, "record def lcurly", 0, 8),
3613 "83:1: " + getCheckMessage(MSG_ERROR, "record def rcurly", 0, 8),
3614 "84:1: " + getCheckMessage(MSG_ERROR, "record def rcurly", 0, 4),
3615 };
3616
3617 verifyWarns(checkConfig,
3618 getPath("InputIndentationLineWrappedRecordDeclaration.java"),
3619 expected);
3620 }
3621
3622 @Test
3623 public void testIndentationAnnotationFieldDefinition() throws Exception {
3624 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3625 checkConfig.addProperty("tabWidth", "4");
3626 checkConfig.addProperty("basicOffset", "4");
3627 checkConfig.addProperty("braceAdjustment", "0");
3628 checkConfig.addProperty("caseIndent", "4");
3629 checkConfig.addProperty("throwsIndent", "8");
3630 checkConfig.addProperty("forceStrictCondition", "true");
3631
3632 final String[] expected = {
3633 "17:5: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 4, 8),
3634 "18:13: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 12, 8),
3635 "25:5: " + getCheckMessage(MSG_ERROR, "member def type", 4, 8),
3636 "26:5: " + getCheckMessage(MSG_ERROR, "member def type", 4, 8),
3637 };
3638
3639 verifyWarns(checkConfig, getPath("InputIndentationAnnotationFieldDef.java"),
3640 expected);
3641 }
3642
3643 @Test
3644 public void testIndentationLongConcatenatedString() throws Exception {
3645 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3646 checkConfig.addProperty("tabWidth", "4");
3647
3648 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3649
3650 verifyWarns(checkConfig, getPath("InputIndentationLongConcatenatedString.java"),
3651 expected);
3652 }
3653
3654 @Test
3655 public void testIndentationLongConcatenatedString1() throws Exception {
3656 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3657 checkConfig.addProperty("tabWidth", "4");
3658
3659 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3660
3661 verifyWarns(checkConfig, getPath("InputIndentationLongConcatenatedString1.java"),
3662 expected);
3663 }
3664
3665 @Test
3666 public void testIndentationLongConcatenatedString2() throws Exception {
3667 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3668 checkConfig.addProperty("tabWidth", "4");
3669
3670 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3671
3672 verifyWarns(checkConfig, getPath("InputIndentationLongConcatenatedString2.java"),
3673 expected);
3674 }
3675
3676 @Test
3677 public void testIndentationLongConcatenatedString3() throws Exception {
3678 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3679 checkConfig.addProperty("tabWidth", "4");
3680
3681 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3682
3683 verifyWarns(checkConfig, getPath("InputIndentationLongConcatenatedString3.java"),
3684 expected);
3685 }
3686
3687 @Test
3688 public void testIndentationLongConcatenatedString4() throws Exception {
3689 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3690 checkConfig.addProperty("tabWidth", "4");
3691
3692 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3693
3694 verifyWarns(checkConfig, getPath("InputIndentationLongConcatenatedString4.java"),
3695 expected);
3696 }
3697
3698 @Test
3699 public void testIndentationLongConcatenatedString5() throws Exception {
3700 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3701 checkConfig.addProperty("tabWidth", "4");
3702
3703 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3704
3705 verifyWarns(checkConfig, getPath("InputIndentationLongConcatenatedString5.java"),
3706 expected);
3707 }
3708
3709 @Test
3710 public void testIndentationLongConcatenatedString6() throws Exception {
3711 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3712 checkConfig.addProperty("tabWidth", "4");
3713
3714 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3715
3716 verifyWarns(checkConfig, getPath("InputIndentationLongConcatenatedString6.java"),
3717 expected);
3718 }
3719
3720 @Test
3721 public void testIndentationLongConcatenatedString7() throws Exception {
3722 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3723 checkConfig.addProperty("tabWidth", "4");
3724
3725 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3726
3727 verifyWarns(checkConfig, getPath("InputIndentationLongConcatenatedString7.java"),
3728 expected);
3729 }
3730
3731 @Test
3732 public void testIndentationLongConcatenatedString8() throws Exception {
3733 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3734 checkConfig.addProperty("tabWidth", "4");
3735
3736 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3737
3738 verifyWarns(checkConfig, getPath("InputIndentationLongConcatenatedString8.java"),
3739 expected);
3740 }
3741
3742 @Test
3743 public void testIndentationLongConcatenatedString9() throws Exception {
3744 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3745 checkConfig.addProperty("tabWidth", "4");
3746
3747 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3748
3749 verifyWarns(checkConfig, getPath("InputIndentationLongConcatenatedString9.java"),
3750 expected);
3751 }
3752
3753 @Test
3754 public void testIndentationLongConcatenatedString10() throws Exception {
3755 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3756 checkConfig.addProperty("tabWidth", "4");
3757
3758 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3759
3760 verifyWarns(checkConfig, getPath("InputIndentationLongConcatenatedString10.java"),
3761 expected);
3762 }
3763
3764 @Test
3765 public void testIndentationLongConcatenatedString11() throws Exception {
3766 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3767 checkConfig.addProperty("tabWidth", "4");
3768
3769 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3770
3771 verifyWarns(checkConfig, getPath("InputIndentationLongConcatenatedString11.java"),
3772 expected);
3773 }
3774
3775 @Test
3776 public void testIndentationLineBreakVariableDeclaration()
3777 throws Exception {
3778 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3779 checkConfig.addProperty("tabWidth", "4");
3780
3781 final String fileName = getPath("InputIndentationLineBreakVariableDeclaration.java");
3782 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3783 verifyWarns(checkConfig, fileName, expected);
3784 }
3785
3786 @Test
3787 public void testIndentationSwitchExpressionOnStartOfTheLine() throws Exception {
3788 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3789 checkConfig.addProperty("tabWidth", "4");
3790 checkConfig.addProperty("basicOffset", "2");
3791 checkConfig.addProperty("braceAdjustment", "2");
3792 checkConfig.addProperty("caseIndent", "2");
3793 checkConfig.addProperty("throwsIndent", "4");
3794 checkConfig.addProperty("lineWrappingIndentation", "4");
3795
3796 final String[] expected = {
3797 "40:7: " + getCheckMessage(MSG_ERROR, "switch", 6, 8),
3798 "41:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 10),
3799 "42:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 10),
3800 "43:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 10),
3801 "44:7: " + getCheckMessage(MSG_ERROR, "switch rcurly", 6, 8),
3802 "49:11: " + getCheckMessage(MSG_ERROR, "switch", 10, 8),
3803 "50:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 10),
3804 "51:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 10),
3805 "52:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 10),
3806 };
3807
3808 verifyWarns(checkConfig,
3809 getPath("InputIndentationSwitchOnStartOfLine.java"), expected);
3810 }
3811
3812 @Test
3813 public void testIndentationSwitchExpressionWrappingIndentation() throws Exception {
3814 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3815 checkConfig.addProperty("tabWidth", "4");
3816 checkConfig.addProperty("basicOffset", "2");
3817 checkConfig.addProperty("braceAdjustment", "2");
3818 checkConfig.addProperty("caseIndent", "2");
3819 checkConfig.addProperty("lineWrappingIndentation", "4");
3820
3821 final String fileName = getPath(
3822 "InputIndentationSwitchExpressionWrappingIndentation.java");
3823 final String[] expected = {
3824 "41:7: " + getCheckMessage(MSG_ERROR, "switch", 6, 8),
3825 "42:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 10),
3826 "43:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 10),
3827 "44:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 10),
3828 "45:7: " + getCheckMessage(MSG_ERROR, "switch rcurly", 6, 8),
3829 "51:9: " + getCheckMessage(MSG_CHILD_ERROR, "lambda", 8, 10),
3830 "52:11: " + getCheckMessage(MSG_CHILD_ERROR, "case", 10, 12),
3831 };
3832 verifyWarns(checkConfig, fileName, expected);
3833 }
3834
3835 @Test
3836 public void testInputIndentationLambdaChild() throws Exception {
3837 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3838 checkConfig.addProperty("tabWidth", "4");
3839 checkConfig.addProperty("basicOffset", "2");
3840 checkConfig.addProperty("braceAdjustment", "2");
3841 checkConfig.addProperty("caseIndent", "2");
3842 checkConfig.addProperty("lineWrappingIndentation", "4");
3843
3844 final String fileName = getPath(
3845 "InputIndentationLambdaChild.java");
3846 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3847 verifyWarns(checkConfig, fileName, expected);
3848 }
3849
3850 @Test
3851 public void testInputLambdaAndChildOnTheSameLine() throws Exception {
3852 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3853 checkConfig.addProperty("tabWidth", "4");
3854 checkConfig.addProperty("basicOffset", "2");
3855 checkConfig.addProperty("braceAdjustment", "2");
3856 checkConfig.addProperty("caseIndent", "2");
3857 checkConfig.addProperty("lineWrappingIndentation", "4");
3858
3859 final String fileName = getPath(
3860 "InputIndentationLambdaAndChildOnTheSameLine.java");
3861 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3862 verifyWarns(checkConfig, fileName, expected);
3863 }
3864
3865 @Test
3866 public void testIndentationPatternMatchingForSwitch()
3867 throws Exception {
3868 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3869 checkConfig.addProperty("forceStrictCondition", "true");
3870 checkConfig.addProperty("tabWidth", "4");
3871 checkConfig.addProperty("basicOffset", "4");
3872 checkConfig.addProperty("braceAdjustment", "0");
3873 checkConfig.addProperty("caseIndent", "4");
3874 checkConfig.addProperty("throwsIndent", "8");
3875
3876 final String fileName = getPath(
3877 "InputIndentationPatternMatchingForSwitch.java");
3878 final String[] expected = {
3879 "21:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 16),
3880 "54:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 16),
3881 "69:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 16),
3882 "70:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 16),
3883 "75:5: " + getCheckMessage(MSG_CHILD_ERROR, "case", 4, 16),
3884 "76:5: " + getCheckMessage(MSG_CHILD_ERROR, "case", 4, 16),
3885 "87:1: " + getCheckMessage(MSG_CHILD_ERROR, "case", 0, 16),
3886 "88:1: " + getCheckMessage(MSG_CHILD_ERROR, "case", 0, 16),
3887 "89:1: " + getCheckMessage(MSG_CHILD_ERROR, "case", 0, 16),
3888 "90:1: " + getCheckMessage(MSG_ERROR, "lambda", 0, 16),
3889 "91:1: " + getCheckMessage(MSG_CHILD_ERROR, "lambda", 0, 20),
3890 };
3891 verifyWarns(checkConfig, fileName, expected);
3892 }
3893
3894 @Test
3895 public void testIndentationSingleSwitchStatementsWithoutCurly()
3896 throws Exception {
3897 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3898 checkConfig.addProperty("forceStrictCondition", "true");
3899 checkConfig.addProperty("tabWidth", "4");
3900 checkConfig.addProperty("basicOffset", "4");
3901 checkConfig.addProperty("braceAdjustment", "0");
3902 checkConfig.addProperty("caseIndent", "4");
3903 checkConfig.addProperty("throwsIndent", "4");
3904
3905 final String fileName = getPath(
3906 "InputIndentationCheckSingleSwitchStatementsWithoutCurly.java");
3907 final String[] expected = {
3908 "31:13: " + getCheckMessage(MSG_ERROR, "lambda", 12, 16),
3909 "32:13: " + getCheckMessage(MSG_CHILD_ERROR, "lambda", 12, 20),
3910 "33:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 12),
3911 "34:1: " + getCheckMessage(MSG_CHILD_ERROR, "lambda", 0, 16),
3912 "44:25: " + getCheckMessage(MSG_CHILD_ERROR, "lambda", 24, 16),
3913 "47:13: " + getCheckMessage(MSG_CHILD_ERROR, "block", 12, 16),
3914 };
3915 verifyWarns(checkConfig, fileName, expected);
3916 }
3917
3918 @Test
3919 public void testIndentationRecordPattern()
3920 throws Exception {
3921 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3922 checkConfig.addProperty("forceStrictCondition", "true");
3923 checkConfig.addProperty("tabWidth", "4");
3924 checkConfig.addProperty("basicOffset", "4");
3925 checkConfig.addProperty("braceAdjustment", "0");
3926 checkConfig.addProperty("caseIndent", "4");
3927 checkConfig.addProperty("throwsIndent", "8");
3928
3929 final String fileName = getPath(
3930 "InputIndentationRecordPattern.java");
3931 final String[] expected = {
3932 "19:17: " + getCheckMessage(MSG_ERROR, "ColoredPoint", 16, 12),
3933 "24:9: " + getCheckMessage(MSG_ERROR, "ColoredPoint", 8, 12),
3934 "29:17: " + getCheckMessage(MSG_ERROR, "ColoredPoint", 16, 12),
3935 "34:9: " + getCheckMessage(MSG_CHILD_ERROR, "if", 8, 12),
3936 "37:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3937 "39:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3938 "40:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3939 "41:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3940 "42:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3941 "56:17: " + getCheckMessage(MSG_ERROR, "Rectangle", 16, 12),
3942 "57:17: " + getCheckMessage(MSG_ERROR, "ColoredPoint", 16, 12),
3943 "58:25: " + getCheckMessage(MSG_ERROR, "boolean", 24, 12),
3944 "59:17: " + getCheckMessage(MSG_ERROR, "int", 16, 12),
3945 "60:25: " + getCheckMessage(MSG_ERROR, "_", 24, 12),
3946 "61:17: " + getCheckMessage(MSG_ERROR, "ColoredPoint", 16, 12),
3947 "62:17: " + getCheckMessage(MSG_ERROR, ")", 16, 8),
3948 "67:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3949 "66:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3950 "68:9: " + getCheckMessage(MSG_CHILD_ERROR, "if", 8, 12),
3951 "69:9: " + getCheckMessage(MSG_CHILD_ERROR, "if", 8, 12),
3952 "70:9: " + getCheckMessage(MSG_CHILD_ERROR, "if", 8, 12),
3953 "71:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3954 "72:9: " + getCheckMessage(MSG_CHILD_ERROR, "if", 8, 12),
3955 "81:13: " + getCheckMessage(MSG_ERROR, ")", 12, 8),
3956 "89:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 16),
3957 "90:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 16),
3958 };
3959 verifyWarns(checkConfig, fileName, expected);
3960 }
3961
3962 @Test
3963 public void testIndentationCodeBlocks1() throws Exception {
3964 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3965 checkConfig.addProperty("tabWidth", "4");
3966 checkConfig.addProperty("basicOffset", "2");
3967 checkConfig.addProperty("braceAdjustment", "2");
3968 checkConfig.addProperty("caseIndent", "2");
3969 checkConfig.addProperty("throwsIndent", "4");
3970 checkConfig.addProperty("lineWrappingIndentation", "4");
3971 final String[] expected = {
3972 "17:5: " + getCheckMessage(MSG_ERROR, "block lcurly", 4, 2),
3973 "18:7: " + getCheckMessage(MSG_CHILD_ERROR, "block", 6, 4),
3974 "19:5: " + getCheckMessage(MSG_ERROR, "block rcurly", 4, 2),
3975 "30:5: " + getCheckMessage(MSG_ERROR, "block lcurly", 4, 2),
3976 "31:7: " + getCheckMessage(MSG_CHILD_ERROR, "block", 6, 4),
3977 "32:5: " + getCheckMessage(MSG_ERROR, "block rcurly", 4, 2),
3978 };
3979 verifyWarns(checkConfig, getPath("InputIndentationCodeBlocks1.java"), expected);
3980 }
3981
3982 @Test
3983 public void testIndentationCodeBlocks2() throws Exception {
3984 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3985 checkConfig.addProperty("tabWidth", "4");
3986 checkConfig.addProperty("basicOffset", "2");
3987 checkConfig.addProperty("braceAdjustment", "2");
3988 checkConfig.addProperty("throwsIndent", "4");
3989 checkConfig.addProperty("caseIndent", "2");
3990 checkConfig.addProperty("lineWrappingIndentation", "4");
3991 final String[] expected = {
3992 "45:13: " + getCheckMessage(MSG_ERROR, "for lcurly", 12, 14),
3993 "47:13: " + getCheckMessage(MSG_ERROR, "for rcurly", 12, 14),
3994 };
3995 verifyWarns(checkConfig,
3996 getPath("InputIndentationCodeBlocks2.java"), expected);
3997 }
3998
3999 @Test
4000 public void testIndentationAnnotationArray() throws Exception {
4001 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
4002 checkConfig.addProperty("tabWidth", "4");
4003 checkConfig.addProperty("basicOffset", "2");
4004 checkConfig.addProperty("braceAdjustment", "2");
4005 checkConfig.addProperty("caseIndent", "2");
4006 checkConfig.addProperty("throwsIndent", "4");
4007 checkConfig.addProperty("lineWrappingIndentation", "4");
4008 checkConfig.addProperty("arrayInitIndent", "2");
4009
4010 final String fileName = getPath(
4011 "InputIndentationAnnotationArray.java");
4012 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
4013 verifyWarns(checkConfig, fileName, expected);
4014 }
4015
4016 @Test
4017 public void testIndentationSealedClasses()
4018 throws Exception {
4019 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
4020 checkConfig.addProperty("forceStrictCondition", "true");
4021 checkConfig.addProperty("tabWidth", "4");
4022 checkConfig.addProperty("basicOffset", "4");
4023 checkConfig.addProperty("braceAdjustment", "0");
4024 checkConfig.addProperty("caseIndent", "4");
4025 checkConfig.addProperty("throwsIndent", "8");
4026
4027 final String fileName = getPath(
4028 "InputIndentationSealedClasses.java");
4029 final String[] expected = {
4030 "14:1: " + getCheckMessage(MSG_ERROR, "class def modifier", 0, 4),
4031 "15:2: " + getCheckMessage(MSG_ERROR, "class", 1, 4),
4032 "16:6: " + getCheckMessage(MSG_ERROR, "permits", 5, 4),
4033 "19:5: " + getCheckMessage(MSG_ERROR, "class", 4, 8),
4034 "20:5: " + getCheckMessage(MSG_ERROR, "permits", 4, 8),
4035 "28:1: " + getCheckMessage(MSG_ERROR, "class def modifier", 0, 4),
4036 "29:9: " + getCheckMessage(MSG_ERROR, "extends", 8, 4),
4037 "32:5: " + getCheckMessage(MSG_ERROR, "extends", 4, 8),
4038 "38:5: " + getCheckMessage(MSG_ERROR, "class", 4, 8),
4039 "39:1: " + getCheckMessage(MSG_ERROR, "permits", 0, 8),
4040 "40:13: " + getCheckMessage(MSG_ERROR, "C", 12, 8),
4041 "48:5: " + getCheckMessage(MSG_ERROR, "class", 4, 8),
4042 "49:5: " + getCheckMessage(MSG_ERROR, "C", 4, 8),
4043 "55:1: " + getCheckMessage(MSG_ERROR, "class def modifier", 0, 4),
4044 "56:9: " + getCheckMessage(MSG_ERROR, "class", 8, 4),
4045 };
4046 verifyWarns(checkConfig, fileName, expected);
4047 }
4048
4049 private static final class IndentAudit implements AuditListener {
4050
4051 private final IndentComment[] comments;
4052 private int position;
4053
4054 private IndentAudit(IndentComment... comments) {
4055 this.comments = Arrays.copyOf(comments, comments.length);
4056 }
4057
4058 @Override
4059 public void auditStarted(AuditEvent event) {
4060
4061 }
4062
4063 @Override
4064 public void auditFinished(AuditEvent event) {
4065
4066 }
4067
4068 @Override
4069 public void fileStarted(AuditEvent event) {
4070
4071 }
4072
4073 @Override
4074 public void fileFinished(AuditEvent event) {
4075
4076 }
4077
4078 @Override
4079 public void addError(AuditEvent event) {
4080 final int line = event.getLine();
4081 final String message = event.getMessage();
4082
4083 assertWithMessage(
4084 "found a warning when none was expected for #%s at line %s with message %s",
4085 position, line, message)
4086 .that(position)
4087 .isLessThan(comments.length);
4088
4089 final IndentComment comment = comments[position];
4090 position++;
4091
4092 final String possibleExceptedMessages = Arrays.stream(comment.getExpectedMessages())
4093 .reduce("", (cur, next) -> cur + "\"" + next + "\", ");
4094 final String assertMessage = String.format(
4095 Locale.ROOT,
4096 "input expected warning #%d at line %d to report one of the following: %s"
4097 + "but got instead: %d: %s",
4098 position, comment.getLineNumber(), possibleExceptedMessages, line, message);
4099 assertWithMessage(assertMessage)
4100 .that(line == comment.getLineNumber() && Arrays
4101 .stream(comment.getExpectedMessages()).anyMatch(message::endsWith))
4102 .isTrue();
4103 }
4104
4105 @Override
4106 public void addException(AuditEvent event, Throwable throwable) {
4107
4108 }
4109
4110 }
4111
4112 private static final class IndentComment {
4113
4114
4115 private static final String FAKE_ARGUMENT_ZERO = "##0##";
4116 private final int lineNumber;
4117 private final int indent;
4118
4119 private final int indentOffset;
4120 private final boolean expectedNonStrict;
4121 private final String expectedWarning;
4122 private final boolean warning;
4123
4124 private IndentComment(IndentComment original, int newLineNumber) {
4125 lineNumber = newLineNumber;
4126 indent = original.indent;
4127 indentOffset = original.indentOffset;
4128 expectedNonStrict = original.expectedNonStrict;
4129 expectedWarning = original.expectedWarning;
4130 warning = original.warning;
4131 }
4132
4133 private IndentComment(Matcher match, int lineNumber) {
4134 this.lineNumber = lineNumber;
4135 indent = Integer.parseInt(match.group(1));
4136 if (match.group(2) == null) {
4137 indentOffset = 0;
4138 }
4139 else {
4140 indentOffset = Integer.parseInt(match.group(2));
4141 }
4142 expectedNonStrict = match.group(3) != null;
4143 expectedWarning = match.group(4).replace(",", ", ");
4144 warning = match.group(5) != null;
4145 }
4146
4147 public String[] getExpectedMessages() {
4148 final String[] expectedMessages;
4149 if (expectedWarning.contains(",")) {
4150 expectedMessages = new String[] {
4151 getExpectedMessagePostfix(MSG_ERROR_MULTI),
4152 getExpectedMessagePostfix(MSG_CHILD_ERROR_MULTI),
4153 };
4154 }
4155 else {
4156 expectedMessages = new String[] {
4157 getExpectedMessagePostfix(MSG_ERROR),
4158 getExpectedMessagePostfix(MSG_CHILD_ERROR),
4159 };
4160 }
4161 return expectedMessages;
4162 }
4163
4164 private String getExpectedMessagePostfix(final String messageKey) {
4165 final String msg = getCheckMessage(IndentationCheck.class, messageKey,
4166 FAKE_ARGUMENT_ZERO, indent + indentOffset, expectedWarning);
4167 final int indexOfMsgPostfix = msg.indexOf(FAKE_ARGUMENT_ZERO)
4168 + FAKE_ARGUMENT_ZERO.length();
4169 return msg.substring(indexOfMsgPostfix);
4170 }
4171
4172 public int getLineNumber() {
4173 return lineNumber;
4174 }
4175
4176 public int getIndent() {
4177 return indent;
4178 }
4179
4180 public int getIndentOffset() {
4181 return indentOffset;
4182 }
4183
4184 public boolean isExpectedNonStrict() {
4185 return expectedNonStrict;
4186 }
4187
4188 public String getExpectedWarning() {
4189 return expectedWarning;
4190 }
4191
4192 public boolean isWarning() {
4193 return warning;
4194 }
4195
4196 }
4197
4198 }