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 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      @Override
30      protected String getPackageLocation() {
31          return "com/puppycrawl/tools/checkstyle/checks/imports/importcontrol";
32      }
33  
34      @Test
35      public void testExample1() throws Exception {
36          final String[] expected = {
37              "14:1: " + getCheckMessage(
38                      ImportControlCheck.MSG_DISALLOWED, "java.io.File"),
39          };
40  
41          System.setProperty("config.folder", "src/xdocs-examples/resources/"
42              + getPackageLocation());
43          verifyWithInlineXmlConfig(getPath("Example1.java"), expected);
44      }
45  
46      @Test
47      public void testExample2() throws Exception {
48          final String[] expected = {};
49  
50          System.setProperty("config.folder", "src/xdocs-examples/resources/"
51              + getPackageLocation());
52          verifyWithInlineXmlConfig(getPath("Example2.java"), expected);
53      }
54  
55      @Test
56      public void testExample3() throws Exception {
57          final String[] expected = {
58              "15:1: " + getCheckMessage(
59                      ImportControlCheck.MSG_DISALLOWED,
60                  "com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck"),
61              "18:1: " + getCheckMessage(
62                      ImportControlCheck.MSG_DISALLOWED, "java.lang.ref.ReferenceQueue"),
63          };
64  
65          final String examplePath = new File("src/" + getResourceLocation()
66                  + "/resources/" + getPackageLocation() + "/"
67                  + "filters/Example3.java").getCanonicalPath();
68  
69          System.setProperty("config.folder", "src/xdocs-examples/resources/"
70              + getPackageLocation());
71          verifyWithInlineXmlConfig(examplePath, expected);
72      }
73  
74      @Test
75      public void testExample4() throws Exception {
76          final String[] expected = {
77              "14:1: " + getCheckMessage(
78                      ImportControlCheck.MSG_DISALLOWED,
79                  "com.puppycrawl.tools.checkstyle.checks.TranslationCheck"),
80              "18:1: " + getCheckMessage(
81                      ImportControlCheck.MSG_DISALLOWED, "javax.swing.ActionMap"),
82          };
83  
84          final String examplePath = new File("src/" + getResourceLocation()
85                  + "/resources/" + getPackageLocation() + "/"
86                  + "newdomain/dao/Example4.java").getCanonicalPath();
87  
88          System.setProperty("config.folder", "src/xdocs-examples/resources/"
89              + getPackageLocation());
90          verifyWithInlineXmlConfig(examplePath, expected);
91      }
92  
93      @Test
94      public void testExample5() throws Exception {
95          final String[] expected = {
96              "14:1: " + getCheckMessage(
97                  ImportControlCheck.MSG_DISALLOWED, "java.awt.Image"),
98              "15:1: " + getCheckMessage(
99                  ImportControlCheck.MSG_DISALLOWED, "java.io.File"),
100         };
101 
102         System.setProperty("config.folder", "src/xdocs-examples/resources/"
103             + getPackageLocation());
104         verifyWithInlineXmlConfig(getPath("Example5.java"), expected);
105     }
106 
107     @Test
108     public void testExample6() throws Exception {
109         final String[] expected = {
110             "17:1: " + getCheckMessage(
111                     ImportControlCheck.MSG_DISALLOWED, "java.util.Random"),
112         };
113 
114         final String examplePath = new File("src/" + getResourceLocation()
115                 + "/resources/" + getPackageLocation() + "/"
116                 + "someImports/Example6.java").getCanonicalPath();
117 
118         System.setProperty("config.folder", "src/xdocs-examples/resources/"
119             + getPackageLocation());
120         verifyWithInlineXmlConfig(examplePath, expected);
121     }
122 
123     @Test
124     public void testExample7() throws Exception {
125         final String[] expected = {
126             "17:1: " + getCheckMessage(
127                     ImportControlCheck.MSG_DISALLOWED, "java.awt.Image"),
128         };
129 
130         final String examplePath = new File("src/" + getResourceLocation()
131                 + "/resources/" + getPackageLocation() + "/"
132                 + "someImports/Example7.java").getCanonicalPath();
133 
134         System.setProperty("config.folder", "src/xdocs-examples/resources/"
135             + getPackageLocation());
136         verifyWithInlineXmlConfig(examplePath, expected);
137     }
138 
139     @Test
140     public void testExample8() throws Exception {
141         final String[] expected = {
142             "14:1: " + getCheckMessage(
143                     ImportControlCheck.MSG_DISALLOWED, "java.sql.Blob"),
144         };
145 
146         final String examplePath = new File("src/" + getResourceLocation()
147                 + "/resources/" + getPackageLocation() + "/"
148                 + "gui/Example8.java").getCanonicalPath();
149 
150         System.setProperty("config.folder", "src/xdocs-examples/resources/"
151             + getPackageLocation());
152         verifyWithInlineXmlConfig(examplePath, expected);
153     }
154 
155     @Test
156     public void testExample9() throws Exception {
157         final String[] expected = {
158             "16:1: " + getCheckMessage(ImportControlCheck.MSG_DISALLOWED, "java.util.Map"),
159         };
160 
161         final String examplePath = new File("src/" + getResourceLocation()
162                 + "/resources/" + getPackageLocation() + "/"
163                 + "filters/Example9.java").getCanonicalPath();
164 
165         System.setProperty("config.folder", "src/xdocs-examples/resources/"
166             + getPackageLocation());
167         verifyWithInlineXmlConfig(examplePath, expected);
168     }
169 
170     @Test
171     public void testExample10() throws Exception {
172         final String[] expected = {
173             "15:1: " + getCheckMessage(
174                     ImportControlCheck.MSG_DISALLOWED, "java.util.stream.Stream"),
175             "16:1: " + getCheckMessage(
176                     ImportControlCheck.MSG_DISALLOWED, "java.util.stream.Collectors"),
177         };
178 
179         final String examplePath = new File("src/" + getResourceLocation()
180                 + "/resources/" + getPackageLocation() + "/"
181                 + "someImports/Example10.java").getCanonicalPath();
182 
183         System.setProperty("config.folder", "src/xdocs-examples/resources/"
184             + getPackageLocation());
185         verifyWithInlineXmlConfig(examplePath, expected);
186     }
187 
188     @Test
189     public void testExample11() throws Exception {
190         final String[] expected = {
191             "15:1: " + getCheckMessage(
192                 ImportControlCheck.MSG_DISALLOWED, "java.util.Date"),
193             "16:1: " + getCheckMessage(
194                 ImportControlCheck.MSG_DISALLOWED, "java.util.List"),
195             "19:1: " + getCheckMessage(
196                 ImportControlCheck.MSG_DISALLOWED, "sun.misc.Signal"),
197         };
198 
199         final String examplePath = new File("src/" + getResourceLocation()
200             + "/resources/" + getPackageLocation() + "/"
201             + "someImports/Example11.java").getCanonicalPath();
202 
203         System.setProperty("config.folder", "src/xdocs-examples/resources/"
204             + getPackageLocation());
205         verifyWithInlineXmlConfig(examplePath, expected);
206     }
207 
208     @Test
209     public void testExample12() throws Exception {
210         final String[] expected = {
211             "14:1: " + getCheckMessage(
212                     ImportControlCheck.MSG_DISALLOWED, "java.util.Date"),
213         };
214 
215         System.setProperty("config.folder", "src/xdocs-examples/resources/"
216             + getPackageLocation());
217         verifyWithInlineXmlConfig(getPath("Example12.java"), expected);
218     }
219 
220 }