View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.imports;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_LEX;
23  import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_LINE_SEPARATOR;
24  import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_NONGROUP_EXPECTED;
25  import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_NONGROUP_IMPORT;
26  import static com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_ORDER;
27  import static org.junit.Assert.assertArrayEquals;
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertTrue;
30  import static org.junit.Assert.fail;
31  
32  import java.io.File;
33  import java.lang.reflect.Method;
34  
35  import org.junit.Test;
36  
37  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
38  import com.puppycrawl.tools.checkstyle.Checker;
39  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
40  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
41  import com.puppycrawl.tools.checkstyle.api.DetailAST;
42  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
43  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
44  
45  public class CustomImportOrderCheckTest extends AbstractModuleTestSupport {
46      /** Shortcuts to make code more compact. */
47      private static final String STATIC = CustomImportOrderCheck.STATIC_RULE_GROUP;
48      private static final String SAME = CustomImportOrderCheck.SAME_PACKAGE_RULE_GROUP;
49      private static final String THIRD = CustomImportOrderCheck.THIRD_PARTY_PACKAGE_RULE_GROUP;
50      private static final String STD = CustomImportOrderCheck.STANDARD_JAVA_PACKAGE_RULE_GROUP;
51      private static final String SPECIAL = CustomImportOrderCheck.SPECIAL_IMPORTS_RULE_GROUP;
52  
53      @Override
54      protected String getPackageLocation() {
55          return "com/puppycrawl/tools/checkstyle/checks/imports/customimportorder";
56      }
57  
58      @Test
59      public void testGetRequiredTokens() {
60          final CustomImportOrderCheck checkObj = new CustomImportOrderCheck();
61          final int[] expected = {
62              TokenTypes.IMPORT,
63              TokenTypes.STATIC_IMPORT,
64              TokenTypes.PACKAGE_DEF,
65          };
66          assertArrayEquals("Default required tokens are invalid",
67              expected, checkObj.getRequiredTokens());
68      }
69  
70      @Test
71      public void testCustom() throws Exception {
72          final DefaultConfiguration checkConfig =
73                  createModuleConfig(CustomImportOrderCheck.class);
74          checkConfig.addAttribute("standardPackageRegExp", "^(java|javax)\\.");
75          checkConfig.addAttribute("thirdPartyPackageRegExp", "com|org");
76          checkConfig.addAttribute("customImportOrderRules",
77                  "STATIC###SAME_PACKAGE(3)###THIRD_PARTY_PACKAGE###STANDARD_JAVA_PACKAGE");
78          checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
79          final String[] expected = {
80              "4: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT",
81                  "java.io.File.createTempFile"),
82              "5: " + getCheckMessage(MSG_LEX, "java.awt.print.Paper.*",
83                  "java.io.File.createTempFile"),
84              "8: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.awt.Button"),
85              "9: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.awt.Frame"),
86              "10: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.awt.Dialog"),
87              "11: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.awt.color.ColorSpace"),
88              "12: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.awt.event.ActionEvent"),
89              "13: " + getCheckMessage(MSG_ORDER, STD, SAME, "javax.swing.JComponent"),
90              "14: " + getCheckMessage(MSG_ORDER, STD, SAME, "javax.swing.JTable"),
91              "15: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.io.File"),
92              "16: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.io.IOException"),
93              "17: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.io.InputStream"),
94              "18: " + getCheckMessage(MSG_ORDER, STD, SAME, "java.io.Reader"),
95          };
96  
97          verify(checkConfig, getPath("InputCustomImportOrderDefault.java"), expected);
98      }
99  
100     /**
101      * Checks different group orderings and imports which are out of those ones
102      * specified in the configuration.
103      */
104     @Test
105     public void testStaticStandardThird() throws Exception {
106         final DefaultConfiguration checkConfig =
107                 createModuleConfig(CustomImportOrderCheck.class);
108         checkConfig.addAttribute("thirdPartyPackageRegExp", "com.|org.");
109         checkConfig.addAttribute("customImportOrderRules",
110                 "STATIC###STANDARD_JAVA_PACKAGE###THIRD_PARTY_PACKAGE");
111         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
112         final String[] expected = {
113             "4: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT",
114                 "java.io.File.createTempFile"),
115             "5: " + getCheckMessage(MSG_LEX, "java.awt.print.Paper.*",
116                 "java.io.File.createTempFile"),
117             "10: " + getCheckMessage(MSG_LEX, "java.awt.Dialog", "java.awt.Frame"),
118             "15: " + getCheckMessage(MSG_LEX, "java.io.File", "javax.swing.JTable"),
119             "16: " + getCheckMessage(MSG_LEX, "java.io.IOException", "javax.swing.JTable"),
120             "17: " + getCheckMessage(MSG_LEX, "java.io.InputStream", "javax.swing.JTable"),
121             "18: " + getCheckMessage(MSG_LEX, "java.io.Reader", "javax.swing.JTable"),
122             "22: " + getCheckMessage(MSG_LEX, "com.google.common.collect.*",
123                 "com.puppycrawl.tools.*"),
124         };
125 
126         verify(checkConfig, getPath("InputCustomImportOrderDefault.java"), expected);
127     }
128 
129     /**
130      * Checks different combinations for same_package group.
131      */
132     @Test
133     public void testNonSpecifiedImports() throws Exception {
134         final DefaultConfiguration checkConfig =
135                 createModuleConfig(CustomImportOrderCheck.class);
136         checkConfig.addAttribute("thirdPartyPackageRegExp", "org.");
137         checkConfig.addAttribute("customImportOrderRules",
138                 "STATIC###STANDARD_JAVA_PACKAGE###THIRD_PARTY_PACKAGE###SAME_PACKAGE(3)");
139         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
140         final String[] expected = {
141             "4: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT",
142                 "java.io.File.createTempFile"),
143             "5: " + getCheckMessage(MSG_LEX, "java.awt.print.Paper.*",
144                 "java.io.File.createTempFile"),
145             "10: " + getCheckMessage(MSG_LEX, "java.awt.Dialog", "java.awt.Frame"),
146             "15: " + getCheckMessage(MSG_LEX, "java.io.File", "javax.swing.JTable"),
147             "16: " + getCheckMessage(MSG_LEX, "java.io.IOException", "javax.swing.JTable"),
148             "17: " + getCheckMessage(MSG_LEX, "java.io.InputStream", "javax.swing.JTable"),
149             "18: " + getCheckMessage(MSG_LEX, "java.io.Reader", "javax.swing.JTable"),
150             "20: " + getCheckMessage(MSG_ORDER, SAME, THIRD, "com.puppycrawl.tools.*"),
151             "22: " + getCheckMessage(MSG_NONGROUP_IMPORT, "com.google.common.collect.*"),
152             "23: " + getCheckMessage(MSG_LINE_SEPARATOR, "org.junit.*"),
153         };
154 
155         verify(checkConfig, getPath("InputCustomImportOrderDefault.java"), expected);
156     }
157 
158     @Test
159     public void testOrderRuleWithOneGroup() throws Exception {
160         final DefaultConfiguration checkConfig =
161                 createModuleConfig(CustomImportOrderCheck.class);
162         checkConfig.addAttribute("thirdPartyPackageRegExp", "org.");
163         checkConfig.addAttribute("customImportOrderRules",
164                 "STANDARD_JAVA_PACKAGE");
165         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
166         final String[] expected = {
167             "4: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT",
168                 "java.io.File.createTempFile"),
169             "7: " + getCheckMessage(MSG_LEX, "java.util.List", "javax.swing.WindowConstants.*"),
170             "8: " + getCheckMessage(MSG_LEX, "java.util.StringTokenizer",
171                 "javax.swing.WindowConstants.*"),
172             "9: " + getCheckMessage(MSG_LEX, "java.util.*", "javax.swing.WindowConstants.*"),
173             "10: " + getCheckMessage(MSG_LEX, "java.util.concurrent.AbstractExecutorService",
174                 "javax.swing.WindowConstants.*"),
175             "11: " + getCheckMessage(MSG_LEX, "java.util.concurrent.*",
176                 "javax.swing.WindowConstants.*"),
177             "14: " + getCheckMessage(MSG_LEX, "com.*", "com.puppycrawl.tools.*"),
178             "16: " + getCheckMessage(MSG_LEX, "com.google.common.base.*", "com.puppycrawl.tools.*"),
179         };
180 
181         verify(checkConfig, getPath("InputCustomImportOrderDefault2.java"), expected);
182     }
183 
184     @Test
185     public void testStaticSamePackage() throws Exception {
186         final DefaultConfiguration checkConfig =
187                 createModuleConfig(CustomImportOrderCheck.class);
188         checkConfig.addAttribute("thirdPartyPackageRegExp", "org.");
189         checkConfig.addAttribute("customImportOrderRules",
190                 "STATIC###SAME_PACKAGE(3)");
191         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
192         final String[] expected = {
193             "5: " + getCheckMessage(MSG_LEX, "java.util.*", "java.util.StringTokenizer"),
194             "6: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.*"),
195             "7: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC, "java.awt.Button.ABORT"),
196             "8: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC, "javax.swing.WindowConstants.*"),
197             "9: " + getCheckMessage(MSG_LEX, "com.puppycrawl.tools.*", "java.util.StringTokenizer"),
198             "10: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME,
199                 "java.util.concurrent.AbstractExecutorService"),
200             "11: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC, "java.io.File.createTempFile"),
201             "12: " + getCheckMessage(MSG_LEX, "com.*", "java.util.StringTokenizer"),
202         };
203 
204         verify(checkConfig, getNonCompilablePath("InputCustomImportOrderSamePackage.java"),
205             expected);
206     }
207 
208     @Test
209     public void testWithoutLineSeparator() throws Exception {
210         final DefaultConfiguration checkConfig =
211                 createModuleConfig(CustomImportOrderCheck.class);
212         checkConfig.addAttribute("thirdPartyPackageRegExp", "org.");
213         checkConfig.addAttribute("separateLineBetweenGroups", "false");
214         checkConfig.addAttribute("customImportOrderRules",
215                 "STATIC###SAME_PACKAGE(3)");
216         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
217         final String[] expected = {
218             "5: " + getCheckMessage(MSG_LEX, "java.util.*", "java.util.StringTokenizer"),
219             "6: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.*"),
220             "7: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC, "java.awt.Button.ABORT"),
221             "8: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC, "javax.swing.WindowConstants.*"),
222             "9: " + getCheckMessage(MSG_LEX, "com.puppycrawl.tools.*", "java.util.StringTokenizer"),
223             "10: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME,
224                 "java.util.concurrent.AbstractExecutorService"),
225             "11: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STATIC, "java.io.File.createTempFile"),
226             "12: " + getCheckMessage(MSG_LEX, "com.*", "java.util.StringTokenizer"),
227         };
228 
229         verify(checkConfig, getNonCompilablePath("InputCustomImportOrderSamePackage.java"),
230             expected);
231     }
232 
233     @Test
234     public void testWithoutLineSeparator2() throws Exception {
235         final DefaultConfiguration checkConfig =
236                 createModuleConfig(CustomImportOrderCheck.class);
237         checkConfig.addAttribute("separateLineBetweenGroups", "false");
238         checkConfig.addAttribute("customImportOrderRules",
239                 "STATIC###STANDARD_JAVA_PACKAGE");
240         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
241         final String[] expected = {
242             "4: " + getCheckMessage(MSG_LEX, "java.io.File.createTempFile",
243                 "javax.swing.WindowConstants.*"),
244             "8: " + getCheckMessage(MSG_LEX, "com.*", "com.puppycrawl.tools.*"),
245         };
246 
247         verify(checkConfig, getPath("InputCustomImportOrder_NoSeparator.java"), expected);
248     }
249 
250     @Test
251     public void testNoValid() throws Exception {
252         final DefaultConfiguration checkConfig =
253                 createModuleConfig(CustomImportOrderCheck.class);
254         checkConfig.addAttribute("thirdPartyPackageRegExp", ".*");
255         checkConfig.addAttribute("specialImportsRegExp", "com.google");
256         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
257         checkConfig.addAttribute("customImportOrderRules",
258                 "STATIC###SPECIAL_IMPORTS###THIRD_PARTY_PACKAGE###STANDARD_JAVA_PACKAGE");
259         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
260 
261         verify(checkConfig, getPath("InputCustomImportOrderNoValid.java"), expected);
262     }
263 
264     @Test
265     public void testPossibleIndexOutOfBoundsException() throws Exception {
266         final DefaultConfiguration checkConfig =
267                 createModuleConfig(CustomImportOrderCheck.class);
268         checkConfig.addAttribute("thirdPartyPackageRegExp", ".*");
269         checkConfig.addAttribute("specialImportsRegExp", "com.google");
270         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
271         checkConfig.addAttribute("customImportOrderRules",
272                 "STATIC###SPECIAL_IMPORTS###THIRD_PARTY_PACKAGE###STANDARD_JAVA_PACKAGE");
273         final String[] expected = {
274             "5: " + getCheckMessage(MSG_NONGROUP_EXPECTED, THIRD, "org.w3c.dom.Node"),
275         };
276 
277         verify(checkConfig,
278             getPath("InputCustomImportOrderPossibleIndexOutOfBoundsException.java"), expected);
279     }
280 
281     @Test
282     public void testDefaultPackage2() throws Exception {
283         final DefaultConfiguration checkConfig =
284                 createModuleConfig(CustomImportOrderCheck.class);
285         checkConfig.addAttribute("thirdPartyPackageRegExp", "com|org");
286         checkConfig.addAttribute("customImportOrderRules",
287             "STATIC###SAME_PACKAGE(3)###THIRD_PARTY_PACKAGE###STANDARD_JAVA_PACKAGE");
288         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
289 
290         final String[] expected = {
291             "7: " + getCheckMessage(MSG_LEX, "java.awt.Button.ABORT",
292                 "java.io.File.createTempFile"),
293             "10: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.Button"),
294             "11: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.Frame"),
295             "12: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.Dialog"),
296             "13: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.awt.event.ActionEvent"),
297             "14: " + getCheckMessage(MSG_ORDER, STD, THIRD, "javax.swing.JComponent"),
298             "15: " + getCheckMessage(MSG_ORDER, STD, THIRD, "javax.swing.JTable"),
299             "16: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.File"),
300             "17: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.IOException"),
301             "18: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.InputStream"),
302             "19: " + getCheckMessage(MSG_ORDER, STD, THIRD, "java.io.Reader"),
303             "23: " + getCheckMessage(MSG_LEX, "com.google.common.*", "com.puppycrawl.tools.*"),
304         };
305 
306         verify(checkConfig, getNonCompilablePath("InputCustomImportOrderDefaultPackage.java"),
307             expected);
308     }
309 
310     @Test
311     public void testWithoutThirdPartyPackage() throws Exception {
312         final DefaultConfiguration checkConfig =
313                 createModuleConfig(CustomImportOrderCheck.class);
314         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
315         checkConfig.addAttribute("separateLineBetweenGroups", "true");
316         checkConfig.addAttribute("customImportOrderRules",
317                 "SAME_PACKAGE(3)###THIRD_PARTY_PACKAGE###STANDARD_JAVA_PACKAGE###STATIC");
318         final String[] expected = {
319             "4: " + getCheckMessage(MSG_LINE_SEPARATOR, "org.junit.*"),
320         };
321 
322         verify(checkConfig, getPath("InputCustomImportOrderThirdPartyPackage.java"), expected);
323     }
324 
325     @Test
326     public void testThirdPartyAndSpecialImports() throws Exception {
327         final DefaultConfiguration checkConfig =
328                 createModuleConfig(CustomImportOrderCheck.class);
329         checkConfig.addAttribute("specialImportsRegExp", "antlr.*");
330         checkConfig.addAttribute("customImportOrderRules",
331                 "SAME_PACKAGE(3)###THIRD_PARTY_PACKAGE###STATIC###SPECIAL_IMPORTS");
332         final String[] expected = {
333             "11: " + getCheckMessage(MSG_ORDER, THIRD, SPECIAL,
334                 "com.google.common.collect.HashMultimap"),
335         };
336 
337         verify(checkConfig, getPath("InputCustomImportOrderThirdPartyAndSpecial.java"), expected);
338     }
339 
340     @Test
341     public void testCompareImports() throws Exception {
342         final DefaultConfiguration checkConfig =
343             createModuleConfig(CustomImportOrderCheck.class);
344         checkConfig.addAttribute("specialImportsRegExp", "com");
345         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
346         checkConfig.addAttribute("customImportOrderRules",
347             "STANDARD_JAVA_PACKAGE###SPECIAL_IMPORTS");
348         final String[] expected = {
349             "4: " + getCheckMessage(MSG_LEX, "java.util.Map",
350                 "java.util.Map.Entry"),
351         };
352 
353         verify(checkConfig, getPath("InputCustomImportOrderCompareImports.java"), expected);
354     }
355 
356     @Test
357     public void testFindBetterPatternMatch() throws Exception {
358         final DefaultConfiguration checkConfig =
359             createModuleConfig(CustomImportOrderCheck.class);
360         checkConfig.addAttribute("standardPackageRegExp", "java|javax|event.*");
361         checkConfig.addAttribute("specialImportsRegExp", "An|lang|java|collect|event");
362         checkConfig.addAttribute("thirdPartyPackageRegExp", "com");
363         checkConfig.addAttribute("separateLineBetweenGroups", "true");
364         checkConfig.addAttribute("customImportOrderRules",
365             "STANDARD_JAVA_PACKAGE###SPECIAL_IMPORTS###THIRD_PARTY_PACKAGE");
366         final String[] expected = {
367             "8: " + getCheckMessage(MSG_ORDER, THIRD, SPECIAL,
368                 "com.google.common.annotations.Beta"),
369         };
370 
371         verify(checkConfig, getPath("InputCustomImportOrderFindBetterPatternMatch.java"), expected);
372     }
373 
374     @Test
375     public void testBeginTreeClear() throws Exception {
376         final DefaultConfiguration checkConfig =
377             createModuleConfig(CustomImportOrderCheck.class);
378         checkConfig.addAttribute("specialImportsRegExp", "com");
379         checkConfig.addAttribute("separateLineBetweenGroups", "false");
380         checkConfig.addAttribute("customImportOrderRules",
381             "STANDARD_JAVA_PACKAGE###SPECIAL_IMPORTS");
382         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
383         final Checker checker = createChecker(checkConfig);
384         final String fileName1 = getPath("InputCustomImportOrderImportsContainingJava.java");
385         final String fileName2 = getPath("InputCustomImportOrderNoValid.java");
386         final File[] files = {
387             new File(fileName1),
388             new File(fileName2),
389         };
390         verify(checker, files, fileName1, expected);
391     }
392 
393     @Test
394     public void testImportsContainingJava() throws Exception {
395         final DefaultConfiguration checkConfig = createModuleConfig(CustomImportOrderCheck.class);
396         checkConfig.addAttribute("customImportOrderRules",
397                 "STANDARD_JAVA_PACKAGE###THIRD_PARTY_PACKAGE");
398         final String[] expected = {
399             "5: " + getCheckMessage(MSG_LINE_SEPARATOR,
400                     "com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck"),
401         };
402 
403         verify(checkConfig, getPath("InputCustomImportOrderImportsContainingJava.java"), expected);
404     }
405 
406     @Test
407     public void testGetAcceptableTokens() {
408         final CustomImportOrderCheck testCheckObject =
409                 new CustomImportOrderCheck();
410         final int[] actual = testCheckObject.getAcceptableTokens();
411         final int[] expected = {
412             TokenTypes.IMPORT,
413             TokenTypes.STATIC_IMPORT,
414             TokenTypes.PACKAGE_DEF,
415         };
416 
417         assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
418     }
419 
420     @Test
421     // UT uses Reflection to avoid removing null-validation from static method,
422     // which is a candidate for utility method in the future
423     public void testGetFullImportIdent() throws Exception {
424         final Class<?> clazz = CustomImportOrderCheck.class;
425         final Object t = clazz.getConstructor().newInstance();
426         final Method method = clazz.getDeclaredMethod("getFullImportIdent", DetailAST.class);
427         method.setAccessible(true);
428         final Object actual = method.invoke(t, (DetailAST) null);
429 
430         final String expected = "";
431         assertEquals("Invalid getFullImportIdent result", expected, actual);
432     }
433 
434     @Test
435     public void testSamePackageDepth2() throws Exception {
436         final DefaultConfiguration checkConfig =
437                 createModuleConfig(CustomImportOrderCheck.class);
438         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "false");
439         checkConfig.addAttribute("separateLineBetweenGroups", "false");
440         checkConfig.addAttribute("customImportOrderRules",
441                 "SAME_PACKAGE(2)");
442         final String[] expected = {
443             "7: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.*"),
444             "8: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.List"),
445             "9: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.StringTokenizer"),
446             "10: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.*"),
447             "11: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME,
448                 "java.util.concurrent.AbstractExecutorService"),
449             "12: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME,
450                 "java.util.concurrent.locks.LockSupport"),
451             "13: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.regex.Pattern"),
452             "14: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.regex.Matcher"),
453             };
454 
455         verify(checkConfig, getNonCompilablePath("InputCustomImportOrderSamePackageDepth25.java"),
456             expected);
457     }
458 
459     @Test
460     public void testSamePackageDepth3() throws Exception {
461         final DefaultConfiguration checkConfig =
462                 createModuleConfig(CustomImportOrderCheck.class);
463         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "false");
464         checkConfig.addAttribute("separateLineBetweenGroups", "false");
465         checkConfig.addAttribute("customImportOrderRules",
466                 "SAME_PACKAGE(3)");
467         final String[] expected = {
468             "10: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME, "java.util.concurrent.*"),
469             "11: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME,
470                 "java.util.concurrent.AbstractExecutorService"),
471             "12: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME,
472                 "java.util.concurrent.locks.LockSupport"),
473             };
474 
475         verify(checkConfig, getNonCompilablePath("InputCustomImportOrderSamePackageDepth25.java"),
476             expected);
477     }
478 
479     @Test
480     public void testSamePackageDepth4() throws Exception {
481         final DefaultConfiguration checkConfig =
482                 createModuleConfig(CustomImportOrderCheck.class);
483         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "false");
484         checkConfig.addAttribute("separateLineBetweenGroups", "false");
485         checkConfig.addAttribute("customImportOrderRules",
486                 "SAME_PACKAGE(4)");
487         final String[] expected = {
488             "12: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SAME,
489                 "java.util.concurrent.locks.LockSupport"),
490             };
491 
492         verify(checkConfig, getNonCompilablePath("InputCustomImportOrderSamePackageDepth25.java"),
493             expected);
494     }
495 
496     @Test
497     public void testSamePackageDepthLongerThenActualPackage() throws Exception {
498         final DefaultConfiguration checkConfig =
499                 createModuleConfig(CustomImportOrderCheck.class);
500         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "false");
501         checkConfig.addAttribute("separateLineBetweenGroups", "false");
502         checkConfig.addAttribute("customImportOrderRules",
503                 "SAME_PACKAGE(5)");
504         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
505 
506         verify(checkConfig, getNonCompilablePath("InputCustomImportOrderSamePackageDepth25.java"),
507                 expected);
508     }
509 
510     @Test
511     public void testSamePackageDepthNegative() throws Exception {
512         final DefaultConfiguration checkConfig =
513                 createModuleConfig(CustomImportOrderCheck.class);
514         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "false");
515         checkConfig.addAttribute("separateLineBetweenGroups", "false");
516         checkConfig.addAttribute("customImportOrderRules",
517                 "SAME_PACKAGE(-1)");
518 
519         try {
520             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
521 
522             verify(checkConfig, getPath("InputCustomImportOrderDefault.java"), expected);
523             fail("exception expected");
524         }
525         catch (CheckstyleException ex) {
526             final String messageStart =
527                 "cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
528                     + "Cannot set property 'customImportOrderRules' to "
529                     + "'SAME_PACKAGE(-1)' in module";
530             assertTrue("Invalid exception message, should start with: " + messageStart,
531                 ex.getMessage().startsWith(messageStart));
532         }
533     }
534 
535     @Test
536     public void testSamePackageDepthZero() throws Exception {
537         final DefaultConfiguration checkConfig =
538                 createModuleConfig(CustomImportOrderCheck.class);
539         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "false");
540         checkConfig.addAttribute("separateLineBetweenGroups", "false");
541         checkConfig.addAttribute("customImportOrderRules",
542                 "SAME_PACKAGE(0)");
543 
544         try {
545             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
546 
547             verify(checkConfig, getPath("InputCustomImportOrderDefault.java"), expected);
548             fail("exception expected");
549         }
550         catch (CheckstyleException ex) {
551             final String messageStart =
552                 "cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
553                     + "Cannot set property 'customImportOrderRules' to "
554                     + "'SAME_PACKAGE(0)' in module";
555             assertTrue("Invalid exception message, should start with: " + messageStart,
556                 ex.getMessage().startsWith(messageStart));
557         }
558     }
559 
560     @Test
561     public void testUnsupportedRule() throws Exception {
562         final DefaultConfiguration checkConfig =
563                 createModuleConfig(CustomImportOrderCheck.class);
564         // #AAA##BBBB###CCCC####DDDD
565         checkConfig.addAttribute("customImportOrderRules", "SAME_PACKAGE(3)###UNSUPPORTED_RULE");
566         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
567 
568         try {
569             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
570 
571             verify(checkConfig, getPath("InputCustomImportOrderDefault.java"), expected);
572             fail("exception expected");
573         }
574         catch (CheckstyleException ex) {
575             final String messageStart =
576                 "cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
577                     + "Cannot set property 'customImportOrderRules' to "
578                     + "'SAME_PACKAGE(3)###UNSUPPORTED_RULE' in module";
579             assertTrue("Invalid exception message, should start with: " + messageStart,
580                 ex.getMessage().startsWith(messageStart));
581         }
582     }
583 
584     @Test
585     public void testSamePackageDepthNotInt() throws Exception {
586         final DefaultConfiguration checkConfig =
587                 createModuleConfig(CustomImportOrderCheck.class);
588         checkConfig.addAttribute("customImportOrderRules", "SAME_PACKAGE(INT_IS_REQUIRED_HERE)");
589         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
590 
591         try {
592             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
593 
594             verify(checkConfig, getPath("InputCustomImportOrderDefault.java"), expected);
595             fail("exception expected");
596         }
597         catch (CheckstyleException ex) {
598             final String messageStart =
599                 "cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
600                     + "Cannot set property 'customImportOrderRules' to "
601                     + "'SAME_PACKAGE(INT_IS_REQUIRED_HERE)' in module";
602             assertTrue("Invalid exception message, should start with: " + messageStart,
603                 ex.getMessage().startsWith(messageStart));
604         }
605     }
606 
607     @Test
608     public void testNoImports() throws Exception {
609         final DefaultConfiguration checkConfig =
610                 createModuleConfig(CustomImportOrderCheck.class);
611         checkConfig.addAttribute("customImportOrderRules", "SAME_PACKAGE(3)");
612         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
613 
614         verify(checkConfig, getPath("InputCustomImportOrder_NoImports.java"), expected);
615     }
616 
617     @Test
618     public void testDefaultConfiguration() throws Exception {
619         final DefaultConfiguration checkConfig =
620             createModuleConfig(CustomImportOrderCheck.class);
621 
622         createChecker(checkConfig);
623         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
624         verify(checkConfig, getPath("InputCustomImportOrderDefault.java"), expected);
625     }
626 
627     @Test
628     public void testRulesWithOverlappingPatterns() throws Exception {
629         final DefaultConfiguration checkConfig =
630             createModuleConfig(CustomImportOrderCheck.class);
631         checkConfig.addAttribute("customImportOrderRules",
632                 "THIRD_PARTY_PACKAGE###SAME_PACKAGE(6)###STANDARD_JAVA_PACKAGE###SPECIAL_IMPORTS");
633         checkConfig.addAttribute("standardPackageRegExp", "com.puppycrawl.tools.*Check$");
634         checkConfig.addAttribute("specialImportsRegExp", "com.puppycrawl.tools.*Tag*");
635         checkConfig.addAttribute("thirdPartyPackageRegExp",
636             "com.puppycrawl.tools.checkstyle.checks.javadoc.*Javadoc*");
637         final String[] expected = {
638             "9: " + getCheckMessage(MSG_ORDER, THIRD, STD,
639                 "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocNodeImpl"),
640             "21: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD,
641                 "com.puppycrawl.tools.checkstyle.checks.javadoc.WriteTagCheck"),
642             "25: " + getCheckMessage(MSG_NONGROUP_EXPECTED, SPECIAL,
643                 "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocTag"),
644             "26: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD,
645                 "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck"),
646             "27: " + getCheckMessage(MSG_NONGROUP_EXPECTED, STD,
647                 "com.puppycrawl.tools.checkstyle.checks.javadoc.NonEmptyAtclauseDescriptionCheck"),
648             };
649 
650         createChecker(checkConfig);
651         verify(checkConfig, getPath("InputCustomImportOrder_OverlappingPatterns.java"), expected);
652     }
653 
654     @Test
655     public void testMultiplePatternMatchesSecondPatternIsLonger() throws Exception {
656         final DefaultConfiguration checkConfig =
657             createModuleConfig(CustomImportOrderCheck.class);
658         checkConfig.addAttribute("customImportOrderRules",
659                 "SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE");
660         checkConfig.addAttribute("specialImportsRegExp", "org");
661         checkConfig.addAttribute("standardPackageRegExp", "junit");
662 
663         createChecker(checkConfig);
664         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
665         verify(checkConfig, getPath("InputCustomImportOrder_MultiplePatternMatches.java"),
666             expected);
667     }
668 
669     @Test
670     public void testMultiplePatternMatchesFirstPatternHasLaterPosition() throws Exception {
671         final DefaultConfiguration checkConfig =
672             createModuleConfig(CustomImportOrderCheck.class);
673         checkConfig.addAttribute("customImportOrderRules",
674                 "SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE");
675         checkConfig.addAttribute("specialImportsRegExp", "Test");
676         checkConfig.addAttribute("standardPackageRegExp", "unit");
677 
678         createChecker(checkConfig);
679         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
680         verify(checkConfig, getPath("InputCustomImportOrder_MultiplePatternMatches.java"),
681             expected);
682     }
683 
684     @Test
685     public void testMultiplePatternMatchesFirstPatternHasEarlierPosition() throws Exception {
686         final DefaultConfiguration checkConfig =
687             createModuleConfig(CustomImportOrderCheck.class);
688         checkConfig.addAttribute("customImportOrderRules",
689                 "SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE");
690         checkConfig.addAttribute("specialImportsRegExp", "unit");
691         checkConfig.addAttribute("standardPackageRegExp", "Test");
692 
693         createChecker(checkConfig);
694         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
695         verify(checkConfig, getPath("InputCustomImportOrder_MultiplePatternMatches.java"),
696             expected);
697     }
698 
699     @Test
700     public void testNoPackage() throws Exception {
701         final DefaultConfiguration checkConfig =
702             createModuleConfig(CustomImportOrderCheck.class);
703         checkConfig.addAttribute("customImportOrderRules",
704                 "STATIC###THIRD_PARTY_PACKAGE");
705         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
706         checkConfig.addAttribute("separateLineBetweenGroups", "true");
707 
708         createChecker(checkConfig);
709         final String[] expected = {
710             "4: " + getCheckMessage(MSG_LINE_SEPARATOR, "java.util.*"),
711         };
712         verify(checkConfig, getPath("InputCustomImportOrderNoPackage.java"),
713             expected);
714     }
715 
716     @Test
717     public void testNoPackage2() throws Exception {
718         final DefaultConfiguration checkConfig =
719             createModuleConfig(CustomImportOrderCheck.class);
720         checkConfig.addAttribute("customImportOrderRules",
721                 "STATIC###THIRD_PARTY_PACKAGE");
722         checkConfig.addAttribute("sortImportsInGroupAlphabetically", "true");
723         checkConfig.addAttribute("separateLineBetweenGroups", "true");
724 
725         createChecker(checkConfig);
726         final String[] expected = {
727             "3: " + getCheckMessage(MSG_LINE_SEPARATOR,
728                 "com.puppycrawl.tools.checkstyle.utils.AnnotationUtility.containsAnnotation"),
729             "7: " + getCheckMessage(MSG_LINE_SEPARATOR,
730                 "com.sun.accessibility.internal.resources.*"),
731             "11: " + getCheckMessage(MSG_LINE_SEPARATOR, "java.util.Arrays"),
732             "19: " + getCheckMessage(MSG_LINE_SEPARATOR,
733                 "org.apache.commons.beanutils.converters.ArrayConverter"),
734         };
735         verify(checkConfig, getPath("InputCustomImportOrderNoPackage2.java"),
736             expected);
737     }
738 }