View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.coding;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.coding.IllegalTypeCheck.MSG_KEY;
23  
24  import java.io.File;
25  
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.api.DetailAST;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
34  
35  public class IllegalTypeCheckTest extends AbstractModuleTestSupport {
36  
37      @Override
38      protected String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/checks/coding/illegaltype";
40      }
41  
42      @Test
43      public void testValidateAbstractClassNamesSetToTrue() throws Exception {
44          final DefaultConfiguration checkConfig = createModuleConfig(IllegalTypeCheck.class);
45          checkConfig.addAttribute("validateAbstractClassNames", "true");
46          final String[] expected = {
47              "27:5: " + getCheckMessage(MSG_KEY, "AbstractClass"),
48              "29:37: " + getCheckMessage(MSG_KEY, "AbstractClass"),
49              "33:12: " + getCheckMessage(MSG_KEY, "AbstractClass"),
50          };
51  
52          verify(checkConfig, getPath("InputIllegalTypeAbstractClassNames.java"), expected);
53      }
54  
55      @Test
56      public void testValidateAbstractClassNamesSetToFalse() throws Exception {
57          final DefaultConfiguration checkConfig = createModuleConfig(IllegalTypeCheck.class);
58          checkConfig.addAttribute("validateAbstractClassNames", "false");
59          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
60  
61          verify(checkConfig, getPath("InputIllegalTypeAbstractClassNames.java"), expected);
62      }
63  
64      @Test
65      public void testDefaults() throws Exception {
66          final DefaultConfiguration checkConfig = createModuleConfig(IllegalTypeCheck.class);
67          final String[] expected = {
68              "16:13: " + getCheckMessage(MSG_KEY, "java.util.TreeSet"),
69              "17:13: " + getCheckMessage(MSG_KEY, "TreeSet"),
70          };
71  
72          verify(checkConfig, getPath("InputIllegalType.java"), expected);
73      }
74  
75      @Test
76      public void testIgnoreMethodNames() throws Exception {
77          final DefaultConfiguration checkConfig = createModuleConfig(IllegalTypeCheck.class);
78          checkConfig.addAttribute("ignoredMethodNames", "table2");
79          checkConfig.addAttribute("validateAbstractClassNames", "true");
80          final String[] expected = {
81              "6:13: " + getCheckMessage(MSG_KEY, "AbstractClass"),
82              "9:13: " + getCheckMessage(MSG_KEY,
83                  "com.puppycrawl.tools.checkstyle.checks.coding.illegaltype."
84                      + "InputIllegalType.AbstractClass"),
85              "16:13: " + getCheckMessage(MSG_KEY, "java.util.TreeSet"),
86              "25:36: " + getCheckMessage(MSG_KEY, "java.util.TreeSet"),
87          };
88  
89          verify(checkConfig, getPath("InputIllegalType.java"), expected);
90      }
91  
92      @Test
93      public void testFormat() throws Exception {
94          final DefaultConfiguration checkConfig = createModuleConfig(IllegalTypeCheck.class);
95          checkConfig.addAttribute("format", "^$");
96  
97          final String[] expected = {
98              "16:13: " + getCheckMessage(MSG_KEY, "java.util.TreeSet"),
99              "17:13: " + getCheckMessage(MSG_KEY, "TreeSet"),
100         };
101 
102         verify(checkConfig, getPath("InputIllegalType.java"), expected);
103     }
104 
105     @Test
106     public void testLegalAbstractClassNames() throws Exception {
107         final DefaultConfiguration checkConfig = createModuleConfig(IllegalTypeCheck.class);
108         checkConfig.addAttribute("validateAbstractClassNames", "true");
109         checkConfig.addAttribute("legalAbstractClassNames", "AbstractClass");
110 
111         final String[] expected = {
112             "9:13: " + getCheckMessage(MSG_KEY,
113                 "com.puppycrawl.tools.checkstyle.checks.coding.illegaltype."
114                     + "InputIllegalType.AbstractClass"),
115             "16:13: " + getCheckMessage(MSG_KEY, "java.util.TreeSet"),
116             "17:13: " + getCheckMessage(MSG_KEY, "TreeSet"),
117         };
118 
119         verify(checkConfig, getPath("InputIllegalType.java"), expected);
120     }
121 
122     @Test
123     public void testSameFileNameFalsePositive() throws Exception {
124         final DefaultConfiguration checkConfig = createModuleConfig(IllegalTypeCheck.class);
125         checkConfig.addAttribute("illegalClassNames", "java.util.GregorianCalendar, SubCalendar, "
126                 + "java.util.List");
127 
128         final String[] expected = {
129             "12:5: " + getCheckMessage(MSG_KEY, "SubCalendar"),
130             "27:5: " + getCheckMessage(MSG_KEY, "java.util.List"),
131         };
132 
133         verify(checkConfig, getPath("InputIllegalTypeSameFileName.java"), expected);
134     }
135 
136     @Test
137     public void testSameFileNameGeneral() throws Exception {
138         final DefaultConfiguration checkConfig = createModuleConfig(IllegalTypeCheck.class);
139         checkConfig.addAttribute("illegalClassNames",
140             "List, InputIllegalTypeGregorianCalendar, java.io.File, ArrayList, Boolean");
141         final String[] expected = {
142             "10:5: " + getCheckMessage(MSG_KEY, "InputIllegalTypeGregorianCalendar"),
143             "16:23: " + getCheckMessage(MSG_KEY, "InputIllegalTypeGregorianCalendar"),
144             "24:9: " + getCheckMessage(MSG_KEY, "List"),
145             "25:9: " + getCheckMessage(MSG_KEY, "java.io.File"),
146             "27:5: " + getCheckMessage(MSG_KEY, "java.util.List"),
147             "28:13: " + getCheckMessage(MSG_KEY, "ArrayList"),
148             "29:13: " + getCheckMessage(MSG_KEY, "Boolean"),
149         };
150         verify(checkConfig, getPath("InputIllegalTypeSameFileName.java"), expected);
151     }
152 
153     @Test
154     public void testArrayTypes() throws Exception {
155         final DefaultConfiguration checkConfig = createModuleConfig(IllegalTypeCheck.class);
156         checkConfig.addAttribute("illegalClassNames", "Boolean[], Boolean[][]");
157         final String[] expected = {
158             "6:12: " + getCheckMessage(MSG_KEY, "Boolean[]"),
159             "8:12: " + getCheckMessage(MSG_KEY, "Boolean[][]"),
160             "10:12: " + getCheckMessage(MSG_KEY, "Boolean[]"),
161             "11:9: " + getCheckMessage(MSG_KEY, "Boolean[]"),
162             "15:12: " + getCheckMessage(MSG_KEY, "Boolean[][]"),
163             "16:9: " + getCheckMessage(MSG_KEY, "Boolean[][]"),
164         };
165         verify(checkConfig, getPath("InputIllegalTypeArrays.java"), expected);
166     }
167 
168     @Test
169     public void testPlainAndArrayTypes() throws Exception {
170         final DefaultConfiguration checkConfig = createModuleConfig(IllegalTypeCheck.class);
171         checkConfig.addAttribute("illegalClassNames", "Boolean, Boolean[][]");
172         final String[] expected = {
173             "6:12: " + getCheckMessage(MSG_KEY, "Boolean"),
174             "10:12: " + getCheckMessage(MSG_KEY, "Boolean[][]"),
175             "12:12: " + getCheckMessage(MSG_KEY, "Boolean"),
176             "21:12: " + getCheckMessage(MSG_KEY, "Boolean[][]"),
177             "22:9: " + getCheckMessage(MSG_KEY, "Boolean[][]"),
178         };
179         verify(checkConfig, getPath("InputIllegalTypePlainAndArrays.java"), expected);
180     }
181 
182     @Test
183     public void testStarImports() throws Exception {
184         final DefaultConfiguration checkConfig = createModuleConfig(IllegalTypeCheck.class);
185         checkConfig.addAttribute("illegalClassNames", "List");
186 
187         final String[] expected = {
188             "10:5: " + getCheckMessage(MSG_KEY, "List"),
189         };
190 
191         verify(checkConfig, getPath("InputIllegalTypeStarImports.java"), expected);
192     }
193 
194     @Test
195     public void testStaticImports() throws Exception {
196         final DefaultConfiguration checkConfig = createModuleConfig(IllegalTypeCheck.class);
197         checkConfig.addAttribute("illegalClassNames", "SomeStaticClass");
198         checkConfig.addAttribute("ignoredMethodNames", "foo1");
199 
200         final String[] expected = {
201             "13:6: " + getCheckMessage(MSG_KEY, "SomeStaticClass"),
202             "15:31: " + getCheckMessage(MSG_KEY, "SomeStaticClass"),
203         };
204 
205         verify(checkConfig, getPath("InputIllegalTypeStaticImports.java"), expected);
206     }
207 
208     @Test
209     public void testMemberModifiers() throws Exception {
210         final DefaultConfiguration checkConfig = createModuleConfig(IllegalTypeCheck.class);
211         checkConfig.addAttribute("validateAbstractClassNames", "true");
212         checkConfig.addAttribute("memberModifiers", "LITERAL_PRIVATE, LITERAL_PROTECTED,"
213                 + " LITERAL_STATIC");
214         final String[] expected = {
215             "6:13: " + getCheckMessage(MSG_KEY, "AbstractClass"),
216             "9:13: " + getCheckMessage(MSG_KEY,
217                 "com.puppycrawl.tools.checkstyle.checks.coding.illegaltype."
218                     + "InputIllegalTypeMemberModifiers.AbstractClass"),
219             "16:13: " + getCheckMessage(MSG_KEY, "java.util.TreeSet"),
220             "17:13: " + getCheckMessage(MSG_KEY, "TreeSet"),
221             "23:15: " + getCheckMessage(MSG_KEY,
222                 "com.puppycrawl.tools.checkstyle.checks.coding.illegaltype."
223                     + "InputIllegalTypeMemberModifiers.AbstractClass"),
224             "25:25: " + getCheckMessage(MSG_KEY, "java.util.TreeSet"),
225             "33:15: " + getCheckMessage(MSG_KEY, "AbstractClass"),
226         };
227 
228         verify(checkConfig, getPath("InputIllegalTypeMemberModifiers.java"), expected);
229     }
230 
231     @Test
232     public void testClearDataBetweenFiles() throws Exception {
233         final DefaultConfiguration checkConfig = createModuleConfig(IllegalTypeCheck.class);
234         final String violationFile = getPath("InputIllegalType.java");
235         checkConfig.addAttribute("illegalClassNames", "java.util.TreeSet");
236         final String[] expected = {
237             "16:13: " + getCheckMessage(MSG_KEY, "java.util.TreeSet"),
238             "17:13: " + getCheckMessage(MSG_KEY, "TreeSet"),
239         };
240 
241         verify(createChecker(checkConfig), new File[] {
242             new File(violationFile),
243             new File(getPath("InputIllegalTypeSimilarClassName.java")),
244         }, violationFile, expected);
245     }
246 
247     @Test
248     public void testTokensNotNull() {
249         final IllegalTypeCheck check = new IllegalTypeCheck();
250         Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
251         Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
252         Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
253     }
254 
255     @Test
256     public void testImproperToken() {
257         final IllegalTypeCheck check = new IllegalTypeCheck();
258 
259         final DetailAST classDefAst = new DetailAST();
260         classDefAst.setType(TokenTypes.CLASS_DEF);
261 
262         try {
263             check.visitToken(classDefAst);
264             Assert.fail("IllegalStateException is expected");
265         }
266         catch (IllegalStateException ex) {
267             // it is OK
268         }
269     }
270 
271 }