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.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.imports.ModuleImportOrderCheck.MSG_ORDERING_LEX;
24  import static com.puppycrawl.tools.checkstyle.checks.imports.ModuleImportOrderCheck.MSG_POSITION;
25  import static com.puppycrawl.tools.checkstyle.checks.imports.ModuleImportOrderCheck.MSG_SEPARATION;
26  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
27  
28  import java.util.List;
29  
30  import org.junit.jupiter.api.Test;
31  
32  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
33  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
34  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
36  
37  public class ModuleImportOrderCheckTest extends AbstractModuleTestSupport {
38  
39      @Override
40      public String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/imports/moduleimportorder";
42      }
43  
44      @Test
45      public void testGetTokens() {
46          final ModuleImportOrderCheck checkObj = new ModuleImportOrderCheck();
47          final int[] expected = {
48              TokenTypes.IMPORT,
49              TokenTypes.STATIC_IMPORT,
50              TokenTypes.MODULE_IMPORT,
51          };
52          assertWithMessage("Default tokens differs from expected")
53              .that(checkObj.getDefaultTokens())
54              .isEqualTo(expected);
55          assertWithMessage("Acceptable tokens differs from expected")
56              .that(checkObj.getAcceptableTokens())
57              .isEqualTo(expected);
58          assertWithMessage("Required tokens differs from expected")
59              .that(checkObj.getRequiredTokens())
60              .isEqualTo(expected);
61      }
62  
63      @Test
64      public void testDefault() throws Exception {
65          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
66  
67          verifyWithInlineConfigParser(
68                  getNonCompilablePath("InputModuleImportOrderDefault.java"), expected);
69      }
70  
71      @Test
72      public void testTopViolation() throws Exception {
73          final String[] expected = {
74              "15:1: " + getCheckMessage(MSG_POSITION, "java.desktop"),
75              "17:1: " + getCheckMessage(MSG_POSITION, "java.sql"),
76          };
77  
78          verifyWithInlineConfigParser(
79                  getNonCompilablePath("InputModuleImportOrderTopViolation.java"), expected);
80      }
81  
82      @Test
83      public void testLexOrder() throws Exception {
84          final String[] expected = {
85              "15:1: " + getCheckMessage(MSG_ORDERING_LEX, "java.desktop", "java.sql"),
86          };
87  
88          verifyWithInlineConfigParser(
89                  getNonCompilablePath("InputModuleImportOrderLexOrder.java"), expected);
90      }
91  
92      @Test
93      public void testBottom() throws Exception {
94          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
95  
96          verifyWithInlineConfigParser(
97                  getNonCompilablePath("InputModuleImportOrderBottom.java"), expected);
98      }
99  
100     @Test
101     public void testBottomViolation() throws Exception {
102         final String[] expected = {
103             "14:1: " + getCheckMessage(MSG_POSITION, "java.desktop"),
104         };
105 
106         verifyWithInlineConfigParser(
107                 getNonCompilablePath("InputModuleImportOrderBottomViolation.java"), expected);
108     }
109 
110     @Test
111     public void testBottomAdjacentViolation() throws Exception {
112         final String[] expected = {
113             "14:1: " + getCheckMessage(MSG_POSITION, "java.desktop"),
114             "16:1: " + getCheckMessage(MSG_POSITION, "java.sql"),
115         };
116 
117         verifyWithInlineConfigParser(
118                 getNonCompilablePath("InputModuleImportOrderBottomAdjacentViolation.java"),
119             expected);
120     }
121 
122     @Test
123     public void testSeparatedTop() throws Exception {
124         final String[] expected = {
125             "15:1: " + getCheckMessage(MSG_SEPARATION, "java.util.List"),
126         };
127 
128         verifyWithInlineConfigParser(
129                 getNonCompilablePath("InputModuleImportOrderSeparatedTop.java"), expected);
130     }
131 
132     @Test
133     public void testSeparatedTopClean() throws Exception {
134         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
135 
136         verifyWithInlineConfigParser(
137                 getNonCompilablePath("InputModuleImportOrderSeparatedTopClean.java"), expected);
138     }
139 
140     @Test
141     public void testSeparatedBottom() throws Exception {
142         final String[] expected = {
143             "14:1: " + getCheckMessage(MSG_SEPARATION, "java.desktop"),
144         };
145 
146         verifyWithInlineConfigParser(
147                 getNonCompilablePath("InputModuleImportOrderSeparatedBottom.java"), expected);
148     }
149 
150     @Test
151     public void testSeparatedMisplaced() throws Exception {
152         final String[] expected = {
153             "15:1: " + getCheckMessage(MSG_POSITION, "java.desktop"),
154         };
155 
156         verifyWithInlineConfigParser(
157                 getNonCompilablePath("InputModuleImportOrderSeparatedMisplaced.java"), expected);
158     }
159 
160     @Test
161     public void testOnlyModules() throws Exception {
162         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
163 
164         verifyWithInlineConfigParser(
165                 getNonCompilablePath("InputModuleImportOrderOnlyModules.java"), expected);
166     }
167 
168     @Test
169     public void testOnlyModulesBottom() throws Exception {
170         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
171 
172         verifyWithInlineConfigParser(
173                 getNonCompilablePath("InputModuleImportOrderOnlyModulesBottom.java"), expected);
174     }
175 
176     @Test
177     public void testNoModules() throws Exception {
178         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
179 
180         verifyWithInlineConfigParser(
181                 getPath("InputModuleImportOrderNoModules.java"), expected);
182     }
183 
184     @Test
185     public void testCompactDefault() throws Exception {
186         final String[] expected = {
187             "13:1: " + getCheckMessage(MSG_POSITION, "java.desktop"),
188         };
189 
190         verifyWithInlineConfigParser(
191                 getNonCompilablePath("compact/InputModuleImportOrderCompactDefault.java"),
192             expected);
193     }
194 
195     @Test
196     public void testCompactNonDefault() throws Exception {
197         final String[] expected = {
198             "12:1: " + getCheckMessage(MSG_SEPARATION, "java.desktop"),
199         };
200 
201         verifyWithInlineConfigParser(
202                 getNonCompilablePath("compact/InputModuleImportOrderCompactNonDefault.java"),
203             expected);
204     }
205 
206     @Test
207     public void testStateIsClearedBetweenFiles() throws Exception {
208         final String filePath1 = getNonCompilablePath("InputModuleImportOrderStateOne.java");
209         final String filePath2 = getNonCompilablePath("InputModuleImportOrderStateTwo.java");
210         final List<String> expectedFromFile1 = List.of();
211         final List<String> expectedFromFile2 = List.of();
212 
213         verifyWithInlineConfigParser(filePath1, filePath2, expectedFromFile1, expectedFromFile2);
214     }
215 
216     @Test
217     public void testDuplicateModuleImports() throws Exception {
218         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
219 
220         verifyWithInlineConfigParser(
221                 getNonCompilablePath("InputModuleImportOrderDuplicate.java"), expected);
222     }
223 
224     @Test
225     public void testSeparatedPositionViolation() throws Exception {
226         final String[] expected = {
227             "15:1: " + getCheckMessage(MSG_POSITION, "java.desktop"),
228         };
229 
230         verifyWithInlineConfigParser(
231                 getNonCompilablePath("InputModuleImportOrderSeparatedPositionViolation.java"),
232             expected);
233     }
234 
235     @Test
236     public void testMultilineModuleImport() throws Exception {
237         final String[] expected = {
238             "15:1: " + getCheckMessage(MSG_SEPARATION, "java.util.List"),
239         };
240 
241         verifyWithInlineConfigParser(
242                 getNonCompilablePath("InputModuleImportOrderMultiline.java"), expected);
243     }
244 
245     @Test
246     public void testOptionWhitespace() throws Exception {
247         final String[] expected = {
248             "16:1: " + getCheckMessage(MSG_POSITION, "java.desktop"),
249         };
250 
251         verifyWithInlineXmlConfig(
252                 getNonCompilablePath("InputModuleImportOrderOptionWhitespace.java"), expected);
253     }
254 
255     @Test
256     public void testInvalidOption() {
257         final CheckstyleException exc = getExpectedThrowable(CheckstyleException.class, () -> {
258             final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
259 
260             verifyWithInlineConfigParser(
261                     getPath("InputModuleImportOrderInvalidOption.java"), expected);
262         });
263         assertWithMessage("Invalid exception message")
264             .that(exc.getMessage())
265             .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
266                     + "cannot initialize module com.puppycrawl.tools.checkstyle.checks"
267                     + ".imports.ModuleImportOrderCheck");
268     }
269 
270 }