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