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.JavadocStyleCheck.MSG_EMPTY;
23  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocStyleCheck.MSG_EXTRA_HTML;
24  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocStyleCheck.MSG_INCOMPLETE_TAG;
25  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocStyleCheck.MSG_JAVADOC_MISSING;
26  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocStyleCheck.MSG_NO_PERIOD;
27  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocStyleCheck.MSG_UNCLOSED_HTML;
28  import static org.junit.Assert.assertArrayEquals;
29  import static org.junit.Assert.assertEquals;
30  
31  import java.io.File;
32  
33  import org.junit.Test;
34  
35  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
36  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
37  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
38  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
39  
40  public class JavadocStyleCheckTest
41      extends AbstractModuleTestSupport {
42  
43      @Override
44      protected String getPackageLocation() {
45          return "com/puppycrawl/tools/checkstyle/checks/javadoc/javadocstyle";
46      }
47  
48      @Test
49      public void testGetAcceptableTokens() {
50          final JavadocStyleCheck javadocStyleCheck = new JavadocStyleCheck();
51  
52          final int[] actual = javadocStyleCheck.getAcceptableTokens();
53          final int[] expected = {
54              TokenTypes.ANNOTATION_DEF,
55              TokenTypes.ANNOTATION_FIELD_DEF,
56              TokenTypes.CLASS_DEF,
57              TokenTypes.CTOR_DEF,
58              TokenTypes.ENUM_CONSTANT_DEF,
59              TokenTypes.ENUM_DEF,
60              TokenTypes.INTERFACE_DEF,
61              TokenTypes.METHOD_DEF,
62              TokenTypes.PACKAGE_DEF,
63              TokenTypes.VARIABLE_DEF,
64          };
65  
66          assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
67      }
68  
69      @Test
70      public void testDefaultSettings()
71              throws Exception {
72          final DefaultConfiguration checkConfig =
73              createModuleConfig(JavadocStyleCheck.class);
74          final String[] expected = {
75              "20: " + getCheckMessage(MSG_NO_PERIOD),
76              "53: " + getCheckMessage(MSG_NO_PERIOD),
77              "63:11: " + getCheckMessage(MSG_UNCLOSED_HTML,
78                  "<b>This guy is missing end of bold tag"),
79              "66:7: " + getCheckMessage(MSG_EXTRA_HTML, "</td>Extra tag shouldn't be here"),
80              "68:19: " + getCheckMessage(MSG_UNCLOSED_HTML, "<code>dummy."),
81              "74: " + getCheckMessage(MSG_NO_PERIOD),
82              "75:23: " + getCheckMessage(MSG_UNCLOSED_HTML, "<b>should fail"),
83              "81: " + getCheckMessage(MSG_NO_PERIOD),
84              "82:31: " + getCheckMessage(MSG_UNCLOSED_HTML, "<b>should fail"),
85              "88: " + getCheckMessage(MSG_NO_PERIOD),
86              "89:31: " + getCheckMessage(MSG_EXTRA_HTML, "</code>"),
87              "90: " + getCheckMessage(MSG_INCOMPLETE_TAG, "    * should fail <"),
88              "109:39: " + getCheckMessage(MSG_EXTRA_HTML, "</img>"),
89              "186:8: " + getCheckMessage(MSG_UNCLOSED_HTML, "<blockquote>"),
90              "193: " + getCheckMessage(MSG_NO_PERIOD),
91              "238: " + getCheckMessage(MSG_NO_PERIOD),
92              "335:33: " + getCheckMessage(MSG_EXTRA_HTML, "</string>"),
93              "361:37: " + getCheckMessage(MSG_UNCLOSED_HTML, "<code>"),
94              "372: " + getCheckMessage(MSG_NO_PERIOD),
95              "378:15: " + getCheckMessage(MSG_UNCLOSED_HTML,
96                  "<b>Note:<b> it's unterminated tag.</p>"),
97              "382: " + getCheckMessage(MSG_NO_PERIOD),
98              "386: " + getCheckMessage(MSG_NO_PERIOD),
99              "393: " + getCheckMessage(MSG_NO_PERIOD),
100             "405: " + getCheckMessage(MSG_NO_PERIOD),
101             "418: " + getCheckMessage(MSG_NO_PERIOD),
102         };
103 
104         verify(checkConfig, getPath("InputJavadocStyle.java"), expected);
105     }
106 
107     @Test
108     public void testFirstSentence() throws Exception {
109         final DefaultConfiguration checkConfig =
110             createModuleConfig(JavadocStyleCheck.class);
111         checkConfig.addAttribute("checkFirstSentence", "true");
112         checkConfig.addAttribute("checkHtml", "false");
113         final String[] expected = {
114             "20: " + getCheckMessage(MSG_NO_PERIOD),
115             "53: " + getCheckMessage(MSG_NO_PERIOD),
116             "74: " + getCheckMessage(MSG_NO_PERIOD),
117             "81: " + getCheckMessage(MSG_NO_PERIOD),
118             "88: " + getCheckMessage(MSG_NO_PERIOD),
119             "193: " + getCheckMessage(MSG_NO_PERIOD),
120             "238: " + getCheckMessage(MSG_NO_PERIOD),
121             "372: " + getCheckMessage(MSG_NO_PERIOD),
122             "382: " + getCheckMessage(MSG_NO_PERIOD),
123             "386: " + getCheckMessage(MSG_NO_PERIOD),
124             "393: " + getCheckMessage(MSG_NO_PERIOD),
125             "405: " + getCheckMessage(MSG_NO_PERIOD),
126             "418: " + getCheckMessage(MSG_NO_PERIOD),
127         };
128 
129         verify(checkConfig, getPath("InputJavadocStyle.java"), expected);
130     }
131 
132     @Test
133     public void testFirstSentenceFormat() throws Exception {
134         final DefaultConfiguration checkConfig =
135             createModuleConfig(JavadocStyleCheck.class);
136         checkConfig.addAttribute("checkFirstSentence", "true");
137         checkConfig.addAttribute("checkHtml", "false");
138         checkConfig.addAttribute("endOfSentenceFormat",
139                 "([.][ \t\n\r\f<])|([.]$)");
140         final String[] expected = {
141             "20: " + getCheckMessage(MSG_NO_PERIOD),
142             "32: " + getCheckMessage(MSG_NO_PERIOD),
143             "39: " + getCheckMessage(MSG_NO_PERIOD),
144             "53: " + getCheckMessage(MSG_NO_PERIOD),
145             "74: " + getCheckMessage(MSG_NO_PERIOD),
146             "81: " + getCheckMessage(MSG_NO_PERIOD),
147             "88: " + getCheckMessage(MSG_NO_PERIOD),
148             "193: " + getCheckMessage(MSG_NO_PERIOD),
149             "238: " + getCheckMessage(MSG_NO_PERIOD),
150             "372: " + getCheckMessage(MSG_NO_PERIOD),
151             "382: " + getCheckMessage(MSG_NO_PERIOD),
152             "386: " + getCheckMessage(MSG_NO_PERIOD),
153             "393: " + getCheckMessage(MSG_NO_PERIOD),
154             "405: " + getCheckMessage(MSG_NO_PERIOD),
155             "418: " + getCheckMessage(MSG_NO_PERIOD),
156         };
157 
158         verify(checkConfig, getPath("InputJavadocStyle.java"), expected);
159     }
160 
161     @Test
162     public void testHtml() throws Exception {
163         final DefaultConfiguration checkConfig = createModuleConfig(JavadocStyleCheck.class);
164         checkConfig.addAttribute("checkFirstSentence", "false");
165         checkConfig.addAttribute("checkHtml", "true");
166         final String[] expected = {
167             "63:11: " + getCheckMessage(MSG_UNCLOSED_HTML,
168                 "<b>This guy is missing end of bold tag"),
169             "66:7: " + getCheckMessage(MSG_EXTRA_HTML, "</td>Extra tag shouldn't be here"),
170             "68:19: " + getCheckMessage(MSG_UNCLOSED_HTML, "<code>dummy."),
171             "75:23: " + getCheckMessage(MSG_UNCLOSED_HTML, "<b>should fail"),
172             "82:31: " + getCheckMessage(MSG_UNCLOSED_HTML, "<b>should fail"),
173             "89:31: " + getCheckMessage(MSG_EXTRA_HTML, "</code>"),
174             "90: " + getCheckMessage(MSG_INCOMPLETE_TAG, "    * should fail <"),
175             "109:39: " + getCheckMessage(MSG_EXTRA_HTML, "</img>"),
176             "186:8: " + getCheckMessage(MSG_UNCLOSED_HTML, "<blockquote>"),
177             "335:33: " + getCheckMessage(MSG_EXTRA_HTML, "</string>"),
178             "361:37: " + getCheckMessage(MSG_UNCLOSED_HTML, "<code>"),
179             "378:15: " + getCheckMessage(MSG_UNCLOSED_HTML,
180                 "<b>Note:<b> it's unterminated tag.</p>"),
181         };
182 
183         verify(checkConfig, getPath("InputJavadocStyle.java"), expected);
184     }
185 
186     @Test
187     public void testHtmlComment() throws Exception {
188         final DefaultConfiguration checkConfig = createModuleConfig(JavadocStyleCheck.class);
189         checkConfig.addAttribute("checkFirstSentence", "false");
190         checkConfig.addAttribute("checkHtml", "true");
191         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
192 
193         verify(checkConfig, getPath("InputJavadocStyleHtmlComment.java"), expected);
194     }
195 
196     @Test
197     public void testOnInputWithNoJavadoc() throws Exception {
198         final DefaultConfiguration checkConfig = createModuleConfig(JavadocStyleCheck.class);
199         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
200 
201         verify(checkConfig, getPath("InputJavadocStyleNoJavadoc.java"), expected);
202     }
203 
204     @Test
205     public void testScopePublic()
206             throws Exception {
207         final DefaultConfiguration checkConfig =
208             createModuleConfig(JavadocStyleCheck.class);
209         checkConfig.addAttribute("checkFirstSentence", "true");
210         checkConfig.addAttribute("checkHtml", "true");
211         checkConfig.addAttribute("checkEmptyJavadoc", "true");
212         checkConfig.addAttribute("scope", "public");
213         final String[] expected = {
214             "88: " + getCheckMessage(MSG_NO_PERIOD),
215             "89:31: " + getCheckMessage(MSG_EXTRA_HTML, "</code>"),
216             "90: " + getCheckMessage(MSG_INCOMPLETE_TAG, "    * should fail <"),
217             "205: " + getCheckMessage(MSG_EMPTY),
218             "230: " + getCheckMessage(MSG_EMPTY),
219             "238: " + getCheckMessage(MSG_NO_PERIOD),
220             "335:33: " + getCheckMessage(MSG_EXTRA_HTML, "</string>"),
221             "382: " + getCheckMessage(MSG_NO_PERIOD),
222             "386: " + getCheckMessage(MSG_NO_PERIOD),
223             "418: " + getCheckMessage(MSG_NO_PERIOD),
224         };
225 
226         verify(checkConfig, getPath("InputJavadocStyle.java"), expected);
227     }
228 
229     @Test
230     public void testScopeProtected()
231             throws Exception {
232         final DefaultConfiguration checkConfig =
233             createModuleConfig(JavadocStyleCheck.class);
234         checkConfig.addAttribute("checkFirstSentence", "true");
235         checkConfig.addAttribute("checkHtml", "true");
236         checkConfig.addAttribute("checkEmptyJavadoc", "true");
237         checkConfig.addAttribute("scope", "protected");
238         final String[] expected = {
239             "74: " + getCheckMessage(MSG_NO_PERIOD),
240             "75:23: " + getCheckMessage(MSG_UNCLOSED_HTML, "<b>should fail"),
241             "88: " + getCheckMessage(MSG_NO_PERIOD),
242             "89:31: " + getCheckMessage(MSG_EXTRA_HTML, "</code>"),
243             "90: " + getCheckMessage(MSG_INCOMPLETE_TAG, "    * should fail <"),
244             "205: " + getCheckMessage(MSG_EMPTY),
245             "211: " + getCheckMessage(MSG_EMPTY),
246             "230: " + getCheckMessage(MSG_EMPTY),
247             "238: " + getCheckMessage(MSG_NO_PERIOD),
248             "335:33: " + getCheckMessage(MSG_EXTRA_HTML, "</string>"),
249             "382: " + getCheckMessage(MSG_NO_PERIOD),
250             "386: " + getCheckMessage(MSG_NO_PERIOD),
251             "418: " + getCheckMessage(MSG_NO_PERIOD),
252         };
253 
254         verify(checkConfig, getPath("InputJavadocStyle.java"), expected);
255     }
256 
257     @Test
258     public void testScopePackage()
259             throws Exception {
260         final DefaultConfiguration checkConfig =
261             createModuleConfig(JavadocStyleCheck.class);
262         checkConfig.addAttribute("checkFirstSentence", "true");
263         checkConfig.addAttribute("checkHtml", "true");
264         checkConfig.addAttribute("checkEmptyJavadoc", "true");
265         checkConfig.addAttribute("scope", "package");
266         final String[] expected = {
267             "74: " + getCheckMessage(MSG_NO_PERIOD),
268             "75:23: " + getCheckMessage(MSG_UNCLOSED_HTML, "<b>should fail"),
269             "81: " + getCheckMessage(MSG_NO_PERIOD),
270             "82:31: " + getCheckMessage(MSG_UNCLOSED_HTML, "<b>should fail"),
271             "88: " + getCheckMessage(MSG_NO_PERIOD),
272             "89:31: " + getCheckMessage(MSG_EXTRA_HTML, "</code>"),
273             "90: " + getCheckMessage(MSG_INCOMPLETE_TAG, "    * should fail <"),
274             "205: " + getCheckMessage(MSG_EMPTY),
275             "211: " + getCheckMessage(MSG_EMPTY),
276             "218: " + getCheckMessage(MSG_EMPTY),
277             "230: " + getCheckMessage(MSG_EMPTY),
278             "238: " + getCheckMessage(MSG_NO_PERIOD),
279             "335:33: " + getCheckMessage(MSG_EXTRA_HTML, "</string>"),
280             "382: " + getCheckMessage(MSG_NO_PERIOD),
281             "386: " + getCheckMessage(MSG_NO_PERIOD),
282             "393: " + getCheckMessage(MSG_NO_PERIOD),
283             "405: " + getCheckMessage(MSG_NO_PERIOD),
284             "418: " + getCheckMessage(MSG_NO_PERIOD),
285         };
286 
287         verify(checkConfig, getPath("InputJavadocStyle.java"), expected);
288     }
289 
290     @Test
291     public void testEmptyJavadoc() throws Exception {
292         final DefaultConfiguration checkConfig = createModuleConfig(JavadocStyleCheck.class);
293         checkConfig.addAttribute("checkFirstSentence", "false");
294         checkConfig.addAttribute("checkHtml", "false");
295         checkConfig.addAttribute("checkEmptyJavadoc", "true");
296         final String[] expected = {
297             "205: " + getCheckMessage(MSG_EMPTY),
298             "211: " + getCheckMessage(MSG_EMPTY),
299             "218: " + getCheckMessage(MSG_EMPTY),
300             "225: " + getCheckMessage(MSG_EMPTY),
301             "230: " + getCheckMessage(MSG_EMPTY),
302         };
303 
304         verify(checkConfig, getPath("InputJavadocStyle.java"), expected);
305     }
306 
307     @Test
308     public void testExcludeScope()
309             throws Exception {
310         final DefaultConfiguration checkConfig =
311             createModuleConfig(JavadocStyleCheck.class);
312         checkConfig.addAttribute("scope", "private");
313         checkConfig.addAttribute("excludeScope", "protected");
314         final String[] expected = {
315             "20: " + getCheckMessage(MSG_NO_PERIOD),
316             "53: " + getCheckMessage(MSG_NO_PERIOD),
317             "63:11: " + getCheckMessage(MSG_UNCLOSED_HTML,
318                 "<b>This guy is missing end of bold tag"),
319             "66:7: " + getCheckMessage(MSG_EXTRA_HTML, "</td>Extra tag shouldn't be here"),
320             "68:19: " + getCheckMessage(MSG_UNCLOSED_HTML, "<code>dummy."),
321             "81: " + getCheckMessage(MSG_NO_PERIOD),
322             "82:31: " + getCheckMessage(MSG_UNCLOSED_HTML, "<b>should fail"),
323             "109:39: " + getCheckMessage(MSG_EXTRA_HTML, "</img>"),
324             "186:8: " + getCheckMessage(MSG_UNCLOSED_HTML, "<blockquote>"),
325             "193: " + getCheckMessage(MSG_NO_PERIOD),
326             "361:37: " + getCheckMessage(MSG_UNCLOSED_HTML, "<code>"),
327             "372: " + getCheckMessage(MSG_NO_PERIOD),
328             "378:15: " + getCheckMessage(MSG_UNCLOSED_HTML,
329                 "<b>Note:<b> it's unterminated tag.</p>"),
330             "393: " + getCheckMessage(MSG_NO_PERIOD),
331             "405: " + getCheckMessage(MSG_NO_PERIOD),
332         };
333 
334         verify(checkConfig, getPath("InputJavadocStyle.java"), expected);
335     }
336 
337     @Test
338     public void packageInfoInheritDoc() throws Exception {
339         final DefaultConfiguration checkConfig =
340             createModuleConfig(JavadocStyleCheck.class);
341         final String[] expected = {
342             "1: " + getCheckMessage(MSG_NO_PERIOD),
343         };
344 
345         verify(createChecker(checkConfig),
346                getPath("pkginfo" + File.separator + "invalidinherit" + File.separator
347                    + "package-info.java"),
348                expected);
349     }
350 
351     @Test
352     public void packageInfoInvalid() throws Exception {
353         final DefaultConfiguration checkConfig =
354             createModuleConfig(JavadocStyleCheck.class);
355         final String[] expected = {
356             "1: " + getCheckMessage(MSG_NO_PERIOD),
357         };
358 
359         verify(createChecker(checkConfig),
360                getPath("pkginfo" + File.separator + "invalidformat" + File.separator
361                    + "package-info.java"),
362                expected);
363     }
364 
365     @Test
366     public void packageInfoAnnotation() throws Exception {
367         final DefaultConfiguration checkConfig =
368             createModuleConfig(JavadocStyleCheck.class);
369         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
370 
371         verify(createChecker(checkConfig),
372                getPath("pkginfo" + File.separator + "annotation" + File.separator
373                    + "package-info.java"),
374                expected);
375     }
376 
377     @Test
378     public void packageInfoMissing() throws Exception {
379         final DefaultConfiguration checkConfig =
380             createModuleConfig(JavadocStyleCheck.class);
381         final String[] expected = {
382             "1: " + getCheckMessage(MSG_JAVADOC_MISSING),
383         };
384 
385         verify(createChecker(checkConfig),
386                getPath("bothfiles" + File.separator + "package-info.java"),
387                expected);
388     }
389 
390     @Test
391     public void packageInfoValid() throws Exception {
392         final DefaultConfiguration checkConfig =
393             createModuleConfig(JavadocStyleCheck.class);
394         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
395 
396         verify(createChecker(checkConfig),
397                getPath("pkginfo" + File.separator + "valid" + File.separator + "package-info.java"),
398                expected);
399     }
400 
401     @Test
402     public void testRestrictedTokenSet()
403             throws Exception {
404         final DefaultConfiguration checkConfig = createModuleConfig(JavadocStyleCheck.class);
405         checkConfig.addAttribute("tokens", "METHOD_DEF");
406         checkConfig.addAttribute("scope", "public");
407         checkConfig.addAttribute("checkFirstSentence", "true");
408         checkConfig.addAttribute("checkEmptyJavadoc", "false");
409         checkConfig.addAttribute("checkHtml", "false");
410         final String[] expected = {
411             "88: " + getCheckMessage(MSG_NO_PERIOD),
412             "386: " + getCheckMessage(MSG_NO_PERIOD),
413             "418: " + getCheckMessage(MSG_NO_PERIOD),
414         };
415         verify(checkConfig, getPath("InputJavadocStyle.java"), expected);
416     }
417 
418     @Test
419     public void testHtmlTagToString() {
420         final HtmlTag tag = new HtmlTag("id", 3, 5, true, false, "<a href=\"URL\"/>");
421         assertEquals("Invalid toString result",
422                 "HtmlTag[id='id', lineNo=3, position=5, text='<a href=\"URL\"/>', "
423                 + "closedTag=true, incompleteTag=false]", tag.toString());
424     }
425 
426 }