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;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck.MSG_KEY_MAX;
23  import static com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck.MSG_KEY_MIN;
24  import static com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck.MSG_KEY_SUM_MAX;
25  import static com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck.MSG_KEY_SUM_MIN;
26  
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.utils.CommonUtils;
32  
33  public class DescendantTokenCheckTest extends AbstractModuleTestSupport {
34  
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/descendanttoken";
38      }
39  
40      @Test
41      public void testDefault()
42              throws Exception {
43          final DefaultConfiguration checkConfig =
44              createModuleConfig(DescendantTokenCheck.class);
45          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
46          verify(checkConfig, getPath("InputDescendantTokenIllegalTokens.java"), expected);
47      }
48  
49      @Test
50      public void testMaximumNumber()
51              throws Exception {
52          final DefaultConfiguration checkConfig =
53              createModuleConfig(DescendantTokenCheck.class);
54          checkConfig.addAttribute("tokens", "LITERAL_NATIVE");
55          checkConfig.addAttribute("limitedTokens", "LITERAL_NATIVE");
56          checkConfig.addAttribute("maximumNumber", "0");
57          final String[] expected = {
58              "17:12: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "LITERAL_NATIVE", "LITERAL_NATIVE"),
59          };
60          verify(checkConfig, getPath("InputDescendantTokenIllegalTokens.java"), expected);
61      }
62  
63      @Test
64      public void testMessage()
65              throws Exception {
66          final DefaultConfiguration checkConfig =
67              createModuleConfig(DescendantTokenCheck.class);
68          checkConfig.addAttribute("tokens", "LITERAL_NATIVE");
69          checkConfig.addAttribute("limitedTokens", "LITERAL_NATIVE");
70          checkConfig.addAttribute("maximumNumber", "0");
71          checkConfig.addAttribute("maximumMessage", "Using ''native'' is not allowed.");
72          final String[] expected = {
73              "17:12: Using 'native' is not allowed.",
74          };
75          verify(checkConfig, getPath("InputDescendantTokenIllegalTokens.java"), expected);
76      }
77  
78      @Test
79      public void testMinimumNumber()
80              throws Exception {
81          final DefaultConfiguration checkConfig =
82              createModuleConfig(DescendantTokenCheck.class);
83          checkConfig.addAttribute("tokens", "LITERAL_SWITCH");
84          checkConfig.addAttribute("limitedTokens", "LITERAL_DEFAULT");
85          checkConfig.addAttribute("minimumNumber", "2");
86          final String[] expected = {
87              "8:9: " + getCheckMessage(MSG_KEY_MIN, 1, 2, "LITERAL_SWITCH", "LITERAL_DEFAULT"),
88          };
89          verify(checkConfig, getPath("InputDescendantTokenIllegalTokens.java"), expected);
90      }
91  
92      @Test
93      public void testMinimumDepth()
94              throws Exception {
95          final DefaultConfiguration checkConfig =
96              createModuleConfig(DescendantTokenCheck.class);
97          checkConfig.addAttribute("tokens", "LITERAL_SWITCH");
98          checkConfig.addAttribute("limitedTokens", "LITERAL_DEFAULT");
99          checkConfig.addAttribute("maximumNumber", "0");
100         checkConfig.addAttribute("minimumDepth", "3");
101         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
102         verify(checkConfig, getPath("InputDescendantTokenIllegalTokens.java"), expected);
103     }
104 
105     @Test
106     public void testMaximumDepth()
107             throws Exception {
108         final DefaultConfiguration checkConfig =
109             createModuleConfig(DescendantTokenCheck.class);
110         checkConfig.addAttribute("tokens", "LITERAL_SWITCH");
111         checkConfig.addAttribute("limitedTokens", "LITERAL_DEFAULT");
112         checkConfig.addAttribute("maximumNumber", "0");
113         checkConfig.addAttribute("maximumDepth", "1");
114         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
115         verify(checkConfig, getPath("InputDescendantTokenIllegalTokens.java"), expected);
116     }
117 
118     @Test
119     public void testEmptyStatements()
120             throws Exception {
121         final DefaultConfiguration checkConfig =
122             createModuleConfig(DescendantTokenCheck.class);
123         checkConfig.addAttribute("tokens", "EMPTY_STAT");
124         checkConfig.addAttribute("limitedTokens", "EMPTY_STAT");
125         checkConfig.addAttribute("maximumNumber", "0");
126         checkConfig.addAttribute("maximumDepth", "0");
127         checkConfig.addAttribute("maximumMessage", "Empty statement.");
128 
129         final String[] expected = {
130             "7:7: Empty statement.",
131             "12:7: Empty statement.",
132             "17:19: Empty statement.",
133             "21:10: Empty statement.",
134             "24:16: Empty statement.",
135             "28:10: Empty statement.",
136             "38:10: Empty statement.",
137             "44:13: Empty statement.",
138             "46:13: Empty statement.",
139             "49:19: Empty statement.",
140             "53:10: Empty statement.",
141             "56:9: Empty statement.",
142             "61:10: Empty statement.",
143             "67:10: Empty statement.",
144             "71:10: Empty statement.",
145             "75:10: Empty statement.",
146         };
147 
148         verify(checkConfig, getPath("InputDescendantTokenEmptyStatement.java"), expected);
149     }
150 
151     @Test
152     public void testMissingSwitchDefault() throws Exception {
153         final DefaultConfiguration checkConfig =
154             createModuleConfig(DescendantTokenCheck.class);
155         checkConfig.addAttribute("tokens", "LITERAL_SWITCH");
156         checkConfig.addAttribute("limitedTokens", "LITERAL_DEFAULT");
157         checkConfig.addAttribute("minimumNumber", "1");
158         checkConfig.addAttribute("maximumDepth", "2");
159         checkConfig.addAttribute("minimumMessage", "switch without \"default\" clause.");
160 
161         final String[] expected = {
162             "17:9: switch without \"default\" clause.",
163         };
164 
165         verify(checkConfig, getPath("InputDescendantTokenMissingSwitchDefault.java"), expected);
166     }
167 
168     @Test
169     public void testStringLiteralEquality() throws Exception {
170         final DefaultConfiguration checkConfig =
171             createModuleConfig(DescendantTokenCheck.class);
172         checkConfig.addAttribute("tokens", "EQUAL,NOT_EQUAL");
173         checkConfig.addAttribute("limitedTokens", "STRING_LITERAL");
174         checkConfig.addAttribute("maximumNumber", "0");
175         checkConfig.addAttribute("maximumDepth", "1");
176         checkConfig.addAttribute("maximumMessage",
177             "Literal Strings should be compared using equals(), not ''==''.");
178 
179         final String[] expected = {
180             "7:18: Literal Strings should be compared using equals(), not '=='.",
181             "12:20: Literal Strings should be compared using equals(), not '=='.",
182             "17:22: Literal Strings should be compared using equals(), not '=='.",
183         };
184         verify(checkConfig, getPath("InputDescendantTokenStringLiteralEquality.java"), expected);
185     }
186 
187     @Test
188     public void testIllegalTokenDefault() throws Exception {
189         final DefaultConfiguration checkConfig =
190             createModuleConfig(DescendantTokenCheck.class);
191         checkConfig.addAttribute("tokens", "LITERAL_SWITCH, POST_INC, POST_DEC");
192         checkConfig.addAttribute("limitedTokens", "LITERAL_SWITCH, POST_INC, POST_DEC");
193         checkConfig.addAttribute("maximumNumber", "0");
194         checkConfig.addAttribute("maximumDepth", "0");
195         checkConfig.addAttribute("maximumMessage", "Using ''{2}'' is not allowed.");
196 
197         final String[] expected = {
198             "8:9: Using 'LITERAL_SWITCH' is not allowed.",
199             "11:18: Using 'POST_DEC' is not allowed.",
200             "12:18: Using 'POST_INC' is not allowed.",
201         };
202         verify(checkConfig, getPath("InputDescendantTokenIllegalTokens.java"), expected);
203     }
204 
205     @Test
206     public void testIllegalTokenNative() throws Exception {
207         final DefaultConfiguration checkConfig =
208             createModuleConfig(DescendantTokenCheck.class);
209         checkConfig.addAttribute("tokens", "LITERAL_NATIVE");
210         checkConfig.addAttribute("limitedTokens", "LITERAL_NATIVE");
211         checkConfig.addAttribute("maximumNumber", "0");
212         checkConfig.addAttribute("maximumDepth", "0");
213         checkConfig.addAttribute("maximumMessage", "Using ''{2}'' is not allowed.");
214 
215         final String[] expected = {
216             "17:12: Using 'LITERAL_NATIVE' is not allowed.",
217         };
218         verify(checkConfig, getPath("InputDescendantTokenIllegalTokens.java"), expected);
219     }
220 
221     @Test
222     public void testReturnFromCatch() throws Exception {
223         final DefaultConfiguration checkConfig = createModuleConfig(DescendantTokenCheck.class);
224 
225         checkConfig.addAttribute("tokens", "LITERAL_CATCH");
226         checkConfig.addAttribute("limitedTokens", "LITERAL_RETURN");
227         checkConfig.addAttribute("maximumNumber", "0");
228         checkConfig.addAttribute("maximumMessage", "Return from catch is not allowed.");
229 
230         final String[] expected = {
231             "7:11: Return from catch is not allowed.",
232             "15:11: Return from catch is not allowed.",
233         };
234 
235         verify(checkConfig, getPath("InputDescendantTokenReturnFromCatch.java"), expected);
236     }
237 
238     @Test
239     public void testReturnFromFinally() throws Exception {
240         final DefaultConfiguration checkConfig = createModuleConfig(DescendantTokenCheck.class);
241 
242         checkConfig.addAttribute("tokens", "LITERAL_FINALLY");
243         checkConfig.addAttribute("limitedTokens", "LITERAL_RETURN");
244         checkConfig.addAttribute("maximumNumber", "0");
245         checkConfig.addAttribute("maximumMessage", "Return from finally is not allowed.");
246 
247         final String[] expected = {
248             "7:11: Return from finally is not allowed.",
249             "15:11: Return from finally is not allowed.",
250         };
251 
252         verify(checkConfig, getPath("InputDescendantTokenReturnFromFinally.java"), expected);
253     }
254 
255     @Test
256     public void testNoSum() throws Exception {
257         final DefaultConfiguration checkConfig = createModuleConfig(DescendantTokenCheck.class);
258 
259         checkConfig.addAttribute("tokens", "NOT_EQUAL,EQUAL");
260         checkConfig.addAttribute("limitedTokens", "LITERAL_THIS,LITERAL_NULL");
261         checkConfig.addAttribute("maximumNumber", "1");
262         checkConfig.addAttribute("maximumMessage", "What are you doing?");
263 
264         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
265 
266         verify(checkConfig, getPath("InputDescendantTokenReturnFromFinally.java"), expected);
267     }
268 
269     @Test
270     public void testWithSumCustomMsg() throws Exception {
271         final DefaultConfiguration checkConfig = createModuleConfig(DescendantTokenCheck.class);
272         checkConfig.addAttribute("tokens", "NOT_EQUAL,EQUAL");
273         checkConfig.addAttribute("limitedTokens", "LITERAL_THIS,LITERAL_NULL");
274         checkConfig.addAttribute("maximumNumber", "1");
275         checkConfig.addAttribute("maximumDepth", "1");
276         checkConfig.addAttribute("maximumMessage", "this cannot be null.");
277         checkConfig.addAttribute("sumTokenCounts", "true");
278 
279         final String[] expected = {
280             "22:32: this cannot be null.",
281             "22:50: this cannot be null.",
282             "23:33: this cannot be null.",
283             "23:51: this cannot be null.",
284         };
285 
286         verify(checkConfig, getPath("InputDescendantTokenReturnFromFinally.java"), expected);
287     }
288 
289     @Test
290     public void testWithSumDefaultMsg() throws Exception {
291         final DefaultConfiguration checkConfig = createModuleConfig(DescendantTokenCheck.class);
292         checkConfig.addAttribute("tokens", "NOT_EQUAL,EQUAL");
293         checkConfig.addAttribute("limitedTokens", "LITERAL_THIS,LITERAL_NULL");
294         checkConfig.addAttribute("maximumNumber", "1");
295         checkConfig.addAttribute("maximumDepth", "1");
296         checkConfig.addAttribute("sumTokenCounts", "true");
297 
298         final String[] expected = {
299             "22:32: " + getCheckMessage(MSG_KEY_SUM_MAX, 2, 1, "EQUAL"),
300             "22:50: " + getCheckMessage(MSG_KEY_SUM_MAX, 2, 1, "EQUAL"),
301             "23:33: " + getCheckMessage(MSG_KEY_SUM_MAX, 2, 1, "NOT_EQUAL"),
302             "23:51: " + getCheckMessage(MSG_KEY_SUM_MAX, 2, 1, "NOT_EQUAL"),
303         };
304 
305         verify(checkConfig, getPath("InputDescendantTokenReturnFromFinally.java"), expected);
306     }
307 
308     @Test
309     public void testWithSumLessThenMinDefMsg() throws Exception {
310         final DefaultConfiguration checkConfig = createModuleConfig(DescendantTokenCheck.class);
311         checkConfig.addAttribute("tokens", "NOT_EQUAL,EQUAL");
312         checkConfig.addAttribute("limitedTokens", "LITERAL_THIS,LITERAL_NULL");
313         checkConfig.addAttribute("minimumNumber", "3");
314         checkConfig.addAttribute("sumTokenCounts", "true");
315 
316         final String[] expected = {
317             "16:44: " + getCheckMessage(MSG_KEY_SUM_MIN, 0, 3, "EQUAL"),
318             "22:32: " + getCheckMessage(MSG_KEY_SUM_MIN, 2, 3, "EQUAL"),
319             "22:50: " + getCheckMessage(MSG_KEY_SUM_MIN, 2, 3, "EQUAL"),
320             "23:33: " + getCheckMessage(MSG_KEY_SUM_MIN, 2, 3, "NOT_EQUAL"),
321             "23:51: " + getCheckMessage(MSG_KEY_SUM_MIN, 2, 3, "NOT_EQUAL"),
322             "24:54: " + getCheckMessage(MSG_KEY_SUM_MIN, 2, 3, "EQUAL"),
323             "24:77: " + getCheckMessage(MSG_KEY_SUM_MIN, 1, 3, "EQUAL"),
324         };
325 
326         verify(checkConfig, getPath("InputDescendantTokenReturnFromFinally.java"), expected);
327     }
328 
329     @Test
330     public void testWithSumLessThenMinCustomMsg() throws Exception {
331         final DefaultConfiguration checkConfig = createModuleConfig(DescendantTokenCheck.class);
332         checkConfig.addAttribute("tokens", "NOT_EQUAL,EQUAL");
333         checkConfig.addAttribute("limitedTokens", "LITERAL_THIS,LITERAL_NULL");
334         checkConfig.addAttribute("minimumNumber", "3");
335         checkConfig.addAttribute("sumTokenCounts", "true");
336         checkConfig.addAttribute("minimumMessage", "custom message");
337 
338         final String[] expected = {
339             "16:44: custom message",
340             "22:32: custom message",
341             "22:50: custom message",
342             "23:33: custom message",
343             "23:51: custom message",
344             "24:54: custom message",
345             "24:77: custom message",
346         };
347 
348         verify(checkConfig, getPath("InputDescendantTokenReturnFromFinally.java"), expected);
349     }
350 
351     @Test
352     public void testMaxTokenType() throws Exception {
353         final DefaultConfiguration checkConfig =
354                 createModuleConfig(DescendantTokenCheck.class);
355         checkConfig.addAttribute("tokens", "OBJBLOCK");
356         checkConfig.addAttribute("limitedTokens", "LCURLY,RCURLY");
357         checkConfig.addAttribute("maximumNumber", "0");
358         checkConfig.addAttribute("maximumDepth", "2");
359         final String[] expected = {
360             "6:48: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "OBJBLOCK", "LCURLY"),
361             "6:48: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "OBJBLOCK", "RCURLY"),
362         };
363         verify(checkConfig, getPath("InputDescendantTokenLastTokenType.java"), expected);
364     }
365 
366     @Test
367     public void testMaxTokenTypeReverseOrder() throws Exception {
368         final DefaultConfiguration checkConfig =
369                 createModuleConfig(DescendantTokenCheck.class);
370         checkConfig.addAttribute("tokens", "OBJBLOCK");
371         checkConfig.addAttribute("limitedTokens", "RCURLY,LCURLY");
372         checkConfig.addAttribute("maximumNumber", "0");
373         checkConfig.addAttribute("maximumDepth", "2");
374         final String[] expected = {
375             "6:48: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "OBJBLOCK", "LCURLY"),
376             "6:48: " + getCheckMessage(MSG_KEY_MAX, 1, 0, "OBJBLOCK", "RCURLY"),
377         };
378         verify(checkConfig, getPath("InputDescendantTokenLastTokenType.java"), expected);
379     }
380 
381 }