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.javadoc;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocVariableCheck.MSG_JAVADOC_MISSING;
23  import static org.junit.Assert.assertArrayEquals;
24  
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.api.Scope;
30  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
32  
33  public class JavadocVariableCheckTest
34      extends AbstractModuleTestSupport {
35  
36      @Override
37      protected String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/javadoc/javadocvariable";
39      }
40  
41      @Test
42      public void testGetRequiredTokens() {
43          final JavadocVariableCheck javadocVariableCheck = new JavadocVariableCheck();
44          final int[] actual = javadocVariableCheck.getRequiredTokens();
45          final int[] expected = {
46              TokenTypes.VARIABLE_DEF,
47          };
48          assertArrayEquals("Default required tokens are invalid", expected, actual);
49      }
50  
51      @Test
52      public void testGetAcceptableTokens() {
53          final JavadocVariableCheck javadocVariableCheck = new JavadocVariableCheck();
54  
55          final int[] actual = javadocVariableCheck.getAcceptableTokens();
56          final int[] expected = {
57              TokenTypes.VARIABLE_DEF,
58              TokenTypes.ENUM_CONSTANT_DEF,
59          };
60  
61          assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
62      }
63  
64      @Test
65      public void testDefault()
66              throws Exception {
67          final DefaultConfiguration checkConfig =
68              createModuleConfig(JavadocVariableCheck.class);
69          final String[] expected = {
70              "11:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
71              "304:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
72              "311:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
73              "330:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
74          };
75          verify(checkConfig, getPath("InputJavadocVariableTags.java"), expected);
76      }
77  
78      @Test
79      public void testAnother()
80              throws Exception {
81          final DefaultConfiguration checkConfig =
82              createModuleConfig(JavadocVariableCheck.class);
83          final String[] expected = {
84              "17:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
85              "24:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
86              "30:13: " + getCheckMessage(MSG_JAVADOC_MISSING),
87          };
88          verify(checkConfig, getPath("InputJavadocVariableInner.java"), expected);
89      }
90  
91      @Test
92      public void testAnother2()
93              throws Exception {
94          final DefaultConfiguration checkConfig =
95              createModuleConfig(JavadocVariableCheck.class);
96          checkConfig.addAttribute("scope", Scope.PUBLIC.getName());
97          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
98          verify(checkConfig, getPath("InputJavadocVariableInner.java"), expected);
99      }
100 
101     @Test
102     public void testAnother3()
103             throws Exception {
104         final DefaultConfiguration checkConfig =
105             createModuleConfig(JavadocVariableCheck.class);
106         final String[] expected = {
107             "11:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
108             "16:13: " + getCheckMessage(MSG_JAVADOC_MISSING),
109             "36:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
110             "43:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
111             "44:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
112             "45:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
113             "46:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
114         };
115         verify(checkConfig, getPath("InputJavadocVariablePublicOnly.java"), expected);
116     }
117 
118     @Test
119     public void testAnother4()
120             throws Exception {
121         final DefaultConfiguration checkConfig =
122             createModuleConfig(JavadocVariableCheck.class);
123         checkConfig.addAttribute("scope", Scope.PUBLIC.getName());
124         final String[] expected = {
125             "46:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
126         };
127         verify(checkConfig, getPath("InputJavadocVariablePublicOnly.java"), expected);
128     }
129 
130     @Test
131     public void testScopes() throws Exception {
132         final DefaultConfiguration checkConfig =
133             createModuleConfig(JavadocVariableCheck.class);
134         final String[] expected = {
135             "5:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
136             "6:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
137             "7:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
138             "8:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
139             "16:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
140             "17:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
141             "18:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
142             "19:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
143             "28:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
144             "29:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
145             "30:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
146             "31:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
147             "40:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
148             "41:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
149             "42:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
150             "43:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
151             "53:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
152             "54:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
153             "55:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
154             "56:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
155             "64:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
156             "65:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
157             "66:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
158             "67:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
159             "76:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
160             "77:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
161             "78:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
162             "79:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
163             "88:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
164             "89:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
165             "90:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
166             "91:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
167             "100:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
168             "101:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
169             "102:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
170             "103:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
171             "113:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
172         };
173         verify(checkConfig,
174                getPath("InputJavadocVariableNoJavadoc.java"),
175                expected);
176     }
177 
178     @Test
179     public void testScopes2() throws Exception {
180         final DefaultConfiguration checkConfig =
181             createModuleConfig(JavadocVariableCheck.class);
182         checkConfig.addAttribute("scope", Scope.PROTECTED.getName());
183         final String[] expected = {
184             "5:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
185             "6:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
186             "16:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
187             "17:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
188         };
189         verify(checkConfig,
190                getPath("InputJavadocVariableNoJavadoc.java"),
191                expected);
192     }
193 
194     @Test
195     public void testExcludeScope() throws Exception {
196         final DefaultConfiguration checkConfig =
197             createModuleConfig(JavadocVariableCheck.class);
198         checkConfig.addAttribute("scope", Scope.PRIVATE.getName());
199         checkConfig.addAttribute("excludeScope", Scope.PROTECTED.getName());
200         final String[] expected = {
201             "7:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
202             "8:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
203             "18:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
204             "19:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
205             "28:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
206             "29:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
207             "30:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
208             "31:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
209             "40:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
210             "41:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
211             "42:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
212             "43:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
213             "53:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
214             "54:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
215             "55:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
216             "56:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
217             "64:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
218             "65:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
219             "66:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
220             "67:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
221             "76:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
222             "77:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
223             "78:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
224             "79:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
225             "88:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
226             "89:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
227             "90:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
228             "91:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
229             "100:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
230             "101:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
231             "102:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
232             "103:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
233             "113:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
234         };
235         verify(checkConfig,
236                getPath("InputJavadocVariableNoJavadoc.java"),
237                expected);
238     }
239 
240     @Test
241     public void testIgnoredVariableNames()
242             throws Exception {
243         final DefaultConfiguration checkConfig =
244                 createModuleConfig(JavadocVariableCheck.class);
245         checkConfig.addAttribute("ignoreNamePattern", "log|logger");
246         final String[] expected = {
247             "5:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
248             "6:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
249             "7:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
250             "8:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
251             "16:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
252             "17:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
253             "18:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
254             "19:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
255             "28:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
256             "29:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
257             "30:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
258             "31:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
259             "40:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
260             "41:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
261             "42:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
262             "43:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
263             "53:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
264             "54:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
265             "55:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
266             "56:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
267             "64:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
268             "65:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
269             "66:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
270             "67:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
271             "76:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
272             "77:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
273             "78:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
274             "79:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
275             "88:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
276             "89:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
277             "90:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
278             "91:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
279             "100:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
280             "101:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
281             "102:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
282             "103:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
283         };
284         verify(checkConfig,
285                 getPath("InputJavadocVariableNoJavadoc.java"),
286                 expected);
287     }
288 
289     @Test
290     public void testDoNotIgnoreAnythingWhenIgnoreNamePatternIsEmpty()
291             throws Exception {
292         final DefaultConfiguration checkConfig =
293                 createModuleConfig(JavadocVariableCheck.class);
294         checkConfig.addAttribute("ignoreNamePattern", "");
295         final String[] expected = {
296             "5:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
297             "6:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
298             "7:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
299             "8:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
300             "16:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
301             "17:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
302             "18:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
303             "19:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
304             "28:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
305             "29:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
306             "30:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
307             "31:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
308             "40:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
309             "41:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
310             "42:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
311             "43:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
312             "53:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
313             "54:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
314             "55:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
315             "56:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
316             "64:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
317             "65:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
318             "66:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
319             "67:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
320             "76:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
321             "77:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
322             "78:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
323             "79:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
324             "88:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
325             "89:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
326             "90:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
327             "91:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
328             "100:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
329             "101:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
330             "102:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
331             "103:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
332             "113:9: " + getCheckMessage(MSG_JAVADOC_MISSING),
333         };
334         verify(checkConfig,
335                 getPath("InputJavadocVariableNoJavadoc.java"),
336                 expected);
337     }
338 
339     @Test
340     public void testLambdaLocalVariablesDoNotNeedJavadoc() throws Exception {
341         final DefaultConfiguration checkConfig = createModuleConfig(JavadocVariableCheck.class);
342         final String[] expected = {
343             "6:5: " + getCheckMessage(MSG_JAVADOC_MISSING),
344         };
345         verify(checkConfig,
346                 getPath("InputJavadocVariableNoJavadocNeededInLambda.java"),
347                 expected);
348     }
349 
350 }