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 java.io.File;
23  
24  import org.junit.jupiter.api.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
27  
28  public class ImportControlCheckExamplesTest extends AbstractExamplesModuleTestSupport {
29  
30      @Override
31      public String getPackageLocation() {
32          return "com/puppycrawl/tools/checkstyle/checks/imports/importcontrol";
33      }
34  
35      @Test
36      public void testExample1() throws Exception {
37          final String[] expected = {
38              "13:1: " + getCheckMessage(
39                      ImportControlCheck.MSG_DISALLOWED, "java.awt.Image"),
40              "14:1: " + getCheckMessage(
41                      ImportControlCheck.MSG_DISALLOWED, "java.lang.ref.SoftReference"),
42              "16:1: " + getCheckMessage(
43                      ImportControlCheck.MSG_DISALLOWED, "java.io.File"),
44              "18:1: " + getCheckMessage(
45                      ImportControlCheck.MSG_DISALLOWED, "java.util.Date"),
46              "19:1: " + getCheckMessage(
47                      ImportControlCheck.MSG_DISALLOWED, "java.util.List"),
48          };
49  
50          System.setProperty("config.folder", "src/xdocs-examples/resources/"
51              + getPackageLocation());
52          verifyWithInlineXmlConfig(getPath("Example1.java"), expected);
53      }
54  
55      @Test
56      public void testExample2() throws Exception {
57          final String[] expected = {};
58  
59          System.setProperty("config.folder", "src/xdocs-examples/resources/"
60              + getPackageLocation());
61          verifyWithInlineXmlConfig(getPath("Example2.java"), expected);
62      }
63  
64      @Test
65      public void testExample3() throws Exception {
66          final String[] expected = {
67              "13:1: " + getCheckMessage(
68                      ImportControlCheck.MSG_DISALLOWED, "java.awt.Image"),
69              "16:1: " + getCheckMessage(
70                      ImportControlCheck.MSG_DISALLOWED, "java.io.File"),
71              "17:1: " + getCheckMessage(
72                      ImportControlCheck.MSG_DISALLOWED, "java.io.FileReader"),
73              "18:1: " + getCheckMessage(
74                      ImportControlCheck.MSG_DISALLOWED, "java.util.Date"),
75              "19:1: " + getCheckMessage(
76                      ImportControlCheck.MSG_DISALLOWED, "java.util.List"),
77          };
78  
79          final String examplePath = new File("src/" + getResourceLocation()
80                  + "/resources/" + getPackageLocation() + "/"
81                  + "filters/Example3.java").getCanonicalPath();
82  
83          System.setProperty("config.folder", "src/xdocs-examples/resources/"
84              + getPackageLocation());
85          verifyWithInlineXmlConfig(examplePath, expected);
86      }
87  
88      @Test
89      public void testUseCase1() throws Exception {
90          final String[] expected = {
91              "14:1: " + getCheckMessage(
92                      ImportControlCheck.MSG_DISALLOWED,
93                  "com.puppycrawl.tools.checkstyle.checks.TranslationCheck"),
94              "18:1: " + getCheckMessage(
95                      ImportControlCheck.MSG_DISALLOWED, "javax.swing.ActionMap"),
96          };
97  
98          final String examplePath = new File("src/" + getResourceLocation()
99                  + "/resources/" + getPackageLocation() + "/"
100                 + "newdomain/dao/UseCase1.java").getCanonicalPath();
101 
102         System.setProperty("config.folder", "src/xdocs-examples/resources/"
103             + getPackageLocation());
104         verifyWithInlineXmlConfig(examplePath, expected);
105     }
106 
107     @Test
108     public void testUseCase2() throws Exception {
109         final String[] expected = {
110             "14:1: " + getCheckMessage(
111                 ImportControlCheck.MSG_DISALLOWED, "java.awt.Image"),
112             "15:1: " + getCheckMessage(
113                 ImportControlCheck.MSG_DISALLOWED, "java.io.File"),
114         };
115 
116         System.setProperty("config.folder", "src/xdocs-examples/resources/"
117             + getPackageLocation());
118         verifyWithInlineXmlConfig(getPath("UseCase2.java"), expected);
119     }
120 
121     @Test
122     public void testUseCase3() throws Exception {
123         final String[] expected = {
124             "16:1: " + getCheckMessage(
125                     ImportControlCheck.MSG_DISALLOWED, "java.awt.Image"),
126             "17:1: " + getCheckMessage(
127                     ImportControlCheck.MSG_DISALLOWED, "java.util.Date"),
128             "18:1: " + getCheckMessage(
129                     ImportControlCheck.MSG_DISALLOWED, "java.util.List"),
130             "20:1: " + getCheckMessage(
131                     ImportControlCheck.MSG_DISALLOWED, "java.util.stream.Collectors"),
132             "21:1: " + getCheckMessage(
133                     ImportControlCheck.MSG_DISALLOWED, "java.util.stream.Stream"),
134             "23:1: " + getCheckMessage(
135                     ImportControlCheck.MSG_DISALLOWED, "sun.misc.Signal"),
136 
137         };
138 
139         final String examplePath = new File("src/" + getResourceLocation()
140                 + "/resources/" + getPackageLocation() + "/"
141                 + "someImports/UseCase3.java").getCanonicalPath();
142 
143         System.setProperty("config.folder", "src/xdocs-examples/resources/"
144             + getPackageLocation());
145         verifyWithInlineXmlConfig(examplePath, expected);
146     }
147 
148     @Test
149     public void testUseCase4() throws Exception {
150         final String[] expected = {
151             "16:1: " + getCheckMessage(
152                     ImportControlCheck.MSG_DISALLOWED, "java.awt.Image"),
153             "17:1: " + getCheckMessage(
154                     ImportControlCheck.MSG_DISALLOWED, "java.util.Date"),
155             "18:1: " + getCheckMessage(
156                     ImportControlCheck.MSG_DISALLOWED, "java.util.List"),
157             "20:1: " + getCheckMessage(
158                     ImportControlCheck.MSG_DISALLOWED, "java.util.stream.Collectors"),
159             "21:1: " + getCheckMessage(
160                     ImportControlCheck.MSG_DISALLOWED, "java.util.stream.Stream"),
161             "23:1: " + getCheckMessage(
162                     ImportControlCheck.MSG_DISALLOWED, "sun.misc.Signal"),
163         };
164 
165         final String examplePath = new File("src/" + getResourceLocation()
166                 + "/resources/" + getPackageLocation() + "/"
167                 + "someImports/UseCase4.java").getCanonicalPath();
168 
169         System.setProperty("config.folder", "src/xdocs-examples/resources/"
170             + getPackageLocation());
171         verifyWithInlineXmlConfig(examplePath, expected);
172     }
173 
174     @Test
175     public void testUseCase5() throws Exception {
176         final String[] expected = {
177             "14:1: " + getCheckMessage(
178                     ImportControlCheck.MSG_DISALLOWED, "java.sql.Blob"),
179         };
180 
181         final String examplePath = new File("src/" + getResourceLocation()
182                 + "/resources/" + getPackageLocation() + "/"
183                 + "gui/UseCase5.java").getCanonicalPath();
184 
185         System.setProperty("config.folder", "src/xdocs-examples/resources/"
186             + getPackageLocation());
187         verifyWithInlineXmlConfig(examplePath, expected);
188     }
189 
190     @Test
191     public void testUseCase6() throws Exception {
192         final String[] expected = {
193             "14:1: " + getCheckMessage(ImportControlCheck.MSG_DISALLOWED,
194                     "com.google.common.io.Files"),
195             "15:1: " + getCheckMessage(ImportControlCheck.MSG_DISALLOWED,
196                     "com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck"),
197             "18:1: " + getCheckMessage(ImportControlCheck.MSG_DISALLOWED,
198                     "java.lang.ref.ReferenceQueue"),
199             "20:1: " + getCheckMessage(ImportControlCheck.MSG_DISALLOWED,
200                     "java.lang.ref.SoftReference"),
201             "24:1: " + getCheckMessage(ImportControlCheck.MSG_DISALLOWED, "java.util.Map"),
202         };
203 
204         final String examplePath = new File("src/" + getResourceLocation()
205                 + "/resources/" + getPackageLocation() + "/"
206                 + "filters/UseCase6.java").getCanonicalPath();
207 
208         System.setProperty("config.folder", "src/xdocs-examples/resources/"
209             + getPackageLocation());
210         verifyWithInlineXmlConfig(examplePath, expected);
211     }
212 
213     @Test
214     public void testUseCase7() throws Exception {
215         final String[] expected = {
216             "15:1: " + getCheckMessage(
217                     ImportControlCheck.MSG_DISALLOWED, "javax.swing.Action"),
218             "17:1: " + getCheckMessage(
219                     ImportControlCheck.MSG_DISALLOWED, "java.awt.Image"),
220             "18:1: " + getCheckMessage(
221                     ImportControlCheck.MSG_DISALLOWED, "java.util.Date"),
222             "19:1: " + getCheckMessage(
223                     ImportControlCheck.MSG_DISALLOWED, "java.util.List"),
224             "21:1: " + getCheckMessage(
225                     ImportControlCheck.MSG_DISALLOWED, "java.util.stream.Collectors"),
226             "22:1: " + getCheckMessage(
227                     ImportControlCheck.MSG_DISALLOWED, "java.util.stream.Stream"),
228             "24:1: " + getCheckMessage(
229                     ImportControlCheck.MSG_DISALLOWED, "sun.misc.Signal"),
230         };
231 
232         final String examplePath = new File("src/" + getResourceLocation()
233                 + "/resources/" + getPackageLocation() + "/"
234                 + "someImports/UseCase7.java").getCanonicalPath();
235 
236         System.setProperty("config.folder", "src/xdocs-examples/resources/"
237             + getPackageLocation());
238         verifyWithInlineXmlConfig(examplePath, expected);
239     }
240 
241     @Test
242     public void testUseCase8() throws Exception {
243         final String[] expected = {
244             "17:1: " + getCheckMessage(
245                 ImportControlCheck.MSG_DISALLOWED, "java.util.Date"),
246             "18:1: " + getCheckMessage(
247                 ImportControlCheck.MSG_DISALLOWED, "java.util.List"),
248             "23:1: " + getCheckMessage(
249                 ImportControlCheck.MSG_DISALLOWED, "sun.misc.Signal"),
250         };
251 
252         final String examplePath = new File("src/" + getResourceLocation()
253             + "/resources/" + getPackageLocation() + "/"
254             + "someImports/UseCase8.java").getCanonicalPath();
255 
256         System.setProperty("config.folder", "src/xdocs-examples/resources/"
257             + getPackageLocation());
258         verifyWithInlineXmlConfig(examplePath, expected);
259     }
260 
261     @Test
262     public void testUseCase9() throws Exception {
263         final String[] expected = {
264             "14:1: " + getCheckMessage(
265                     ImportControlCheck.MSG_DISALLOWED, "java.awt.Image"),
266             "15:1: " + getCheckMessage(
267                     ImportControlCheck.MSG_DISALLOWED, "java.io.File"),
268             "16:1: " + getCheckMessage(
269                     ImportControlCheck.MSG_DISALLOWED, "java.io.FileReader"),
270             "17:1: " + getCheckMessage(
271                     ImportControlCheck.MSG_DISALLOWED, "java.util.Date"),
272         };
273 
274         System.setProperty("config.folder", "src/xdocs-examples/resources/"
275             + getPackageLocation());
276         verifyWithInlineXmlConfig(getPath("UseCase9.java"), expected);
277     }
278 
279     @Test
280     public void testExample4() throws Exception {
281         final String[] expected = {
282             "16:1: " + getCheckMessage(
283                     ImportControlCheck.MSG_DISALLOWED, "java.logging"),
284             "17:1: " + getCheckMessage(
285                     ImportControlCheck.MSG_DISALLOWED, "java.sql"),
286         };
287 
288         System.setProperty("config.folder", "src/xdocs-examples/resources/"
289             + getPackageLocation());
290         verifyWithInlineXmlConfig(getNonCompilablePath("Example4.java"), expected);
291     }
292 
293     @Test
294     public void testExample5() throws Exception {
295         final String[] expected = {
296             "16:1: " + getCheckMessage(
297                     ImportControlCheck.MSG_DISALLOWED, "java.sql"),
298             "17:1: " + getCheckMessage(
299                     ImportControlCheck.MSG_DISALLOWED, "java.xml"),
300         };
301 
302         System.setProperty("config.folder", "src/xdocs-examples/resources/"
303             + getPackageLocation());
304         verifyWithInlineXmlConfig(getNonCompilablePath("Example5.java"), expected);
305     }
306 
307 }