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