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