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.naming;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.naming.AbbreviationAsWordInNameCheck.MSG_KEY;
23  
24  import org.junit.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
27  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
28  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
29  
30  public class AbbreviationAsWordInNameCheckTest extends AbstractModuleTestSupport {
31  
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/naming/abbreviationaswordinname";
35      }
36  
37      @Test
38      public void testTypeNamesForThreePermittedCapitalLetters() throws Exception {
39  
40          final DefaultConfiguration checkConfig =
41              createModuleConfig(AbbreviationAsWordInNameCheck.class);
42          checkConfig.addAttribute("allowedAbbreviationLength", "3");
43          checkConfig.addAttribute("allowedAbbreviations", "III");
44          checkConfig.addAttribute("tokens", "CLASS_DEF");
45          checkConfig.addAttribute("ignoreOverriddenMethods", "true");
46          final int expectedCapitalCount = 4;
47  
48          final String[] expected = {
49              "9: " + getWarningMessage("FactoryWithBADNAme", expectedCapitalCount),
50              "12: " + getWarningMessage("AbstractCLASSName", expectedCapitalCount),
51              "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount),
52              "37: " + getWarningMessage("WellNamedFACTORY", expectedCapitalCount),
53          };
54  
55          verify(checkConfig, getPath("InputAbbreviationAsWordInNameType.java"), expected);
56      }
57  
58      @Test
59      public void testTypeNamesForFourPermittedCapitalLetters() throws Exception {
60  
61          final DefaultConfiguration checkConfig =
62              createModuleConfig(AbbreviationAsWordInNameCheck.class);
63          checkConfig.addAttribute("allowedAbbreviationLength", "4");
64          checkConfig.addAttribute("allowedAbbreviations", "CLASS,FACTORY");
65          checkConfig.addAttribute("tokens", "CLASS_DEF");
66          checkConfig.addAttribute("ignoreOverriddenMethods", "true");
67          final int expectedCapitalCount = 5;
68  
69          final String[] expected = {
70              "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount),
71          };
72  
73          verify(checkConfig, getPath("InputAbbreviationAsWordInNameType.java"), expected);
74      }
75  
76      @Test
77      public void testTypeNamesForFivePermittedCapitalLetters() throws Exception {
78  
79          final DefaultConfiguration checkConfig =
80              createModuleConfig(AbbreviationAsWordInNameCheck.class);
81          checkConfig.addAttribute("allowedAbbreviationLength", "5");
82          checkConfig.addAttribute("allowedAbbreviations", "CLASS");
83          checkConfig.addAttribute("tokens", "CLASS_DEF");
84          checkConfig.addAttribute("ignoreOverriddenMethods", "true");
85          final int expectedCapitalCount = 6;
86          final String[] expected = {
87              "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount),
88              "37: " + getWarningMessage("WellNamedFACTORY", expectedCapitalCount),
89          };
90  
91          verify(checkConfig, getPath("InputAbbreviationAsWordInNameType.java"), expected);
92      }
93  
94      @Test
95      public void testTypeAndVariablesAndMethodNames() throws Exception {
96  
97          final DefaultConfiguration checkConfig =
98              createModuleConfig(AbbreviationAsWordInNameCheck.class);
99          checkConfig.addAttribute("allowedAbbreviationLength", "5");
100         checkConfig.addAttribute("allowedAbbreviations", "CLASS");
101         checkConfig.addAttribute("tokens", "CLASS_DEF"
102             + ",VARIABLE_DEF"
103             + ",METHOD_DEF,ENUM_DEF,ENUM_CONSTANT_DEF"
104             + ",PARAMETER_DEF,INTERFACE_DEF,ANNOTATION_DEF");
105         checkConfig.addAttribute("ignoreOverriddenMethods", "true");
106         final int expectedCapitalCount = 6;
107 
108         final String[] expected = {
109             "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount),
110             "37: " + getWarningMessage("WellNamedFACTORY", expectedCapitalCount),
111             "38: " + getWarningMessage("marazmaticMETHODName", expectedCapitalCount),
112             "39: " + getWarningMessage("marazmaticVARIABLEName", expectedCapitalCount),
113             "40: " + getWarningMessage("MARAZMATICVariableName", expectedCapitalCount),
114             "58: " + getWarningMessage("serialNUMBER", expectedCapitalCount),
115         };
116 
117         verify(checkConfig, getPath("InputAbbreviationAsWordInNameType.java"), expected);
118     }
119 
120     @Test
121     public void testTypeAndVariablesAndMethodNamesWithNoIgnores() throws Exception {
122 
123         final DefaultConfiguration checkConfig =
124             createModuleConfig(AbbreviationAsWordInNameCheck.class);
125         checkConfig.addAttribute("allowedAbbreviationLength", "5");
126         checkConfig.addAttribute("allowedAbbreviations", "NUMBER,MARAZMATIC,VARIABLE");
127         checkConfig.addAttribute("ignoreStatic", "false");
128         checkConfig.addAttribute("ignoreFinal", "false");
129         checkConfig.addAttribute("tokens", "CLASS_DEF"
130             + ",VARIABLE_DEF"
131             + ",METHOD_DEF,ENUM_DEF,ENUM_CONSTANT_DEF"
132             + ",PARAMETER_DEF,INTERFACE_DEF,ANNOTATION_DEF");
133         checkConfig.addAttribute("ignoreOverriddenMethods", "true");
134         final int expectedCapitalCount = 6;
135 
136         final String[] expected = {
137             "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount),
138             "37: " + getWarningMessage("WellNamedFACTORY", expectedCapitalCount),
139             "38: " + getWarningMessage("marazmaticMETHODName", expectedCapitalCount),
140             "66: " + getWarningMessage("VALUEEEE", expectedCapitalCount),
141             "72: " + getWarningMessage("VALUEEEE", expectedCapitalCount),
142             "78: " + getWarningMessage("VALUEEEE", expectedCapitalCount),
143             "84: " + getWarningMessage("VALUEEEE", expectedCapitalCount),
144         };
145 
146         verify(checkConfig, getPath("InputAbbreviationAsWordInNameType.java"), expected);
147     }
148 
149     @Test
150     public void testTypeAndVariablesAndMethodNamesWithIgnores() throws Exception {
151 
152         final DefaultConfiguration checkConfig =
153             createModuleConfig(AbbreviationAsWordInNameCheck.class);
154         checkConfig.addAttribute("allowedAbbreviationLength", "5");
155         checkConfig.addAttribute("allowedAbbreviations", "NUMBER,MARAZMATIC,VARIABLE");
156         checkConfig.addAttribute("ignoreStatic", "true");
157         checkConfig.addAttribute("ignoreFinal", "true");
158         checkConfig.addAttribute("tokens", "CLASS_DEF"
159             + ",VARIABLE_DEF"
160             + ",METHOD_DEF,ENUM_DEF,ENUM_CONSTANT_DEF"
161             + ",PARAMETER_DEF,INTERFACE_DEF,ANNOTATION_DEF");
162         checkConfig.addAttribute("ignoreOverriddenMethods", "true");
163         final int expectedCapitalCount = 6;
164 
165         final String[] expected = {
166             "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount),
167             "37: " + getWarningMessage("WellNamedFACTORY", expectedCapitalCount),
168             "38: " + getWarningMessage("marazmaticMETHODName", expectedCapitalCount),
169         };
170 
171         verify(checkConfig, getPath("InputAbbreviationAsWordInNameType.java"), expected);
172     }
173 
174     @Test
175     public void testTypeAndVariablesAndMethodNamesWithIgnoresFinal() throws Exception {
176 
177         final DefaultConfiguration checkConfig =
178             createModuleConfig(AbbreviationAsWordInNameCheck.class);
179         checkConfig.addAttribute("allowedAbbreviationLength", "4");
180         checkConfig.addAttribute("allowedAbbreviations", "MARAZMATIC,VARIABLE");
181         checkConfig.addAttribute("ignoreStatic", "false");
182         checkConfig.addAttribute("ignoreFinal", "true");
183         checkConfig.addAttribute("tokens", "CLASS_DEF"
184             + ",VARIABLE_DEF"
185             + ",METHOD_DEF,ENUM_DEF,ENUM_CONSTANT_DEF"
186             + ",PARAMETER_DEF,INTERFACE_DEF,ANNOTATION_DEF");
187         checkConfig.addAttribute("ignoreOverriddenMethods", "true");
188         final int expectedCapitalCount = 5;
189 
190         final String[] expected = {
191             "12: " + getWarningMessage("AbstractCLASSName", expectedCapitalCount),
192             "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount),
193             "37: " + getWarningMessage("WellNamedFACTORY", expectedCapitalCount),
194             "38: " + getWarningMessage("marazmaticMETHODName", expectedCapitalCount),
195             "58: " + getWarningMessage("serialNUMBER", expectedCapitalCount), // not in ignore list
196             "60: "
197                 + getWarningMessage("s2erialNUMBER", expectedCapitalCount), // no ignore for static
198         };
199 
200         verify(checkConfig, getPath("InputAbbreviationAsWordInNameType.java"), expected);
201     }
202 
203     @Test
204     public void testTypeAndVariablesAndMethodNamesWithIgnoresStatic() throws Exception {
205 
206         final DefaultConfiguration checkConfig =
207             createModuleConfig(AbbreviationAsWordInNameCheck.class);
208         checkConfig.addAttribute("allowedAbbreviationLength", "5");
209         checkConfig.addAttribute("allowedAbbreviations", "MARAZMATIC,VARIABLE");
210         checkConfig.addAttribute("ignoreStatic", "true");
211         checkConfig.addAttribute("ignoreFinal", "false");
212         checkConfig.addAttribute("tokens", "CLASS_DEF"
213             + ",VARIABLE_DEF"
214             + ",METHOD_DEF,ENUM_DEF,ENUM_CONSTANT_DEF"
215             + ",PARAMETER_DEF,INTERFACE_DEF,ANNOTATION_DEF");
216         checkConfig.addAttribute("ignoreOverriddenMethods", "true");
217         final int expectedCapitalCount = 6;
218 
219         final String[] expected = {
220             "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount),
221             "37: " + getWarningMessage("WellNamedFACTORY", expectedCapitalCount),
222             "38: " + getWarningMessage("marazmaticMETHODName", expectedCapitalCount),
223             "58: " + getWarningMessage("serialNUMBER", expectedCapitalCount), // not in ignore list
224             "59: "
225                 + getWarningMessage("s1erialNUMBER", expectedCapitalCount), // no ignore for final
226         };
227 
228         verify(checkConfig, getPath("InputAbbreviationAsWordInNameType.java"), expected);
229     }
230 
231     @Test
232     public void testTypeNamesForThreePermittedCapitalLettersWithOverriddenMethod()
233             throws Exception {
234 
235         final DefaultConfiguration checkConfig =
236             createModuleConfig(AbbreviationAsWordInNameCheck.class);
237         checkConfig.addAttribute("allowedAbbreviationLength", "3");
238         checkConfig.addAttribute("allowedAbbreviations", "");
239         checkConfig.addAttribute("tokens", "CLASS_DEF, METHOD_DEF");
240         checkConfig.addAttribute("ignoreOverriddenMethods", "true");
241         final int expectedCapitalCount = 4;
242 
243         final String[] expected = {
244             "22: " + getWarningMessage("oveRRRRRrriddenMethod", expectedCapitalCount),
245         };
246 
247         verify(checkConfig,
248                 getPath("InputAbbreviationAsWordInNameOverridableMethod.java"), expected);
249     }
250 
251     @Test
252     public void testTypeNamesForZeroPermittedCapitalLetter() throws Exception {
253         final DefaultConfiguration checkConfig =
254                 createModuleConfig(AbbreviationAsWordInNameCheck.class);
255         checkConfig.addAttribute("allowedAbbreviationLength",
256                 "0");
257         checkConfig.addAttribute("allowedAbbreviations", "");
258         checkConfig.addAttribute("ignoreStatic", "false");
259         checkConfig.addAttribute("ignoreFinal", "false");
260         checkConfig.addAttribute("ignoreOverriddenMethods", "false");
261         checkConfig.addAttribute("tokens", "CLASS_DEF,INTERFACE_DEF,ENUM_DEF,"
262             + "ANNOTATION_DEF,ANNOTATION_FIELD_DEF,ENUM_CONSTANT_DEF,"
263             + "PARAMETER_DEF,VARIABLE_DEF,METHOD_DEF");
264         final int expectedCapitalCount = 1;
265         final String[] expected = {
266             "3: " + getWarningMessage("IIIInputAbstractClassName", expectedCapitalCount),
267             "6: " + getWarningMessage("NonAAAAbstractClassName", expectedCapitalCount),
268             "9: " + getWarningMessage("FactoryWithBADNAme", expectedCapitalCount),
269             "12: " + getWarningMessage("AbstractCLASSName", expectedCapitalCount),
270             "32: " + getWarningMessage("AbstractINNERRClass", expectedCapitalCount),
271             "37: " + getWarningMessage("WellNamedFACTORY", expectedCapitalCount),
272             "38: " + getWarningMessage("marazmaticMETHODName", expectedCapitalCount),
273             "39: " + getWarningMessage("marazmaticVARIABLEName", expectedCapitalCount),
274             "40: " + getWarningMessage("MARAZMATICVariableName", expectedCapitalCount),
275             "46: " + getWarningMessage("RIGHT", expectedCapitalCount),
276             "47: " + getWarningMessage("LEFT", expectedCapitalCount),
277             "48: " + getWarningMessage("UP", expectedCapitalCount),
278             "49: " + getWarningMessage("DOWN", expectedCapitalCount),
279             "57: " + getWarningMessage("NonAAAAbstractClassName2", expectedCapitalCount),
280             "58: " + getWarningMessage("serialNUMBER", expectedCapitalCount),
281             "59: " + getWarningMessage("s1erialNUMBER", expectedCapitalCount),
282             "60: " + getWarningMessage("s2erialNUMBER", expectedCapitalCount),
283             "61: " + getWarningMessage("s3erialNUMBER", expectedCapitalCount),
284             "66: " + getWarningMessage("VALUEEEE", expectedCapitalCount),
285             "72: " + getWarningMessage("VALUEEEE", expectedCapitalCount),
286             "78: " + getWarningMessage("VALUEEEE", expectedCapitalCount),
287             "84: " + getWarningMessage("VALUEEEE", expectedCapitalCount),
288             "88: " + getWarningMessage("FIleNameFormatException", expectedCapitalCount),
289             "90: " + getWarningMessage("serialVersionUID", expectedCapitalCount),
290             "98: " + getWarningMessage("userID", expectedCapitalCount),
291             "107: " + getWarningMessage("VALUE", expectedCapitalCount),
292             "111: " + getWarningMessage("VALUE", expectedCapitalCount),
293             "115: " + getWarningMessage("VALUE", expectedCapitalCount),
294             "119: " + getWarningMessage("VALUE", expectedCapitalCount),
295         };
296         verify(checkConfig,
297                 getPath("InputAbbreviationAsWordInNameType.java"), expected);
298     }
299 
300     @Test
301     public void testNullPointerException() throws Exception {
302 
303         final DefaultConfiguration checkConfig =
304             createModuleConfig(AbbreviationAsWordInNameCheck.class);
305         checkConfig.addAttribute("allowedAbbreviationLength", "2");
306         checkConfig.addAttribute("ignoreFinal", "false");
307         checkConfig.addAttribute("allowedAbbreviations", null);
308 
309         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
310 
311         verify(checkConfig,
312                 getPath("InputAbbreviationAsWordInNameAbstractMultisetSetCount.java"),
313                 expected);
314     }
315 
316     private String getWarningMessage(String typeName, int expectedCapitalCount) {
317         return getCheckMessage(MSG_KEY, typeName, expectedCapitalCount);
318     }
319 
320     @Test
321     public void testReceiver() throws Exception {
322         final DefaultConfiguration checkConfig =
323             createModuleConfig(AbbreviationAsWordInNameCheck.class);
324         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
325 
326         verify(checkConfig, getPath("InputAbbreviationAsWordInNameReceiver.java"),
327                 expected);
328     }
329 }