View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.ImportControlCheck.MSG_DISALLOWED;
23  import static com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheck.MSG_MISSING_FILE;
24  import static com.puppycrawl.tools.checkstyle.checks.imports.ImportControlCheck.MSG_UNKNOWN_PKG;
25  import static org.junit.Assert.assertArrayEquals;
26  import static org.junit.Assert.assertTrue;
27  import static org.junit.Assert.fail;
28  
29  import java.io.File;
30  import java.nio.charset.StandardCharsets;
31  import java.nio.file.Files;
32  
33  import org.junit.Rule;
34  import org.junit.Test;
35  import org.junit.rules.TemporaryFolder;
36  
37  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
38  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
39  import com.puppycrawl.tools.checkstyle.TreeWalker;
40  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
41  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
42  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
43  
44  public class ImportControlCheckTest extends AbstractModuleTestSupport {
45  
46      @Rule
47      public final TemporaryFolder temporaryFolder = new TemporaryFolder();
48  
49      @Override
50      protected String getPackageLocation() {
51          return "com/puppycrawl/tools/checkstyle/checks/imports/importcontrol";
52      }
53  
54      @Test
55      public void testGetRequiredTokens() {
56          final ImportControlCheck checkObj = new ImportControlCheck();
57          final int[] expected = {
58              TokenTypes.PACKAGE_DEF,
59              TokenTypes.IMPORT,
60              TokenTypes.STATIC_IMPORT,
61          };
62          assertArrayEquals("Default required tokens are invalid",
63              expected, checkObj.getRequiredTokens());
64      }
65  
66      @Test
67      public void testOne() throws Exception {
68          final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
69          checkConfig.addAttribute("file", getPath("InputImportControlOne.xml"));
70          final String[] expected = {"5:1: " + getCheckMessage(MSG_DISALLOWED, "java.io.File")};
71  
72          verify(checkConfig, getPath("InputImportControl.java"), expected);
73      }
74  
75      @Test
76      public void testTwo() throws Exception {
77          final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
78          checkConfig.addAttribute("file", getPath("InputImportControlTwo.xml"));
79          final String[] expected = {
80              "3:1: " + getCheckMessage(MSG_DISALLOWED, "java.awt.Image"),
81              "4:1: " + getCheckMessage(MSG_DISALLOWED, "javax.swing.border.*"),
82              "6:1: " + getCheckMessage(MSG_DISALLOWED, "java.awt.Button.ABORT"),
83          };
84  
85          verify(checkConfig, getPath("InputImportControl.java"), expected);
86      }
87  
88      @Test
89      public void testWrong() throws Exception {
90          final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
91          checkConfig.addAttribute("file", getPath("InputImportControlWrong.xml"));
92          final String[] expected = {"1:1: " + getCheckMessage(MSG_UNKNOWN_PKG)};
93          verify(checkConfig, getPath("InputImportControl.java"), expected);
94      }
95  
96      @Test
97      public void testMissing() throws Exception {
98          final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
99          final String[] expected = {"1:1: " + getCheckMessage(MSG_MISSING_FILE)};
100         verify(checkConfig, getPath("InputImportControl.java"), expected);
101     }
102 
103     @Test
104     public void testEmpty() throws Exception {
105         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
106         checkConfig.addAttribute("file", "   ");
107         final String[] expected = {"1:1: " + getCheckMessage(MSG_MISSING_FILE)};
108         verify(checkConfig, getPath("InputImportControl.java"), expected);
109     }
110 
111     @Test
112     public void testNull() throws Exception {
113         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
114         checkConfig.addAttribute("file", null);
115         final String[] expected = {"1:1: " + getCheckMessage(MSG_MISSING_FILE)};
116         verify(checkConfig, getPath("InputImportControl.java"), expected);
117     }
118 
119     @Test
120     public void testUnknown() throws Exception {
121         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
122         checkConfig.addAttribute("file", "unknown-file");
123         try {
124             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
125             verify(checkConfig, getPath("InputImportControl.java"), expected);
126             fail("Test should fail if exception was not thrown");
127         }
128         catch (CheckstyleException ex) {
129             final String message = getCheckstyleExceptionMessage(ex);
130             final String messageStart = "Unable to find: ";
131 
132             assertTrue("Invalid message, should start with: " + messageStart,
133                 message.startsWith(message));
134         }
135     }
136 
137     @Test
138     public void testBroken() throws Exception {
139         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
140         checkConfig.addAttribute("file", getPath("InputImportControlBroken.xml"));
141         try {
142             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
143             verify(checkConfig, getPath("InputImportControl.java"), expected);
144             fail("Test should fail if exception was not thrown");
145         }
146         catch (CheckstyleException ex) {
147             final String message = getCheckstyleExceptionMessage(ex);
148             final String messageStart = "Unable to load ";
149 
150             assertTrue("Invalid message, should start with: " + messageStart,
151                 message.startsWith(message));
152         }
153     }
154 
155     @Test
156     public void testOneRegExp() throws Exception {
157         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
158         checkConfig.addAttribute("file", getPath("InputImportControlOneRegExp.xml"));
159         final String[] expected = {"5:1: " + getCheckMessage(MSG_DISALLOWED, "java.io.File")};
160 
161         verify(checkConfig, getPath("InputImportControl.java"), expected);
162     }
163 
164     @Test
165     public void testTwoRegExp() throws Exception {
166         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
167         checkConfig.addAttribute("file", getPath("InputImportControlTwoRegExp.xml"));
168         final String[] expected = {
169             "3:1: " + getCheckMessage(MSG_DISALLOWED, "java.awt.Image"),
170             "4:1: " + getCheckMessage(MSG_DISALLOWED, "javax.swing.border.*"),
171             "6:1: " + getCheckMessage(MSG_DISALLOWED, "java.awt.Button.ABORT"),
172         };
173 
174         verify(checkConfig, getPath("InputImportControl.java"), expected);
175     }
176 
177     @Test
178     public void testBlacklist() throws Exception {
179         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
180         checkConfig.addAttribute("file", getPath("InputImportControlBlacklist.xml"));
181         final String[] expected = {
182             "3:1: " + getCheckMessage(MSG_DISALLOWED, "java.util.stream.Stream"),
183             "4:1: " + getCheckMessage(MSG_DISALLOWED, "java.util.Date"),
184             "6:1: " + getCheckMessage(MSG_DISALLOWED, "java.util.stream.Collectors"),
185             "7:1: " + getCheckMessage(MSG_DISALLOWED, "java.util.stream.IntStream"),
186         };
187 
188         verify(checkConfig, getPath("InputImportControl_Blacklist.java"), expected);
189     }
190 
191     @Test
192     public void testStrategyOnMismatchOne() throws Exception {
193         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
194         checkConfig.addAttribute("file", getPath("InputImportControlStrategyOnMismatchOne.xml"));
195         final String[] expected = {
196             "3:1: " + getCheckMessage(MSG_DISALLOWED, "java.awt.Image"),
197             "4:1: " + getCheckMessage(MSG_DISALLOWED, "javax.swing.border.*"),
198             "6:1: " + getCheckMessage(MSG_DISALLOWED, "java.awt.Button.ABORT"),
199         };
200 
201         verify(checkConfig, getPath("InputImportControl.java"), expected);
202     }
203 
204     @Test
205     public void testStrategyOnMismatchTwo() throws Exception {
206         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
207         checkConfig.addAttribute("file", getPath("InputImportControlStrategyOnMismatchTwo.xml"));
208         final String[] expected = {
209             "3:1: " + getCheckMessage(MSG_DISALLOWED, "java.awt.Image"),
210             "6:1: " + getCheckMessage(MSG_DISALLOWED, "java.awt.Button.ABORT"),
211         };
212 
213         verify(checkConfig, getPath("InputImportControl.java"), expected);
214     }
215 
216     @Test
217     public void testStrategyOnMismatchThree() throws Exception {
218         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
219         checkConfig.addAttribute("file", getPath("InputImportControlStrategyOnMismatchThree.xml"));
220         final String[] expected = {
221             "3:1: " + getCheckMessage(MSG_DISALLOWED, "java.awt.Image"),
222         };
223 
224         verify(checkConfig, getPath("InputImportControl.java"), expected);
225     }
226 
227     @Test
228     public void testStrategyOnMismatchFour() throws Exception {
229         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
230         checkConfig.addAttribute("file", getPath("InputImportControlStrategyOnMismatchFour.xml"));
231         final String[] expected = {
232             "3:1: " + getCheckMessage(MSG_DISALLOWED, "java.awt.Image"),
233             "4:1: " + getCheckMessage(MSG_DISALLOWED, "javax.swing.border.*"),
234         };
235 
236         verify(checkConfig, getPath("InputImportControl.java"), expected);
237     }
238 
239     @Test
240     public void testPkgRegExpInParent() throws Exception {
241         testRegExpInPackage("InputImportControlPkgRegExpInParent.xml");
242     }
243 
244     @Test
245     public void testPkgRegExpInChild() throws Exception {
246         testRegExpInPackage("InputImportControlPkgRegExpInChild.xml");
247     }
248 
249     @Test
250     public void testPkgRegExpInBoth() throws Exception {
251         testRegExpInPackage("InputImportControlPkgRegExpInBoth.xml");
252     }
253 
254     // all import-control_pkg-re* files should be equivalent so use one test for all
255     private void testRegExpInPackage(String file) throws Exception {
256         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
257         checkConfig.addAttribute("file", getPath(file));
258         final String[] expected = {"5:1: " + getCheckMessage(MSG_DISALLOWED, "java.io.File")};
259 
260         verify(checkConfig, getPath("InputImportControl.java"), expected);
261     }
262 
263     @Test
264     public void testGetAcceptableTokens() {
265         final ImportControlCheck testCheckObject =
266                 new ImportControlCheck();
267         final int[] actual = testCheckObject.getAcceptableTokens();
268         final int[] expected = {
269             TokenTypes.PACKAGE_DEF,
270             TokenTypes.IMPORT,
271             TokenTypes.STATIC_IMPORT,
272         };
273 
274         assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
275     }
276 
277     @Test
278     public void testResource() throws Exception {
279         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
280         checkConfig.addAttribute("file", getResourcePath("InputImportControlOne.xml"));
281         final String[] expected = {"5:1: " + getCheckMessage(MSG_DISALLOWED, "java.io.File")};
282 
283         verify(checkConfig, getPath("InputImportControl.java"), expected);
284     }
285 
286     @Test
287     public void testResourceUnableToLoad() throws Exception {
288         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
289         checkConfig.addAttribute("file", getResourcePath("import-control_unknown.xml"));
290 
291         try {
292             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
293             verify(checkConfig, getPath("InputImportControl.java"), expected);
294             fail("Test should fail if exception was not thrown");
295         }
296         catch (CheckstyleException ex) {
297             final String message = getCheckstyleExceptionMessage(ex);
298             final String messageStart = "Unable to find: ";
299 
300             assertTrue("Invalid message, should start with: " + messageStart,
301                 message.startsWith(message));
302         }
303     }
304 
305     @Test
306     public void testUrlInFileProperty() throws Exception {
307         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
308         checkConfig.addAttribute("file", getUriString("InputImportControlOne.xml"));
309         final String[] expected = {"5:1: " + getCheckMessage(MSG_DISALLOWED, "java.io.File")};
310 
311         verify(checkConfig, getPath("InputImportControl.java"), expected);
312     }
313 
314     @Test
315     public void testUrlInFilePropertyUnableToLoad() throws Exception {
316         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
317         checkConfig.addAttribute("file", "https://UnableToLoadThisURL");
318 
319         try {
320             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
321             verify(checkConfig, getPath("InputImportControl.java"), expected);
322             fail("Test should fail if exception was not thrown");
323         }
324         catch (CheckstyleException ex) {
325             final String message = getCheckstyleExceptionMessage(ex);
326             final String messageStart = "Unable to load ";
327 
328             assertTrue("Invalid message, should start with: " + messageStart,
329                 message.startsWith(message));
330         }
331     }
332 
333     @Test
334     public void testCacheWhenFileExternalResourceContentDoesNotChange() throws Exception {
335         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
336         checkConfig.addAttribute("file", getPath("InputImportControlOneRegExp.xml"));
337 
338         final DefaultConfiguration treeWalkerConfig = createModuleConfig(TreeWalker.class);
339         treeWalkerConfig.addChild(checkConfig);
340 
341         final DefaultConfiguration checkerConfig = createRootConfig(treeWalkerConfig);
342         final File cacheFile = temporaryFolder.newFile();
343         checkerConfig.addAttribute("cacheFile", cacheFile.getPath());
344 
345         final String filePath = temporaryFolder.newFile("EmptyFile.java").getPath();
346         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
347 
348         verify(checkerConfig, filePath, expected);
349         // One more time to use cache.
350         verify(checkerConfig, filePath, expected);
351 
352         assertTrue("External resource is not present in cache",
353                 new String(Files.readAllBytes(cacheFile.toPath()),
354                         StandardCharsets.UTF_8).contains("InputImportControlOneRegExp.xml"));
355     }
356 
357     @Test
358     public void testPathRegexMatches() throws Exception {
359         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
360         checkConfig.addAttribute("file", getResourcePath("InputImportControlOne.xml"));
361         checkConfig.addAttribute("path", "^.*[\\\\/]src[\\\\/]test[\\\\/].*$");
362         final String[] expected = {"5:1: " + getCheckMessage(MSG_DISALLOWED, "java.io.File")};
363 
364         verify(checkConfig, getPath("InputImportControl.java"), expected);
365     }
366 
367     @Test
368     public void testPathRegexMatchesPartially() throws Exception {
369         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
370         checkConfig.addAttribute("file", getResourcePath("InputImportControlOne.xml"));
371         checkConfig.addAttribute("path", "[\\\\/]InputImportControl\\.java");
372         final String[] expected = {"5:1: " + getCheckMessage(MSG_DISALLOWED, "java.io.File")};
373 
374         verify(checkConfig, getPath("InputImportControl.java"), expected);
375     }
376 
377     @Test
378     public void testPathRegexDoesntMatch() throws Exception {
379         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
380         checkConfig.addAttribute("file", getResourcePath("InputImportControlOne.xml"));
381         checkConfig.addAttribute("path", "^.*[\\\\/]src[\\\\/]main[\\\\/].*$");
382         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
383 
384         verify(checkConfig, getPath("InputImportControl.java"), expected);
385     }
386 
387     @Test
388     public void testPathRegexDoesntMatchPartially() throws Exception {
389         final DefaultConfiguration checkConfig = createModuleConfig(ImportControlCheck.class);
390         checkConfig.addAttribute("file", getResourcePath("InputImportControlOne.xml"));
391         checkConfig.addAttribute("path", "[\\\\/]NoMatch\\.java");
392         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
393 
394         verify(checkConfig, getPath("InputImportControl.java"), expected);
395     }
396 
397     /**
398      * Returns String message of original exception that was thrown in
399      * ImportControlCheck.setUrl or ImportControlCheck.setFile
400      * and caught in test (it was caught and re-thrown twice after that)
401      * Note: this is helper method with hard-coded structure of exception causes. It works
402      * fine for methods mentioned, you may need to adjust it if you try to use it for other needs
403      * @param exception Exception
404      * @return String message of original exception
405      */
406     private static String getCheckstyleExceptionMessage(CheckstyleException exception) {
407         return exception.getCause().getCause().getCause().getMessage();
408     }
409 }