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.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  
30  public class IllegalImportCheckTest extends AbstractModuleTestSupport {
31  
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/imports/illegalimport";
35      }
36  
37      @Test
38      public void testGetRequiredTokens() {
39          final IllegalImportCheck checkObj = new IllegalImportCheck();
40          final int[] expected = {
41              TokenTypes.IMPORT,
42              TokenTypes.STATIC_IMPORT,
43              TokenTypes.MODULE_IMPORT,
44          };
45          assertWithMessage("Default required tokens are invalid")
46              .that(checkObj.getRequiredTokens())
47              .isEqualTo(expected);
48      }
49  
50      @Test
51      public void testWithSupplied()
52              throws Exception {
53          final String[] expected = {
54              "13:1: " + getCheckMessage(MSG_KEY, "java.io.*"),
55              "27:1: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
56              "31:1: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
57          };
58          verifyWithInlineConfigParser(
59                  getPath("InputIllegalImportDefault1.java"), expected);
60      }
61  
62      @Test
63      public void testWithDefault()
64              throws Exception {
65          final String[] expected = {};
66          verifyWithInlineConfigParser(
67                  getPath("InputIllegalImportDefault2.java"), expected);
68      }
69  
70      @Test
71      public void testCustomSunPackageWithRegexp()
72              throws Exception {
73          final String[] expected = {
74              "18:1: " + getCheckMessage(MSG_KEY, "sun.reflect.*"),
75          };
76          verifyWithInlineConfigParser(
77                  getNonCompilablePath("InputIllegalImportDefault.java"), expected);
78      }
79  
80      @Test
81      public void testGetAcceptableTokens() {
82          final IllegalImportCheck testCheckObject =
83                  new IllegalImportCheck();
84          final int[] actual = testCheckObject.getAcceptableTokens();
85          final int[] expected = {
86              TokenTypes.IMPORT,
87              TokenTypes.STATIC_IMPORT,
88              TokenTypes.MODULE_IMPORT,
89          };
90  
91          assertWithMessage("Default acceptable tokens are invalid")
92              .that(actual)
93              .isEqualTo(expected);
94      }
95  
96      @Test
97      public void testIllegalClasses()
98              throws Exception {
99          final String[] expected = {
100             "15:1: " + getCheckMessage(MSG_KEY, "java.sql.Connection"),
101             "19:1: " + getCheckMessage(MSG_KEY, "org.junit.jupiter.api.*"),
102             "32:1: " + getCheckMessage(MSG_KEY, "org.junit.jupiter.api.*"),
103         };
104         verifyWithInlineConfigParser(
105                 getPath("InputIllegalImportDefault3.java"), expected);
106     }
107 
108     @Test
109     public void testIllegalClassesStarImport()
110             throws Exception {
111         final String[] expected = {
112             "13:1: " + getCheckMessage(MSG_KEY, "java.io.*"),
113             "19:1: " + getCheckMessage(MSG_KEY, "org.junit.jupiter.api.*"),
114             "32:1: " + getCheckMessage(MSG_KEY, "org.junit.jupiter.api.*"),
115         };
116         verifyWithInlineConfigParser(
117                 getPath("InputIllegalImportDefault4.java"), expected);
118     }
119 
120     @Test
121     public void testIllegalPackagesRegularExpression()
122             throws Exception {
123         final String[] expected = {
124             "16:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
125             "17:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
126             "20:1: " + getCheckMessage(MSG_KEY, "java.util.Enumeration"),
127             "21:1: " + getCheckMessage(MSG_KEY, "java.util.Arrays"),
128             "38:1: " + getCheckMessage(MSG_KEY, "java.util.Date"),
129             "39:1: " + getCheckMessage(MSG_KEY, "java.util.Calendar"),
130             "40:1: " + getCheckMessage(MSG_KEY, "java.util.BitSet"),
131         };
132         verifyWithInlineConfigParser(
133                 getPath("InputIllegalImportDefault5.java"), expected);
134     }
135 
136     @Test
137     public void testIllegalClassesRegularExpression()
138             throws Exception {
139         final String[] expected = {
140             "16:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
141             "17:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
142             "21:1: " + getCheckMessage(MSG_KEY, "java.util.Arrays"),
143         };
144         verifyWithInlineConfigParser(
145                 getPath("InputIllegalImportDefault6.java"), expected);
146     }
147 
148     @Test
149     public void testIllegalPackagesAndClassesRegularExpression()
150             throws Exception {
151         final String[] expected = {
152             "16:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
153             "17:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
154             "20:1: " + getCheckMessage(MSG_KEY, "java.util.Enumeration"),
155             "21:1: " + getCheckMessage(MSG_KEY, "java.util.Arrays"),
156             "34:1: " + getCheckMessage(MSG_KEY, "java.awt.Component"),
157             "35:1: " + getCheckMessage(MSG_KEY, "java.awt.Graphics2D"),
158             "36:1: " + getCheckMessage(MSG_KEY, "java.awt.HeadlessException"),
159             "37:1: " + getCheckMessage(MSG_KEY, "java.awt.Label"),
160             "38:1: " + getCheckMessage(MSG_KEY, "java.util.Date"),
161             "39:1: " + getCheckMessage(MSG_KEY, "java.util.Calendar"),
162             "40:1: " + getCheckMessage(MSG_KEY, "java.util.BitSet"),
163         };
164         verifyWithInlineConfigParser(
165                 getPath("InputIllegalImportDefault7.java"), expected);
166     }
167 
168     @Test
169     public void testIllegalModulesNoRegex() throws Exception {
170         final String[] expected = {
171             "14:1: " + getCheckMessage(MSG_KEY, "java.base"),
172             "17:1: " + getCheckMessage(MSG_KEY, "java.logging"),
173             "20:1: " + getCheckMessage(MSG_KEY, "java.sql.Connection"),
174         };
175         verifyWithInlineConfigParser(
176                 getNonCompilablePath("InputIllegalImportModuleNoRegex.java"), expected);
177     }
178 
179     @Test
180     public void testIllegalModulesWithRegex() throws Exception {
181         final String[] expected = {
182             "14:1: " + getCheckMessage(MSG_KEY, "java.base"),
183             "17:1: " + getCheckMessage(MSG_KEY, "java.logging"),
184             "20:1: " + getCheckMessage(MSG_KEY, "java.sql.Connection"),
185             "21:1: " + getCheckMessage(MSG_KEY, "java.sql.Driver"),
186         };
187         verifyWithInlineConfigParser(
188                 getNonCompilablePath("InputIllegalImportModuleWithRegex.java"), expected);
189     }
190 
191 }