1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.indentation;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck.MSG_CHILD_ERROR;
24 import static com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck.MSG_CHILD_ERROR_MULTI;
25 import static com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck.MSG_ERROR;
26 import static com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck.MSG_ERROR_MULTI;
27
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.nio.charset.StandardCharsets;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36 import java.util.Locale;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39
40 import org.junit.jupiter.api.Test;
41
42 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
43 import com.puppycrawl.tools.checkstyle.Checker;
44 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
45 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
46 import com.puppycrawl.tools.checkstyle.api.AuditListener;
47 import com.puppycrawl.tools.checkstyle.api.Configuration;
48 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
49
50
51
52
53 public class IndentationCheckTest extends AbstractModuleTestSupport {
54
55 private static final Pattern LINE_WITH_COMMENT_REGEX =
56 Pattern.compile(".*?//indent:(\\d+)(?: ioffset:(\\d+))?"
57 + " exp:(>=)?(\\d+(?:,\\d+)*?)( warn)?$");
58
59 private static final IndentComment[] EMPTY_INDENT_COMMENT_ARRAY = new IndentComment[0];
60
61 private static IndentComment[] getLinesWithWarnAndCheckComments(String aFileName,
62 final int tabWidth)
63 throws IOException {
64 final List<IndentComment> result = new ArrayList<>();
65 try (BufferedReader br = Files.newBufferedReader(Paths.get(aFileName),
66 StandardCharsets.UTF_8)) {
67 int lineNumber = 1;
68 for (String line = br.readLine(); line != null; line = br.readLine()) {
69 final Matcher match = LINE_WITH_COMMENT_REGEX.matcher(line);
70 if (match.matches()) {
71 final IndentComment warn = new IndentComment(match, lineNumber);
72 final int actualIndent = getLineStart(line, tabWidth);
73
74 if (actualIndent != warn.getIndent()) {
75 throw new IllegalStateException(String.format(Locale.ROOT,
76 "File \"%1$s\" has incorrect indentation in comment. "
77 + "Line %2$d: comment:%3$d, actual:%4$d.",
78 aFileName,
79 lineNumber,
80 warn.getIndent(),
81 actualIndent));
82 }
83
84 if (!isCommentConsistent(warn)) {
85 throw new IllegalStateException(String.format(Locale.ROOT,
86 "File \"%1$s\" has inconsistent comment on line %2$d",
87 aFileName,
88 lineNumber));
89 }
90
91 if (warn.isWarning()) {
92 result.add(warn);
93 }
94 }
95 else if (!line.isEmpty()) {
96 throw new IllegalStateException(String.format(Locale.ROOT,
97 "File \"%1$s\" has no indentation comment or its format "
98 + "malformed. Error on line: %2$d",
99 aFileName,
100 lineNumber));
101 }
102 lineNumber++;
103 }
104 }
105 return result.toArray(EMPTY_INDENT_COMMENT_ARRAY);
106 }
107
108 private static boolean isCommentConsistent(IndentComment comment) {
109 final String[] levels = comment.getExpectedWarning().split(", ");
110 final int indent = comment.getIndent() + comment.getIndentOffset();
111 final boolean result;
112 if (levels.length > 1) {
113
114 final boolean containsActualLevel =
115 Arrays.asList(levels).contains(String.valueOf(indent));
116
117 result = containsActualLevel != comment.isWarning();
118 }
119 else {
120 final int expectedWarning = Integer.parseInt(comment.getExpectedWarning());
121
122 final boolean test;
123 if (comment.isExpectedNonStrict()) {
124
125 test = indent >= expectedWarning;
126 }
127 else {
128
129 test = expectedWarning == indent;
130 }
131 result = test != comment.isWarning();
132
133 }
134 return result;
135 }
136
137 private static int getLineStart(String line, final int tabWidth) {
138 int lineStart = 0;
139 for (int index = 0; index < line.length(); ++index) {
140 if (!Character.isWhitespace(line.charAt(index))) {
141 lineStart = CommonUtil.lengthExpandedTabs(line, index, tabWidth);
142 break;
143 }
144 }
145 return lineStart;
146 }
147
148 private void verifyWarns(Configuration config, String filePath,
149 String... expected)
150 throws Exception {
151 final int tabWidth = Integer.parseInt(config.getProperty("tabWidth"));
152 final IndentComment[] linesWithWarn =
153 getLinesWithWarnAndCheckComments(filePath, tabWidth);
154 verify(config, filePath, expected, linesWithWarn);
155 assertWithMessage("Expected warning count in UT does not match warn comment count "
156 + "in input file")
157 .that(expected.length)
158 .isEqualTo(linesWithWarn.length);
159 }
160
161 private void verify(Configuration config, String filePath, String[] expected,
162 final IndentComment... linesWithWarn) throws Exception {
163 final Checker checker = createChecker(config);
164 checker.addListener(new IndentAudit(linesWithWarn));
165 verify(checker, filePath, expected);
166 }
167
168 @Override
169 protected String getPackageLocation() {
170 return "com/puppycrawl/tools/checkstyle/checks/indentation/indentation";
171 }
172
173 @Test
174 public void testGetRequiredTokens() {
175 final IndentationCheck checkObj = new IndentationCheck();
176 final int[] requiredTokens = checkObj.getRequiredTokens();
177 final HandlerFactory handlerFactory = new HandlerFactory();
178 final int[] expected = handlerFactory.getHandledTypes();
179 Arrays.sort(expected);
180 Arrays.sort(requiredTokens);
181 assertWithMessage("Default required tokens are invalid")
182 .that(requiredTokens)
183 .isEqualTo(expected);
184 }
185
186 @Test
187 public void testGetAcceptableTokens() {
188 final IndentationCheck checkObj = new IndentationCheck();
189 final int[] acceptableTokens = checkObj.getAcceptableTokens();
190 final HandlerFactory handlerFactory = new HandlerFactory();
191 final int[] expected = handlerFactory.getHandledTypes();
192 Arrays.sort(expected);
193 Arrays.sort(acceptableTokens);
194 assertWithMessage("Default acceptable tokens are invalid")
195 .that(acceptableTokens)
196 .isEqualTo(expected);
197 }
198
199 @Test
200 public void testThrowsIndentProperty() {
201 final IndentationCheck indentationCheck = new IndentationCheck();
202
203 indentationCheck.setThrowsIndent(1);
204
205 assertWithMessage("Invalid throws indent")
206 .that(indentationCheck.getThrowsIndent())
207 .isEqualTo(1);
208 }
209
210 @Test
211 public void testStrictCondition() throws Exception {
212 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
213 checkConfig.addProperty("arrayInitIndent", "4");
214 checkConfig.addProperty("basicOffset", "4");
215 checkConfig.addProperty("braceAdjustment", "4");
216 checkConfig.addProperty("caseIndent", "4");
217 checkConfig.addProperty("forceStrictCondition", "true");
218 checkConfig.addProperty("lineWrappingIndentation", "8");
219 checkConfig.addProperty("tabWidth", "4");
220 checkConfig.addProperty("throwsIndent", "8");
221 final String[] expected = {
222 "10:29: " + getCheckMessage(MSG_ERROR_MULTI, "method def rcurly", 28, "16, 20, 24"),
223 "13:9: " + getCheckMessage(MSG_ERROR, "method def rcurly", 8, 4),
224 "14:5: " + getCheckMessage(MSG_ERROR, "class def rcurly", 4, 0),
225 };
226 verifyWarns(checkConfig, getPath("InputIndentationStrictCondition.java"), expected);
227 }
228
229 @Test
230 public void forbidOldStyle() throws Exception {
231 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
232 checkConfig.addProperty("arrayInitIndent", "4");
233 checkConfig.addProperty("basicOffset", "4");
234 checkConfig.addProperty("braceAdjustment", "0");
235 checkConfig.addProperty("caseIndent", "4");
236 checkConfig.addProperty("forceStrictCondition", "true");
237 checkConfig.addProperty("lineWrappingIndentation", "8");
238 checkConfig.addProperty("tabWidth", "4");
239 checkConfig.addProperty("throwsIndent", "8");
240 final String[] expected = {
241 "20:30: " + getCheckMessage(MSG_ERROR, "int", 29, 12),
242 "21:30: " + getCheckMessage(MSG_ERROR, "int", 29, 12),
243 };
244 verifyWarns(checkConfig, getPath("InputIndentationMethodCStyle.java"), expected);
245 }
246
247 @Test
248 public void testZeroCaseLevel() throws Exception {
249 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
250 checkConfig.addProperty("arrayInitIndent", "4");
251 checkConfig.addProperty("basicOffset", "4");
252 checkConfig.addProperty("braceAdjustment", "0");
253 checkConfig.addProperty("caseIndent", "0");
254 checkConfig.addProperty("forceStrictCondition", "false");
255 checkConfig.addProperty("lineWrappingIndentation", "4");
256 checkConfig.addProperty("tabWidth", "4");
257 checkConfig.addProperty("throwsIndent", "4");
258 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
259 verifyWarns(checkConfig, getPath("InputIndentationZeroCaseLevel.java"), expected);
260 }
261
262 @Test
263 public void testAndroidStyle() throws Exception {
264 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
265 checkConfig.addProperty("arrayInitIndent", "4");
266 checkConfig.addProperty("basicOffset", "4");
267 checkConfig.addProperty("braceAdjustment", "0");
268 checkConfig.addProperty("caseIndent", "4");
269 checkConfig.addProperty("forceStrictCondition", "false");
270 checkConfig.addProperty("lineWrappingIndentation", "8");
271 checkConfig.addProperty("tabWidth", "4");
272 checkConfig.addProperty("throwsIndent", "8");
273 final String[] expected = {
274 "42:4: " + getCheckMessage(MSG_ERROR, "extends", 3, 8),
275 "44:4: " + getCheckMessage(MSG_ERROR, "member def type", 3, 4),
276 "47:9: " + getCheckMessage(MSG_ERROR, "foo", 8, 12),
277 "50:9: " + getCheckMessage(MSG_ERROR, "int", 8, 12),
278 "53:14: " + getCheckMessage(MSG_ERROR, "true", 13, 16),
279 "56:17: " + getCheckMessage(MSG_ERROR, "+", 16, 20),
280 "57:9: " + getCheckMessage(MSG_ERROR, "if", 8, 12),
281 "60:12: " + getCheckMessage(MSG_ERROR, "if rcurly", 11, 12),
282 "62:8: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 7, 8),
283 };
284 verifyWarns(checkConfig, getPath("InputIndentationAndroidStyle.java"), expected);
285 }
286
287 @Test
288 public void testMethodCallLineWrap() throws Exception {
289 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
290
291 checkConfig.addProperty("arrayInitIndent", "4");
292 checkConfig.addProperty("basicOffset", "4");
293 checkConfig.addProperty("braceAdjustment", "0");
294 checkConfig.addProperty("caseIndent", "4");
295 checkConfig.addProperty("forceStrictCondition", "false");
296 checkConfig.addProperty("lineWrappingIndentation", "4");
297 checkConfig.addProperty("tabWidth", "4");
298 checkConfig.addProperty("throwsIndent", "4");
299 final String[] expected = {
300 "53:19: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 18, 20),
301 "54:15: " + getCheckMessage(MSG_ERROR, "method call rparen", 14, 16),
302 "75:13: " + getCheckMessage(MSG_ERROR, "lambda arguments", 12, 16),
303 };
304 verifyWarns(checkConfig, getPath("InputIndentationMethodCallLineWrap.java"), expected);
305 }
306
307 @Test
308 public void testDifficultAnnotations() throws Exception {
309 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
310
311 checkConfig.addProperty("arrayInitIndent", "4");
312 checkConfig.addProperty("basicOffset", "4");
313 checkConfig.addProperty("braceAdjustment", "0");
314 checkConfig.addProperty("caseIndent", "4");
315 checkConfig.addProperty("forceStrictCondition", "false");
316 checkConfig.addProperty("lineWrappingIndentation", "4");
317 checkConfig.addProperty("tabWidth", "4");
318 checkConfig.addProperty("throwsIndent", "4");
319 final String[] expected = {
320 "40:1: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
321 "annotation array initialization", 0, "4, 23, 25"),
322 "41:1: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
323 "annotation array initialization", 0, "4, 23, 25"),
324 "50:7: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
325 "annotation array initialization", 6, "8, 27, 29"),
326 };
327 verifyWarns(checkConfig, getPath("InputIndentationDifficultAnnotations.java"), expected);
328 }
329
330 @Test
331 public void testAnnotationClosingParenthesisEndsInSameIndentationAsOpening() throws Exception {
332 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
333
334 checkConfig.addProperty("basicOffset", "4");
335 checkConfig.addProperty("forceStrictCondition", "true");
336 checkConfig.addProperty("tabWidth", "4");
337
338 final String[] expected = {
339 "34:17: " + getCheckMessage(MSG_ERROR, ")", 16, 0),
340 "36:17: " + getCheckMessage(MSG_ERROR, ")", 16, 0),
341 "40:9: " + getCheckMessage(MSG_ERROR, ")", 8, 4),
342 "42:9: " + getCheckMessage(MSG_ERROR, ")", 8, 4),
343 "46:9: " + getCheckMessage(MSG_ERROR, ")", 8, 4),
344 };
345
346 verifyWarns(checkConfig,
347 getPath("InputIndentation"
348 + "AnnotationClosingParenthesisEndsInSameIndentationAsOpening.java"),
349 expected);
350 }
351
352 @Test
353 public void testAnonClassesFromGuava() throws Exception {
354 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
355
356 checkConfig.addProperty("arrayInitIndent", "4");
357 checkConfig.addProperty("basicOffset", "2");
358 checkConfig.addProperty("braceAdjustment", "0");
359 checkConfig.addProperty("caseIndent", "4");
360 checkConfig.addProperty("forceStrictCondition", "false");
361 checkConfig.addProperty("lineWrappingIndentation", "4");
362 checkConfig.addProperty("tabWidth", "4");
363 checkConfig.addProperty("throwsIndent", "4");
364 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
365 verifyWarns(checkConfig, getPath("InputIndentationFromGuava2.java"), expected);
366 }
367
368 @Test
369 public void testAnnotations() throws Exception {
370 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
371
372 checkConfig.addProperty("arrayInitIndent", "4");
373 checkConfig.addProperty("basicOffset", "2");
374 checkConfig.addProperty("braceAdjustment", "0");
375 checkConfig.addProperty("caseIndent", "4");
376 checkConfig.addProperty("forceStrictCondition", "false");
377 checkConfig.addProperty("lineWrappingIndentation", "4");
378 checkConfig.addProperty("tabWidth", "4");
379 checkConfig.addProperty("throwsIndent", "4");
380 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
381 verifyWarns(checkConfig, getPath("InputIndentationFromGuava.java"), expected);
382 }
383
384 @Test
385 public void testCorrectIfAndParameters() throws Exception {
386 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
387
388 checkConfig.addProperty("arrayInitIndent", "4");
389 checkConfig.addProperty("basicOffset", "2");
390 checkConfig.addProperty("braceAdjustment", "0");
391 checkConfig.addProperty("caseIndent", "4");
392 checkConfig.addProperty("forceStrictCondition", "false");
393 checkConfig.addProperty("lineWrappingIndentation", "4");
394 checkConfig.addProperty("tabWidth", "4");
395 checkConfig.addProperty("throwsIndent", "4");
396 final String[] expected = {
397 "33:9: " + getCheckMessage(MSG_ERROR_MULTI, "new", 8, "10, 12"),
398 "40:9: " + getCheckMessage(MSG_ERROR_MULTI, "new", 8, "10, 12"),
399 "90:11: " + getCheckMessage(MSG_ERROR_MULTI, "new", 10, "12, 14"),
400 "97:13: " + getCheckMessage(MSG_ERROR_MULTI, "new", 12, "14, 16"),
401 "119:13: " + getCheckMessage(MSG_ERROR_MULTI, "new", 12, "14, 16"),
402 "126:15: " + getCheckMessage(MSG_ERROR_MULTI, "new", 14, "16, 18"),
403 };
404 verifyWarns(checkConfig, getPath("InputIndentationCorrectIfAndParameter.java"), expected);
405 }
406
407 @Test
408 public void testAnonymousClasses() throws Exception {
409 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
410
411 checkConfig.addProperty("arrayInitIndent", "4");
412 checkConfig.addProperty("basicOffset", "2");
413 checkConfig.addProperty("braceAdjustment", "0");
414 checkConfig.addProperty("caseIndent", "4");
415 checkConfig.addProperty("forceStrictCondition", "false");
416 checkConfig.addProperty("lineWrappingIndentation", "4");
417 checkConfig.addProperty("tabWidth", "4");
418 checkConfig.addProperty("throwsIndent", "4");
419 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
420 verifyWarns(checkConfig, getPath("InputIndentationAnonymousClasses.java"), expected);
421 }
422
423 @Test
424 public void testArrays() throws Exception {
425 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
426
427 checkConfig.addProperty("arrayInitIndent", "2");
428 checkConfig.addProperty("basicOffset", "2");
429 checkConfig.addProperty("braceAdjustment", "0");
430 checkConfig.addProperty("caseIndent", "4");
431 checkConfig.addProperty("forceStrictCondition", "false");
432 checkConfig.addProperty("lineWrappingIndentation", "4");
433 checkConfig.addProperty("tabWidth", "4");
434 checkConfig.addProperty("throwsIndent", "4");
435 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
436 verifyWarns(checkConfig, getPath("InputIndentationArrays.java"), expected);
437 }
438
439 @Test
440 public void testLabels() throws Exception {
441 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
442
443 checkConfig.addProperty("arrayInitIndent", "4");
444 checkConfig.addProperty("basicOffset", "2");
445 checkConfig.addProperty("braceAdjustment", "0");
446 checkConfig.addProperty("caseIndent", "4");
447 checkConfig.addProperty("forceStrictCondition", "false");
448 checkConfig.addProperty("lineWrappingIndentation", "4");
449 checkConfig.addProperty("tabWidth", "4");
450 checkConfig.addProperty("throwsIndent", "4");
451 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
452 verifyWarns(checkConfig, getPath("InputIndentationLabels.java"), expected);
453 }
454
455 @Test
456 public void testClassesAndMethods() throws Exception {
457 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
458
459 checkConfig.addProperty("arrayInitIndent", "4");
460 checkConfig.addProperty("basicOffset", "2");
461 checkConfig.addProperty("braceAdjustment", "0");
462 checkConfig.addProperty("caseIndent", "4");
463 checkConfig.addProperty("forceStrictCondition", "false");
464 checkConfig.addProperty("lineWrappingIndentation", "4");
465 checkConfig.addProperty("tabWidth", "4");
466 checkConfig.addProperty("throwsIndent", "4");
467 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
468 verifyWarns(checkConfig, getPath("InputIndentationClassesMethods.java"), expected);
469 }
470
471 @Test
472 public void testCtorCall() throws Exception {
473 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
474
475 checkConfig.addProperty("basicOffset", "2");
476 checkConfig.addProperty("braceAdjustment", "0");
477 checkConfig.addProperty("lineWrappingIndentation", "4");
478 checkConfig.addProperty("tabWidth", "4");
479 final String[] expected = {
480 "28:5: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 4, 6),
481 "29:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 6),
482 "30:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 6),
483 "34:5: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 4, 6),
484 "35:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 6),
485 "39:5: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 4, 6),
486 "40:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 8),
487 "41:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 8),
488 "45:5: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 4, 6),
489 "46:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 8),
490 "50:5: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 4, 6),
491 "51:5: " + getCheckMessage(MSG_ERROR, "(", 4, 8),
492 "52:5: " + getCheckMessage(MSG_ERROR, "x", 4, 8),
493 "56:5: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 4, 6),
494 "57:5: " + getCheckMessage(MSG_ERROR, "method call lparen", 4, 6),
495 "62:5: " + getCheckMessage(MSG_ERROR, ".", 4, 10),
496 "63:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 8),
497 "68:5: " + getCheckMessage(MSG_ERROR, "super", 4, 10),
498 "69:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 8),
499 "75:11: " + getCheckMessage(MSG_ERROR_MULTI, "lambda arguments", 10, "12, 14"),
500 };
501 verifyWarns(checkConfig, getPath("InputIndentationCtorCall.java"), expected);
502 }
503
504 @Test
505 public void testMembers() throws Exception {
506 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
507
508 checkConfig.addProperty("arrayInitIndent", "4");
509 checkConfig.addProperty("basicOffset", "2");
510 checkConfig.addProperty("braceAdjustment", "0");
511 checkConfig.addProperty("caseIndent", "4");
512 checkConfig.addProperty("forceStrictCondition", "false");
513 checkConfig.addProperty("lineWrappingIndentation", "4");
514 checkConfig.addProperty("tabWidth", "4");
515 checkConfig.addProperty("throwsIndent", "4");
516 final String[] expected = {
517 "22:6: " + getCheckMessage(MSG_ERROR, "=", 5, 6),
518 "57:4: " + getCheckMessage(MSG_ERROR, "class def rcurly", 3, 2),
519 };
520
521 verifyWarns(checkConfig, getPath("InputIndentationMembers.java"), expected);
522 }
523
524 @Test
525 public void testAnnotationArrayInit() throws Exception {
526 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
527
528 checkConfig.addProperty("arrayInitIndent", "6");
529 checkConfig.addProperty("basicOffset", "2");
530 checkConfig.addProperty("braceAdjustment", "0");
531 checkConfig.addProperty("caseIndent", "4");
532 checkConfig.addProperty("forceStrictCondition", "false");
533 checkConfig.addProperty("lineWrappingIndentation", "4");
534 checkConfig.addProperty("tabWidth", "8");
535 checkConfig.addProperty("throwsIndent", "4");
536 final String[] expected = {
537
538 "17:1: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization", 0,
539 "4, 6, 34, 36"),
540 "22:14: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
541 13, "4, 6, 34, 36"),
542 "23:3: " + getCheckMessage(MSG_ERROR_MULTI,
543 "annotation array initialization rcurly", 2, "0, 4"),
544 "35:7: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization", 6,
545 "8, 10, 31, 33"),
546 "36:3: " + getCheckMessage(MSG_ERROR_MULTI,
547 "annotation array initialization rcurly", 2, "4, 8"),
548
549 "52:6: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
550 "annotation array initialization", 5, "6, 8, 10"),
551 "54:6: " + getCheckMessage(MSG_ERROR_MULTI,
552 "annotation array initialization rcurly", 5, "2, 6"),
553 };
554 final String fileName = getPath("InputIndentationAnnArrInit.java");
555 verifyWarns(checkConfig, fileName, expected);
556 }
557
558 @Test
559 public void testAnnotationArrayInitTwo() throws Exception {
560 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
561
562 checkConfig.addProperty("arrayInitIndent", "0");
563 checkConfig.addProperty("basicOffset", "2");
564 checkConfig.addProperty("braceAdjustment", "0");
565 checkConfig.addProperty("caseIndent", "4");
566 checkConfig.addProperty("forceStrictCondition", "false");
567 checkConfig.addProperty("lineWrappingIndentation", "0");
568 checkConfig.addProperty("tabWidth", "8");
569 checkConfig.addProperty("throwsIndent", "4");
570 final String[] expected = {
571
572 "17:5: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
573 "annotation array initialization", 4, "0, 33, 35"),
574 "30:9: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
575 "annotation array initialization", 8, "4, 29, 31"),
576 "32:3: " + getCheckMessage(MSG_ERROR,
577 "annotation array initialization rcurly", 2, 4),
578 "47:7: " + getCheckMessage(MSG_ERROR,
579 "annotation array initialization lcurly", 6, 2),
580 "49:5: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
581 "annotation array initialization", 4, "2, 6, 8"),
582 };
583 final String fileName = getPath("InputIndentationAnnArrInit2.java");
584 verifyWarns(checkConfig, fileName, expected);
585 }
586
587 @Test
588 public void testAnnotationArrayInitWithEmoji() throws Exception {
589 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
590
591 checkConfig.addProperty("arrayInitIndent", "0");
592 checkConfig.addProperty("basicOffset", "2");
593 checkConfig.addProperty("braceAdjustment", "0");
594 checkConfig.addProperty("caseIndent", "4");
595 checkConfig.addProperty("forceStrictCondition", "false");
596 checkConfig.addProperty("lineWrappingIndentation", "0");
597 checkConfig.addProperty("tabWidth", "8");
598 checkConfig.addProperty("throwsIndent", "4");
599 final String[] expected = {
600 "17:5: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
601 "annotation array initialization", 4, "0, 41, 43"),
602 "30:9: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
603 "annotation array initialization", 8, "4, 29, 31"),
604 "32:3: " + getCheckMessage(MSG_ERROR,
605 "annotation array initialization rcurly", 2, 4),
606 "42:7: " + getCheckMessage(MSG_ERROR,
607 "member def type", 6, "4"),
608 "47:7: " + getCheckMessage(MSG_ERROR,
609 "annotation array initialization lcurly", 6, "2"),
610 "48:11: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
611 "annotation array initialization", 10, "2, 6, 8"),
612 "49:13: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
613 "annotation array initialization", 12, "2, 6, 8"),
614 "50:21: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
615 "annotation array initialization", 20, "2, 6, 8"),
616 "52:5: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
617 "annotation array initialization", 4, "2, 6, 8"),
618 };
619 final String fileName = getPath("InputIndentationAnnArrInitWithEmoji.java");
620 verifyWarns(checkConfig, fileName, expected);
621
622 }
623
624 @Test
625 public void testOddAnnotations()
626 throws Exception {
627 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
628
629 checkConfig.addProperty("arrayInitIndent", "3");
630 checkConfig.addProperty("basicOffset", "4");
631 checkConfig.addProperty("braceAdjustment", "0");
632 checkConfig.addProperty("caseIndent", "4");
633
634 checkConfig.addProperty("forceStrictCondition", "false");
635 checkConfig.addProperty("lineWrappingIndentation", "9");
636 checkConfig.addProperty("tabWidth", "4");
637 checkConfig.addProperty("throwsIndent", "4");
638 final String fileName = getPath("InputIndentationOddLineWrappingAndArrayInit.java");
639 final String[] expected = {
640 "25:17: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
641 16, "11, 17, 47, 54"),
642 };
643 verifyWarns(checkConfig, fileName, expected);
644 }
645
646 @Test
647 public void testAnnotationOddStyles() throws Exception {
648 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
649
650 checkConfig.addProperty("tabWidth", "8");
651
652 final String fileName = getPath("InputIndentationAnnotationArrayInitOldStyle.java");
653
654 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
655
656 verifyWarns(checkConfig, fileName, expected);
657 }
658
659 @Test
660 public void testZeroAnnotationArrayInit()
661 throws Exception {
662 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
663
664 checkConfig.addProperty("arrayInitIndent", "0");
665 checkConfig.addProperty("basicOffset", "4");
666 checkConfig.addProperty("braceAdjustment", "0");
667 checkConfig.addProperty("caseIndent", "4");
668 checkConfig.addProperty("forceStrictCondition", "false");
669 checkConfig.addProperty("lineWrappingIndentation", "4");
670 checkConfig.addProperty("tabWidth", "4");
671 checkConfig.addProperty("throwsIndent", "4");
672 final String fileName = getPath("InputIndentationZeroArrayInit.java");
673
674 final String[] expected = {
675 "22:12: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
676 11, "8, 12, 35, 37"),
677 };
678 verifyWarns(checkConfig, fileName, expected);
679 }
680
681 @Test
682 public void testAnnotationArrayInitGoodCase()
683 throws Exception {
684 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
685
686 checkConfig.addProperty("arrayInitIndent", "4");
687 checkConfig.addProperty("basicOffset", "4");
688 checkConfig.addProperty("braceAdjustment", "0");
689 checkConfig.addProperty("caseIndent", "4");
690 checkConfig.addProperty("forceStrictCondition", "false");
691 checkConfig.addProperty("lineWrappingIndentation", "4");
692 checkConfig.addProperty("tabWidth", "4");
693 checkConfig.addProperty("throwsIndent", "4");
694 final String fileName = getPath("InputIndentationAnnotationArrayInitGood.java");
695 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
696 verifyWarns(checkConfig, fileName, expected);
697 }
698
699 @Test
700 public void testAnnotationArrayInitGoodCaseTwo()
701 throws Exception {
702 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
703
704 checkConfig.addProperty("arrayInitIndent", "4");
705 checkConfig.addProperty("basicOffset", "4");
706 checkConfig.addProperty("braceAdjustment", "2");
707 checkConfig.addProperty("caseIndent", "4");
708 checkConfig.addProperty("forceStrictCondition", "false");
709 checkConfig.addProperty("lineWrappingIndentation", "4");
710 checkConfig.addProperty("tabWidth", "4");
711 checkConfig.addProperty("throwsIndent", "4");
712 final String fileName = getPath("InputIndentationAnnotationArrayInitGood.java");
713 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
714 verifyWarns(checkConfig, fileName, expected);
715 }
716
717 @Test
718 public void testInvalidLabel() throws Exception {
719 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
720
721 checkConfig.addProperty("arrayInitIndent", "4");
722 checkConfig.addProperty("basicOffset", "4");
723 checkConfig.addProperty("braceAdjustment", "0");
724 checkConfig.addProperty("caseIndent", "4");
725 checkConfig.addProperty("forceStrictCondition", "false");
726 checkConfig.addProperty("lineWrappingIndentation", "4");
727 checkConfig.addProperty("tabWidth", "4");
728 checkConfig.addProperty("throwsIndent", "4");
729 final String[] expected = {
730 "24:11: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "label", 10, "8, 12"),
731 "33:3: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "label", 2, "4, 8"),
732 "36:19: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "label", 18, "8, 12"),
733 "37:19: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 18, 8),
734 "39:7: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "label", 6, "8, 12"),
735 "41:7: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "label", 6, "8, 12"),
736 };
737 verifyWarns(checkConfig, getPath("InputIndentationInvalidLabelIndent.java"), expected);
738 }
739
740 @Test
741 public void testInvalidLabelWithWhileLoop() throws Exception {
742 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
743
744 checkConfig.addProperty("arrayInitIndent", "4");
745 checkConfig.addProperty("basicOffset", "4");
746 checkConfig.addProperty("braceAdjustment", "0");
747 checkConfig.addProperty("caseIndent", "4");
748 checkConfig.addProperty("forceStrictCondition", "false");
749 checkConfig.addProperty("lineWrappingIndentation", "4");
750 checkConfig.addProperty("tabWidth", "4");
751 checkConfig.addProperty("throwsIndent", "4");
752 final String[] expected = {
753 "18:10: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "label", 9, "4, 8"),
754 "19:10: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "label", 9, "8, 12"),
755 };
756 verifyWarns(checkConfig, getPath("InputIndentationInvalidLabelWithWhileLoopIndent.java"),
757 expected);
758 }
759
760 @Test
761 public void testValidLabel() throws Exception {
762 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
763
764 checkConfig.addProperty("arrayInitIndent", "4");
765 checkConfig.addProperty("basicOffset", "4");
766 checkConfig.addProperty("braceAdjustment", "0");
767 checkConfig.addProperty("caseIndent", "4");
768 checkConfig.addProperty("forceStrictCondition", "false");
769 checkConfig.addProperty("lineWrappingIndentation", "4");
770 checkConfig.addProperty("tabWidth", "4");
771 checkConfig.addProperty("throwsIndent", "4");
772 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
773 verifyWarns(checkConfig, getPath("InputIndentationValidLabelIndent.java"), expected);
774 }
775
776 @Test
777 public void testValidIfWithChecker() throws Exception {
778 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
779
780 checkConfig.addProperty("arrayInitIndent", "4");
781 checkConfig.addProperty("basicOffset", "4");
782 checkConfig.addProperty("braceAdjustment", "0");
783 checkConfig.addProperty("caseIndent", "4");
784 checkConfig.addProperty("forceStrictCondition", "false");
785 checkConfig.addProperty("lineWrappingIndentation", "4");
786 checkConfig.addProperty("tabWidth", "4");
787 checkConfig.addProperty("throwsIndent", "4");
788 final String fileName = getPath("InputIndentationValidIfIndent.java");
789 final String[] expected = {
790 "231:9: " + getCheckMessage(MSG_ERROR, "(", 8, 12),
791 };
792 verifyWarns(checkConfig, fileName, expected);
793 }
794
795 @Test
796 public void testValidDotWithChecker()
797 throws Exception {
798 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
799
800 checkConfig.addProperty("arrayInitIndent", "4");
801 checkConfig.addProperty("basicOffset", "4");
802 checkConfig.addProperty("braceAdjustment", "0");
803 checkConfig.addProperty("caseIndent", "4");
804 checkConfig.addProperty("forceStrictCondition", "false");
805 checkConfig.addProperty("lineWrappingIndentation", "4");
806 checkConfig.addProperty("tabWidth", "4");
807 checkConfig.addProperty("throwsIndent", "4");
808 final String fileName = getPath("InputIndentationValidDotIndent.java");
809 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
810 verifyWarns(checkConfig, fileName, expected);
811 }
812
813 @Test
814 public void testValidMethodWithChecker()
815 throws Exception {
816 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
817
818 checkConfig.addProperty("arrayInitIndent", "4");
819 checkConfig.addProperty("basicOffset", "4");
820 checkConfig.addProperty("braceAdjustment", "0");
821 checkConfig.addProperty("caseIndent", "4");
822 checkConfig.addProperty("forceStrictCondition", "false");
823 checkConfig.addProperty("lineWrappingIndentation", "4");
824 checkConfig.addProperty("tabWidth", "4");
825 checkConfig.addProperty("throwsIndent", "4");
826 final String fileName = getPath("InputIndentationValidMethodIndent.java");
827 final String[] expected = {
828 "129:5: " + getCheckMessage(MSG_ERROR, "void", 4, 8),
829 "130:5: " + getCheckMessage(MSG_ERROR, "method5", 4, 8),
830 };
831 verifyWarns(checkConfig, fileName, expected);
832 }
833
834 @Test
835 public void testInvalidMethodWithChecker()
836 throws Exception {
837 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
838
839 checkConfig.addProperty("arrayInitIndent", "4");
840 checkConfig.addProperty("basicOffset", "4");
841 checkConfig.addProperty("braceAdjustment", "0");
842 checkConfig.addProperty("caseIndent", "4");
843 checkConfig.addProperty("forceStrictCondition", "false");
844 checkConfig.addProperty("lineWrappingIndentation", "4");
845 checkConfig.addProperty("tabWidth", "4");
846 checkConfig.addProperty("throwsIndent", "4");
847 final String fileName = getPath("InputIndentationInvalidMethodIndent.java");
848 final String[] expected = {
849 "23:7: " + getCheckMessage(MSG_ERROR, "ctor def rcurly", 6, 4),
850 "26:7: " + getCheckMessage(MSG_ERROR, "ctor def modifier", 6, 4),
851 "27:3: " + getCheckMessage(MSG_ERROR, "ctor def lcurly", 2, 4),
852 "28:7: " + getCheckMessage(MSG_ERROR, "ctor def rcurly", 6, 4),
853 "31:3: " + getCheckMessage(MSG_ERROR, "method def modifier", 2, 4),
854 "32:7: " + getCheckMessage(MSG_ERROR, "method def rcurly", 6, 4),
855 "69:6: " + getCheckMessage(MSG_ERROR, "method def modifier", 5, 4),
856 "70:6: " + getCheckMessage(MSG_ERROR, "final", 5, 9),
857 "71:6: " + getCheckMessage(MSG_ERROR, "void", 5, 9),
858 "72:5: " + getCheckMessage(MSG_ERROR, "method5", 4, 9),
859 "80:4: " + getCheckMessage(MSG_ERROR, "method def modifier", 3, 4),
860 "81:4: " + getCheckMessage(MSG_ERROR, "final", 3, 7),
861 "82:4: " + getCheckMessage(MSG_ERROR, "void", 3, 7),
862 "83:6: " + getCheckMessage(MSG_ERROR, "method6", 5, 7),
863 "93:5: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 4, 8),
864 "98:7: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 6, 8),
865 "99:7: " + getCheckMessage(MSG_ERROR, "if", 6, 8),
866 "100:11: " + getCheckMessage(MSG_CHILD_ERROR, "if", 10, 12),
867 "101:7: " + getCheckMessage(MSG_ERROR, "if rcurly", 6, 8),
868 "104:11: " + getCheckMessage(MSG_ERROR, "Arrays", 10, 12),
869 "110:15: " + getCheckMessage(MSG_ERROR, "new", 14, 16),
870 "113:11: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 10, 12),
871 "118:15: " + getCheckMessage(MSG_ERROR, "new", 14, 16),
872 "122:11: " + getCheckMessage(MSG_ERROR, "new", 10, 12),
873 "126:11: " + getCheckMessage(MSG_ERROR, "new", 10, 12),
874 "127:7: " + getCheckMessage(MSG_ERROR, ")", 6, 8),
875 "131:7: " + getCheckMessage(MSG_ERROR, "method call rparen", 6, 8),
876 "145:11: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 10, 12),
877 "148:11: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 10, 12),
878 "158:7: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 6, 12),
879 "170:5: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 4, 8),
880 "175:5: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 4, 8),
881 "179:1: " + getCheckMessage(MSG_ERROR, "int", 0, 8),
882 "180:5: " + getCheckMessage(MSG_ERROR, "method9", 4, 8),
883 "190:13: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 12, 8),
884 };
885 verifyWarns(checkConfig, fileName, expected);
886 }
887
888 @Test
889 public void testAlternativeGoogleStyleSwitchCaseAndEnums()
890 throws Exception {
891 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
892
893 checkConfig.addProperty("arrayInitIndent", "4");
894 checkConfig.addProperty("basicOffset", "2");
895 checkConfig.addProperty("braceAdjustment", "2");
896 checkConfig.addProperty("caseIndent", "2");
897 checkConfig.addProperty("forceStrictCondition", "false");
898 checkConfig.addProperty("lineWrappingIndentation", "4");
899 checkConfig.addProperty("tabWidth", "4");
900 checkConfig.addProperty("throwsIndent", "4");
901 final String fileName = getPath("InputIndentationSwitchCasesAndEnums.java");
902 final String[] expected = {
903 "18:7: " + getCheckMessage(MSG_CHILD_ERROR, "block", 6, 4),
904 "35:7: " + getCheckMessage(MSG_ERROR, "block lcurly", 6, 8),
905 "38:11: " + getCheckMessage(MSG_ERROR, "block rcurly", 10, 8),
906 "54:5: " + getCheckMessage(MSG_ERROR, "block lcurly", 4, 2),
907 "55:3: " + getCheckMessage(MSG_CHILD_ERROR, "block", 2, 4),
908 };
909 verifyWarns(checkConfig, fileName, expected);
910 }
911
912 @Test
913 public void testInvalidSwitchWithChecker()
914 throws Exception {
915 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
916
917 checkConfig.addProperty("arrayInitIndent", "4");
918 checkConfig.addProperty("basicOffset", "4");
919 checkConfig.addProperty("braceAdjustment", "0");
920 checkConfig.addProperty("caseIndent", "4");
921 checkConfig.addProperty("forceStrictCondition", "false");
922 checkConfig.addProperty("lineWrappingIndentation", "4");
923 checkConfig.addProperty("tabWidth", "4");
924 checkConfig.addProperty("throwsIndent", "4");
925 final String fileName = getPath("InputIndentationInvalidSwitchIndent.java");
926 final String[] expected = {
927 "30:7: " + getCheckMessage(MSG_ERROR, "switch", 6, 8),
928 "32:11: " + getCheckMessage(MSG_CHILD_ERROR, "case", 10, 12),
929 "33:15: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
930 "37:15: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
931 "39:15: " + getCheckMessage(MSG_CHILD_ERROR, "case", 14, 12),
932 "40:11: " + getCheckMessage(MSG_CHILD_ERROR, "case", 10, 12),
933 "43:11: " + getCheckMessage(MSG_CHILD_ERROR, "case", 10, 12),
934 "44:15: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
935 "45:15: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
936 "53:15: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
937 "54:19: " + getCheckMessage(MSG_CHILD_ERROR, "block", 18, 16),
938 "55:11: " + getCheckMessage(MSG_ERROR, "block rcurly", 10, 12),
939 "59:11: " + getCheckMessage(MSG_ERROR, "block lcurly", 10, 12),
940 "62:15: " + getCheckMessage(MSG_ERROR, "block rcurly", 14, 12),
941 "66:15: " + getCheckMessage(MSG_ERROR, "block lcurly", 14, 12),
942 "69:11: " + getCheckMessage(MSG_ERROR, "block rcurly", 10, 12),
943 "76:15: " + getCheckMessage(MSG_CHILD_ERROR, "case", 14, 16),
944 "81:15: " + getCheckMessage(MSG_CHILD_ERROR, "case", 14, 16),
945 "89:7: " + getCheckMessage(MSG_ERROR, "switch rcurly", 6, 8),
946 "92:7: " + getCheckMessage(MSG_ERROR, "switch lcurly", 6, 8),
947 "93:11: " + getCheckMessage(MSG_ERROR, "switch rcurly", 10, 8),
948 "95:11: " + getCheckMessage(MSG_ERROR, "switch lcurly", 10, 8),
949 "96:7: " + getCheckMessage(MSG_ERROR, "switch rcurly", 6, 8),
950 "99:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 12),
951 "100:13: " + getCheckMessage(MSG_ERROR, "if", 12, 16),
952 "101:17: " + getCheckMessage(MSG_CHILD_ERROR, "if", 16, 20),
953 "102:13: " + getCheckMessage(MSG_ERROR, "else", 12, 16),
954 "103:17: " + getCheckMessage(MSG_CHILD_ERROR, "else", 16, 20),
955 "106:17: " + getCheckMessage(MSG_CHILD_ERROR, "case", 4, 12),
956 };
957 verifyWarns(checkConfig, fileName, expected);
958 }
959
960 @Test
961 public void testIfElseWithNoCurly()
962 throws Exception {
963 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
964
965 checkConfig.addProperty("arrayInitIndent", "4");
966 checkConfig.addProperty("basicOffset", "4");
967 checkConfig.addProperty("braceAdjustment", "0");
968 checkConfig.addProperty("caseIndent", "4");
969 checkConfig.addProperty("forceStrictCondition", "false");
970 checkConfig.addProperty("lineWrappingIndentation", "4");
971 checkConfig.addProperty("tabWidth", "4");
972 checkConfig.addProperty("throwsIndent", "4");
973 final String fileName = getPath("InputIndentationIfElseWithNoCurly.java");
974 final String[] expected = {
975 "20:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
976 "25:5: " + getCheckMessage(MSG_ERROR, "if", 4, 8),
977 "26:9: " + getCheckMessage(MSG_CHILD_ERROR, "if", 8, 12),
978 "37:13: " + getCheckMessage(MSG_ERROR, "else", 12, 8),
979 "39:9: " + getCheckMessage(MSG_ERROR, "if", 8, 12),
980 "43:9: " + getCheckMessage(MSG_CHILD_ERROR, "if", 8, 16),
981 };
982 verifyWarns(checkConfig, fileName, expected);
983 }
984
985 @Test
986 public void testWhileWithNoCurly()
987 throws Exception {
988 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
989
990 checkConfig.addProperty("arrayInitIndent", "4");
991 checkConfig.addProperty("basicOffset", "4");
992 checkConfig.addProperty("braceAdjustment", "0");
993 checkConfig.addProperty("caseIndent", "4");
994 checkConfig.addProperty("forceStrictCondition", "false");
995 checkConfig.addProperty("lineWrappingIndentation", "4");
996 checkConfig.addProperty("tabWidth", "4");
997 checkConfig.addProperty("throwsIndent", "4");
998 final String fileName = getPath("InputIndentationWhileNoCurly.java");
999 final String[] expected = {
1000 "21:1: " + getCheckMessage(MSG_CHILD_ERROR, "while", 0, 12),
1001 "26:5: " + getCheckMessage(MSG_ERROR, "while", 4, 8),
1002 "27:9: " + getCheckMessage(MSG_CHILD_ERROR, "while", 8, 12),
1003 "32:9: " + getCheckMessage(MSG_ERROR, "while", 8, 12),
1004 "36:9: " + getCheckMessage(MSG_CHILD_ERROR, "while", 8, 16),
1005 };
1006 verifyWarns(checkConfig, fileName, expected);
1007 }
1008
1009 @Test
1010 public void testForWithNoCurly()
1011 throws Exception {
1012 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1013
1014 checkConfig.addProperty("arrayInitIndent", "4");
1015 checkConfig.addProperty("basicOffset", "4");
1016 checkConfig.addProperty("braceAdjustment", "0");
1017 checkConfig.addProperty("caseIndent", "4");
1018 checkConfig.addProperty("forceStrictCondition", "false");
1019 checkConfig.addProperty("lineWrappingIndentation", "4");
1020 checkConfig.addProperty("tabWidth", "4");
1021 checkConfig.addProperty("throwsIndent", "4");
1022 final String fileName = getPath("InputIndentationForWithoutCurly.java");
1023 final String[] expected = {
1024 "21:1: " + getCheckMessage(MSG_CHILD_ERROR, "for", 0, 12),
1025 "26:5: " + getCheckMessage(MSG_ERROR, "for", 4, 8),
1026 "27:9: " + getCheckMessage(MSG_CHILD_ERROR, "for", 8, 12),
1027 "32:9: " + getCheckMessage(MSG_ERROR, "for", 8, 12),
1028 "33:9: " + getCheckMessage(MSG_CHILD_ERROR, "for", 8, 16),
1029 "37:9: " + getCheckMessage(MSG_CHILD_ERROR, "for", 8, 16),
1030
1031 };
1032 verifyWarns(checkConfig, fileName, expected);
1033 }
1034
1035 @Test
1036 public void testDoWhileWithoutCurly()
1037 throws Exception {
1038 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1039
1040 checkConfig.addProperty("arrayInitIndent", "4");
1041 checkConfig.addProperty("basicOffset", "4");
1042 checkConfig.addProperty("braceAdjustment", "0");
1043 checkConfig.addProperty("caseIndent", "4");
1044 checkConfig.addProperty("forceStrictCondition", "false");
1045 checkConfig.addProperty("lineWrappingIndentation", "4");
1046 checkConfig.addProperty("tabWidth", "4");
1047 checkConfig.addProperty("throwsIndent", "4");
1048 final String fileName = getPath("InputIndentationDoWhile.java");
1049 final String[] expected = {
1050 "23:9: " + getCheckMessage(MSG_CHILD_ERROR, "do..while", 8, 12),
1051 "30:5: " + getCheckMessage(MSG_ERROR, "do..while while", 4, 8),
1052 "33:13: " + getCheckMessage(MSG_ERROR, "do..while while", 12, 8),
1053 };
1054 verifyWarns(checkConfig, fileName, expected);
1055 }
1056
1057 @Test
1058 public void testValidSwitchWithChecker()
1059 throws Exception {
1060 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1061
1062 checkConfig.addProperty("arrayInitIndent", "4");
1063 checkConfig.addProperty("basicOffset", "4");
1064 checkConfig.addProperty("braceAdjustment", "0");
1065 checkConfig.addProperty("caseIndent", "4");
1066 checkConfig.addProperty("forceStrictCondition", "false");
1067 checkConfig.addProperty("lineWrappingIndentation", "4");
1068 checkConfig.addProperty("tabWidth", "4");
1069 checkConfig.addProperty("throwsIndent", "4");
1070 final String fileName = getPath("InputIndentationValidSwitchIndent.java");
1071 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1072 verifyWarns(checkConfig, fileName, expected);
1073 }
1074
1075 @Test
1076 public void testNewKeyword() throws Exception {
1077 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1078
1079 checkConfig.addProperty("basicOffset", "4");
1080 checkConfig.addProperty("forceStrictCondition", "false");
1081 checkConfig.addProperty("lineWrappingIndentation", "8");
1082 checkConfig.addProperty("tabWidth", "4");
1083 checkConfig.addProperty("throwsIndent", "8");
1084 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1085 verifyWarns(checkConfig, getPath("InputIndentationNew.java"), expected);
1086 }
1087
1088 @Test
1089 public void testNewKeyword2() throws Exception {
1090 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1091
1092 checkConfig.addProperty("basicOffset", "4");
1093 checkConfig.addProperty("forceStrictCondition", "true");
1094 checkConfig.addProperty("lineWrappingIndentation", "8");
1095 checkConfig.addProperty("tabWidth", "4");
1096 checkConfig.addProperty("throwsIndent", "8");
1097 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1098 verifyWarns(checkConfig, getPath("InputIndentationNew.java"), expected);
1099 }
1100
1101 @Test
1102 public void testValidNewKeywordWithForceStrictCondition() throws Exception {
1103 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1104
1105 checkConfig.addProperty("basicOffset", "4");
1106 checkConfig.addProperty("forceStrictCondition", "true");
1107 checkConfig.addProperty("lineWrappingIndentation", "8");
1108 checkConfig.addProperty("tabWidth", "4");
1109 checkConfig.addProperty("throwsIndent", "8");
1110 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1111 verifyWarns(checkConfig, getPath("InputIndentationNew.java"), expected);
1112 }
1113
1114 @Test
1115 public void testInvalidNewKeywordWithForceStrictCondition() throws Exception {
1116 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1117
1118 checkConfig.addProperty("basicOffset", "4");
1119 checkConfig.addProperty("forceStrictCondition", "true");
1120 checkConfig.addProperty("lineWrappingIndentation", "8");
1121 checkConfig.addProperty("tabWidth", "4");
1122 checkConfig.addProperty("throwsIndent", "8");
1123 final String[] expected = {
1124 "21:12: " + getCheckMessage(MSG_ERROR, "]", 11, 12),
1125 "25:5: " + getCheckMessage(MSG_ERROR, "[", 4, 12),
1126 "32:17: " + getCheckMessage(MSG_ERROR, "new", 16, 24),
1127 "33:21: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "object def", 20, "28, 32, 36"),
1128 "34:17: " + getCheckMessage(MSG_ERROR_MULTI, "object def rcurly", 16, "24, 28, 32"),
1129 "37:36: " + getCheckMessage(MSG_ERROR, "+", 35, 16),
1130 "41:35: " + getCheckMessage(MSG_ERROR, "]", 34, 16),
1131 "45:36: " + getCheckMessage(MSG_ERROR, "42", 35, 16),
1132 "49:36: " + getCheckMessage(MSG_ERROR, "+", 35, 16),
1133 "50:36: " + getCheckMessage(MSG_ERROR, "+", 35, 16),
1134 "55:21: " + getCheckMessage(MSG_ERROR, "1", 20, 16),
1135 "59:13: " + getCheckMessage(MSG_ERROR, "fun2", 12, 16),
1136 "78:11: " + getCheckMessage(MSG_ERROR, "Object", 10, 12),
1137 "82:16: " + getCheckMessage(MSG_ERROR, "]", 15, 12),
1138 };
1139 verifyWarns(checkConfig,
1140 getPath("InputIndentationNewWithForceStrictCondition.java"), expected);
1141 }
1142
1143 @Test
1144 public void testValidArrayInitDefaultIndentWithChecker()
1145 throws Exception {
1146 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1147
1148 checkConfig.addProperty("arrayInitIndent", "4");
1149 checkConfig.addProperty("basicOffset", "4");
1150 checkConfig.addProperty("braceAdjustment", "0");
1151 checkConfig.addProperty("caseIndent", "4");
1152 checkConfig.addProperty("forceStrictCondition", "false");
1153 checkConfig.addProperty("lineWrappingIndentation", "4");
1154 checkConfig.addProperty("tabWidth", "4");
1155 checkConfig.addProperty("throwsIndent", "4");
1156 final String fileName = getPath("InputIndentationValidArrayInitDefaultIndent.java");
1157 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1158 verifyWarns(checkConfig, fileName, expected);
1159 }
1160
1161 @Test
1162 public void testValidArrayInitWithChecker()
1163 throws Exception {
1164 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1165
1166 checkConfig.addProperty("arrayInitIndent", "8");
1167 checkConfig.addProperty("basicOffset", "4");
1168 checkConfig.addProperty("braceAdjustment", "0");
1169 checkConfig.addProperty("caseIndent", "4");
1170 checkConfig.addProperty("forceStrictCondition", "false");
1171 checkConfig.addProperty("lineWrappingIndentation", "4");
1172 checkConfig.addProperty("tabWidth", "4");
1173 checkConfig.addProperty("throwsIndent", "4");
1174 final String fileName = getPath("InputIndentationValidArrayInitIndent.java");
1175 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1176 verifyWarns(checkConfig, fileName, expected);
1177 }
1178
1179 @Test
1180 public void testValidArrayInitTwoDimensional() throws Exception {
1181 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1182
1183 checkConfig.addProperty("arrayInitIndent", "2");
1184 checkConfig.addProperty("basicOffset", "4");
1185 checkConfig.addProperty("braceAdjustment", "4");
1186 checkConfig.addProperty("caseIndent", "4");
1187 checkConfig.addProperty("forceStrictCondition", "false");
1188 checkConfig.addProperty("lineWrappingIndentation", "4");
1189 checkConfig.addProperty("tabWidth", "4");
1190 checkConfig.addProperty("throwsIndent", "4");
1191 final String fileName = getPath("InputIndentationValidArrayInitIndentTwoDimensional.java");
1192 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1193 verifyWarns(checkConfig, fileName, expected);
1194 }
1195
1196 @Test
1197 public void testInvalidArrayInitTwoDimensional() throws Exception {
1198 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1199
1200 checkConfig.addProperty("arrayInitIndent", "2");
1201 checkConfig.addProperty("basicOffset", "4");
1202 checkConfig.addProperty("braceAdjustment", "4");
1203 checkConfig.addProperty("caseIndent", "4");
1204 checkConfig.addProperty("forceStrictCondition", "false");
1205 checkConfig.addProperty("lineWrappingIndentation", "4");
1206 checkConfig.addProperty("tabWidth", "4");
1207 checkConfig.addProperty("throwsIndent", "4");
1208 final String fileName =
1209 getPath("InputIndentationInvalidArrayInitIndentTwoDimensional.java");
1210 final String[] expected = {
1211 "18:5: " + getCheckMessage(MSG_ERROR_MULTI,
1212 "array initialization lcurly", 4, "6, 8, 18, 20, 24"),
1213 "23:10: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
1214 "array initialization", 9, "8, 10, 12, 20, 22, 24"),
1215 "26:7: " + getCheckMessage(MSG_CHILD_ERROR_MULTI,
1216 "array initialization", 6, "8, 10, 12, 20, 22, 24"),
1217 "28:5: " + getCheckMessage(MSG_ERROR_MULTI,
1218 "array initialization lcurly", 4, "6, 8, 18, 20, 24"),
1219 "30:5: " + getCheckMessage(MSG_ERROR_MULTI,
1220 "array initialization rcurly", 4, "6, 8, 18, 20, 24"),
1221
1222 };
1223 verifyWarns(checkConfig, fileName, expected);
1224 }
1225
1226 @Test
1227 public void testValidArrayInit()
1228 throws Exception {
1229 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1230
1231 checkConfig.addProperty("arrayInitIndent", "2");
1232 checkConfig.addProperty("basicOffset", "2");
1233 checkConfig.addProperty("braceAdjustment", "2");
1234 checkConfig.addProperty("caseIndent", "2");
1235 checkConfig.addProperty("forceStrictCondition", "false");
1236 checkConfig.addProperty("lineWrappingIndentation", "4");
1237 checkConfig.addProperty("tabWidth", "4");
1238 checkConfig.addProperty("throwsIndent", "4");
1239 final String fileName = getPath("InputIndentationValidArrayInitIndentTwo.java");
1240 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1241 verifyWarns(checkConfig, fileName, expected);
1242 }
1243
1244 @Test
1245 public void testInvalidArrayInitWithChecker()
1246 throws Exception {
1247 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1248
1249 checkConfig.addProperty("arrayInitIndent", "4");
1250 checkConfig.addProperty("basicOffset", "4");
1251 checkConfig.addProperty("braceAdjustment", "0");
1252 checkConfig.addProperty("caseIndent", "4");
1253 checkConfig.addProperty("forceStrictCondition", "false");
1254 checkConfig.addProperty("lineWrappingIndentation", "4");
1255 checkConfig.addProperty("tabWidth", "4");
1256 checkConfig.addProperty("throwsIndent", "4");
1257 final String fileName = getPath("InputIndentationInvalidArrayInitIndent.java");
1258 final String[] expected = {
1259 "21:3: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4),
1260 "22:7: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4),
1261 "24:3: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4),
1262 "28:7: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4),
1263 "29:9: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 8, 10),
1264 "30:5: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 4, "6, 10"),
1265 "33:10: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 9, 8),
1266 "34:8: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 7, 8),
1267 "35:10: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 9, 8),
1268 "40:3: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 2, "4, 8"),
1269 "44:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "4, 8"),
1270 "48:3: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 2, "4, 8"),
1271 "52:21: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 20,
1272 "8, 31, 33"),
1273 "53:5: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1274 4, "8, 31, 33"),
1275 "58:7: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 6, 8),
1276 "63:3: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4),
1277 "65:7: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4),
1278 "66:3: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 2, "6, 10"),
1279 "69:7: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 6, 8),
1280 "76:11: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 10, 12),
1281 "89:9: " + getCheckMessage(MSG_ERROR, "1", 8, 12),
1282 "100:11: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 10, 12),
1283 "101:15: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 14, 12),
1284 "104:11: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 10, 12),
1285 "105:15: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 14, 12),
1286 "106:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "8, 12"),
1287 "109:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 6, "8, 12"),
1288 "110:15: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 14, 12),
1289 "111:11: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 10, 12),
1290 "112:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "8, 12"),
1291
1292 "120:13: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1293 12, "16, 46, 48"),
1294 "124:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1295 14, "12, 16"),
1296 "128:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1297 14, "16, 28, 30"),
1298 "129:9: " + getCheckMessage(MSG_ERROR_MULTI, "annotation array initialization rcurly",
1299 8, "12, 16"),
1300 "131:13: " + getCheckMessage(MSG_CHILD_ERROR, "annotation array initialization",
1301 12, 16),
1302 };
1303
1304
1305 verify(checkConfig, fileName, expected);
1306 }
1307
1308 @Test
1309 public void testArrayInitWithEmoji() throws Exception {
1310 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1311
1312 checkConfig.addProperty("arrayInitIndent", "2");
1313 checkConfig.addProperty("basicOffset", "2");
1314 checkConfig.addProperty("braceAdjustment", "2");
1315 checkConfig.addProperty("caseIndent", "2");
1316 checkConfig.addProperty("forceStrictCondition", "false");
1317 checkConfig.addProperty("lineWrappingIndentation", "4");
1318 checkConfig.addProperty("tabWidth", "4");
1319 checkConfig.addProperty("throwsIndent", "4");
1320 final String fileName = getPath("InputIndentationArrayInitIndentWithEmoji.java");
1321 final String[] expected = {
1322 "19:6: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1323 5, "4, 6, 52, 54"),
1324 "24:9: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1325 8, "4, 6, 35, 37"),
1326 "25:11: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1327 10, "4, 6, 35, 37"),
1328 "30:11: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly",
1329 10, "4, 6, 19, 21, 25"),
1330 };
1331 verifyWarns(checkConfig, fileName, expected);
1332 }
1333
1334 @Test
1335 public void testChainedMethodCalling() throws Exception {
1336 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1337
1338 checkConfig.addProperty("arrayInitIndent", "2");
1339 checkConfig.addProperty("basicOffset", "2");
1340 checkConfig.addProperty("braceAdjustment", "2");
1341 checkConfig.addProperty("caseIndent", "2");
1342 checkConfig.addProperty("forceStrictCondition", "false");
1343 checkConfig.addProperty("lineWrappingIndentation", "4");
1344 checkConfig.addProperty("tabWidth", "4");
1345 checkConfig.addProperty("throwsIndent", "4");
1346 final String fileName = getPath("InputIndentationChainedMethodCalls.java");
1347 final String[] expected = {
1348 "32:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 8),
1349 "37:5: " + getCheckMessage(MSG_ERROR, ".", 4, 8),
1350 "38:5: " + getCheckMessage(MSG_ERROR, ".", 4, 8),
1351 "41:5: " + getCheckMessage(MSG_ERROR, "new", 4, 8),
1352 };
1353 verifyWarns(checkConfig, fileName, expected);
1354 }
1355
1356 @Test
1357 public void testInvalidArrayInitWithTrueStrictCondition()
1358 throws Exception {
1359 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1360
1361 checkConfig.addProperty("arrayInitIndent", "4");
1362 checkConfig.addProperty("basicOffset", "4");
1363 checkConfig.addProperty("braceAdjustment", "0");
1364 checkConfig.addProperty("caseIndent", "4");
1365 checkConfig.addProperty("forceStrictCondition", "true");
1366 checkConfig.addProperty("lineWrappingIndentation", "4");
1367 checkConfig.addProperty("tabWidth", "4");
1368 checkConfig.addProperty("throwsIndent", "4");
1369 final String fileName = getPath("InputIndentationInvalidArrayInitIndent.java");
1370 final String[] expected = {
1371 "21:3: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4),
1372 "22:7: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4),
1373 "24:3: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4),
1374 "28:7: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4),
1375 "29:9: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 8, 10),
1376 "30:5: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 4, "6, 10"),
1377 "33:10: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 9, 8),
1378 "34:8: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 7, 8),
1379 "35:10: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 9, 8),
1380 "40:3: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 2, "4, 8"),
1381 "44:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "4, 8"),
1382 "48:3: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 2, "4, 8"),
1383 "52:21: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization", 20,
1384 "8, 31, 33"),
1385 "53:5: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "array initialization",
1386 4, "8, 31, 33"),
1387 "58:7: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 6, 8),
1388 "63:3: " + getCheckMessage(MSG_ERROR, "member def type", 2, 4),
1389 "65:7: " + getCheckMessage(MSG_ERROR, "member def type", 6, 4),
1390 "66:3: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 2, "6, 10"),
1391 "69:7: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 6, 8),
1392 "76:11: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 10, 12),
1393 "89:9: " + getCheckMessage(MSG_ERROR, "1", 8, 12),
1394 "100:11: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 10, 12),
1395 "101:15: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 14, 12),
1396 "104:11: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 10, 12),
1397 "105:15: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 14, 12),
1398 "106:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "8, 12"),
1399 "109:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization lcurly", 6, "8, 12"),
1400 "110:15: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 14, 12),
1401 "111:11: " + getCheckMessage(MSG_CHILD_ERROR, "array initialization", 10, 12),
1402 "112:7: " + getCheckMessage(MSG_ERROR_MULTI, "array initialization rcurly", 6, "8, 12"),
1403
1404 "120:13: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1405 12, "16, 46, 48"),
1406 "124:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1407 14, "12, 16"),
1408 "128:15: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "annotation array initialization",
1409 14, "16, 28, 30"),
1410 "129:9: " + getCheckMessage(MSG_ERROR_MULTI, "annotation array initialization rcurly",
1411 8, "12, 16"),
1412 "131:13: " + getCheckMessage(MSG_CHILD_ERROR, "annotation array initialization",
1413 12, 16),
1414 };
1415
1416
1417 verify(checkConfig, fileName, expected);
1418 }
1419
1420 @Test
1421 public void testValidTryWithChecker()
1422 throws Exception {
1423 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1424
1425 checkConfig.addProperty("arrayInitIndent", "4");
1426 checkConfig.addProperty("basicOffset", "4");
1427 checkConfig.addProperty("braceAdjustment", "0");
1428 checkConfig.addProperty("caseIndent", "4");
1429 checkConfig.addProperty("forceStrictCondition", "false");
1430 checkConfig.addProperty("lineWrappingIndentation", "4");
1431 checkConfig.addProperty("tabWidth", "4");
1432 checkConfig.addProperty("throwsIndent", "4");
1433 final String fileName = getPath("InputIndentationValidTryIndent.java");
1434 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1435 verifyWarns(checkConfig, fileName, expected);
1436 }
1437
1438 @Test
1439 public void testInvalidTryWithChecker()
1440 throws Exception {
1441 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1442
1443 checkConfig.addProperty("arrayInitIndent", "4");
1444 checkConfig.addProperty("basicOffset", "4");
1445 checkConfig.addProperty("braceAdjustment", "0");
1446 checkConfig.addProperty("caseIndent", "4");
1447 checkConfig.addProperty("forceStrictCondition", "false");
1448 checkConfig.addProperty("lineWrappingIndentation", "4");
1449 checkConfig.addProperty("tabWidth", "4");
1450 checkConfig.addProperty("throwsIndent", "4");
1451 final String fileName = getPath("InputIndentationInvalidTryIndent.java");
1452 final String[] expected = {
1453 "25:10: " + getCheckMessage(MSG_ERROR, "try", 9, 8),
1454 "26:8: " + getCheckMessage(MSG_ERROR, "try rcurly", 7, 8),
1455 "28:8: " + getCheckMessage(MSG_ERROR, "catch rcurly", 7, 8),
1456 "30:5: " + getCheckMessage(MSG_ERROR, "try", 4, 8),
1457 "31:9: " + getCheckMessage(MSG_CHILD_ERROR, "try", 8, 12),
1458 "32:5: " + getCheckMessage(MSG_ERROR, "try rcurly", 4, 8),
1459 "33:9: " + getCheckMessage(MSG_CHILD_ERROR, "finally", 8, 12),
1460 "38:9: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 8, 12),
1461 "43:11: " + getCheckMessage(MSG_ERROR, "try rcurly", 10, 8),
1462 "45:7: " + getCheckMessage(MSG_ERROR, "catch rcurly", 6, 8),
1463 "52:6: " + getCheckMessage(MSG_ERROR, "catch rcurly", 5, 8),
1464 "59:11: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 10, 12),
1465 "60:15: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 14, 12),
1466 "61:11: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 10, 12),
1467 "63:7: " + getCheckMessage(MSG_ERROR, "catch", 6, 8),
1468 "70:11: " + getCheckMessage(MSG_ERROR, "try lcurly", 10, 8),
1469 "72:11: " + getCheckMessage(MSG_ERROR, "try rcurly", 10, 8),
1470 "74:7: " + getCheckMessage(MSG_ERROR, "catch lcurly", 6, 8),
1471 "77:11: " + getCheckMessage(MSG_ERROR, "catch rcurly", 10, 8),
1472 "80:11: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 10, 12),
1473 "86:1: " + getCheckMessage(MSG_ERROR, "try", 0, 8),
1474 "87:1: " + getCheckMessage(MSG_ERROR, "try rcurly", 0, 8),
1475 "88:1: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 0, 12),
1476 "89:1: " + getCheckMessage(MSG_ERROR, "catch rcurly", 0, 8),
1477 "91:1: " + getCheckMessage(MSG_ERROR, "try", 0, 8),
1478 "92:1: " + getCheckMessage(MSG_ERROR, "try rcurly", 0, 8),
1479 "93:1: " + getCheckMessage(MSG_CHILD_ERROR, "catch", 0, 12),
1480 "94:1: " + getCheckMessage(MSG_ERROR, "catch rcurly", 0, 8),
1481 };
1482 verifyWarns(checkConfig, fileName, expected);
1483 }
1484
1485 @Test
1486 public void testInvalidClassDefWithChecker()
1487 throws Exception {
1488 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1489
1490 checkConfig.addProperty("arrayInitIndent", "4");
1491 checkConfig.addProperty("basicOffset", "4");
1492 checkConfig.addProperty("braceAdjustment", "0");
1493 checkConfig.addProperty("caseIndent", "4");
1494 checkConfig.addProperty("forceStrictCondition", "false");
1495 checkConfig.addProperty("lineWrappingIndentation", "4");
1496 checkConfig.addProperty("tabWidth", "4");
1497 checkConfig.addProperty("throwsIndent", "4");
1498 final String fileName = getPath("InputIndentationInvalidClassDefIndent.java");
1499 final String[] expected = {
1500 "22:3: " + getCheckMessage(MSG_ERROR, "class def modifier", 2, 0),
1501 "28:3: " + getCheckMessage(MSG_ERROR, "class def lcurly", 2, 0),
1502 "31:3: " + getCheckMessage(MSG_ERROR, "class def rcurly", 2, 0),
1503 "34:9: " + getCheckMessage(MSG_ERROR, "class def ident", 2, 0),
1504 "38:3: " + getCheckMessage(MSG_ERROR, "class def rcurly", 2, 0),
1505 "43:3: " + getCheckMessage(MSG_ERROR, "extends", 2, 4),
1506 "44:3: " + getCheckMessage(MSG_ERROR, "implements", 2, 4),
1507 "50:3: " + getCheckMessage(MSG_ERROR, "extends", 2, 4),
1508 "58:3: " + getCheckMessage(MSG_ERROR, "implements", 2, 4),
1509 "59:3: " + getCheckMessage(MSG_ERROR, "java", 2, 4),
1510 "64:3: " + getCheckMessage(MSG_ERROR, "class def modifier", 2, 0),
1511 "65:3: " + getCheckMessage(MSG_ERROR, "class def lcurly", 2, 0),
1512 "73:3: " + getCheckMessage(MSG_ERROR, "class def rcurly", 2, 0),
1513 "77:3: " + getCheckMessage(MSG_ERROR, "extends", 2, 4),
1514 "86:9: " + getCheckMessage(MSG_ERROR, "class def ident", 2, 4),
1515 "88:13: " + getCheckMessage(MSG_ERROR, "class def ident", 6, 4),
1516 "91:9: " + getCheckMessage(MSG_ERROR, "class def ident", 2, 4),
1517 "95:7: " + getCheckMessage(MSG_ERROR, "member def modifier", 6, 8),
1518 "101:11: " + getCheckMessage(MSG_ERROR, "int", 10, 12),
1519 "106:7: " + getCheckMessage(MSG_ERROR, "member def modifier", 6, 8),
1520 "111:7: " + getCheckMessage(MSG_ERROR, "class def rcurly", 6, 4),
1521 "113:13: " + getCheckMessage(MSG_ERROR, "class def ident", 6, 4),
1522 "119:13: " + getCheckMessage(MSG_ERROR, "class def ident", 6, 8),
1523 "122:17: " + getCheckMessage(MSG_ERROR, "class def ident", 10, 8),
1524 "124:11: " + getCheckMessage(MSG_ERROR, "class def rcurly", 10, 8),
1525 "127:11: " + getCheckMessage(MSG_ERROR, "member def type", 10, 12),
1526 "132:11: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 10, 8),
1527 "133:9: " + getCheckMessage(MSG_ERROR_MULTI, "object def lcurly", 8, "10, 14"),
1528 "137:9: " + getCheckMessage(MSG_ERROR_MULTI, "object def rcurly", 8, "10, 14"),
1529 "141:7: " + getCheckMessage(MSG_ERROR_MULTI, "object def lcurly", 6, "8, 12"),
1530 "145:7: " + getCheckMessage(MSG_ERROR_MULTI, "object def rcurly", 6, "8, 12"),
1531 "150:11: " + getCheckMessage(MSG_ERROR, "method def modifier", 10, 12),
1532 "152:11: " + getCheckMessage(MSG_ERROR, "method def rcurly", 10, 12),
1533 "188:1: " + getCheckMessage(MSG_ERROR, "class", 0, 4),
1534 };
1535 verifyWarns(checkConfig, fileName, expected);
1536 }
1537
1538 @Test
1539 public void testInvalidBlockWithChecker()
1540 throws Exception {
1541 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1542
1543 checkConfig.addProperty("arrayInitIndent", "4");
1544 checkConfig.addProperty("basicOffset", "4");
1545 checkConfig.addProperty("braceAdjustment", "0");
1546 checkConfig.addProperty("caseIndent", "4");
1547 checkConfig.addProperty("forceStrictCondition", "false");
1548 checkConfig.addProperty("lineWrappingIndentation", "4");
1549 checkConfig.addProperty("tabWidth", "4");
1550 checkConfig.addProperty("throwsIndent", "4");
1551 final String fileName = getPath("InputIndentationInvalidBlockIndent.java");
1552 final String[] expected = {
1553 "26:8: " + getCheckMessage(MSG_ERROR, "block lcurly", 7, 8),
1554 "27:10: " + getCheckMessage(MSG_ERROR, "block lcurly", 9, 8),
1555 "29:10: " + getCheckMessage(MSG_ERROR, "block lcurly", 9, 8),
1556 "30:8: " + getCheckMessage(MSG_ERROR, "block rcurly", 7, 8),
1557 "32:7: " + getCheckMessage(MSG_ERROR, "block lcurly", 6, 8),
1558 "34:7: " + getCheckMessage(MSG_ERROR, "block rcurly", 6, 8),
1559 "35:7: " + getCheckMessage(MSG_ERROR, "block lcurly", 6, 8),
1560 "38:10: " + getCheckMessage(MSG_ERROR, "block lcurly", 9, 8),
1561 "39:14: " + getCheckMessage(MSG_CHILD_ERROR, "block", 13, 12),
1562 "41:14: " + getCheckMessage(MSG_CHILD_ERROR, "block", 13, 12),
1563 "42:10: " + getCheckMessage(MSG_ERROR, "block rcurly", 9, 8),
1564 "45:7: " + getCheckMessage(MSG_ERROR, "block lcurly", 6, 8),
1565 "46:11: " + getCheckMessage(MSG_CHILD_ERROR, "block", 10, 12),
1566 "48:11: " + getCheckMessage(MSG_CHILD_ERROR, "block", 10, 12),
1567 "49:7: " + getCheckMessage(MSG_ERROR, "block rcurly", 6, 8),
1568 "52:7: " + getCheckMessage(MSG_ERROR, "block lcurly", 6, 8),
1569 "55:11: " + getCheckMessage(MSG_CHILD_ERROR, "block", 10, 12),
1570 "59:11: " + getCheckMessage(MSG_ERROR, "block lcurly", 10, 12),
1571 "63:11: " + getCheckMessage(MSG_ERROR, "block rcurly", 10, 12),
1572 "68:11: " + getCheckMessage(MSG_CHILD_ERROR, "block", 10, 12),
1573 "70:11: " + getCheckMessage(MSG_ERROR, "block lcurly", 10, 12),
1574 "71:15: " + getCheckMessage(MSG_CHILD_ERROR, "block", 14, 16),
1575 "86:11: " + getCheckMessage(MSG_ERROR, "block rcurly", 10, 12),
1576 "95:3: " + getCheckMessage(MSG_ERROR, "static initialization", 2, 4),
1577 "96:7: " + getCheckMessage(MSG_ERROR, "static initialization", 6, 4),
1578 "100:8: " + getCheckMessage(MSG_CHILD_ERROR, "static initialization", 7, 8),
1579 "103:7: " + getCheckMessage(MSG_ERROR, "static initialization", 6, 4),
1580 "105:3: " + getCheckMessage(MSG_ERROR, "static initialization rcurly", 2, 4),
1581 "107:3: " + getCheckMessage(MSG_ERROR, "static initialization", 2, 4),
1582 "109:7: " + getCheckMessage(MSG_ERROR, "static initialization rcurly", 6, 4),
1583 "111:3: " + getCheckMessage(MSG_ERROR, "static initialization", 2, 4),
1584 "113:7: " + getCheckMessage(MSG_CHILD_ERROR, "static initialization", 6, 8),
1585 "116:3: " + getCheckMessage(MSG_ERROR, "static initialization lcurly", 2, 4),
1586 "117:7: " + getCheckMessage(MSG_CHILD_ERROR, "static initialization", 6, 8),
1587 "118:7: " + getCheckMessage(MSG_ERROR, "static initialization rcurly", 6, 4),
1588 "123:7: " + getCheckMessage(MSG_CHILD_ERROR, "static initialization", 6, 8),
1589 "128:5: " + getCheckMessage(MSG_CHILD_ERROR, "static initialization", 4, 8),
1590 "129:3: " + getCheckMessage(MSG_ERROR, "static initialization rcurly", 2, 4),
1591 "134:7: " + getCheckMessage(MSG_ERROR, "static initialization rcurly", 6, 4),
1592 "137:3: " + getCheckMessage(MSG_ERROR, "block lcurly", 2, 4),
1593 "138:7: " + getCheckMessage(MSG_ERROR, "block lcurly", 6, 4),
1594 "141:3: " + getCheckMessage(MSG_ERROR, "block lcurly", 2, 4),
1595 "143:7: " + getCheckMessage(MSG_ERROR, "block rcurly", 6, 4),
1596 "145:7: " + getCheckMessage(MSG_ERROR, "block lcurly", 6, 4),
1597 "147:3: " + getCheckMessage(MSG_ERROR, "block rcurly", 2, 4),
1598 "150:7: " + getCheckMessage(MSG_CHILD_ERROR, "block", 6, 8),
1599 };
1600 verifyWarns(checkConfig, fileName, expected);
1601 }
1602
1603 @Test
1604 public void testInvalidIfWithChecker()
1605 throws Exception {
1606 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1607
1608 checkConfig.addProperty("arrayInitIndent", "4");
1609 checkConfig.addProperty("basicOffset", "4");
1610 checkConfig.addProperty("braceAdjustment", "0");
1611 checkConfig.addProperty("caseIndent", "4");
1612 checkConfig.addProperty("forceStrictCondition", "false");
1613 checkConfig.addProperty("lineWrappingIndentation", "4");
1614 checkConfig.addProperty("tabWidth", "4");
1615 checkConfig.addProperty("throwsIndent", "4");
1616 final String fileName = getPath("InputIndentationInvalidIfIndent.java");
1617 final String[] expected = {
1618 "55:2: " + getCheckMessage(MSG_ERROR, "if", 1, 8),
1619 "60:10: " + getCheckMessage(MSG_ERROR, "if", 9, 8),
1620 "61:10: " + getCheckMessage(MSG_ERROR, "if lcurly", 9, 8),
1621 "62:8: " + getCheckMessage(MSG_ERROR, "if rcurly", 7, 8),
1622 "64:7: " + getCheckMessage(MSG_ERROR, "if", 6, 8),
1623 "65:6: " + getCheckMessage(MSG_ERROR, "if lcurly", 5, 8),
1624 "66:6: " + getCheckMessage(MSG_ERROR, "if rcurly", 5, 8),
1625 "70:11: " + getCheckMessage(MSG_ERROR, "if rcurly", 10, 8),
1626 "71:8: " + getCheckMessage(MSG_ERROR, "else rcurly", 7, 8),
1627 "74:10: " + getCheckMessage(MSG_ERROR, "if", 9, 8),
1628
1629 "75:8: " + getCheckMessage(MSG_ERROR, "if lcurly", 7, 8),
1630 "77:10: " + getCheckMessage(MSG_ERROR, "else", 9, 8),
1631 "79:10: " + getCheckMessage(MSG_ERROR, "else rcurly", 9, 8),
1632 "82:11: " + getCheckMessage(MSG_ERROR, "if", 10, 8),
1633 "83:8: " + getCheckMessage(MSG_ERROR, "if rcurly", 7, 8),
1634 "84:10: " + getCheckMessage(MSG_ERROR, "else", 9, 8),
1635 "85:8: " + getCheckMessage(MSG_ERROR, "else lcurly", 7, 8),
1636 "86:10: " + getCheckMessage(MSG_ERROR, "else rcurly", 9, 8),
1637
1638 "90:10: " + getCheckMessage(MSG_ERROR, "if", 9, 8),
1639 "91:10: " + getCheckMessage(MSG_ERROR, "if lcurly", 9, 8),
1640 "92:10: " + getCheckMessage(MSG_ERROR, "if rcurly", 9, 8),
1641 "93:8: " + getCheckMessage(MSG_ERROR, "else lcurly", 7, 8),
1642 "94:11: " + getCheckMessage(MSG_ERROR, "else rcurly", 10, 8),
1643 "97:7: " + getCheckMessage(MSG_ERROR, "if", 6, 8),
1644 "98:11: " + getCheckMessage(MSG_ERROR, "if lcurly", 10, 8),
1645 "99:11: " + getCheckMessage(MSG_ERROR, "if rcurly", 10, 8),
1646 "100:8: " + getCheckMessage(MSG_ERROR, "else rcurly", 7, 8),
1647 "103:6: " + getCheckMessage(MSG_ERROR, "if", 5, 8),
1648 "104:12: " + getCheckMessage(MSG_ERROR, "if rcurly", 11, 8),
1649 "105:6: " + getCheckMessage(MSG_ERROR, "else", 5, 8),
1650 "106:12: " + getCheckMessage(MSG_ERROR, "else rcurly", 11, 8),
1651
1652 "126:15: " + getCheckMessage(MSG_CHILD_ERROR, "if", 14, 12),
1653 "131:11: " + getCheckMessage(MSG_ERROR, "if lcurly", 10, 8),
1654 "132:11: " + getCheckMessage(MSG_CHILD_ERROR, "if", 10, 12),
1655 "137:15: " + getCheckMessage(MSG_CHILD_ERROR, "if", 14, 12),
1656 "138:11: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 10, 12),
1657 "140:11: " + getCheckMessage(MSG_CHILD_ERROR, "else", 10, 12),
1658 "141:9: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 8, 12),
1659
1660 "148:17: " + getCheckMessage(MSG_CHILD_ERROR, "if", 16, 12),
1661 "149:10: " + getCheckMessage(MSG_ERROR, "if rcurly", 9, 8),
1662 "152:17: " + getCheckMessage(MSG_CHILD_ERROR, "else", 16, 12),
1663 "158:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
1664 "162:41: " + getCheckMessage(MSG_CHILD_ERROR, "else", 40, 12),
1665 "169:15: " + getCheckMessage(MSG_CHILD_ERROR, "if", 14, 12),
1666
1667 "172:15: " + getCheckMessage(MSG_CHILD_ERROR, "else", 14, 12),
1668 "178:11: " + getCheckMessage(MSG_CHILD_ERROR, "if", 10, 12),
1669 "180:11: " + getCheckMessage(MSG_CHILD_ERROR, "else", 10, 12),
1670 "184:11: " + getCheckMessage(MSG_ERROR, "if", 10, 8),
1671 "185:15: " + getCheckMessage(MSG_CHILD_ERROR, "if", 14, 12),
1672 "186:11: " + getCheckMessage(MSG_ERROR, "if rcurly", 10, 8),
1673 "187:11: " + getCheckMessage(MSG_ERROR, "else", 10, 8),
1674
1675 "188:15: " + getCheckMessage(MSG_CHILD_ERROR, "else", 14, 12),
1676 "189:11: " + getCheckMessage(MSG_ERROR, "else rcurly", 10, 8),
1677 "192:10: " + getCheckMessage(MSG_CHILD_ERROR, "if", 9, 12),
1678 "193:12: " + getCheckMessage(MSG_CHILD_ERROR, "if", 11, 12),
1679 "197:11: " + getCheckMessage(MSG_CHILD_ERROR, "if", 10, 12),
1680 "200:8: " + getCheckMessage(MSG_ERROR, "if rcurly", 7, 8),
1681 "207:11: " + getCheckMessage(MSG_CHILD_ERROR, "if", 10, 12),
1682 "209:11: " + getCheckMessage(MSG_CHILD_ERROR, "if", 10, 12),
1683
1684 "216:11: " + getCheckMessage(MSG_CHILD_ERROR, "if", 10, 12),
1685 "225:11: " + getCheckMessage(MSG_ERROR, "if", 10, 12),
1686 "229:19: " + getCheckMessage(MSG_CHILD_ERROR, "if", 18, 20),
1687 "240:11: " + getCheckMessage(MSG_ERROR, "if rparen", 10, 8),
1688 "245:7: " + getCheckMessage(MSG_ERROR, "if rparen", 6, 8),
1689 "251:7: " + getCheckMessage(MSG_ERROR, "if lparen", 6, 8),
1690 "253:7: " + getCheckMessage(MSG_ERROR, "if rparen", 6, 8),
1691 "256:1: " + getCheckMessage(MSG_ERROR, "if", 0, 8),
1692 "257:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
1693 "258:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
1694 "259:1: " + getCheckMessage(MSG_ERROR, "if rcurly", 0, 8),
1695 "260:1: " + getCheckMessage(MSG_ERROR, "if", 0, 8),
1696 "261:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
1697 "262:1: " + getCheckMessage(MSG_ERROR, "else", 0, 8),
1698 "263:1: " + getCheckMessage(MSG_CHILD_ERROR, "else", 0, 12),
1699 };
1700 verifyWarns(checkConfig, fileName, expected);
1701 }
1702
1703 @Test
1704 public void testInvalidWhileWithChecker()
1705 throws Exception {
1706 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1707
1708 checkConfig.addProperty("arrayInitIndent", "4");
1709 checkConfig.addProperty("basicOffset", "4");
1710 checkConfig.addProperty("braceAdjustment", "0");
1711 checkConfig.addProperty("caseIndent", "4");
1712 checkConfig.addProperty("forceStrictCondition", "false");
1713 checkConfig.addProperty("lineWrappingIndentation", "4");
1714 checkConfig.addProperty("tabWidth", "4");
1715 checkConfig.addProperty("throwsIndent", "4");
1716 final String fileName = getPath("InputIndentationInvalidWhileIndent.java");
1717 final String[] expected = {
1718 "25:10: " + getCheckMessage(MSG_ERROR, "while", 9, 8),
1719 "26:8: " + getCheckMessage(MSG_ERROR, "while rcurly", 7, 8),
1720 "28:8: " + getCheckMessage(MSG_ERROR, "while", 7, 8),
1721 "29:10: " + getCheckMessage(MSG_ERROR, "while lcurly", 9, 8),
1722 "30:10: " + getCheckMessage(MSG_ERROR, "while rcurly", 9, 8),
1723
1724 "32:10: " + getCheckMessage(MSG_ERROR, "while", 9, 8),
1725 "33:7: " + getCheckMessage(MSG_ERROR, "while lcurly", 6, 8),
1726 "34:15: " + getCheckMessage(MSG_CHILD_ERROR, "while", 14, 12),
1727 "35:7: " + getCheckMessage(MSG_ERROR, "while rcurly", 6, 8),
1728
1729 "37:11: " + getCheckMessage(MSG_ERROR, "while", 10, 8),
1730 "39:11: " + getCheckMessage(MSG_ERROR, "while rcurly", 10, 8),
1731 "41:11: " + getCheckMessage(MSG_ERROR, "while", 10, 8),
1732 "44:11: " + getCheckMessage(MSG_ERROR, "while rcurly", 10, 8),
1733
1734 "46:7: " + getCheckMessage(MSG_ERROR, "while", 6, 8),
1735 "47:11: " + getCheckMessage(MSG_ERROR, "while lcurly", 10, 8),
1736 "50:7: " + getCheckMessage(MSG_ERROR, "while rcurly", 6, 8),
1737 "53:15: " + getCheckMessage(MSG_ERROR, "if", 14, 12),
1738 "54:19: " + getCheckMessage(MSG_CHILD_ERROR, "if", 18, 16),
1739 "55:15: " + getCheckMessage(MSG_ERROR, "if rcurly", 14, 12),
1740 "56:15: " + getCheckMessage(MSG_CHILD_ERROR, "while", 14, 12),
1741 "57:11: " + getCheckMessage(MSG_ERROR, "while rcurly", 10, 8),
1742
1743 "60:11: " + getCheckMessage(MSG_CHILD_ERROR, "while", 10, 12),
1744 "66:11: " + getCheckMessage(MSG_CHILD_ERROR, "while", 10, 12),
1745 "71:11: " + getCheckMessage(MSG_CHILD_ERROR, "while", 10, 12),
1746 "78:6: " + getCheckMessage(MSG_ERROR, "while rparen", 5, 8),
1747 "85:11: " + getCheckMessage(MSG_ERROR, "while rparen", 10, 8),
1748 "92:11: " + getCheckMessage(MSG_ERROR, "while rparen", 10, 8),
1749 "99:9: " + getCheckMessage(MSG_CHILD_ERROR, "while", 8, 12),
1750 };
1751 verifyWarns(checkConfig, fileName, expected);
1752 }
1753
1754 @Test
1755 public void testInvalidInvalidAnonymousClass() throws Exception {
1756 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1757
1758 checkConfig.addProperty("arrayInitIndent", "4");
1759 checkConfig.addProperty("basicOffset", "4");
1760 checkConfig.addProperty("braceAdjustment", "0");
1761 checkConfig.addProperty("caseIndent", "4");
1762 checkConfig.addProperty("forceStrictCondition", "false");
1763 checkConfig.addProperty("lineWrappingIndentation", "4");
1764 checkConfig.addProperty("tabWidth", "4");
1765 checkConfig.addProperty("throwsIndent", "4");
1766 final String fileName = getPath("InputIndentationInvalidAnonymousClassIndent.java");
1767 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1768 verifyWarns(checkConfig, fileName, expected);
1769 }
1770
1771 @Test
1772 public void testInvalidForWithChecker()
1773 throws Exception {
1774 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1775
1776 checkConfig.addProperty("arrayInitIndent", "4");
1777 checkConfig.addProperty("basicOffset", "4");
1778 checkConfig.addProperty("braceAdjustment", "0");
1779 checkConfig.addProperty("caseIndent", "4");
1780 checkConfig.addProperty("forceStrictCondition", "false");
1781 checkConfig.addProperty("lineWrappingIndentation", "4");
1782 checkConfig.addProperty("tabWidth", "4");
1783 checkConfig.addProperty("throwsIndent", "4");
1784 final String fileName = getPath("InputIndentationInvalidForIndent.java");
1785 final String[] expected = {
1786 "26:7: " + getCheckMessage(MSG_ERROR, "for", 6, 8),
1787 "27:11: " + getCheckMessage(MSG_ERROR, "for rcurly", 10, 8),
1788 "29:10: " + getCheckMessage(MSG_ERROR, "for", 9, 8),
1789 "30:7: " + getCheckMessage(MSG_ERROR, "for lcurly", 6, 8),
1790 "31:7: " + getCheckMessage(MSG_ERROR, "for rcurly", 6, 8),
1791 "35:11: " + getCheckMessage(MSG_CHILD_ERROR, "for", 10, 12),
1792
1793 "36:11: " + getCheckMessage(MSG_ERROR, "for rcurly", 10, 8),
1794 "39:11: " + getCheckMessage(MSG_ERROR, "for lcurly", 10, 8),
1795 "40:11: " + getCheckMessage(MSG_CHILD_ERROR, "for", 10, 12),
1796 "48:11: " + getCheckMessage(MSG_CHILD_ERROR, "for", 10, 12),
1797 "54:8: " + getCheckMessage(MSG_ERROR, "for", 7, 8),
1798
1799 "55:11: " + getCheckMessage(MSG_CHILD_ERROR, "for", 10, 12),
1800 "64:8: " + getCheckMessage(MSG_CHILD_ERROR, "for", 7, 12),
1801
1802 "69:7: " + getCheckMessage(MSG_ERROR, "for", 6, 8),
1803 "70:11: " + getCheckMessage(MSG_CHILD_ERROR, "for", 10, 12),
1804 "71:15: " + getCheckMessage(MSG_CHILD_ERROR, "for", 14, 16),
1805 "72:11: " + getCheckMessage(MSG_CHILD_ERROR, "for", 10, 12),
1806 "81:13: " + getCheckMessage(MSG_ERROR, "for rparen", 12, 8),
1807 "86:3: " + getCheckMessage(MSG_ERROR, "method def modifier", 2, 4),
1808 "87:5: " + getCheckMessage(MSG_ERROR, "for", 4, 8),
1809 "88:9: " + getCheckMessage(MSG_CHILD_ERROR, "for", 8, 12),
1810 "89:7: " + getCheckMessage(MSG_CHILD_ERROR, "for", 6, 12),
1811 "90:9: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 8, 16),
1812 "92:1: " + getCheckMessage(MSG_ERROR, "for", 0, 8),
1813 "93:1: " + getCheckMessage(MSG_ERROR, "for lparen", 0, 8),
1814 "94:1: " + getCheckMessage(MSG_CHILD_ERROR, "for", 0, 12),
1815 "95:1: " + getCheckMessage(MSG_ERROR, ";", 0, 4),
1816 "96:1: " + getCheckMessage(MSG_CHILD_ERROR, "for", 0, 12),
1817 "97:1: " + getCheckMessage(MSG_ERROR, ";", 0, 4),
1818 "98:1: " + getCheckMessage(MSG_CHILD_ERROR, "for", 0, 12),
1819 };
1820 verifyWarns(checkConfig, fileName, expected);
1821 }
1822
1823 @Test
1824 public void testValidForWithChecker()
1825 throws Exception {
1826 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1827
1828 checkConfig.addProperty("arrayInitIndent", "4");
1829 checkConfig.addProperty("basicOffset", "4");
1830 checkConfig.addProperty("braceAdjustment", "0");
1831 checkConfig.addProperty("caseIndent", "4");
1832 checkConfig.addProperty("forceStrictCondition", "false");
1833 checkConfig.addProperty("lineWrappingIndentation", "4");
1834 checkConfig.addProperty("tabWidth", "4");
1835 checkConfig.addProperty("throwsIndent", "4");
1836 final String fileName = getPath("InputIndentationValidForIndent.java");
1837 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1838 verifyWarns(checkConfig, fileName, expected);
1839 }
1840
1841 @Test
1842 public void testValidDoWhileWithChecker()
1843 throws Exception {
1844 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1845
1846 checkConfig.addProperty("arrayInitIndent", "4");
1847 checkConfig.addProperty("basicOffset", "4");
1848 checkConfig.addProperty("braceAdjustment", "0");
1849 checkConfig.addProperty("caseIndent", "4");
1850 checkConfig.addProperty("forceStrictCondition", "false");
1851 checkConfig.addProperty("lineWrappingIndentation", "4");
1852 checkConfig.addProperty("tabWidth", "4");
1853 checkConfig.addProperty("throwsIndent", "4");
1854 final String fileName = getPath("InputIndentationValidDoWhileIndent.java");
1855 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1856 verifyWarns(checkConfig, fileName, expected);
1857 }
1858
1859 @Test
1860 public void testInvalidDoWhileWithChecker()
1861 throws Exception {
1862 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1863
1864 checkConfig.addProperty("arrayInitIndent", "4");
1865 checkConfig.addProperty("basicOffset", "4");
1866 checkConfig.addProperty("braceAdjustment", "0");
1867 checkConfig.addProperty("caseIndent", "4");
1868 checkConfig.addProperty("forceStrictCondition", "false");
1869 checkConfig.addProperty("lineWrappingIndentation", "4");
1870 checkConfig.addProperty("tabWidth", "4");
1871 checkConfig.addProperty("throwsIndent", "4");
1872 final String fileName = getPath("InputIndentationInvalidDoWhileIndent.java");
1873 final String[] expected = {
1874 "7:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
1875 "8:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
1876 "9:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
1877 "10:1: " + getCheckMessage(MSG_ERROR, "do..while rcurly", 0, 8),
1878 "11:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
1879 "12:1: " + getCheckMessage(MSG_ERROR, "do..while while", 0, 8),
1880 "13:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
1881 "14:1: " + getCheckMessage(MSG_ERROR, "do..while lcurly", 0, 8),
1882 "15:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
1883 "16:1: " + getCheckMessage(MSG_ERROR, "do..while while", 0, 8),
1884 "17:1: " + getCheckMessage(MSG_ERROR, "do..while lparen", 0, 8),
1885 "18:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
1886 "19:1: " + getCheckMessage(MSG_ERROR, "do..while lparen", 0, 8),
1887 "20:1: " + getCheckMessage(MSG_ERROR, "do..while", 0, 8),
1888 "21:1: " + getCheckMessage(MSG_ERROR, "do..while lparen", 0, 8),
1889 "22:1: " + getCheckMessage(MSG_CHILD_ERROR, "do..while", 0, 8),
1890 "23:1: " + getCheckMessage(MSG_ERROR, "do..while rparen", 0, 8),
1891 };
1892 verifyWarns(checkConfig, fileName, expected);
1893 }
1894
1895 @Test
1896 public void testValidBlockWithChecker()
1897 throws Exception {
1898 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1899
1900 checkConfig.addProperty("arrayInitIndent", "4");
1901 checkConfig.addProperty("basicOffset", "4");
1902 checkConfig.addProperty("braceAdjustment", "0");
1903 checkConfig.addProperty("caseIndent", "4");
1904 checkConfig.addProperty("forceStrictCondition", "false");
1905 checkConfig.addProperty("lineWrappingIndentation", "4");
1906 checkConfig.addProperty("tabWidth", "4");
1907 checkConfig.addProperty("throwsIndent", "4");
1908 final String fileName = getPath("InputIndentationValidBlockIndent.java");
1909 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1910 verifyWarns(checkConfig, fileName, expected);
1911 }
1912
1913 @Test
1914 public void testValidWhileWithChecker()
1915 throws Exception {
1916 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1917
1918 checkConfig.addProperty("arrayInitIndent", "4");
1919 checkConfig.addProperty("basicOffset", "4");
1920 checkConfig.addProperty("braceAdjustment", "0");
1921 checkConfig.addProperty("caseIndent", "4");
1922 checkConfig.addProperty("forceStrictCondition", "false");
1923 checkConfig.addProperty("lineWrappingIndentation", "4");
1924 checkConfig.addProperty("tabWidth", "4");
1925 checkConfig.addProperty("throwsIndent", "4");
1926 final String fileName = getPath("InputIndentationValidWhileIndent.java");
1927 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1928 verifyWarns(checkConfig, fileName, expected);
1929 }
1930
1931 @Test
1932 public void testValidClassDefWithChecker()
1933 throws Exception {
1934 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1935
1936 checkConfig.addProperty("arrayInitIndent", "4");
1937 checkConfig.addProperty("basicOffset", "4");
1938 checkConfig.addProperty("braceAdjustment", "0");
1939 checkConfig.addProperty("caseIndent", "4");
1940 checkConfig.addProperty("forceStrictCondition", "false");
1941 checkConfig.addProperty("lineWrappingIndentation", "4");
1942 checkConfig.addProperty("tabWidth", "4");
1943 checkConfig.addProperty("throwsIndent", "4");
1944 final String fileName = getPath("InputIndentationValidClassDefIndent.java");
1945 final String[] expected = {
1946 "49:1: " + getCheckMessage(MSG_ERROR, "class", 0, 4),
1947 "71:9: " + getCheckMessage(MSG_ERROR, "int", 8, 12),
1948 };
1949 verifyWarns(checkConfig, fileName, expected);
1950 }
1951
1952 @Test
1953 public void testValidInterfaceDefWithChecker()
1954 throws Exception {
1955 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1956
1957 checkConfig.addProperty("arrayInitIndent", "4");
1958 checkConfig.addProperty("basicOffset", "4");
1959 checkConfig.addProperty("braceAdjustment", "0");
1960 checkConfig.addProperty("caseIndent", "4");
1961 checkConfig.addProperty("forceStrictCondition", "false");
1962 checkConfig.addProperty("lineWrappingIndentation", "4");
1963 checkConfig.addProperty("tabWidth", "4");
1964 checkConfig.addProperty("throwsIndent", "4");
1965 final String fileName = getPath("InputIndentationValidInterfaceDefIndent.java");
1966 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1967 verifyWarns(checkConfig, fileName, expected);
1968 }
1969
1970 @Test
1971 public void testValidCommaWithChecker()
1972 throws Exception {
1973 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1974
1975 checkConfig.addProperty("arrayInitIndent", "4");
1976 checkConfig.addProperty("basicOffset", "4");
1977 checkConfig.addProperty("braceAdjustment", "0");
1978 checkConfig.addProperty("caseIndent", "4");
1979 checkConfig.addProperty("forceStrictCondition", "false");
1980 checkConfig.addProperty("lineWrappingIndentation", "4");
1981 checkConfig.addProperty("tabWidth", "4");
1982 checkConfig.addProperty("throwsIndent", "4");
1983 final String fileName = getPath("InputIndentationValidCommaIndent.java");
1984 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
1985 verifyWarns(checkConfig, fileName, expected);
1986 }
1987
1988 @Test
1989 public void testTabs() throws Exception {
1990 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
1991
1992 checkConfig.addProperty("arrayInitIndent", "4");
1993 checkConfig.addProperty("basicOffset", "4");
1994 checkConfig.addProperty("braceAdjustment", "0");
1995 checkConfig.addProperty("caseIndent", "4");
1996 checkConfig.addProperty("forceStrictCondition", "false");
1997 checkConfig.addProperty("lineWrappingIndentation", "4");
1998 checkConfig.addProperty("tabWidth", "4");
1999 checkConfig.addProperty("throwsIndent", "4");
2000 final String[] expected = {
2001 "29:10: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 9, 8),
2002 };
2003 verifyWarns(checkConfig, getPath("InputIndentationUseTabs.java"), expected);
2004 }
2005
2006 @Test
2007 public void testIndentationLevel() throws Exception {
2008 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2009
2010 checkConfig.addProperty("arrayInitIndent", "4");
2011 checkConfig.addProperty("basicOffset", "2");
2012 checkConfig.addProperty("braceAdjustment", "0");
2013 checkConfig.addProperty("caseIndent", "4");
2014 checkConfig.addProperty("forceStrictCondition", "false");
2015 checkConfig.addProperty("lineWrappingIndentation", "2");
2016 checkConfig.addProperty("tabWidth", "4");
2017 checkConfig.addProperty("throwsIndent", "4");
2018 final String[] expected = {
2019 "29:6: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 5, 4),
2020 };
2021 verifyWarns(checkConfig, getPath("InputIndentationUseTwoSpaces.java"), expected);
2022 }
2023
2024 @Test
2025 public void testThrowsIndentationLevel() throws Exception {
2026 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2027
2028 checkConfig.addProperty("arrayInitIndent", "4");
2029 checkConfig.addProperty("basicOffset", "4");
2030 checkConfig.addProperty("braceAdjustment", "0");
2031 checkConfig.addProperty("caseIndent", "4");
2032 checkConfig.addProperty("forceStrictCondition", "false");
2033 checkConfig.addProperty("lineWrappingIndentation", "4");
2034 checkConfig.addProperty("tabWidth", "4");
2035 checkConfig.addProperty("throwsIndent", "8");
2036 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2037 verifyWarns(checkConfig, getPath("InputIndentationInvalidThrowsIndent.java"), expected);
2038 }
2039
2040 @Test
2041 public void testThrowsIndentationLevel2() throws Exception {
2042 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2043
2044 checkConfig.addProperty("basicOffset", "1");
2045 checkConfig.addProperty("forceStrictCondition", "true");
2046 checkConfig.addProperty("lineWrappingIndentation", "3");
2047 checkConfig.addProperty("tabWidth", "4");
2048 checkConfig.addProperty("throwsIndent", "5");
2049 final String[] expected = {
2050 "7:1: " + getCheckMessage(MSG_ERROR, "Exception", 0, 6),
2051 "10:1: " + getCheckMessage(MSG_ERROR, "NullPointerException", 0, 6),
2052 "13:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 6),
2053 "16:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 6),
2054 "18:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 6),
2055 "19:1: " + getCheckMessage(MSG_ERROR, "Exception", 0, 6),
2056 "22:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 6),
2057 "23:1: " + getCheckMessage(MSG_ERROR, "Exception", 0, 6),
2058 "24:1: " + getCheckMessage(MSG_ERROR, "NullPointerException", 0, 6),
2059 "27:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 6),
2060 "28:1: " + getCheckMessage(MSG_ERROR, "Exception", 0, 6),
2061 "31:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 6),
2062 "37:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 6),
2063 };
2064 verifyWarns(checkConfig, getPath("InputIndentationInvalidThrowsIndent2.java"), expected);
2065 }
2066
2067 @Test
2068 public void testCaseLevel() throws Exception {
2069 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2070
2071 checkConfig.addProperty("arrayInitIndent", "4");
2072 checkConfig.addProperty("basicOffset", "4");
2073 checkConfig.addProperty("braceAdjustment", "0");
2074 checkConfig.addProperty("caseIndent", "0");
2075 checkConfig.addProperty("forceStrictCondition", "false");
2076 checkConfig.addProperty("lineWrappingIndentation", "4");
2077 checkConfig.addProperty("tabWidth", "4");
2078 checkConfig.addProperty("throwsIndent", "4");
2079 final String[] expected = {
2080 "27:11: " + getCheckMessage(MSG_CHILD_ERROR, "case", 10, 8),
2081 };
2082 verifyWarns(checkConfig, getPath("InputIndentationCaseLevel.java"), expected);
2083 }
2084
2085 @Test
2086 public void testBraceAdjustment() throws Exception {
2087 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2088
2089 checkConfig.addProperty("arrayInitIndent", "4");
2090 checkConfig.addProperty("basicOffset", "4");
2091 checkConfig.addProperty("braceAdjustment", "2");
2092 checkConfig.addProperty("caseIndent", "4");
2093 checkConfig.addProperty("forceStrictCondition", "false");
2094 checkConfig.addProperty("lineWrappingIndentation", "4");
2095 checkConfig.addProperty("tabWidth", "4");
2096 checkConfig.addProperty("throwsIndent", "4");
2097 final String[] expected = {
2098 "24:9: " + getCheckMessage(MSG_CHILD_ERROR, "ctor def", 8, 10),
2099 "25:9: " + getCheckMessage(MSG_ERROR, "if", 8, 10),
2100 "26:11: " + getCheckMessage(MSG_ERROR, "if lcurly", 10, 12),
2101 "27:13: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "if", 12, "14, 16"),
2102 "28:9: " + getCheckMessage(MSG_ERROR, "if rcurly", 8, 12),
2103 };
2104 verifyWarns(checkConfig, getPath("InputIndentationBraceAdjustment.java"), expected);
2105 }
2106
2107 @Test
2108 public void testInvalidAssignWithChecker() throws Exception {
2109 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2110
2111 checkConfig.addProperty("arrayInitIndent", "4");
2112 checkConfig.addProperty("basicOffset", "4");
2113 checkConfig.addProperty("braceAdjustment", "0");
2114 checkConfig.addProperty("caseIndent", "4");
2115 checkConfig.addProperty("forceStrictCondition", "false");
2116 checkConfig.addProperty("lineWrappingIndentation", "4");
2117 checkConfig.addProperty("tabWidth", "4");
2118 checkConfig.addProperty("throwsIndent", "4");
2119 final String[] expected = {
2120 "22:11: " + getCheckMessage(MSG_ERROR, "getLineNo", 10, 12),
2121 "24:11: " + getCheckMessage(MSG_ERROR, "getLine", 10, 12),
2122 "28:10: " + getCheckMessage(MSG_ERROR, "=", 9, 12),
2123 "29:11: " + getCheckMessage(MSG_ERROR, "1", 10, 12),
2124 };
2125 verifyWarns(checkConfig, getPath("InputIndentationInvalidAssignIndent.java"), expected);
2126 }
2127
2128 @Test
2129 public void testInvalidImportIndent() throws Exception {
2130 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2131 checkConfig.addProperty("basicOffset", "8");
2132 checkConfig.addProperty("tabWidth", "4");
2133 final String[] expected = {
2134 "4:3: " + getCheckMessage(MSG_ERROR, ".", 2, 4),
2135 "5:2: " + getCheckMessage(MSG_ERROR, "import", 1, 0),
2136 };
2137 verifyWarns(checkConfig, getPath("InputIndentationInvalidImportIndent.java"), expected);
2138 }
2139
2140 @Test
2141 public void testValidAssignWithChecker() throws Exception {
2142 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2143
2144 checkConfig.addProperty("arrayInitIndent", "4");
2145 checkConfig.addProperty("basicOffset", "4");
2146 checkConfig.addProperty("braceAdjustment", "0");
2147 checkConfig.addProperty("caseIndent", "4");
2148 checkConfig.addProperty("forceStrictCondition", "false");
2149 checkConfig.addProperty("lineWrappingIndentation", "4");
2150 checkConfig.addProperty("tabWidth", "4");
2151 checkConfig.addProperty("throwsIndent", "4");
2152 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2153 verifyWarns(checkConfig, getPath("InputIndentationValidAssignIndent.java"), expected);
2154 }
2155
2156 @Test
2157 public void test15Extensions() throws Exception {
2158 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2159
2160 checkConfig.addProperty("arrayInitIndent", "4");
2161 checkConfig.addProperty("basicOffset", "4");
2162 checkConfig.addProperty("braceAdjustment", "0");
2163 checkConfig.addProperty("caseIndent", "4");
2164 checkConfig.addProperty("forceStrictCondition", "false");
2165 checkConfig.addProperty("lineWrappingIndentation", "4");
2166 checkConfig.addProperty("tabWidth", "4");
2167 checkConfig.addProperty("throwsIndent", "4");
2168 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2169 verifyWarns(checkConfig, getPath("InputIndentation15Extensions.java"), expected);
2170 }
2171
2172 @Test
2173 public void testTryResources() throws Exception {
2174 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2175
2176 checkConfig.addProperty("arrayInitIndent", "4");
2177 checkConfig.addProperty("basicOffset", "4");
2178 checkConfig.addProperty("braceAdjustment", "0");
2179 checkConfig.addProperty("caseIndent", "4");
2180 checkConfig.addProperty("forceStrictCondition", "false");
2181 checkConfig.addProperty("lineWrappingIndentation", "4");
2182 checkConfig.addProperty("tabWidth", "4");
2183 checkConfig.addProperty("throwsIndent", "4");
2184 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2185 verifyWarns(checkConfig, getPath("InputIndentationValidTryResourcesIndent.java"),
2186 expected);
2187 }
2188
2189 @Test
2190 public void testSwitchCustom() 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", "8");
2199 checkConfig.addProperty("tabWidth", "4");
2200 checkConfig.addProperty("throwsIndent", "8");
2201 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2202 verifyWarns(checkConfig, getPath("InputIndentationSwitchCustom.java"),
2203 expected);
2204 }
2205
2206 @Test
2207 public void testSynchronizedStatement() throws Exception {
2208 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2209 checkConfig.addProperty("arrayInitIndent", "4");
2210 checkConfig.addProperty("basicOffset", "4");
2211 checkConfig.addProperty("braceAdjustment", "0");
2212 checkConfig.addProperty("caseIndent", "4");
2213 checkConfig.addProperty("forceStrictCondition", "false");
2214 checkConfig.addProperty("lineWrappingIndentation", "8");
2215 checkConfig.addProperty("tabWidth", "4");
2216 checkConfig.addProperty("throwsIndent", "8");
2217 final String[] expected = {
2218 "27:1: " + getCheckMessage(MSG_CHILD_ERROR, "synchronized", 0, 12),
2219 "30:13: " + getCheckMessage(MSG_ERROR, "synchronized lparen", 12, 8),
2220 };
2221 verifyWarns(checkConfig, getPath("InputIndentationSynchronizedStatement.java"), expected);
2222 }
2223
2224 @Test
2225 public void testSynchronizedMethod() throws Exception {
2226 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2227 checkConfig.addProperty("arrayInitIndent", "4");
2228 checkConfig.addProperty("basicOffset", "4");
2229 checkConfig.addProperty("braceAdjustment", "0");
2230 checkConfig.addProperty("caseIndent", "4");
2231 checkConfig.addProperty("forceStrictCondition", "false");
2232 checkConfig.addProperty("lineWrappingIndentation", "8");
2233 checkConfig.addProperty("tabWidth", "4");
2234 checkConfig.addProperty("throwsIndent", "8");
2235 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2236 verifyWarns(checkConfig, getPath("InputIndentationSynchronizedMethod.java"), expected);
2237 }
2238
2239 @Test
2240 public void testAnonymousClassInMethod() throws Exception {
2241 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2242 checkConfig.addProperty("tabWidth", "8");
2243 checkConfig.addProperty("basicOffset", "2");
2244 checkConfig.addProperty("braceAdjustment", "0");
2245 checkConfig.addProperty("caseIndent", "2");
2246 checkConfig.addProperty("lineWrappingIndentation", "4");
2247 checkConfig.addProperty("throwsIndent", "4");
2248 checkConfig.addProperty("arrayInitIndent", "2");
2249 final String[] expected = {
2250 "19:9: " + getCheckMessage(MSG_ERROR, "method def modifier", 8, 2),
2251 "20:17: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 16, 4),
2252 "21:25: " + getCheckMessage(MSG_ERROR_MULTI, "method def modifier", 24, "18, 20, 22"),
2253 "23:33: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "method def", 32, "20, 22, 24"),
2254 "24:25: " + getCheckMessage(MSG_ERROR_MULTI, "method def rcurly", 24, "18, 20, 22"),
2255 "26:9: " + getCheckMessage(MSG_ERROR, "method def rcurly", 8, 2),
2256 };
2257 verifyWarns(checkConfig, getPath("InputIndentationAnonymousClassInMethod.java"), expected);
2258 }
2259
2260 @Test
2261 public void testAnonymousClassInMethodWithCurlyOnNewLine() throws Exception {
2262 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2263 checkConfig.addProperty("tabWidth", "4");
2264 checkConfig.addProperty("basicOffset", "4");
2265 checkConfig.addProperty("braceAdjustment", "0");
2266 checkConfig.addProperty("caseIndent", "4");
2267 checkConfig.addProperty("lineWrappingIndentation", "8");
2268 checkConfig.addProperty("throwsIndent", "4");
2269 checkConfig.addProperty("arrayInitIndent", "4");
2270 final String[] expected = {
2271 "38:19: " + getCheckMessage(MSG_ERROR_MULTI, "object def rcurly", 18, "16, 20, 24"),
2272 "40:15: " + getCheckMessage(MSG_ERROR, "new", 14, 16),
2273 "46:15: " + getCheckMessage(MSG_ERROR_MULTI, "object def rcurly", 14, "16, 20, 24"),
2274 "58:19: " + getCheckMessage(MSG_ERROR_MULTI, "object def lcurly", 18, "16, 20, 24"),
2275 "64:19: " + getCheckMessage(MSG_ERROR_MULTI, "object def rcurly", 18, "16, 20, 24"),
2276 "67:15: " + getCheckMessage(MSG_ERROR_MULTI, "object def lcurly", 14, "16, 20, 24"),
2277 "73:15: " + getCheckMessage(MSG_ERROR_MULTI, "object def rcurly", 14, "16, 20, 24"),
2278 };
2279 verifyWarns(checkConfig,
2280 getPath("InputIndentationAnonymousClassInMethodCurlyOnNewLine.java"), expected);
2281 }
2282
2283 @Test
2284 public void testAnnotationDefinition() throws Exception {
2285 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2286 checkConfig.addProperty("tabWidth", "4");
2287 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2288 verifyWarns(checkConfig, getPath("InputIndentationAnnotationDefinition.java"), expected);
2289 }
2290
2291 @Test
2292 public void testPackageDeclaration() throws Exception {
2293 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2294 checkConfig.addProperty("tabWidth", "4");
2295 final String[] expected = {
2296 "1:2: " + getCheckMessage(MSG_ERROR, "package def", 1, 0),
2297 };
2298 verifyWarns(checkConfig, getPath("InputIndentationPackageDeclaration.java"), expected);
2299 }
2300
2301 @Test
2302 public void testPackageDeclaration2() throws Exception {
2303 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2304 checkConfig.addProperty("tabWidth", "4");
2305 final String[] expected = {
2306 "2:2: " + getCheckMessage(MSG_ERROR, "package def", 1, 0),
2307 };
2308 verifyWarns(checkConfig,
2309 getPath("package-info.java"), expected);
2310 }
2311
2312 @Test
2313 public void testPackageDeclaration3() throws Exception {
2314 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2315 checkConfig.addProperty("tabWidth", "4");
2316 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2317 verifyWarns(checkConfig, getPath("InputIndentationPackageDeclaration3.java"), expected);
2318 }
2319
2320 @Test
2321 public void testPackageDeclaration4() throws Exception {
2322 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2323 checkConfig.addProperty("tabWidth", "4");
2324 final String[] expected = {
2325 "2:1: " + getCheckMessage(MSG_ERROR, "com", 0, 4),
2326 "3:1: " + getCheckMessage(MSG_ERROR, "checks", 0, 4),
2327 };
2328 verifyWarns(checkConfig, getPath("InputIndentationPackageDeclaration4.java"), expected);
2329 }
2330
2331 @Test
2332 public void testLambda1() throws Exception {
2333 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2334 checkConfig.addProperty("tabWidth", "2");
2335 checkConfig.addProperty("basicOffset", "2");
2336 checkConfig.addProperty("lineWrappingIndentation", "4");
2337 final String[] expected = {
2338 "46:6: " + getCheckMessage(MSG_ERROR_MULTI, "block lcurly", 5, "4, 8"),
2339 "47:6: " + getCheckMessage(MSG_ERROR_MULTI, "block rcurly", 5, "4, 8"),
2340 "51:12: " + getCheckMessage(MSG_ERROR, "lambda", 11, 12),
2341 "52:10: " + getCheckMessage(MSG_ERROR, "block lcurly", 9, 8),
2342 "64:8: " + getCheckMessage(MSG_CHILD_ERROR, "block", 7, 6),
2343 "65:6: " + getCheckMessage(MSG_ERROR, "block rcurly", 5, 4),
2344 "179:10: " + getCheckMessage(MSG_CHILD_ERROR, "block", 9, 10),
2345 "180:12: " + getCheckMessage(MSG_CHILD_ERROR, "block", 11, 10),
2346 "185:8: " + getCheckMessage(MSG_ERROR, "block rcurly", 7, 8),
2347 };
2348 verifyWarns(checkConfig, getPath("InputIndentationLambda1.java"), expected);
2349 }
2350
2351 @Test
2352 public void testLambda2() throws Exception {
2353 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2354 checkConfig.addProperty("tabWidth", "4");
2355 checkConfig.addProperty("basicOffset", "4");
2356 checkConfig.addProperty("lineWrappingIndentation", "8");
2357 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2358 verifyWarns(checkConfig, getPath("InputIndentationLambda2.java"), expected);
2359 }
2360
2361 @Test
2362 public void testLambda3() throws Exception {
2363 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2364 checkConfig.addProperty("tabWidth", "4");
2365 checkConfig.addProperty("basicOffset", "4");
2366 checkConfig.addProperty("lineWrappingIndentation", "8");
2367 final String[] expected = {
2368 "15:13: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 12, 8),
2369 "29:13: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 12, 8),
2370 "30:13: " + getCheckMessage(MSG_CHILD_ERROR, "block", 12, 16),
2371 "31:9: " + getCheckMessage(MSG_ERROR, "block rcurly", 8, 12),
2372 "65:13: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 12, 8),
2373 "87:13: " + getCheckMessage(MSG_ERROR, "method def rcurly", 12, 8),
2374 };
2375 verifyWarns(checkConfig, getPath("InputIndentationLambda3.java"), expected);
2376 }
2377
2378 @Test
2379 public void testLambda4() throws Exception {
2380 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2381 checkConfig.addProperty("tabWidth", "4");
2382 checkConfig.addProperty("basicOffset", "4");
2383 checkConfig.addProperty("lineWrappingIndentation", "8");
2384 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2385 verifyWarns(checkConfig, getPath("InputIndentationLambda4.java"), expected);
2386 }
2387
2388 @Test
2389 public void testLambda5() throws Exception {
2390 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2391 checkConfig.addProperty("tabWidth", "3");
2392 checkConfig.addProperty("basicOffset", "3");
2393 checkConfig.addProperty("caseIndent", "0");
2394 checkConfig.addProperty("lineWrappingIndentation", "6");
2395 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2396 verifyWarns(checkConfig, getPath("InputIndentationLambda5.java"), expected);
2397 }
2398
2399 @Test
2400 public void testLambdaFalseForceStrictCondition() throws Exception {
2401 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2402 checkConfig.addProperty("tabWidth", "4");
2403 checkConfig.addProperty("basicOffset", "4");
2404 checkConfig.addProperty("braceAdjustment", "0");
2405 checkConfig.addProperty("forceStrictCondition", "false");
2406 checkConfig.addProperty("lineWrappingIndentation", "0");
2407 final String[] expected = {
2408 "34:5: " + getCheckMessage(MSG_ERROR, "(", 4, 8),
2409 "35:5: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 4, 12),
2410 "36:5: " + getCheckMessage(MSG_ERROR, "(", 4, 8),
2411 "47:5: " + getCheckMessage(MSG_ERROR_MULTI, "block rcurly", 4, "8, 16"),
2412 "73:5: " + getCheckMessage(MSG_ERROR, "->", 4, 8),
2413 };
2414
2415 verifyWarns(checkConfig, getPath("InputIndentationLambda6.java"), expected);
2416 }
2417
2418 @Test
2419 public void testLambdaTrueForceStrictCondition() throws Exception {
2420 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2421 checkConfig.addProperty("tabWidth", "4");
2422 checkConfig.addProperty("basicOffset", "4");
2423 checkConfig.addProperty("braceAdjustment", "0");
2424 checkConfig.addProperty("forceStrictCondition", "true");
2425 checkConfig.addProperty("lineWrappingIndentation", "4");
2426 final String[] expected = {
2427 "23:17: " + getCheckMessage(MSG_ERROR, "(", 16, 12),
2428 "24:17: " + getCheckMessage(MSG_ERROR, "->", 16, 12),
2429 "26:27: " + getCheckMessage(MSG_ERROR, "\"SECOND_ARG\"", 26, 12),
2430 "27:26: " + getCheckMessage(MSG_ERROR, "(", 25, 12),
2431 "30:17: " + getCheckMessage(MSG_ERROR, "(", 16, 12),
2432 "31:21: " + getCheckMessage(MSG_ERROR, "if", 20, 16),
2433 "32:25: " + getCheckMessage(MSG_CHILD_ERROR, "if", 24, 20),
2434 "33:21: " + getCheckMessage(MSG_ERROR, "if rcurly", 20, 16),
2435 "34:25: " + getCheckMessage(MSG_CHILD_ERROR, "else", 24, 20),
2436 "35:21: " + getCheckMessage(MSG_ERROR, "else rcurly", 20, 16),
2437 "36:17: " + getCheckMessage(MSG_ERROR, "block rcurly", 16, 12),
2438 "39:17: " + getCheckMessage(MSG_ERROR, "(", 16, 12),
2439 "40:17: " + getCheckMessage(MSG_ERROR, "->", 16, 12),
2440 "41:21: " + getCheckMessage(MSG_ERROR, "if", 20, 16),
2441 "44:1: " + getCheckMessage(MSG_ERROR, "block rcurly", 0, 12),
2442 };
2443
2444 verifyWarns(checkConfig, getPath("InputIndentationLambda7.java"), expected);
2445 }
2446
2447 @Test
2448 public void testLambdaOddConditions() throws Exception {
2449 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2450 checkConfig.addProperty("tabWidth", "4");
2451 checkConfig.addProperty("basicOffset", "3");
2452 checkConfig.addProperty("braceAdjustment", "0");
2453 checkConfig.addProperty("forceStrictCondition", "false");
2454 checkConfig.addProperty("lineWrappingIndentation", "7");
2455 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2456
2457 verifyWarns(checkConfig, getPath("InputIndentationLambda8.java"), expected);
2458 }
2459
2460 @Test
2461 public void testSeparatedStatements() throws Exception {
2462 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2463 checkConfig.addProperty("tabWidth", "4");
2464 final String fileName = getPath("InputIndentationSeparatedStatements.java");
2465 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2466 verifyWarns(checkConfig, fileName, expected);
2467 }
2468
2469 @Test
2470 public void testSeparatedLineWithJustSpaces() throws Exception {
2471 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2472 checkConfig.addProperty("tabWidth", "4");
2473 final String fileName = getPath("InputIndentationSeparatedStatementWithSpaces.java");
2474 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2475 verify(checkConfig, fileName, expected);
2476 }
2477
2478 @Test
2479 public void testTwoStatementsPerLine() throws Exception {
2480 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2481 checkConfig.addProperty("tabWidth", "4");
2482 checkConfig.addProperty("basicOffset", "4");
2483 final String fileName = getPath("InputIndentationTwoStatementsPerLine.java");
2484 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2485 verifyWarns(checkConfig, fileName, expected);
2486 }
2487
2488 @Test
2489 public void testMethodChaining() throws Exception {
2490 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2491 checkConfig.addProperty("tabWidth", "4");
2492 checkConfig.addProperty("basicOffset", "4");
2493 final String fileName = getPath("InputIndentationChainedMethods.java");
2494 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2495 verifyWarns(checkConfig, fileName, expected);
2496 }
2497
2498 @Test
2499 public void testMultipleAnnotationsWithWrappedLines() throws Exception {
2500 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2501 checkConfig.addProperty("tabWidth", "4");
2502 checkConfig.addProperty("basicOffset", "4");
2503 checkConfig.addProperty("forceStrictCondition", "true");
2504 final String fileName =
2505 getPath("InputIndentationCorrectMultipleAnnotationsWithWrappedLines.java");
2506 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2507 verifyWarns(checkConfig, fileName, expected);
2508 }
2509
2510 @Test
2511 public void testMethodPrecedeByAnnotationsWithParameterOnSeparateLine() throws Exception {
2512 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2513 checkConfig.addProperty("tabWidth", "4");
2514 checkConfig.addProperty("basicOffset", "2");
2515 checkConfig.addProperty("braceAdjustment", "0");
2516 checkConfig.addProperty("caseIndent", "2");
2517 checkConfig.addProperty("throwsIndent", "4");
2518 checkConfig.addProperty("lineWrappingIndentation", "4");
2519 checkConfig.addProperty("arrayInitIndent", "2");
2520 final String fileName =
2521 getPath("InputIndentationMethodPrecededByAnnotationWithParameterOnSeparateLine.java");
2522 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2523 verify(checkConfig, fileName, expected);
2524 }
2525
2526 @Test
2527 public void testAnnotationIncorrect() throws Exception {
2528 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2529 checkConfig.addProperty("tabWidth", "4");
2530 checkConfig.addProperty("basicOffset", "4");
2531 checkConfig.addProperty("braceAdjustment", "0");
2532 checkConfig.addProperty("lineWrappingIndentation", "4");
2533 final String fileName =
2534 getPath("InputIndentationAnnotationIncorrect.java");
2535 final String[] expected = {
2536 "11:5: " + getCheckMessage(MSG_ERROR, "(", 4, 8),
2537 "14:9: " + getCheckMessage(MSG_ERROR, "(", 8, 12),
2538 "19:5: " + getCheckMessage(MSG_ERROR, "(", 4, 8),
2539 };
2540 verify(checkConfig, fileName, expected);
2541 }
2542
2543 @Test
2544 public void testInputAnnotationScopeIndentationCheck() throws Exception {
2545 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2546 checkConfig.addProperty("tabWidth", "4");
2547 checkConfig.addProperty("basicOffset", "4");
2548 checkConfig.addProperty("forceStrictCondition", "true");
2549 final String fileName = getPath("InputIndentationAnnotationScopeIndentationCheck.java");
2550 final String[] expected = {
2551 "9:9: " + getCheckMessage(MSG_ERROR_MULTI,
2552 "annotation array initialization rcurly", 8, "0, 4"),
2553 };
2554 verifyWarns(checkConfig, fileName, expected);
2555 }
2556
2557 @Test
2558 public void testInputAnnotationDefIndentationCheck() throws Exception {
2559 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2560 checkConfig.addProperty("tabWidth", "4");
2561 checkConfig.addProperty("arrayInitIndent", "4");
2562 checkConfig.addProperty("basicOffset", "4");
2563 checkConfig.addProperty("braceAdjustment", "0");
2564 checkConfig.addProperty("lineWrappingIndentation", "4");
2565 checkConfig.addProperty("forceStrictCondition", "true");
2566 final String fileName = getPath("InputIndentationCustomAnnotation.java");
2567 final String[] expected = {
2568 "14:6: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 5, 0),
2569 "15:6: " + getCheckMessage(MSG_ERROR, "annotation def rcurly", 5, 0),
2570 "16:6: " + getCheckMessage(MSG_ERROR, "@", 5, 0),
2571 "17:1: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 0, 4),
2572 "18:6: " + getCheckMessage(MSG_ERROR, "annotation def rcurly", 5, 0),
2573 "20:4: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 3, 0),
2574 "22:1: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 0, 4),
2575 "23:6: " + getCheckMessage(MSG_ERROR, "annotation def rcurly", 5, 0),
2576 "25:6: " + getCheckMessage(MSG_ERROR, "@", 5, 0),
2577 "26:6: " + getCheckMessage(MSG_ERROR, "AnnotationWithLineWrap", 5, 0),
2578 "30:6: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 5, 0),
2579 "31:4: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 3, 0),
2580 "34:6: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 5, 4),
2581 "35:4: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 3, 4),
2582 "36:1: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 0, 4),
2583 "37:1: " + getCheckMessage(MSG_ERROR, "@", 0, 4),
2584 "38:9: " + getCheckMessage(MSG_ERROR, "AnnotationInnerLineWrap", 8, 4),
2585 "41:8: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 7, 8),
2586 "58:5: " + getCheckMessage(MSG_ERROR, "AnnotationInnerLineWrap2", 4, 0),
2587 "59:4: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 3, 4),
2588 "60:8: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 7, 4),
2589 "61:5: " + getCheckMessage(MSG_ERROR, "annotation def rcurly", 4, 0),
2590 "72:4: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 3, 4),
2591 "87:29: " + getCheckMessage(MSG_ERROR_MULTI, "new", 28, "20, 24"),
2592 "117:6: " + getCheckMessage(MSG_ERROR, "annotation def modifier", 5, 4),
2593 "128:2: " + getCheckMessage(MSG_ERROR, "interface", 1, 0),
2594 "134:12: " + getCheckMessage(MSG_ERROR, "@", 11, 0),
2595 "137:17: " + getCheckMessage(MSG_ERROR, "@", 16, 0),
2596 "144:13: " + getCheckMessage(MSG_ERROR, "@", 12, 4),
2597 "148:23: " + getCheckMessage(MSG_ERROR, "class def ident", 16, 0),
2598 };
2599 verifyWarns(checkConfig, fileName, expected);
2600 }
2601
2602 @Test
2603 public void testTryResourcesStrict() throws Exception {
2604 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2605 checkConfig.addProperty("tabWidth", "4");
2606 checkConfig.addProperty("forceStrictCondition", "true");
2607 checkConfig.addProperty("braceAdjustment", "0");
2608 checkConfig.addProperty("lineWrappingIndentation", "4");
2609 final String fileName = getPath("InputIndentationTryWithResourcesStrict.java");
2610 final String[] expected = {
2611 "26:1: " + getCheckMessage(MSG_ERROR, "try resource", 0, 12),
2612 "28:14: " + getCheckMessage(MSG_ERROR_MULTI, "try rparen", 13, "8, 12"),
2613 "33:1: " + getCheckMessage(MSG_ERROR, "newBufferedWriter", 0, 16),
2614 "39:1: " + getCheckMessage(MSG_ERROR, "newBufferedWriter", 0, 12),
2615 "59:21: " + getCheckMessage(MSG_ERROR, "try resource", 20, 16),
2616 "84:20: " + getCheckMessage(MSG_ERROR, "writ", 19, 12),
2617 "91:20: " + getCheckMessage(MSG_ERROR, "writ", 19, 16),
2618 "98:22: " + getCheckMessage(MSG_ERROR, "writ", 21, 16),
2619 "113:18: " + getCheckMessage(MSG_ERROR, "zipFileName", 17, 16),
2620 "120:16: " + getCheckMessage(MSG_ERROR, "zipFileName", 15, 16),
2621 "130:8: " + getCheckMessage(MSG_ERROR, "try", 7, 8),
2622 "135:16: " + getCheckMessage(MSG_CHILD_ERROR, "try", 15, 12),
2623 "141:12: " + getCheckMessage(MSG_ERROR, "try resource", 11, 12),
2624 "142:10: " + getCheckMessage(MSG_CHILD_ERROR, "try", 9, 12),
2625 "146:12: " + getCheckMessage(MSG_ERROR, "try resource", 11, 12),
2626 "147:12: " + getCheckMessage(MSG_ERROR, "newBufferedWriter", 11, 16),
2627 "148:14: " + getCheckMessage(MSG_CHILD_ERROR, "try", 13, 12),
2628 "150:8: " + getCheckMessage(MSG_ERROR, "try", 7, 8),
2629 "151:8: " + getCheckMessage(MSG_ERROR_MULTI, "try rparen", 7, "8, 12"),
2630 "155:10: " + getCheckMessage(MSG_ERROR, "try", 9, 8),
2631 "161:14: " + getCheckMessage(MSG_ERROR, ".", 13, 12),
2632 "167:12: " + getCheckMessage(MSG_ERROR, ".", 11, 12),
2633 };
2634 verifyWarns(checkConfig, fileName, expected);
2635 }
2636
2637 @Test
2638 public void testTryResourcesNotStrict() throws Exception {
2639 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2640 checkConfig.addProperty("tabWidth", "4");
2641 checkConfig.addProperty("braceAdjustment", "0");
2642 checkConfig.addProperty("lineWrappingIndentation", "4");
2643 final String fileName = getPath("InputIndentationTryResourcesNotStrict.java");
2644 final String[] expected = {
2645 "27:1: " + getCheckMessage(MSG_ERROR, "try resource", 0, 12),
2646 "33:1: " + getCheckMessage(MSG_ERROR, "newBufferedWriter", 0, 16),
2647 "39:1: " + getCheckMessage(MSG_ERROR, "newBufferedWriter", 0, 12),
2648 "120:16: " + getCheckMessage(MSG_ERROR, "zipFileName", 15, 16),
2649 "130:8: " + getCheckMessage(MSG_ERROR, "try", 7, 8),
2650 "135:16: " + getCheckMessage(MSG_CHILD_ERROR, "try", 15, 12),
2651 "141:12: " + getCheckMessage(MSG_ERROR, "try resource", 11, 12),
2652 "142:10: " + getCheckMessage(MSG_CHILD_ERROR, "try", 9, 12),
2653 "146:12: " + getCheckMessage(MSG_ERROR, "try resource", 11, 12),
2654 "147:12: " + getCheckMessage(MSG_ERROR, "newBufferedWriter", 11, 16),
2655 "148:14: " + getCheckMessage(MSG_CHILD_ERROR, "try", 13, 12),
2656 "150:8: " + getCheckMessage(MSG_ERROR, "try", 7, 8),
2657 "151:8: " + getCheckMessage(MSG_ERROR_MULTI, "try rparen", 7, "8, 12"),
2658 "164:9: " + getCheckMessage(MSG_ERROR, ".", 8, 12),
2659 "172:12: " + getCheckMessage(MSG_ERROR, "new", 11, 12),
2660 };
2661 verifyWarns(checkConfig, fileName, expected);
2662 }
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676 @Test
2677 public void testArgumentOrderOfErrorMessages() {
2678 final Object[] arguments = {"##0##", "##1##", "##2##"};
2679 final String[] messages = {
2680 getCheckMessage(MSG_ERROR, arguments),
2681 getCheckMessage(MSG_CHILD_ERROR, arguments),
2682 getCheckMessage(MSG_ERROR_MULTI, arguments),
2683 getCheckMessage(MSG_CHILD_ERROR_MULTI, arguments),
2684 };
2685 final boolean isInOrder = Arrays.stream(messages).allMatch(msg -> {
2686 final int indexOfArgumentZero = msg.indexOf((String) arguments[0]);
2687 return Arrays.stream(arguments)
2688 .map(String.class::cast)
2689 .mapToInt(msg::indexOf)
2690 .allMatch(index -> index >= indexOfArgumentZero);
2691 });
2692 assertWithMessage(
2693 "the argument 0 of error messages (indentation.error, indentation.child.error,"
2694 + " indentation.error.multi, indentation.child.error.multi)"
2695 + " is required to be the first argument of them")
2696 .that(isInOrder)
2697 .isTrue();
2698 }
2699
2700 @Test
2701 public void testEmptyArray() throws Exception {
2702 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2703 checkConfig.addProperty("tabWidth", "4");
2704 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2705 verifyWarns(checkConfig, getPath("InputIndentationEmptyArray.java"), expected);
2706 }
2707
2708 @Test
2709 public void testNewHandler() throws Exception {
2710 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2711 checkConfig.addProperty("tabWidth", "4");
2712 final String[] expected = {
2713 "10:1: " + getCheckMessage(MSG_ERROR, "Object", 0, 12),
2714 "12:1: " + getCheckMessage(MSG_ERROR, "(", 0, 12),
2715 "15:1: " + getCheckMessage(MSG_CHILD_ERROR, "new", 0, 8),
2716 "17:1: " + getCheckMessage(MSG_ERROR, "new lparen", 0, 8),
2717 "25:1: " + getCheckMessage(MSG_ERROR, "=", 0, 8),
2718 };
2719 verifyWarns(checkConfig, getPath("InputIndentationNewHandler.java"), expected);
2720 }
2721
2722 @Test
2723 public void testTryHandler() throws Exception {
2724 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2725 checkConfig.addProperty("tabWidth", "4");
2726 checkConfig.addProperty("braceAdjustment", "0");
2727 checkConfig.addProperty("lineWrappingIndentation", "8");
2728 checkConfig.addProperty("forceStrictCondition", "true");
2729 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2730 verifyWarns(checkConfig, getPath("InputIndentationTryBlockWithResources.java"), expected);
2731 }
2732
2733 @Test
2734 public void testTryHandler2() throws Exception {
2735 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2736 checkConfig.addProperty("tabWidth", "4");
2737 checkConfig.addProperty("braceAdjustment", "0");
2738 checkConfig.addProperty("lineWrappingIndentation", "8");
2739 checkConfig.addProperty("forceStrictCondition", "true");
2740 final String[] expected = {
2741 "25:17: " + getCheckMessage(MSG_ERROR, "new", 16, 20),
2742 "27:13: " + getCheckMessage(MSG_ERROR, "new", 12, 20),
2743 };
2744 verifyWarns(checkConfig, getPath("InputIndentationTryBlock.java"), expected);
2745 }
2746
2747 @Test
2748 public void testChainedMethodWithBracketOnNewLine() throws Exception {
2749 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2750
2751 checkConfig.addProperty("arrayInitIndent", "2");
2752 checkConfig.addProperty("basicOffset", "2");
2753 checkConfig.addProperty("braceAdjustment", "0");
2754 checkConfig.addProperty("caseIndent", "2");
2755 checkConfig.addProperty("forceStrictCondition", "false");
2756 checkConfig.addProperty("lineWrappingIndentation", "4");
2757 checkConfig.addProperty("tabWidth", "2");
2758 checkConfig.addProperty("throwsIndent", "2");
2759 final String[] expected = {
2760 "44:7: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 6, 8),
2761 "45:9: " + getCheckMessage(MSG_CHILD_ERROR, "method call", 8, 10),
2762 "47:7: " + getCheckMessage(MSG_ERROR, "method call rparen", 6, 8),
2763 "61:6: " + getCheckMessage(MSG_ERROR, "foo", 5, 8),
2764 "82:5: " + getCheckMessage(MSG_ERROR, "if rcurly", 4, 6),
2765 "84:3: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 2, 4),
2766 };
2767 final String fileName = "InputIndentationChainedMethodWithBracketOnNewLine.java";
2768 verifyWarns(checkConfig, getPath(fileName), expected);
2769 }
2770
2771 @Test
2772 public void testIndentationSwitchExpression() throws Exception {
2773 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2774 checkConfig.addProperty("tabWidth", "4");
2775 final String[] expected = {
2776 "17:1: " + getCheckMessage(MSG_CHILD_ERROR, "case", 0, 12),
2777 "18:9: " + getCheckMessage(MSG_CHILD_ERROR, "block", 8, 16),
2778 "21:25: " + getCheckMessage(MSG_CHILD_ERROR, "case", 24, 12),
2779 "22:9: " + getCheckMessage(MSG_CHILD_ERROR, "block", 8, 16),
2780 "27:9: " + getCheckMessage(MSG_CHILD_ERROR, "block", 8, 20),
2781 "29:1: " + getCheckMessage(MSG_CHILD_ERROR, "block", 0, 16),
2782 "30:1: " + getCheckMessage(MSG_ERROR, "yield", 0, 16),
2783 "34:5: " + getCheckMessage(MSG_CHILD_ERROR, "block", 4, 20),
2784 "44:1: " + getCheckMessage(MSG_CHILD_ERROR, "block", 0, 16),
2785 "46:21: " + getCheckMessage(MSG_CHILD_ERROR, "case", 20, 12),
2786 "47:1: " + getCheckMessage(MSG_CHILD_ERROR, "block", 0, 16),
2787 "51:9: " + getCheckMessage(MSG_CHILD_ERROR, "block", 8, 20),
2788 "56:33: " + getCheckMessage(MSG_CHILD_ERROR, "block", 32, 20),
2789 };
2790
2791 verifyWarns(checkConfig,
2792 getNonCompilablePath("InputIndentationCheckSwitchExpression.java"),
2793 expected);
2794 }
2795
2796 @Test
2797 public void testIndentationYieldStatement() throws Exception {
2798 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2799 checkConfig.addProperty("tabWidth", "4");
2800 final String[] expected = {
2801 "23:13: " + getCheckMessage(MSG_ERROR, "yield", 12, 16),
2802 "28:9: " + getCheckMessage(MSG_CHILD_ERROR, "yield", 8, 16),
2803 "40:5: " + getCheckMessage(MSG_ERROR, "yield", 4, 16),
2804 "41:9: " + getCheckMessage(MSG_CHILD_ERROR, "yield", 8, 16),
2805 "71:1: " + getCheckMessage(MSG_ERROR, "yield", 0, 16),
2806 "74:37: " + getCheckMessage(MSG_ERROR, "yield", 36, 16),
2807 };
2808
2809 verifyWarns(checkConfig,
2810 getNonCompilablePath("InputIndentationYieldStatement.java"),
2811 expected);
2812 }
2813
2814 @Test
2815 public void testIndentationSwitchExpressionCorrect() throws Exception {
2816 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2817 checkConfig.addProperty("tabWidth", "4");
2818 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2819 verifyWarns(checkConfig,
2820 getNonCompilablePath("InputIndentationCheckSwitchExpressionCorrect.java"),
2821 expected);
2822 }
2823
2824 @Test
2825 public void testIndentationSwitchExpressionDeclaration() throws Exception {
2826 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2827 checkConfig.addProperty("tabWidth", "4");
2828 checkConfig.addProperty("caseIndent", "4");
2829 checkConfig.addProperty("lineWrappingIndentation", "8");
2830 final String[] expected = {
2831 "33:17: " + getCheckMessage(MSG_CHILD_ERROR, "case", 16, 12),
2832 "34:17: " + getCheckMessage(MSG_CHILD_ERROR, "case", 16, 12),
2833 "41:17: " + getCheckMessage(MSG_CHILD_ERROR, "case", 16, 12),
2834 "42:17: " + getCheckMessage(MSG_CHILD_ERROR, "case", 16, 12),
2835 "49:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 12),
2836 "50:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 12),
2837 "57:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 12),
2838 "58:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 12),
2839 };
2840 verifyWarns(checkConfig,
2841 getNonCompilablePath("InputIndentationCheckSwitchExpressionDeclaration.java"),
2842 expected);
2843 }
2844
2845 @Test
2846 public void testIndentationSwitchExpressionDeclarationLeftCurlyNewLine() throws Exception {
2847 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2848 checkConfig.addProperty("tabWidth", "4");
2849 final String[] expected = {
2850 "34:5: " + getCheckMessage(MSG_ERROR, "switch lcurly", 4, 8),
2851 "42:5: " + getCheckMessage(MSG_ERROR, "switch lcurly", 4, 8),
2852 "50:13: " + getCheckMessage(MSG_ERROR, "switch lcurly", 12, 8),
2853 "58:13: " + getCheckMessage(MSG_ERROR, "switch lcurly", 12, 8),
2854 };
2855 verifyWarns(checkConfig,
2856 getNonCompilablePath(
2857 "InputIndentationCheckSwitchExpressionDeclarationLCurlyNewLine.java"),
2858 expected);
2859 }
2860
2861 @Test
2862 public void testIndentationRecords() throws Exception {
2863 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2864 checkConfig.addProperty("tabWidth", "4");
2865 checkConfig.addProperty("basicOffset", "4");
2866 checkConfig.addProperty("braceAdjustment", "0");
2867 checkConfig.addProperty("caseIndent", "4");
2868 checkConfig.addProperty("throwsIndent", "4");
2869 checkConfig.addProperty("arrayInitIndent", "4");
2870 checkConfig.addProperty("lineWrappingIndentation", "4");
2871 checkConfig.addProperty("forceStrictCondition", "false");
2872
2873 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
2874
2875 verifyWarns(checkConfig,
2876 getNonCompilablePath("InputIndentationRecords.java"),
2877 expected);
2878 }
2879
2880 @Test
2881 public void testIndentationRecordsAndCompactCtors() throws Exception {
2882 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2883 checkConfig.addProperty("tabWidth", "4");
2884 final String[] expected = {
2885 "13:1: " + getCheckMessage(MSG_ERROR, "(", 0, 8),
2886 "25:1: " + getCheckMessage(MSG_ERROR, "String", 0, 12),
2887 "38:1: " + getCheckMessage(MSG_CHILD_ERROR, "compact ctor def", 0, 12),
2888 "48:8: " + getCheckMessage(MSG_ERROR, "record def ident", 0, 4),
2889 "53:1: " + getCheckMessage(MSG_ERROR, "compact ctor def rcurly", 0, 8),
2890 "61:1: " + getCheckMessage(MSG_ERROR, "ctor def rcurly", 0, 8),
2891 };
2892
2893 verifyWarns(checkConfig,
2894 getNonCompilablePath("InputIndentationRecordsAndCompactCtors.java"),
2895 expected);
2896 }
2897
2898 @Test
2899 public void testIndentationSwitchExpressionNewLine() throws Exception {
2900 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2901 checkConfig.addProperty("tabWidth", "4");
2902 final String[] expected = {
2903 "30:13: " + getCheckMessage(MSG_ERROR, "lambda", 12, 16),
2904 "32:13: " + getCheckMessage(MSG_ERROR, "lambda", 12, 16),
2905 };
2906
2907 verifyWarns(checkConfig,
2908 getNonCompilablePath("InputIndentationCheckSwitchExpressionNewLine.java"),
2909 expected);
2910 }
2911
2912 @Test
2913 public void testIndentationMethodParenthesisOnNewLine() throws Exception {
2914 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2915 checkConfig.addProperty("tabWidth", "4");
2916 final String[] expected = {
2917 "13:9: " + getCheckMessage(MSG_ERROR, "method def rparen", 8, 4),
2918 "18:9: " + getCheckMessage(MSG_ERROR, "method def rparen", 8, 4),
2919 };
2920
2921 verifyWarns(checkConfig,
2922 getPath("InputIndentationCheckMethodParenOnNewLine.java"),
2923 expected);
2924 }
2925
2926 @Test
2927 public void testIndentationMethodParenthesisOnNewLine1() throws Exception {
2928 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2929 checkConfig.addProperty("tabWidth", "4");
2930 final String[] expected = {
2931 "11:10: " + getCheckMessage(MSG_ERROR, "2", 9, 12),
2932 "17:8: " + getCheckMessage(MSG_ERROR, "int", 7, 8),
2933 "18:9: " + getCheckMessage(MSG_ERROR, "method def rparen", 8, 4),
2934 };
2935
2936 verifyWarns(checkConfig,
2937 getPath("InputIndentationCheckMethodParenOnNewLine1.java"),
2938 expected);
2939 }
2940
2941 @Test
2942 public void testIndentationLineWrappedRecordDeclaration() throws Exception {
2943 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2944 checkConfig.addProperty("tabWidth", "4");
2945 checkConfig.addProperty("basicOffset", "4");
2946 checkConfig.addProperty("braceAdjustment", "0");
2947 checkConfig.addProperty("caseIndent", "4");
2948 checkConfig.addProperty("throwsIndent", "4");
2949 checkConfig.addProperty("arrayInitIndent", "4");
2950 checkConfig.addProperty("lineWrappingIndentation", "4");
2951
2952 final String[] expected = {
2953 "33:1: " + getCheckMessage(MSG_ERROR, ")", 0, 4),
2954 "55:11: " + getCheckMessage(MSG_ERROR, "interface def ident", 0, 4),
2955 "56:1: " + getCheckMessage(MSG_ERROR, "method def modifier", 0, 8),
2956 "57:1: " + getCheckMessage(MSG_ERROR, "void", 0, 4),
2957 "58:1: " + getCheckMessage(MSG_ERROR, "method", 0, 4),
2958 "59:1: " + getCheckMessage(MSG_ERROR, "throws", 0, 4),
2959 "60:1: " + getCheckMessage(MSG_ERROR, "IOException", 0, 4),
2960 "61:1: " + getCheckMessage(MSG_ERROR, "method def rcurly", 0, 8),
2961 "62:1: " + getCheckMessage(MSG_ERROR, "interface def rcurly", 0, 4),
2962 "75:8: " + getCheckMessage(MSG_ERROR, "record def ident", 0, 4),
2963 "76:1: " + getCheckMessage(MSG_ERROR, "record def rparen", 0, 4),
2964 "77:1: " + getCheckMessage(MSG_ERROR, "implements", 0, 4),
2965 "78:1: " + getCheckMessage(MSG_ERROR, "SimpleInterface2", 0, 4),
2966 "79:8: " + getCheckMessage(MSG_ERROR, "record def ident", 0, 8),
2967 "80:1: " + getCheckMessage(MSG_ERROR, "(", 0, 4),
2968 "81:1: " + getCheckMessage(MSG_ERROR, "record def rparen", 0, 8),
2969 "82:1: " + getCheckMessage(MSG_ERROR, "record def lcurly", 0, 8),
2970 "83:1: " + getCheckMessage(MSG_ERROR, "record def rcurly", 0, 8),
2971 "84:1: " + getCheckMessage(MSG_ERROR, "record def rcurly", 0, 4),
2972 };
2973
2974 verifyWarns(checkConfig,
2975 getNonCompilablePath("InputIndentationLineWrappedRecordDeclaration.java"),
2976 expected);
2977 }
2978
2979 @Test
2980 public void testIndentationAnnotationFieldDefinition() throws Exception {
2981 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
2982 checkConfig.addProperty("tabWidth", "4");
2983 checkConfig.addProperty("basicOffset", "4");
2984 checkConfig.addProperty("braceAdjustment", "0");
2985 checkConfig.addProperty("caseIndent", "4");
2986 checkConfig.addProperty("throwsIndent", "8");
2987 checkConfig.addProperty("forceStrictCondition", "true");
2988
2989 final String[] expected = {
2990 "17:5: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 4, 8),
2991 "18:13: " + getCheckMessage(MSG_ERROR, "annotation field def modifier", 12, 8),
2992 "25:5: " + getCheckMessage(MSG_ERROR, "member def type", 4, 8),
2993 "26:5: " + getCheckMessage(MSG_ERROR, "member def type", 4, 8),
2994 };
2995
2996 verifyWarns(checkConfig, getPath("InputIndentationAnnotationFieldDefinition.java"),
2997 expected);
2998 }
2999
3000 @Test
3001 public void testIndentationLongConcatenatedString() throws Exception {
3002 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3003 checkConfig.addProperty("tabWidth", "4");
3004
3005 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3006
3007 verifyWarns(checkConfig, getPath("InputIndentationLongConcatenatedString.java"),
3008 expected);
3009 }
3010
3011 @Test
3012 public void testIndentationLineBreakVariableDeclaration()
3013 throws Exception {
3014 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3015 checkConfig.addProperty("tabWidth", "4");
3016
3017 final String fileName = getPath("InputIndentationLineBreakVariableDeclaration.java");
3018 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
3019 verifyWarns(checkConfig, fileName, expected);
3020 }
3021
3022 @Test
3023 public void testIndentationPatternMatchingForSwitch()
3024 throws Exception {
3025 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3026 checkConfig.addProperty("forceStrictCondition", "true");
3027 checkConfig.addProperty("tabWidth", "4");
3028 checkConfig.addProperty("basicOffset", "4");
3029 checkConfig.addProperty("braceAdjustment", "0");
3030 checkConfig.addProperty("caseIndent", "4");
3031 checkConfig.addProperty("throwsIndent", "8");
3032
3033 final String fileName = getNonCompilablePath(
3034 "InputIndentationPatternMatchingForSwitch.java");
3035 final String[] expected = {
3036 "21:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 16),
3037 "54:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 16),
3038 "69:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 16),
3039 "70:13: " + getCheckMessage(MSG_CHILD_ERROR, "case", 12, 16),
3040 "75:5: " + getCheckMessage(MSG_CHILD_ERROR, "case", 4, 16),
3041 "76:5: " + getCheckMessage(MSG_CHILD_ERROR, "case", 4, 16),
3042 "87:1: " + getCheckMessage(MSG_CHILD_ERROR, "case", 0, 16),
3043 "88:1: " + getCheckMessage(MSG_CHILD_ERROR, "case", 0, 16),
3044 "89:1: " + getCheckMessage(MSG_CHILD_ERROR, "case", 0, 16),
3045 "90:1: " + getCheckMessage(MSG_ERROR, "lambda", 0, 16),
3046 };
3047 verifyWarns(checkConfig, fileName, expected);
3048 }
3049
3050 @Test
3051 public void testIndentationRecordPattern()
3052 throws Exception {
3053 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3054 checkConfig.addProperty("forceStrictCondition", "true");
3055 checkConfig.addProperty("tabWidth", "4");
3056 checkConfig.addProperty("basicOffset", "4");
3057 checkConfig.addProperty("braceAdjustment", "0");
3058 checkConfig.addProperty("caseIndent", "4");
3059 checkConfig.addProperty("throwsIndent", "8");
3060
3061 final String fileName = getNonCompilablePath(
3062 "InputIndentationRecordPattern.java");
3063 final String[] expected = {
3064 "19:17: " + getCheckMessage(MSG_ERROR, "ColoredPoint", 16, 12),
3065 "24:9: " + getCheckMessage(MSG_ERROR, "ColoredPoint", 8, 12),
3066 "29:17: " + getCheckMessage(MSG_ERROR, "ColoredPoint", 16, 12),
3067 "34:9: " + getCheckMessage(MSG_CHILD_ERROR, "if", 8, 12),
3068 "37:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3069 "39:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3070 "40:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3071 "41:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3072 "42:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3073 "56:17: " + getCheckMessage(MSG_ERROR, "Rectangle", 16, 12),
3074 "57:17: " + getCheckMessage(MSG_ERROR, "ColoredPoint", 16, 12),
3075 "58:25: " + getCheckMessage(MSG_ERROR, "boolean", 24, 12),
3076 "59:17: " + getCheckMessage(MSG_ERROR, "int", 16, 12),
3077 "60:25: " + getCheckMessage(MSG_ERROR, "_", 24, 12),
3078 "61:17: " + getCheckMessage(MSG_ERROR, "ColoredPoint", 16, 12),
3079 "62:17: " + getCheckMessage(MSG_ERROR, ")", 16, 8),
3080 "67:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3081 "66:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3082 "68:9: " + getCheckMessage(MSG_CHILD_ERROR, "if", 8, 12),
3083 "69:9: " + getCheckMessage(MSG_CHILD_ERROR, "if", 8, 12),
3084 "70:9: " + getCheckMessage(MSG_CHILD_ERROR, "if", 8, 12),
3085 "71:1: " + getCheckMessage(MSG_CHILD_ERROR, "if", 0, 12),
3086 "72:9: " + getCheckMessage(MSG_CHILD_ERROR, "if", 8, 12),
3087 "81:13: " + getCheckMessage(MSG_ERROR, ")", 12, 8),
3088 "89:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 16),
3089 "90:9: " + getCheckMessage(MSG_CHILD_ERROR, "case", 8, 16),
3090 };
3091 verifyWarns(checkConfig, fileName, expected);
3092 }
3093
3094 @Test
3095 public void testIndentationSealedClasses()
3096 throws Exception {
3097 final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class);
3098 checkConfig.addProperty("forceStrictCondition", "true");
3099 checkConfig.addProperty("tabWidth", "4");
3100 checkConfig.addProperty("basicOffset", "4");
3101 checkConfig.addProperty("braceAdjustment", "0");
3102 checkConfig.addProperty("caseIndent", "4");
3103 checkConfig.addProperty("throwsIndent", "8");
3104
3105 final String fileName = getNonCompilablePath(
3106 "InputIndentationSealedClasses.java");
3107 final String[] expected = {
3108 "14:1: " + getCheckMessage(MSG_ERROR, "class def modifier", 0, 4),
3109 "15:2: " + getCheckMessage(MSG_ERROR, "class", 1, 4),
3110 "16:6: " + getCheckMessage(MSG_ERROR, "permits", 5, 4),
3111 "19:5: " + getCheckMessage(MSG_ERROR, "class", 4, 8),
3112 "20:5: " + getCheckMessage(MSG_ERROR, "permits", 4, 8),
3113 "28:1: " + getCheckMessage(MSG_ERROR, "class def modifier", 0, 4),
3114 "29:9: " + getCheckMessage(MSG_ERROR, "extends", 8, 4),
3115 "32:5: " + getCheckMessage(MSG_ERROR, "extends", 4, 8),
3116 "38:5: " + getCheckMessage(MSG_ERROR, "class", 4, 8),
3117 "39:1: " + getCheckMessage(MSG_ERROR, "permits", 0, 8),
3118 "40:13: " + getCheckMessage(MSG_ERROR, "C", 12, 8),
3119 "48:5: " + getCheckMessage(MSG_ERROR, "class", 4, 8),
3120 "49:5: " + getCheckMessage(MSG_ERROR, "C", 4, 8),
3121 "55:1: " + getCheckMessage(MSG_ERROR, "class def modifier", 0, 4),
3122 "56:9: " + getCheckMessage(MSG_ERROR, "class", 8, 4),
3123 };
3124 verifyWarns(checkConfig, fileName, expected);
3125 }
3126
3127 private static final class IndentAudit implements AuditListener {
3128
3129 private final IndentComment[] comments;
3130 private int position;
3131
3132 private IndentAudit(IndentComment... comments) {
3133 this.comments = Arrays.copyOf(comments, comments.length);
3134 }
3135
3136 @Override
3137 public void auditStarted(AuditEvent event) {
3138
3139 }
3140
3141 @Override
3142 public void auditFinished(AuditEvent event) {
3143
3144 }
3145
3146 @Override
3147 public void fileStarted(AuditEvent event) {
3148
3149 }
3150
3151 @Override
3152 public void fileFinished(AuditEvent event) {
3153
3154 }
3155
3156 @Override
3157 public void addError(AuditEvent event) {
3158 final int line = event.getLine();
3159 final String message = event.getMessage();
3160
3161 assertWithMessage(
3162 "found a warning when none was expected for #%s at line %s with message %s",
3163 position, line, message)
3164 .that(position)
3165 .isLessThan(comments.length);
3166
3167 final IndentComment comment = comments[position];
3168 position++;
3169
3170 final String possibleExceptedMessages = Arrays.stream(comment.getExpectedMessages())
3171 .reduce("", (cur, next) -> cur + "\"" + next + "\", ");
3172 final String assertMessage = String.format(
3173 Locale.ROOT,
3174 "input expected warning #%d at line %d to report one of the following: %s"
3175 + "but got instead: %d: %s",
3176 position, comment.getLineNumber(), possibleExceptedMessages, line, message);
3177 assertWithMessage(assertMessage)
3178 .that(line == comment.getLineNumber() && Arrays
3179 .stream(comment.getExpectedMessages()).anyMatch(message::endsWith))
3180 .isTrue();
3181 }
3182
3183 @Override
3184 public void addException(AuditEvent event, Throwable throwable) {
3185
3186 }
3187
3188 }
3189
3190 private static final class IndentComment {
3191
3192
3193 private static final String FAKE_ARGUMENT_ZERO = "##0##";
3194 private final int lineNumber;
3195 private final int indent;
3196
3197 private final int indentOffset;
3198 private final boolean expectedNonStrict;
3199 private final String expectedWarning;
3200 private final boolean warning;
3201
3202 private IndentComment(Matcher match, int lineNumber) {
3203 this.lineNumber = lineNumber;
3204 indent = Integer.parseInt(match.group(1));
3205 if (match.group(2) == null) {
3206 indentOffset = 0;
3207 }
3208 else {
3209 indentOffset = Integer.parseInt(match.group(2));
3210 }
3211 expectedNonStrict = match.group(3) != null;
3212 expectedWarning = match.group(4).replace(",", ", ");
3213 warning = match.group(5) != null;
3214 }
3215
3216 public String[] getExpectedMessages() {
3217 final String[] expectedMessages;
3218 if (expectedWarning.contains(",")) {
3219 expectedMessages = new String[] {
3220 getExpectedMessagePostfix(MSG_ERROR_MULTI),
3221 getExpectedMessagePostfix(MSG_CHILD_ERROR_MULTI),
3222 };
3223 }
3224 else {
3225 expectedMessages = new String[] {
3226 getExpectedMessagePostfix(MSG_ERROR),
3227 getExpectedMessagePostfix(MSG_CHILD_ERROR),
3228 };
3229 }
3230 return expectedMessages;
3231 }
3232
3233 private String getExpectedMessagePostfix(final String messageKey) {
3234 final String msg = getCheckMessage(IndentationCheck.class, messageKey,
3235 FAKE_ARGUMENT_ZERO, indent + indentOffset, expectedWarning);
3236 final int indexOfMsgPostfix = msg.indexOf(FAKE_ARGUMENT_ZERO)
3237 + FAKE_ARGUMENT_ZERO.length();
3238 return msg.substring(indexOfMsgPostfix);
3239 }
3240
3241 public int getLineNumber() {
3242 return lineNumber;
3243 }
3244
3245 public int getIndent() {
3246 return indent;
3247 }
3248
3249 public int getIndentOffset() {
3250 return indentOffset;
3251 }
3252
3253 public boolean isExpectedNonStrict() {
3254 return expectedNonStrict;
3255 }
3256
3257 public String getExpectedWarning() {
3258 return expectedWarning;
3259 }
3260
3261 public boolean isWarning() {
3262 return warning;
3263 }
3264
3265 }
3266
3267 }