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