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