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