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.IllegalImportCheck.MSG_KEY;
23  import static org.junit.Assert.assertArrayEquals;
24  
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  
31  public class IllegalImportCheckTest extends AbstractModuleTestSupport {
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 = {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
41          assertArrayEquals("Default required tokens are invalid",
42              expected, checkObj.getRequiredTokens());
43      }
44  
45      @Test
46      public void testWithSupplied()
47              throws Exception {
48          final DefaultConfiguration checkConfig =
49              createModuleConfig(IllegalImportCheck.class);
50          checkConfig.addAttribute("illegalPkgs", "java.io");
51          final String[] expected = {
52              "9:1: " + getCheckMessage(MSG_KEY, "java.io.*"),
53              "23:1: " + getCheckMessage(MSG_KEY, "java.io.File.listRoots"),
54              "27:1: " + getCheckMessage(MSG_KEY, "java.io.File.createTempFile"),
55          };
56          verify(checkConfig, getPath("InputIllegalImportDefault.java"), expected);
57      }
58  
59      @Test
60      public void testWithDefault()
61              throws Exception {
62          final DefaultConfiguration checkConfig =
63              createModuleConfig(IllegalImportCheck.class);
64          final String[] expected = {
65              "15:1: " + getCheckMessage(MSG_KEY, "sun.applet.*"),
66              "28:1: " + getCheckMessage(MSG_KEY, "sun.*"),
67          };
68          verify(checkConfig, getPath("InputIllegalImportDefault.java"), expected);
69      }
70  
71      @Test
72      public void testGetAcceptableTokens() {
73          final IllegalImportCheck testCheckObject =
74                  new IllegalImportCheck();
75          final int[] actual = testCheckObject.getAcceptableTokens();
76          final int[] expected = {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
77  
78          assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
79      }
80  
81      @Test
82      public void testIllegalClasses()
83              throws Exception {
84          final DefaultConfiguration checkConfig =
85                  createModuleConfig(IllegalImportCheck.class);
86          checkConfig.addAttribute("illegalClasses", "java.sql.Connection");
87          final String[] expected = {
88              "11:1: " + getCheckMessage(MSG_KEY, "java.sql.Connection"),
89              "15:1: " + getCheckMessage(MSG_KEY, "sun.applet.*"),
90              "28:1: " + getCheckMessage(MSG_KEY, "sun.*"),
91          };
92          verify(checkConfig, getPath("InputIllegalImportDefault.java"), expected);
93      }
94  
95      @Test
96      public void testIllegalPackagesRegularExpression()
97              throws Exception {
98          final DefaultConfiguration checkConfig =
99                  createModuleConfig(IllegalImportCheck.class);
100         checkConfig.addAttribute("illegalPkgs", "java\\.util");
101         checkConfig.addAttribute("regexp", "true");
102         final String[] expected = {
103             "12:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
104             "13:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
105             "16:1: " + getCheckMessage(MSG_KEY, "java.util.Enumeration"),
106             "17:1: " + getCheckMessage(MSG_KEY, "java.util.Arrays"),
107             "34:1: " + getCheckMessage(MSG_KEY, "java.util.Date"),
108             "35:1: " + getCheckMessage(MSG_KEY, "java.util.Calendar"),
109             "36:1: " + getCheckMessage(MSG_KEY, "java.util.BitSet"),
110         };
111         verify(checkConfig, getPath("InputIllegalImportDefault.java"), expected);
112     }
113 
114     @Test
115     public void testIllegalClassesRegularExpression()
116             throws Exception {
117         final DefaultConfiguration checkConfig =
118                 createModuleConfig(IllegalImportCheck.class);
119         checkConfig.addAttribute("illegalPkgs", "");
120         checkConfig.addAttribute("illegalClasses", "^java\\.util\\.(List|Arrays)");
121         checkConfig.addAttribute("regexp", "true");
122         final String[] expected = {
123             "12:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
124             "13:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
125             "17:1: " + getCheckMessage(MSG_KEY, "java.util.Arrays"),
126         };
127         verify(checkConfig, getPath("InputIllegalImportDefault.java"), expected);
128     }
129 
130     @Test
131     public void testIllegalPackagesAndClassesRegularExpression()
132             throws Exception {
133         final DefaultConfiguration checkConfig =
134                 createModuleConfig(IllegalImportCheck.class);
135         checkConfig.addAttribute("illegalPkgs", "java\\.util");
136         checkConfig.addAttribute("illegalClasses",
137                 "^com\\.puppycrawl\\.tools\\.checkstyle\\.Checker.*");
138         checkConfig.addAttribute("regexp", "true");
139         final String[] expected = {
140             "12:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
141             "13:1: " + getCheckMessage(MSG_KEY, "java.util.List"),
142             "16:1: " + getCheckMessage(MSG_KEY, "java.util.Enumeration"),
143             "17:1: " + getCheckMessage(MSG_KEY, "java.util.Arrays"),
144             "34:1: " + getCheckMessage(MSG_KEY, "java.util.Date"),
145             "35:1: " + getCheckMessage(MSG_KEY, "java.util.Calendar"),
146             "36:1: " + getCheckMessage(MSG_KEY, "java.util.BitSet"),
147             "38:1: " + getCheckMessage(MSG_KEY, "com.puppycrawl.tools.checkstyle.Checker"),
148             "39:1: " + getCheckMessage(MSG_KEY, "com.puppycrawl.tools.checkstyle.CheckerTest"),
149         };
150         verify(checkConfig, getPath("InputIllegalImportDefault.java"), expected);
151     }
152 }