View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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 com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck.MSG_SEPARATED_IN_GROUP;
28  
29  import org.junit.jupiter.api.Test;
30  
31  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
32  
33  public class CustomImportOrderCheckExamplesTest extends AbstractExamplesModuleTestSupport {
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/imports/customimportorder";
37      }
38  
39      @Test
40      public void testExample1() throws Exception {
41          final String[] expected = {
42  
43          };
44  
45          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
46      }
47  
48      @Test
49      public void testExample2() throws Exception {
50          final String[] expected = {
51              "17:1: " + getCheckMessage(MSG_ORDER,
52                      "STATIC", "THIRD_PARTY_PACKAGE", "java.io.File.separator"),
53          };
54  
55          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
56      }
57  
58      @Test
59      public void testExample3() throws Exception {
60          final String[] expected = {
61              "19:1: " + getCheckMessage(MSG_LINE_SEPARATOR, "javax.net.*"),
62              "21:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP,
63                      "org.apache.commons.io.FileUtils"),
64          };
65  
66          verifyWithInlineConfigParser(getPath("Example3.java"), expected);
67      }
68  
69      @Test
70      public void testExample4() throws Exception {
71          final String[] expected = {
72              "21:1: " + getCheckMessage(MSG_NONGROUP_IMPORT,
73                      "org.apache.commons.io.FileUtils"),
74              "22:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
75                      "com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck"),
76          };
77  
78          verifyWithInlineConfigParser(getPath("Example4.java"), expected);
79      }
80  
81      @Test
82      public void testExample5() throws Exception {
83          final String[] expected = {
84              "24:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED, "SPECIAL_IMPORTS",
85                      "org.apache.commons.io.FileUtils"),
86          };
87  
88          verifyWithInlineConfigParser(getPath("Example5.java"), expected);
89      }
90  
91      @Test
92      public void testExample6() throws Exception {
93          final String[] expected = {
94              "23:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
95                      "org.apache.commons.io.FileUtils"),
96              "24:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
97                      "com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck"),
98          };
99  
100         verifyWithInlineConfigParser(getPath("Example6.java"), expected);
101     }
102 
103     @Test
104     public void testExample7() throws Exception {
105         final String[] expected = {
106             "20:1: " + getCheckMessage(MSG_LEX,
107                     "java.io.File.separator", "java.util.Collections.*"),
108         };
109 
110         verifyWithInlineConfigParser(getPath("Example7.java"), expected);
111     }
112 
113     @Test
114     public void testExample8() throws Exception {
115         final String[] expected = {
116             "19:1: " + getCheckMessage(MSG_LEX,
117                     "java.io.File.separator",
118                     "java.util.Collections.*"),
119             "23:1: " + getCheckMessage(MSG_LINE_SEPARATOR,
120                     "org.apache.commons.io.FileUtils"),
121         };
122 
123         verifyWithInlineConfigParser(getPath("Example8.java"), expected);
124     }
125 
126     @Test
127     public void testExample9() throws Exception {
128         final String[] expected = {
129             "25:1: " + getCheckMessage(MSG_ORDER,
130                     "THIRD_PARTY_PACKAGE",
131                     "SPECIAL_IMPORTS",
132                     "com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck"),
133             "26:1: " + getCheckMessage(MSG_ORDER, "THIRD_PARTY_PACKAGE",
134                     "SPECIAL_IMPORTS",
135                     "com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck"),
136         };
137 
138         verifyWithInlineConfigParser(getPath("Example9.java"), expected);
139     }
140 
141     @Test
142     public void testExample10() throws Exception {
143         final String[] expected = {
144             "22:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED,
145                     "STANDARD_JAVA_PACKAGE", "java.time.*"),
146             "24:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED,
147                     "SPECIAL_IMPORTS", "javax.net.*"),
148             "26:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED,
149                     "THIRD_PARTY_PACKAGE", "org.apache.commons.io.FileUtils"),
150             "28:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED,
151                     "THIRD_PARTY_PACKAGE",
152                     "com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck"),
153             "29:1: " + getCheckMessage(MSG_NONGROUP_EXPECTED,
154                     "THIRD_PARTY_PACKAGE",
155                     "com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck"),
156         };
157 
158         verifyWithInlineConfigParser(getPath("Example10.java"), expected);
159     }
160 
161     @Test
162     public void testExample11() throws Exception {
163         final String[] expected = {
164             "19:1: " + getCheckMessage(MSG_SEPARATED_IN_GROUP, "org.apache.commons.io.FileUtils"),
165         };
166 
167         verifyWithInlineConfigParser(getPath("Example11.java"), expected);
168     }
169 
170     @Test
171     public void testExample12() throws Exception {
172         final String[] expected = {
173             "19:1: " + getCheckMessage(MSG_ORDER,
174                     "STANDARD_JAVA_PACKAGE", "SAME_PACKAGE", "java.time.*"),
175             "20:1: " + getCheckMessage(MSG_ORDER,
176                     "STANDARD_JAVA_PACKAGE", "SAME_PACKAGE", "javax.net.*"),
177         };
178 
179         verifyWithInlineConfigParser(getPath("Example12.java"), expected);
180     }
181 
182     @Test
183     public void testExample13() throws Exception {
184         final String[] expected = {
185 
186         };
187 
188         verifyWithInlineConfigParser(getPath("Example13.java"), expected);
189     }
190 
191     @Test
192     public void testExample14() throws Exception {
193         final String[] expected = {
194             "17:1: " + getCheckMessage(MSG_LEX,
195                     "java.awt.Frame", "java.awt.color.ColorSpace"),
196         };
197 
198         verifyWithInlineConfigParser(getPath("Example14.java"), expected);
199     }
200 
201     @Test
202     public void testExample15() throws Exception {
203         final String[] expected = {
204             "23:1: " + getCheckMessage(MSG_ORDER,
205                     "THIRD_PARTY_PACKAGE",
206                     "SPECIAL_IMPORTS",
207                     "com.google.common.annotations.GwtCompatible"),
208         };
209 
210         verifyWithInlineConfigParser(getPath("Example15.java"), expected);
211     }
212 }