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.ImportOrderCheck.MSG_ORDERING;
24 import static com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck.MSG_SEPARATED_IN_GROUP;
25 import static com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck.MSG_SEPARATION;
26
27 import java.io.File;
28 import java.util.Optional;
29
30 import org.antlr.v4.runtime.CommonToken;
31 import org.junit.jupiter.api.Test;
32
33 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
34 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
35 import com.puppycrawl.tools.checkstyle.DetailAstImpl;
36 import com.puppycrawl.tools.checkstyle.JavaParser;
37 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
38 import com.puppycrawl.tools.checkstyle.api.DetailAST;
39 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
40 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
41 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
42
43 public class ImportOrderCheckTest extends AbstractModuleTestSupport {
44
45 @Override
46 public String getPackageLocation() {
47 return "com/puppycrawl/tools/checkstyle/checks/imports/importorder";
48 }
49
50 @Test
51 public void testVeryPreciseGrouping() throws Exception {
52 final String[] expected = {};
53
54 verifyWithInlineConfigParser(
55 getNonCompilablePath("InputImportOrder6.java"), expected);
56 }
57
58 @Test
59 public void testGetTokens() {
60 final ImportOrderCheck checkObj = new ImportOrderCheck();
61 final int[] expected = {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
62 assertWithMessage("Default tokens differs from expected")
63 .that(checkObj.getDefaultTokens())
64 .isEqualTo(expected);
65 assertWithMessage("Acceptable tokens differs from expected")
66 .that(checkObj.getAcceptableTokens())
67 .isEqualTo(expected);
68 assertWithMessage("Required tokens differs from expected")
69 .that(checkObj.getRequiredTokens())
70 .isEqualTo(expected);
71 }
72
73
74
75
76
77 @Test
78 public void testImportOrderOptionValueOf() {
79 final ImportOrderOption option = ImportOrderOption.valueOf("TOP");
80 assertWithMessage("Invalid valueOf result")
81 .that(option)
82 .isEqualTo(ImportOrderOption.TOP);
83 }
84
85 @Test
86 public void testDefault() throws Exception {
87 final String[] expected = {
88 "21:1: " + getCheckMessage(MSG_ORDERING, "java.awt.Dialog"),
89 "25:1: " + getCheckMessage(MSG_ORDERING, "javax.swing.JComponent"),
90 "27:1: " + getCheckMessage(MSG_ORDERING, "java.io.File"),
91 "29:1: " + getCheckMessage(MSG_ORDERING, "java.io.IOException"),
92 "34:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
93 "sun.tools.util.ModifierFilter.ALL_ACCESS"),
94 };
95
96 verifyWithInlineConfigParser(
97 getNonCompilablePath("InputImportOrder1.java"), expected);
98 }
99
100 @Test
101 public void testWrongSequenceInNonStaticImports() throws Exception {
102
103 final String[] expected = {
104 "19:1: " + getCheckMessage(MSG_ORDERING,
105 "java.util.HashMap"),
106 };
107
108 verifyWithInlineConfigParser(
109 getNonCompilablePath("InputImportOrderNonStaticWrongSequence.java"), expected);
110 }
111
112 @Test
113 public void testMultilineImport() throws Exception {
114 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
115
116 verifyWithInlineConfigParser(
117 getNonCompilablePath("InputImportOrderMultiline.java"), expected);
118 }
119
120 @Test
121 public void testGroups() throws Exception {
122 final String[] expected = {
123 "21:1: " + getCheckMessage(MSG_ORDERING, "java.awt.Dialog"),
124 "29:1: " + getCheckMessage(MSG_ORDERING, "java.io.IOException"),
125 "32:1: " + getCheckMessage(MSG_ORDERING, "javax.swing.WindowConstants.*"),
126 "34:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
127 "sun.tools.util.ModifierFilter.ALL_ACCESS"),
128 };
129
130 verifyWithInlineConfigParser(
131 getNonCompilablePath("InputImportOrder2.java"), expected);
132 }
133
134 @Test
135 public void testGroupsRegexp() throws Exception {
136 final String[] expected = {
137 "27:1: " + getCheckMessage(MSG_ORDERING, "java.io.File"),
138 "34:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
139 "sun.tools.util.ModifierFilter.ALL_ACCESS"),
140 };
141
142 verifyWithInlineConfigParser(
143 getNonCompilablePath("InputImportOrder3.java"), expected);
144 }
145
146 @Test
147 public void testSeparated() throws Exception {
148 final String[] expected = {
149 "25:1: " + getCheckMessage(MSG_SEPARATION, "javax.swing.JComponent"),
150 "27:1: " + getCheckMessage(MSG_SEPARATION, "java.io.File"),
151 "32:1: " + getCheckMessage(MSG_ORDERING, "javax.swing.WindowConstants.*"),
152 };
153
154 verifyWithInlineConfigParser(
155 getNonCompilablePath("InputImportOrder4.java"), expected);
156 }
157
158 @Test
159 public void testStaticImportSeparated() throws Exception {
160 final String[] expected = {
161 "21:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.lang.Math.cos"),
162 "23:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "org.junit.Assert.assertEquals"),
163 };
164
165 verifyWithInlineConfigParser(
166 getPath("InputImportOrderStaticGroupSeparated.java"), expected);
167 }
168
169 @Test
170 public void testNoGapBetweenStaticImports() throws Exception {
171 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
172
173 verifyWithInlineConfigParser(
174 getPath("InputImportOrderNoGapBetweenStaticImports.java"), expected);
175 }
176
177 @Test
178 public void testSortStaticImportsAlphabeticallyFalse() throws Exception {
179 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
180
181 verifyWithInlineConfigParser(
182 getPath("InputImportOrderSortStaticImportsAlphabetically1.java"),
183 expected);
184 }
185
186 @Test
187 public void testSortStaticImportsAlphabeticallyTrue() throws Exception {
188 final String[] expected = {
189 "20:1: " + getCheckMessage(MSG_ORDERING,
190 "javax.xml.transform.TransformerFactory.newInstance"),
191 "21:1: " + getCheckMessage(MSG_ORDERING, "java.lang.Math.cos"),
192 "22:1: " + getCheckMessage(MSG_ORDERING, "java.lang.Math.abs"),
193 };
194
195 verifyWithInlineConfigParser(
196 getPath("InputImportOrderSortStaticImportsAlphabetically2.java"),
197 expected);
198 }
199
200 @Test
201 public void testCaseInsensitive() throws Exception {
202 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
203
204 verifyWithInlineConfigParser(
205 getPath("InputImportOrderCaseInsensitive.java"), expected);
206 }
207
208 @Test
209 public void testContainerCaseInsensitive() throws Exception {
210 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
211
212 verifyWithInlineConfigParser(
213 getNonCompilablePath("InputImportOrderEclipseStaticCaseSensitive.java"),
214 expected);
215 }
216
217 @Test
218 public void testSimilarGroupPattern() throws Exception {
219 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
220
221 verifyWithInlineConfigParser(
222 getNonCompilablePath("InputImportOrderSimilarGroupPattern.java"),
223 expected);
224 }
225
226 @Test
227 public void testInvalidOption() throws Exception {
228
229 try {
230 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
231
232 verifyWithInlineConfigParser(
233 getPath("InputImportOrder_Top1.java"), expected);
234 assertWithMessage("exception expected").fail();
235 }
236 catch (CheckstyleException exc) {
237 assertWithMessage("Invalid exception message")
238 .that(exc.getMessage())
239 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
240 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks"
241 + ".imports.ImportOrderCheck");
242 }
243 }
244
245 @Test
246 public void testTop() throws Exception {
247 final String[] expected = {
248 "23:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.awt.Button"),
249 "28:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.io.IOException"),
250 "31:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "javax.swing.JComponent"),
251 "34:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.io.File.*"),
252 "34:1: " + getCheckMessage(MSG_ORDERING, "java.io.File.*"),
253 };
254
255 verifyWithInlineConfigParser(
256 getPath("InputImportOrder_Top2.java"), expected);
257 }
258
259 @Test
260 public void testAbove() throws Exception {
261 final String[] expected = {
262 "21:1: " + getCheckMessage(MSG_ORDERING, "java.awt.Button.ABORT"),
263 "24:1: " + getCheckMessage(MSG_ORDERING, "java.awt.Dialog"),
264 "29:1: " + getCheckMessage(MSG_ORDERING, "java.io.File"),
265 "29:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.io.File"),
266 "30:1: " + getCheckMessage(MSG_ORDERING, "java.io.File.createTempFile"),
267 };
268
269 verifyWithInlineConfigParser(
270 getPath("InputImportOrder_Above.java"), expected);
271 }
272
273 @Test
274 public void testInFlow() throws Exception {
275 final String[] expected = {
276 "22:1: " + getCheckMessage(MSG_ORDERING, "java.awt.Dialog"),
277 "25:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "javax.swing.JComponent"),
278 "27:1: " + getCheckMessage(MSG_ORDERING,
279 "javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE"),
280 "28:1: " + getCheckMessage(MSG_ORDERING, "javax.swing.WindowConstants.*"),
281 "29:1: " + getCheckMessage(MSG_ORDERING, "javax.swing.JTable"),
282 "31:1: " + getCheckMessage(MSG_ORDERING, "java.io.File.createTempFile"),
283 "31:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.io.File.createTempFile"),
284 "32:1: " + getCheckMessage(MSG_ORDERING, "java.io.File"),
285 };
286
287 verifyWithInlineConfigParser(
288 getPath("InputImportOrder_InFlow.java"), expected);
289 }
290
291 @Test
292 public void testUnder() throws Exception {
293
294 final String[] expected = {
295 "21:1: " + getCheckMessage(MSG_ORDERING, "java.awt.Dialog"),
296 "27:1: " + getCheckMessage(MSG_ORDERING, "java.awt.Button.ABORT"),
297 "29:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.io.File.createTempFile"),
298 "30:1: " + getCheckMessage(MSG_ORDERING, "java.io.File"),
299 };
300
301 verifyWithInlineConfigParser(
302 getPath("InputImportOrder_Under.java"), expected);
303 }
304
305 @Test
306 public void testBottom() throws Exception {
307 final String[] expected = {
308 "24:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.io.IOException"),
309 "27:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "javax.swing.JComponent"),
310 "30:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.io.File.*"),
311 "31:1: " + getCheckMessage(MSG_ORDERING, "java.io.File"),
312 "33:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.io.File.createTempFile"),
313 "37:1: " + getCheckMessage(MSG_ORDERING, "java.io.Reader"),
314 "37:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.io.Reader"),
315 };
316
317 verifyWithInlineConfigParser(
318 getPath("InputImportOrder_Bottom.java"), expected);
319 }
320
321 @Test
322 public void testGetGroupNumber() throws Exception {
323 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
324
325 verifyWithInlineConfigParser(
326 getNonCompilablePath("InputImportOrderGetGroupNumber.java"), expected);
327 }
328
329 @Test
330 public void testHonorsTokenProperty() throws Exception {
331 final String[] expected = {
332 "20:1: " + getCheckMessage(MSG_ORDERING, "java.awt.Button.ABORT"),
333 "21:1: " + getCheckMessage(MSG_ORDERING, "java.awt.Dialog"),
334 "22:1: " + getCheckMessage(MSG_ORDERING, "java.awt.Button"),
335 };
336
337 verifyWithInlineConfigParser(
338 getPath("InputImportOrder_HonorsTokensProperty.java"), expected);
339 }
340
341 @Test
342 public void testWildcard() throws Exception {
343 final String[] expected = {
344 "21:1: " + getCheckMessage(MSG_ORDERING, "javax.crypto.Cipher"),
345 };
346
347 verifyWithInlineConfigParser(
348 getPath("InputImportOrder_Wildcard.java"), expected);
349 }
350
351 @Test
352 public void testWildcardUnspecified() throws Exception {
353 final String[] expected = {
354 "23:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
355 "javax.crypto.Cipher"),
356 };
357
358 verifyWithInlineConfigParser(
359 getPath("InputImportOrder_WildcardUnspecified.java"), expected);
360 }
361
362 @Test
363 public void testNoFailureForRedundantImports() throws Exception {
364 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
365 verifyWithInlineConfigParser(
366 getPath("InputImportOrder_NoFailureForRedundantImports.java"),
367 expected);
368 }
369
370 @Test
371 public void testStaticGroupsAlphabeticalOrder() throws Exception {
372 final String[] expected = {
373 "22:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "org.antlr.v4.runtime.*"),
374 "24:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.Set"),
375 };
376
377 verifyWithInlineConfigParser(
378 getPath("InputImportOrderStaticGroupOrder1.java"), expected);
379 }
380
381 @Test
382 public void testStaticGroupsOrder() throws Exception {
383 final String[] expected = {
384 "22:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "org.antlr.v4.runtime.*"),
385 "24:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.Set"),
386 };
387 verifyWithInlineConfigParser(
388 getPath("InputImportOrderStaticGroupOrder2.java"), expected);
389 }
390
391 @Test
392 public void testStaticGroupsAlphabeticalOrderBottom() throws Exception {
393 final String[] expected = {
394 "21:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.Set"),
395 "23:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.lang.Math.PI"),
396 };
397 verifyWithInlineConfigParser(
398 getPath("InputImportOrderStaticGroupOrderBottom1.java"), expected);
399 }
400
401 @Test
402 public void testStaticGroupsAlphabeticalOrderBottomNegative() throws Exception {
403 final String[] expected = {
404 "24:1: " + getCheckMessage(MSG_ORDERING, "java.util.Set"),
405 };
406 verifyWithInlineConfigParser(
407 getPath("InputImportOrderStaticGroupOrderBottom_Negative1.java"),
408 expected);
409 }
410
411
412
413
414
415 @Test
416 public void testStaticGroupsAlphabeticalOrderTopNegative() throws Exception {
417 final String[] expected = {
418 "21:1: " + getCheckMessage(MSG_ORDERING, "java.lang.Math.PI"),
419 };
420 verifyWithInlineConfigParser(
421 getPath("InputImportOrderStaticGroupOrderBottom_Negative2.java"),
422 expected);
423 }
424
425
426
427
428
429 @Test
430 public void testStaticGroupsAlphabeticalOrderBottomNegative2() throws Exception {
431 final String[] expected = {
432 "24:1: " + getCheckMessage(MSG_ORDERING, "java.util.Set"),
433 };
434 verifyWithInlineConfigParser(
435 getPath("InputImportOrderStaticGroupOrderBottom_Negative3.java"),
436 expected);
437 }
438
439 @Test
440 public void testStaticGroupsOrderBottom() throws Exception {
441 final String[] expected = {
442 "21:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.Set"),
443 "23:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.lang.Math.PI"),
444 };
445 verifyWithInlineConfigParser(
446 getPath("InputImportOrderStaticGroupOrderBottom2.java"), expected);
447 }
448
449 @Test
450 public void testImportReception() throws Exception {
451 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
452 verifyWithInlineConfigParser(
453 getPath("InputImportOrderRepetition.java"), expected);
454 }
455
456 @Test
457 public void testStaticImportReceptionTop() throws Exception {
458 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
459 verifyWithInlineConfigParser(
460 getPath("InputImportOrderStaticRepetition1.java"), expected);
461 }
462
463 @Test
464 public void testStaticImportReception() throws Exception {
465 final String[] expected = {
466 "20:1: " + getCheckMessage(MSG_SEPARATION, "org.antlr.v4.runtime.CommonToken.*"),
467 "23:1: " + getCheckMessage(MSG_ORDERING, "java.util.Set"),
468 };
469 verifyWithInlineConfigParser(
470 getPath("InputImportOrderStaticRepetition2.java"), expected);
471 }
472
473 @Test
474 public void testStaticGroupsOrderAbove() throws Exception {
475 final String[] expected = {
476 "21:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.Set"),
477 "23:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.lang.Math.PI"),
478 "23:1: " + getCheckMessage(MSG_ORDERING, "java.lang.Math.PI"),
479 "24:1: " + getCheckMessage(MSG_ORDERING, "org.antlr.v4.runtime.Recognizer.EOF"),
480 };
481 verifyWithInlineConfigParser(
482 getPath("InputImportOrderStaticGroupOrderBottom3.java"), expected);
483 }
484
485 @Test
486 public void testStaticOnDemandGroupsOrder() throws Exception {
487 final String[] expected = {
488 "22:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "org.antlr.v4.runtime.*"),
489 "24:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.Set"),
490 "25:1: " + getCheckMessage(MSG_ORDERING, "org.junit.Test"),
491 };
492 verifyWithInlineConfigParser(
493 getPath("InputImportOrderStaticOnDemandGroupOrder1.java"), expected);
494 }
495
496 @Test
497 public void testStaticOnDemandGroupsAlphabeticalOrder() throws Exception {
498 final String[] expected = {
499 "22:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "org.antlr.v4.runtime.*"),
500 "24:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.Set"),
501 "25:1: " + getCheckMessage(MSG_ORDERING, "org.junit.Test"),
502 };
503 verifyWithInlineConfigParser(
504 getPath("InputImportOrderStaticOnDemandGroupOrder2.java"), expected);
505 }
506
507 @Test
508 public void testStaticOnDemandGroupsOrderBottom() throws Exception {
509 final String[] expected = {
510 "21:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.Set"),
511 "23:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.lang.Math.*"),
512 };
513 verifyWithInlineConfigParser(
514 getPath("InputImportOrderStaticOnDemandGroupOrderBottom1.java"),
515 expected);
516 }
517
518 @Test
519 public void testStaticOnDemandGroupsAlphabeticalOrderBottom() throws Exception {
520 final String[] expected = {
521 "21:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.Set"),
522 "23:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.lang.Math.*"),
523 };
524 verifyWithInlineConfigParser(
525 getPath("InputImportOrderStaticOnDemandGroupOrderBottom2.java"),
526 expected);
527 }
528
529 @Test
530 public void testStaticOnDemandGroupsOrderAbove() throws Exception {
531 final String[] expected = {
532 "21:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.util.Set"),
533 "23:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "java.lang.Math.*"),
534 "23:1: " + getCheckMessage(MSG_ORDERING, "java.lang.Math.*"),
535 "24:1: " + getCheckMessage(MSG_ORDERING, "org.antlr.v4.runtime.CommonToken.*"),
536 };
537 verifyWithInlineConfigParser(
538 getPath("InputImportOrderStaticOnDemandGroupOrderBottom3.java"),
539 expected);
540 }
541
542 @Test
543 public void testGroupWithSlashes() throws Exception {
544 final DefaultConfiguration checkConfig = createModuleConfig(ImportOrderCheck.class);
545 checkConfig.addProperty("groups", "/^javax");
546
547 try {
548 execute(checkConfig, getNonCompilablePath("InputImportOrder5.java"));
549 assertWithMessage("exception expected").fail();
550 }
551 catch (CheckstyleException exc) {
552 assertWithMessage("Invalid exception message")
553 .that(exc.getMessage())
554 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
555 + "cannot initialize module com.puppycrawl.tools.checkstyle.checks"
556 + ".imports.ImportOrderCheck");
557 assertWithMessage("Invalid exception message")
558 .that(exc.getCause().getCause().getCause().getCause().getMessage())
559 .isEqualTo("Invalid group: /^javax");
560 }
561 }
562
563 @Test
564 public void testGroupWithDot() throws Exception {
565 final String[] expected = {
566 "21:1: " + getCheckMessage(MSG_ORDERING, "java.awt.Dialog"),
567 "23:1: " + getCheckMessage(MSG_ORDERING, "javax.swing.JComponent"),
568 };
569 verifyWithInlineConfigParser(
570 getPath("InputImportOrder_DotPackageName.java"),
571 expected);
572 }
573
574 @Test
575 public void testMultiplePatternMatches() throws Exception {
576 final String[] expected = {
577 "21:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "org.*"),
578 };
579
580 verifyWithInlineConfigParser(
581 getNonCompilablePath("InputImportOrder_MultiplePatternMatches1.java"),
582 expected);
583 }
584
585
586
587
588
589
590
591 @Test
592 public void testVisitTokenSwitchReflection() {
593
594 final DetailAstImpl astImport = mockAST(TokenTypes.IMPORT, "import", 0, 0);
595 final DetailAstImpl astIdent = mockAST(TokenTypes.IDENT, "myTestImport", 0, 0);
596 astImport.addChild(astIdent);
597 final DetailAST astSemi = mockAST(TokenTypes.SEMI, ";", 0, 0);
598 astIdent.addNextSibling(astSemi);
599
600
601 final ImportOrderCheck mock = new ImportOrderCheck();
602 TestUtil.setInternalState(mock, "option", null);
603
604
605 try {
606 mock.visitToken(astImport);
607 assertWithMessage("An exception is expected").fail();
608 }
609 catch (IllegalStateException exc) {
610 assertWithMessage("invalid exception message")
611 .that(exc.getMessage())
612 .endsWith(": null");
613 }
614 }
615
616
617
618
619
620
621
622
623
624
625 private static DetailAstImpl mockAST(final int tokenType, final String tokenText,
626 final int tokenRow, final int tokenColumn) {
627 final CommonToken tokenImportSemi = new CommonToken(tokenType, tokenText);
628 tokenImportSemi.setLine(tokenRow);
629 tokenImportSemi.setCharPositionInLine(tokenColumn);
630 final DetailAstImpl astSemi = new DetailAstImpl();
631 astSemi.initialize(tokenImportSemi);
632 return astSemi;
633 }
634
635 @Test
636 public void testEclipseDefaultPositive() throws Exception {
637 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
638
639 verifyWithInlineConfigParser(
640 getNonCompilablePath("InputImportOrder_EclipseDefaultPositive.java"), expected);
641 }
642
643 @Test
644 public void testStaticImportEclipseRepetition() throws Exception {
645 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
646 verifyWithInlineConfigParser(
647 getNonCompilablePath("InputImportOrderEclipseStaticRepetition.java"), expected);
648 }
649
650 @Test
651 public void testEclipseDefaultNegative() throws Exception {
652 final String[] expected = {
653 "28:1: " + getCheckMessage(MSG_SEPARATION, "javax.swing.JComponent"),
654 "33:1: " + getCheckMessage(MSG_ORDERING, "org.junit.Test"),
655 };
656
657 verifyWithInlineConfigParser(
658 getNonCompilablePath("InputImportOrder_EclipseDefaultNegative.java"), expected);
659 }
660
661 @Test
662 public void testUseContainerOrderingForStaticTrue() throws Exception {
663 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
664 verifyWithInlineConfigParser(
665 getNonCompilablePath("InputImportOrderEclipseStatic1.java"), expected);
666 }
667
668 @Test
669 public void testUseContainerOrderingForStaticFalse() throws Exception {
670 final String[] expected = {
671 "22:1: " + getCheckMessage(MSG_ORDERING,
672 "io.netty.handler.codec.http.HttpHeaders.Names.addDate"),
673 };
674 verifyWithInlineConfigParser(
675 getNonCompilablePath("InputImportOrderEclipseStatic2.java"), expected);
676 }
677
678 @Test
679 public void testUseContainerOrderingForStaticTrueCaseSensitive() throws Exception {
680 final String[] expected = {
681 "23:1: " + getCheckMessage(MSG_ORDERING,
682 "io.netty.handler.codec.http.HttpHeaders.Names.DATE"),
683 };
684 verifyWithInlineConfigParser(
685 getNonCompilablePath("InputImportOrderEclipseStatic3.java"), expected);
686 }
687
688 @Test
689 public void testUseContainerOrderingForStatic() throws Exception {
690 final String[] expected = {
691 "22:1: " + getCheckMessage(MSG_ORDERING,
692 "io.netty.handler.Codec.HTTP.HttpHeaders.tmp.same"),
693 "23:1: " + getCheckMessage(MSG_ORDERING,
694 "io.netty.handler.Codec.HTTP.HttpHeaders.TKN.same"),
695 };
696 verifyWithInlineConfigParser(
697 getNonCompilablePath("InputImportOrderContainerOrdering.java"),
698 expected);
699 }
700
701 @Test
702 public void testImportGroupsRedundantSeparatedInternally() throws Exception {
703 final String[] expected = {
704 "21:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "org.*"),
705 };
706 verifyWithInlineConfigParser(
707 getNonCompilablePath("InputImportOrder_MultiplePatternMatches2.java"),
708 expected);
709 }
710
711 @Test
712 public void testStaticGroupsAbove() throws Exception {
713 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
714
715 verifyWithInlineConfigParser(
716 getNonCompilablePath("InputImportOrderStaticGroupsAbove.java"),
717 expected);
718 }
719
720 @Test
721 public void testStaticGroupsBottom() throws Exception {
722 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
723
724 verifyWithInlineConfigParser(
725 getNonCompilablePath("InputImportOrderStaticGroupsBottom.java"),
726 expected);
727 }
728
729 @Test
730 public void testStaticGroupsBottomSeparated() throws Exception {
731 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
732
733 verifyWithInlineConfigParser(
734 getNonCompilablePath("InputImportOrderStaticGroupsBottomSeparated.java"), expected);
735 }
736
737 @Test
738 public void testStaticGroupsInflow() throws Exception {
739 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
740
741 verifyWithInlineConfigParser(
742 getNonCompilablePath("InputImportOrderStaticGroupsInflow.java"),
743 expected);
744 }
745
746 @Test
747 public void testStaticGroupsNegative() throws Exception {
748 final String[] expected = {
749 "21:1: " + getCheckMessage(MSG_ORDERING, "org.junit.Assert.fail"),
750 "23:1: " + getCheckMessage(MSG_ORDERING, "org.infinispan.test.TestingUtil.extract"),
751 };
752
753 verifyWithInlineConfigParser(
754 getNonCompilablePath("InputImportOrderStaticGroupsNegative.java"),
755 expected);
756 }
757
758 @Test
759 public void testStaticGroupsTop() throws Exception {
760 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
761
762 verifyWithInlineConfigParser(
763 getNonCompilablePath("InputImportOrderStaticGroupsTop.java"),
764 expected);
765 }
766
767 @Test
768 public void testStaticGroupsTopSeparated() throws Exception {
769 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
770
771 verifyWithInlineConfigParser(
772 getNonCompilablePath("InputImportOrderStaticGroupsTopSeparated.java"),
773 expected);
774 }
775
776 @Test
777 public void testStaticGroupsUnordered() throws Exception {
778 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
779
780 verifyWithInlineConfigParser(
781 getNonCompilablePath("InputImportOrderStaticGroupsUnordered.java"),
782 expected);
783 }
784
785 @Test
786 public void testTrimOption() throws Exception {
787 final String[] expected = {
788 "25:1: " + getCheckMessage(MSG_ORDERING, "java.util.Set"),
789 };
790
791 verifyWithInlineConfigParser(
792 getPath("InputImportOrderTestTrimInOption.java"),
793 expected);
794 }
795
796
797
798
799
800
801
802
803
804 @Test
805 public void testClearState() throws Exception {
806 final ImportOrderCheck check = new ImportOrderCheck();
807 final DetailAST root = JavaParser.parseFile(
808 new File(getPath("InputImportOrderBeginTree.java")),
809 JavaParser.Options.WITHOUT_COMMENTS);
810 final Optional<DetailAST> staticImport = TestUtil.findTokenInAstByPredicate(root,
811 ast -> ast.getType() == TokenTypes.STATIC_IMPORT);
812
813 assertWithMessage("Ast should contain STATIC_IMPORT")
814 .that(staticImport.isPresent())
815 .isTrue();
816 assertWithMessage("State is not cleared on beginTree")
817 .that(TestUtil.isStatefulFieldClearedDuringBeginTree(check,
818 staticImport.orElseThrow(), "lastImportStatic",
819 lastImportStatic -> !(boolean) lastImportStatic))
820 .isTrue();
821
822 }
823 }