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