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.imports;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_LEX;
24 import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_LINE_SEPARATOR;
25 import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_NONGROUP_EXPECTED;
26 import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_NONGROUP_IMPORT;
27 import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_ORDER;
28 import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_SEPARATED_IN_GROUP;
29
30 import java.io.File;
31
32 import org.junit.jupiter.api.Test;
33
34 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35 import com.puppycrawl.tools.checkstyle.Checker;
36 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
37 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
38 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
39 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
40 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
41
42 public class CustomImportOrderCheckTest extends AbstractModuleTestSupport {
43
44
45 private static final String STATIC = CustomImportOrderCheck.STATIC_RULE_GROUP;
46 private static final String SAME = CustomImportOrderCheck.SAME_PACKAGE_RULE_GROUP;
47 private static final String THIRD = CustomImportOrderCheck.THIRD_PARTY_PACKAGE_RULE_GROUP;
48 private static final String STD = CustomImportOrderCheck.STANDARD_JAVA_PACKAGE_RULE_GROUP;
49 private static final String SPECIAL = CustomImportOrderCheck.SPECIAL_IMPORTS_RULE_GROUP;
50
51 @Override
52 public String getPackageLocation() {
53 return "com/puppycrawl/tools/checkstyle/checks/imports/customimportorder";
54 }
55
56 @Test
57 public void testGetRequiredTokens() {
58 final CustomImportOrderCheck checkObj = new CustomImportOrderCheck();
59 final int[] expected = {
60 TokenTypes.IMPORT,
61 TokenTypes.STATIC_IMPORT,
62 TokenTypes.PACKAGE_DEF,
63 };
64 assertWithMessage("Default required tokens are invalid")
65 .that(checkObj.getRequiredTokens())
66 .isEqualTo(expected);
67 }
68
69 @Test
70 public void testCustom() throws Exception {
71 final String[] expected = {
72 "16:1: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT",
73 "java.io.File.createTempFile"),
74 "17:1: " + getCheckMessage(MSG_LEX, "java.awt.print.Paper.*",
75 "java.io.File.createTempFile"),
76 "20:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.Button"),
77 "21:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.Frame"),
78 "22:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.Dialog"),
79 "23:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.color.ColorSpace"),
80 "24:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.event.ActionEvent"),
81 "25:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "javax.swing.JComponent"),
82 "26:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "javax.swing.JTable"),
83 "27:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.File"),
84 "28:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.IOException"),
85 "29:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.InputStream"),
86 "30:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.Reader"),
87 };
88
89 verifyWithInlineConfigParser(
90 getPath("InputCustomImportOrderDefault.java"), expected);
91 }
92
93
94
95
96
97 @Test
98 public void testStaticStandardThird() throws Exception {
99 final String[] expected = {
100 "16:1: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT",
101 "java.io.File.createTempFile"),
102 "17:1: " + getCheckMessage(MSG_LEX, "java.awt.print.Paper.*",
103 "java.io.File.createTempFile"),
104 "22:1: " + getCheckMessage(MSG_LEX, "java.awt.Dialog", "java.awt.Frame"),
105 "27:1: " + getCheckMessage(MSG_LEX, "java.io.File", "javax.swing.JTable"),
106 "28:1: " + getCheckMessage(MSG_LEX, "java.io.IOException", "javax.swing.JTable"),
107 "29:1: " + getCheckMessage(MSG_LEX, "java.io.InputStream", "javax.swing.JTable"),
108 "30:1: " + getCheckMessage(MSG_LEX, "java.io.Reader", "javax.swing.JTable"),
109 "34:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "com.google.common.collect.*"),
110 "34:1: " + getCheckMessage(MSG_LEX, "com.google.common.collect.*",
111 "com.google.errorprone.annotations.*"),
112 };
113
114 verifyWithInlineConfigParser(
115 getPath("InputCustomImportOrderDefault3.java"), expected);
116 }
117
118 @Test
119 public void testStaticStandardThirdListCustomRules() throws Exception {
120 final String[] expected = {
121 "16:1: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT",
122 "java.io.File.createTempFile"),
123 "17:1: " + getCheckMessage(MSG_LEX, "java.awt.print.Paper.*",
124 "java.io.File.createTempFile"),
125 "22:1: " + getCheckMessage(MSG_LEX, "java.awt.Dialog", "java.awt.Frame"),
126 "27:1: " + getCheckMessage(MSG_LEX, "java.io.File", "javax.swing.JTable"),
127 "28:1: " + getCheckMessage(MSG_LEX, "java.io.IOException", "javax.swing.JTable"),
128 "29:1: " + getCheckMessage(MSG_LEX, "java.io.InputStream", "javax.swing.JTable"),
129 "30:1: " + getCheckMessage(MSG_LEX, "java.io.Reader", "javax.swing.JTable"),
130 "34:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "com.google.common.collect.*"),
131 "34:1: " + getCheckMessage(MSG_LEX, "com.google.common.collect.*",
132 "com.google.errorprone.annotations.*"),
133 };
134
135 verifyWithInlineConfigParser(
136 getPath("InputCustomImportOrderListRules.java"), expected);
137 }
138
139 @Test
140 public void testStaticStandardThirdListCustomRulesWhitespace() throws Exception {
141 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
142
143 verifyWithInlineConfigParser(
144 getPath("InputCustomImportOrderListRulesWhitespace.java"), expected);
145 }
146
147 @Test
148 public void testInputCustomImportOrderSingleLineList() throws Exception {
149 final String[] expected = {
150 "14:112: " + getCheckMessage(MSG_LINE_SEPARATOR,
151 "java.util.Map"),
152 "15:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
153 "com.google.common.annotations.Beta"),
154 "22:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
155 "com.puppycrawl.tools.checkstyle.*"),
156 "26:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
157 "picocli.*"),
158 };
159 verifyWithInlineConfigParser(
160 getPath("InputCustomImportOrderSingleLineList.java"),
161 expected);
162 }
163
164
165
166
167 @Test
168 public void testNonSpecifiedImports() throws Exception {
169 final String[] expected = {
170 "16:1: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT",
171 "java.io.File.createTempFile"),
172 "17:1: " + getCheckMessage(MSG_LEX, "java.awt.print.Paper.*",
173 "java.io.File.createTempFile"),
174 "22:1: " + getCheckMessage(MSG_LEX, "java.awt.Dialog", "java.awt.Frame"),
175 "27:1: " + getCheckMessage(MSG_LEX, "java.io.File", "javax.swing.JTable"),
176 "28:1: " + getCheckMessage(MSG_LEX, "java.io.IOException", "javax.swing.JTable"),
177 "29:1: " + getCheckMessage(MSG_LEX, "java.io.InputStream", "javax.swing.JTable"),
178 "30:1: " + getCheckMessage(MSG_LEX, "java.io.Reader", "javax.swing.JTable"),
179 "32:1: " + getCheckMessage(MSG_ORDER, SAME, THIRD, "com.puppycrawl.tools.checkstyle.*"),
180 "34:1: " + getCheckMessage(MSG_NONGROUP_IMPORT, "com.google.common.collect.*"),
181 "35:1: " + getCheckMessage(MSG_LINE_SEPARATOR, "org.junit.*"),
182 };
183
184 verifyWithInlineConfigParser(
185 getPath("InputCustomImportOrderDefault4.java"), expected);
186 }
187
188 @Test
189 public void testOrderRuleEmpty() throws Exception {
190 final String[] expected = {
191 "17:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.List"),
192 };
193
194 verifyWithInlineConfigParser(
195 getPath("InputCustomImportOrderEmptyRule.java"), expected);
196 }
197
198 @Test
199 public void testOrderRuleWithOneGroup() throws Exception {
200 final String[] expected = {
201 "16:1: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT",
202 "java.io.File.createTempFile"),
203 "19:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.List"),
204 "19:1: " + getCheckMessage(MSG_LEX, "java.util.List", "javax.swing.WindowConstants.*"),
205 "20:1: " + getCheckMessage(MSG_LEX, "java.util.StringTokenizer",
206 "javax.swing.WindowConstants.*"),
207 "21:1: " + getCheckMessage(MSG_LEX, "java.util.*", "javax.swing.WindowConstants.*"),
208 "22:1: " + getCheckMessage(MSG_LEX, "java.util.concurrent.AbstractExecutorService",
209 "javax.swing.WindowConstants.*"),
210 "23:1: " + getCheckMessage(MSG_LEX, "java.util.concurrent.*",
211 "javax.swing.WindowConstants.*"),
212 "26:1: " + getCheckMessage(MSG_LEX, "com.google.errorprone.annotations.*",
213 "com.google.errorprone.annotations.concurrent.*"),
214 "28:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "com.google.common.base.*"),
215 "28:1: " + getCheckMessage(MSG_LEX, "com.google.common.base.*",
216 "com.google.errorprone.annotations.concurrent.*"),
217 };
218
219 verifyWithInlineConfigParser(
220 getPath("InputCustomImportOrderDefault2.java"), expected);
221 }
222
223 @Test
224 public void testStaticSamePackage() throws Exception {
225 final String[] expected = {
226 "17:1: " + getCheckMessage(MSG_LEX, "java.util.*", "java.util.StringTokenizer"),
227 "18:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.*"),
228 "19:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC, "java.awt.Button.ABORT"),
229 "20:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC,
230 "javax.swing.WindowConstants.*"),
231 "21:1: " + getCheckMessage(MSG_LEX, "com.puppycrawl.tools.*",
232 "java.util.StringTokenizer"),
233 "22:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME,
234 "java.util.concurrent.AbstractExecutorService"),
235 "23:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC,
236 "java.io.File.createTempFile"),
237 "24:1: " + getCheckMessage(MSG_LEX, "com.*", "java.util.StringTokenizer"),
238 };
239
240 verifyWithInlineConfigParser(
241 getNonCompilablePath("InputCustomImportOrderSamePackage.java"),
242 expected);
243 }
244
245 @Test
246 public void testWithoutLineSeparator() throws Exception {
247 final String[] expected = {
248 "17:1: " + getCheckMessage(MSG_LEX, "java.util.*", "java.util.StringTokenizer"),
249 "18:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.*"),
250 "19:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC, "java.awt.Button.ABORT"),
251 "20:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC,
252 "javax.swing.WindowConstants.*"),
253 "21:1: " + getCheckMessage(MSG_LEX, "com.puppycrawl.tools.*",
254 "java.util.StringTokenizer"),
255 "22:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME,
256 "java.util.concurrent.AbstractExecutorService"),
257 "23:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC,
258 "java.io.File.createTempFile"),
259 "24:1: " + getCheckMessage(MSG_LEX, "com.*", "java.util.StringTokenizer"),
260 };
261
262 verifyWithInlineConfigParser(
263 getNonCompilablePath("InputCustomImportOrderSamePackage2.java"),
264 expected);
265 }
266
267 @Test
268 public void testWithoutLineSeparator2() throws Exception {
269 final String[] expected = {
270 "16:1: " + getCheckMessage(MSG_LEX, "java.io.File.createTempFile",
271 "javax.swing.WindowConstants.*"),
272 "20:1: " + getCheckMessage(MSG_LEX, "java.util.concurrent.locks.*",
273 "java.util.concurrent.locks.AbstractOwnableSynchronizer.*"),
274 };
275
276 verifyWithInlineConfigParser(
277 getPath("InputCustomImportOrder_NoSeparator.java"), expected);
278 }
279
280 @Test
281 public void testNoValid() throws Exception {
282 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
283
284 verifyWithInlineConfigParser(
285 getPath("InputCustomImportOrderNoValid.java"), expected);
286 }
287
288 @Test
289 public void testPossibleIndexOutOfBoundsException() throws Exception {
290 final String[] expected = {
291 "17:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, THIRD, "org.w3c.dom.Node"),
292 };
293
294 verifyWithInlineConfigParser(
295 getPath("InputCustomImportOrderPossibleIndexOutOfBoundsException.java"), expected);
296 }
297
298 @Test
299 public void testDefaultPackage2() throws Exception {
300
301 final String[] expected = {
302 "19:1: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT",
303 "java.io.File.createTempFile"),
304 "22:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.Button"),
305 "23:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.Frame"),
306 "24:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.Dialog"),
307 "25:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.event.ActionEvent"),
308 "26:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "javax.swing.JComponent"),
309 "27:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "javax.swing.JTable"),
310 "28:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.File"),
311 "29:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.IOException"),
312 "30:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.InputStream"),
313 "31:1: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.Reader"),
314 "35:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "com.google.common.*"),
315 "35:1: " + getCheckMessage(MSG_LEX, "com.google.common.*", "com.puppycrawl.tools.*"),
316 };
317
318 verifyWithInlineConfigParser(
319 getNonCompilablePath("InputCustomImportOrderDefaultPackage.java"),
320 expected);
321 }
322
323 @Test
324 public void testWithoutThirdPartyPackage() throws Exception {
325 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
326
327 verifyWithInlineConfigParser(
328 getNonCompilablePath("InputCustomImportOrderThirdPartyPackage.java"), expected);
329 }
330
331 @Test
332 public void testThirdPartyAndSpecialImports() throws Exception {
333 final String[] expected = {
334 "23:1: " + getCheckMessage(MSG_ORDER, THIRD, SPECIAL,
335 "com.google.common.collect.HashMultimap"),
336 };
337
338 verifyWithInlineConfigParser(
339 getNonCompilablePath("InputCustomImportOrderThirdPartyAndSpecial.java"), expected);
340 }
341
342 @Test
343 public void testCompareImports() throws Exception {
344 final String[] expected = {
345 "16:1: " + getCheckMessage(MSG_LEX, "java.util.Map",
346 "java.util.Map.Entry"),
347 };
348
349 verifyWithInlineConfigParser(
350 getPath("InputCustomImportOrderCompareImports.java"), expected);
351 }
352
353 @Test
354 public void testFindBetterPatternMatch() throws Exception {
355 final String[] expected = {
356 "20:1: " + getCheckMessage(MSG_ORDER, THIRD, SPECIAL,
357 "com.google.common.annotations.Beta"),
358 };
359
360 verifyWithInlineConfigParser(
361 getPath("InputCustomImportOrderFindBetterPatternMatch.java"), expected);
362 }
363
364 @Test
365 public void testBeginTreeClear() throws Exception {
366 final DefaultConfiguration checkConfig =
367 createModuleConfig(CustomImportOrderCheck.class);
368 checkConfig.addProperty("specialImportsRegExp", "com");
369 checkConfig.addProperty("separateLineBetweenGroups", "false");
370 checkConfig.addProperty("customImportOrderRules",
371 "STANDARD_JAVA_PACKAGE###SPECIAL_IMPORTS");
372 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
373 final Checker checker = createChecker(checkConfig);
374 final String fileName1 = getPath("InputCustomImportOrderImportsContainingJava.java");
375 final String fileName2 = getPath("InputCustomImportOrderNoValid.java");
376 final File[] files = {
377 new File(fileName1),
378 new File(fileName2),
379 };
380 verify(checker, files, fileName1, expected);
381 }
382
383 @Test
384 public void testImportsContainingJava() throws Exception {
385 final String[] expected = {
386 "17:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
387 "com.google.errorprone.annotations.*"),
388 };
389
390 verifyWithInlineConfigParser(
391 getPath("InputCustomImportOrderImportsContainingJava.java"), expected);
392 }
393
394 @Test
395 public void testGetAcceptableTokens() {
396 final CustomImportOrderCheck testCheckObject =
397 new CustomImportOrderCheck();
398 final int[] actual = testCheckObject.getAcceptableTokens();
399 final int[] expected = {
400 TokenTypes.IMPORT,
401 TokenTypes.STATIC_IMPORT,
402 TokenTypes.PACKAGE_DEF,
403 };
404
405 assertWithMessage("Default acceptable tokens are invalid")
406 .that(actual)
407 .isEqualTo(expected);
408 }
409
410 @Test
411
412
413 public void testGetFullImportIdent() throws Exception {
414 final Class<?> clazz = CustomImportOrderCheck.class;
415 final Object t = TestUtil.instantiate(clazz);
416 final Object actual = TestUtil.invokeMethod(t, "getFullImportIdent",
417 Object.class, (Object) null);
418
419 final String expected = "";
420 assertWithMessage("Invalid getFullImportIdent result")
421 .that(actual)
422 .isEqualTo(expected);
423 }
424
425 @Test
426 public void testSamePackageDepth2() throws Exception {
427 final String[] expected = {
428 "20:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.*"),
429 "21:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.List"),
430 "22:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.StringTokenizer"),
431 "23:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.*"),
432 "24:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME,
433 "java.util.concurrent.AbstractExecutorService"),
434 "25:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME,
435 "java.util.concurrent.locks.LockSupport"),
436 "26:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.regex.Pattern"),
437 "27:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.regex.Matcher"),
438 };
439
440 verifyWithInlineConfigParser(
441 getNonCompilablePath("InputCustomImportOrderSamePackageDepth25.java"),
442 expected);
443 }
444
445 @Test
446 public void testSamePackageDepth3() throws Exception {
447 final String[] expected = {
448 "23:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.*"),
449 "24:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME,
450 "java.util.concurrent.AbstractExecutorService"),
451 "25:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME,
452 "java.util.concurrent.locks.LockSupport"),
453 };
454
455 verifyWithInlineConfigParser(
456 getNonCompilablePath("InputCustomImportOrderSamePackageDepth252.java"),
457 expected);
458 }
459
460 @Test
461 public void testSamePackageDepth4() throws Exception {
462 final String[] expected = {
463 "25:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME,
464 "java.util.concurrent.locks.LockSupport"),
465 };
466
467 verifyWithInlineConfigParser(
468 getNonCompilablePath("InputCustomImportOrderSamePackageDepth253.java"),
469 expected);
470 }
471
472 @Test
473 public void testSamePackageDepthLongerThenActualPackage() throws Exception {
474 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
475
476 verifyWithInlineConfigParser(
477 getNonCompilablePath("InputCustomImportOrderSamePackageDepth254.java"),
478 expected);
479 }
480
481 @Test
482 public void testSamePackageDepthNegative() throws Exception {
483
484 try {
485 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
486
487 verifyWithInlineConfigParser(
488 getPath("InputCustomImportOrderDefault5.java"), expected);
489 assertWithMessage("exception expected").fail();
490 }
491 catch (CheckstyleException exc) {
492 assertWithMessage("Invalid exception message")
493 .that(exc)
494 .hasMessageThat()
495 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
496 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks"
497 + ".imports.CustomImportOrderCheck");
498 assertWithMessage("Invalid exception message")
499 .that(exc.getCause().getCause().getCause().getCause().getMessage())
500 .isEqualTo("SAME_PACKAGE rule parameter should be positive integer: "
501 + "SAME_PACKAGE(-1)");
502 }
503 }
504
505 @Test
506 public void testSamePackageDepthZero() throws Exception {
507 try {
508 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
509
510 verifyWithInlineConfigParser(
511 getPath("InputCustomImportOrderDefault6.java"), expected);
512 assertWithMessage("exception expected").fail();
513 }
514 catch (CheckstyleException exc) {
515 assertWithMessage("Invalid exception message")
516 .that(exc.getMessage())
517 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
518 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks"
519 + ".imports.CustomImportOrderCheck");
520 assertWithMessage("Invalid exception message")
521 .that(exc.getCause().getCause().getCause().getCause().getMessage())
522 .isEqualTo("SAME_PACKAGE rule parameter should be positive integer: "
523 + "SAME_PACKAGE(0)");
524 }
525 }
526
527 @Test
528 public void testUnsupportedRule() throws Exception {
529 try {
530 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
531
532 verifyWithInlineConfigParser(
533 getPath("InputCustomImportOrderDefault7.java"), expected);
534 assertWithMessage("exception expected").fail();
535 }
536 catch (CheckstyleException exc) {
537 assertWithMessage("Invalid exception message")
538 .that(exc.getMessage())
539 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
540 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks"
541 + ".imports.CustomImportOrderCheck");
542 assertWithMessage("Invalid exception message")
543 .that(exc.getCause().getCause().getCause().getCause().getMessage())
544 .isEqualTo("Unexpected rule: UNSUPPORTED_RULE");
545 }
546 }
547
548 @Test
549 public void testSamePackageDepthNotInt() throws Exception {
550 try {
551 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
552
553 verifyWithInlineConfigParser(
554 getPath("InputCustomImportOrderDefault8.java"), expected);
555 assertWithMessage("exception expected").fail();
556 }
557 catch (CheckstyleException exc) {
558 assertWithMessage("Invalid exception message")
559 .that(exc.getMessage())
560 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
561 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks"
562 + ".imports.CustomImportOrderCheck");
563 assertWithMessage("Invalid exception message")
564 .that(exc.getCause().getCause().getCause().getCause().getMessage())
565 .isEqualTo("For input string: \"INT_IS_REQUIRED_HERE\"");
566 }
567 }
568
569 @Test
570 public void testNoImports() throws Exception {
571 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
572
573 verifyWithInlineConfigParser(
574 getPath("InputCustomImportOrder_NoImports.java"), expected);
575 }
576
577 @Test
578 public void testDefaultConfiguration() throws Exception {
579 final String[] expected = {
580 "20:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.awt.Button"),
581 "32:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.io.*"),
582 "34:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "com.google.common.collect.*"),
583 };
584 verifyWithInlineConfigParser(
585 getPath("InputCustomImportOrderDefault9.java"), expected);
586 }
587
588 @Test
589 public void testRulesWithOverlappingPatterns() throws Exception {
590 final String[] expected = {
591 "23:1: " + getCheckMessage(MSG_ORDER, THIRD, STD,
592 "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocNodeImpl"),
593 "27:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
594 "com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck"),
595 "33:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
596 "com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocTag"),
597 "35:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD,
598 "com.puppycrawl.tools.checkstyle.checks.javadoc.WriteTagCheck"),
599 "39:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SPECIAL,
600 "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag"),
601 "40:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD,
602 "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck"),
603 "41:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD,
604 "com.puppycrawl.tools.checkstyle.checks.javadoc.NonEmptyAtclauseDescriptionCheck"),
605 };
606
607 verifyWithInlineConfigParser(
608 getPath("InputCustomImportOrder_OverlappingPatterns.java"), expected);
609 }
610
611 @Test
612 public void testMultiplePatternMatchesSecondPatternIsLonger() throws Exception {
613 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
614 verifyWithInlineConfigParser(
615 getPath("InputCustomImportOrder_MultiplePatternMatches.java"),
616 expected);
617 }
618
619 @Test
620 public void testMultiplePatternMatchesFirstPatternHasLaterPosition() throws Exception {
621 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
622 verifyWithInlineConfigParser(
623 getPath("InputCustomImportOrder_MultiplePatternMatches2.java"),
624 expected);
625 }
626
627 @Test
628 public void testMultiplePatternMatchesFirstPatternHasEarlierPosition() throws Exception {
629 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
630 verifyWithInlineConfigParser(
631 getPath("InputCustomImportOrder_MultiplePatternMatches3.java"),
632 expected);
633 }
634
635 @Test
636 public void testMultiplePatternMultipleImportFirstPatternHasLaterPosition() throws Exception {
637 final String[] expected = {
638 "16:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD, "org.junit.Test"),
639 };
640 verifyWithInlineConfigParser(
641 getPath("InputCustomImportOrder_MultiplePatternMultipleImport.java"),
642 expected);
643 }
644
645 @Test
646 public void testNoPackage() throws Exception {
647 final String[] expected = {
648 "17:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.*"),
649 "19:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.HashMap"),
650 "23:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "javax.net.ServerSocketFactory"),
651 "26:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "javax.net.SocketFactory"),
652 };
653 verifyWithInlineConfigParser(
654 getNonCompilablePath("InputCustomImportOrderNoPackage.java"),
655 expected);
656 }
657
658 @Test
659 public void testNoPackage2() throws Exception {
660 final String[] expected = {
661 "18:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
662 "com.sun.accessibility.internal.resources.*"),
663 "22:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.Arrays"),
664 "30:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
665 "org.apache.commons.beanutils.converters.ArrayConverter"),
666 };
667 verifyWithInlineConfigParser(
668 getNonCompilablePath("InputCustomImportOrderNoPackage2.java"),
669 expected);
670 }
671
672 @Test
673 public void testNoPackage3() throws Exception {
674 final String[] expected = {
675 "17:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
676 "java.util.Map"),
677 "25:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
678 "org.apache.*"),
679 "29:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
680 "antlr.*"),
681 };
682 verifyWithInlineConfigParser(
683 getNonCompilablePath("InputCustomImportOrderNoPackage3.java"),
684 expected);
685 }
686
687 @Test
688 public void testInputCustomImportOrderSingleLine() throws Exception {
689 final String[] expected = {
690 "14:112: " + getCheckMessage(MSG_LINE_SEPARATOR,
691 "java.util.Map"),
692 "15:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
693 "com.google.common.annotations.Beta"),
694 "22:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
695 "com.puppycrawl.tools.checkstyle.*"),
696 "26:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
697 "picocli.*"),
698 };
699 verifyWithInlineConfigParser(
700 getPath("InputCustomImportOrderSingleLine.java"),
701 expected);
702 }
703
704 @Test
705 public void testInputCustomImportOrderSingleLine2() throws Exception {
706 final String[] expected = {
707 "14:118: " + getCheckMessage(MSG_LINE_SEPARATOR,
708 "java.util.Map"),
709 };
710 verifyWithInlineConfigParser(
711 getNonCompilablePath("InputCustomImportOrderSingleLine2.java"),
712 expected);
713 }
714
715 @Test
716 public void testInputCustomImportOrderThirdPartyAndSpecial2() throws Exception {
717 final String[] expected = {
718 "21:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
719 "javax.swing.WindowConstants.*"),
720 "24:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
721 "java.awt.Button"),
722 "28:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
723 "java.awt.Dialog"),
724 "34:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
725 "com.google.common.*"),
726 "40:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
727 "org.apache.commons.collections.*"),
728 "45:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
729 "com.puppycrawl.tools.checkstyle.checks.imports.AbstractImportRule"),
730 "51:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
731 "antlr.Token"),
732 "53:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
733 "antlr.collections.AST"),
734 };
735 verifyWithInlineConfigParser(
736 getNonCompilablePath("InputCustomImportOrderThirdPartyAndSpecial2.java"), expected);
737 }
738
739 @Test
740 public void testInputCustomImportOrderMultipleViolationsSameLine() throws Exception {
741 final String[] expected = {
742 "17:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC,
743 "java.util.Collections.*"),
744 "18:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC,
745 "java.lang.String.CASE_INSENSITIVE_ORDER"),
746 "21:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
747 "java.net.Socket"),
748 "21:1: " + getCheckMessage(MSG_LEX, "java.net.Socket",
749 "java.util.*"),
750 };
751 verifyWithInlineConfigParser(
752 getNonCompilablePath("InputCustomImportOrderViolationsSameLine.java"),
753 expected);
754 }
755
756 @Test
757 public void testInputCustomImportOrderSpanMultipleLines() throws Exception {
758 final String[] expected = {
759 "30:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.BitSet"),
760 "45:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.HashSet"),
761 "49:1: " + getCheckMessage(MSG_LINE_SEPARATOR, "org.apache.tools.ant.*"),
762 "54:1: " + getCheckMessage(MSG_LINE_SEPARATOR, "com.puppycrawl.tools.checkstyle.*"),
763 "58:1: " + getCheckMessage(MSG_LINE_SEPARATOR, "picocli.*"),
764 "61:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "picocli.CommandLine"),
765 };
766 verifyWithInlineConfigParser(
767 getPath("InputCustomImportOrderSpanMultipleLines.java"), expected);
768 }
769
770 @Test
771 public void testInputCustomImportOrderEclipseDefaultPositive() throws Exception {
772 final String[] expected = {
773 "22:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD, "java.awt.Button"),
774 "23:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD, "java.awt.Dialog"),
775 "24:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD, "java.io.InputStream"),
776 "26:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SPECIAL, "javax.swing.JComponent"),
777 "27:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SPECIAL, "javax.swing.JTable"),
778 "29:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, THIRD, "org.junit.Test"),
779 "30:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, THIRD,
780 "org.mockito.Mock"),
781 "34:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "sun.tools.java.ArrayType"),
782 };
783 verifyWithInlineConfigParser(
784 getNonCompilablePath("InputCustomImportOrderEclipseDefaultPositive.java"),
785 expected);
786 }
787
788 @Test
789 public void testInputCustomImportOrderSpecialImportsRegExp() throws Exception {
790 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
791 verifyWithInlineConfigParser(
792 getPath("InputCustomImportOrderSpecialImportsRegExp.java"),
793 expected);
794 }
795 }