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.javadoc;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck.MSG_SUMMARY_FIRST_SENTENCE;
23  import static com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck.MSG_SUMMARY_JAVADOC;
24  import static com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck.MSG_SUMMARY_JAVADOC_MISSING;
25  import static org.junit.Assert.assertArrayEquals;
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.api.TokenTypes;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
33  
34  public class SummaryJavadocCheckTest extends AbstractModuleTestSupport {
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/javadoc/summaryjavadoc";
38      }
39  
40      @Test
41      public void testGetRequiredTokens() {
42          final SummaryJavadocCheck checkObj = new SummaryJavadocCheck();
43          final int[] expected = {TokenTypes.BLOCK_COMMENT_BEGIN };
44          assertArrayEquals("Default required tokens are invalid",
45              expected, checkObj.getRequiredTokens());
46      }
47  
48      @Test
49      public void testCorrect() throws Exception {
50          final DefaultConfiguration checkConfig = createModuleConfig(SummaryJavadocCheck.class);
51          checkConfig.addAttribute("forbiddenSummaryFragments",
52                  "^@return the *|^This method returns *|^A [{]@code [a-zA-Z0-9]+[}]( is a )");
53          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
54  
55          verify(checkConfig, getPath("InputSummaryJavadocCorrect.java"), expected);
56      }
57  
58      @Test
59      public void testIncorrect() throws Exception {
60          final DefaultConfiguration checkConfig = createModuleConfig(SummaryJavadocCheck.class);
61          checkConfig.addAttribute("forbiddenSummaryFragments",
62                  "^@return the *|^This method returns |^A [{]@code [a-zA-Z0-9]+[}]( is a )");
63          final String[] expected = {
64              "14: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
65              "32: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
66              "37: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
67              "47: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
68              "53: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
69              "58: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
70              "69: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
71              "83: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
72              "103: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
73              "116: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
74              "121: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
75              "126: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
76              "132: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
77          };
78          verify(checkConfig, getPath("InputSummaryJavadocIncorrect.java"), expected);
79      }
80  
81      @Test
82      public void testPeriod() throws Exception {
83          final DefaultConfiguration checkConfig = createModuleConfig(SummaryJavadocCheck.class);
84          checkConfig.addAttribute("period", "_");
85          final String[] expected = {
86              "5: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
87              "10: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
88          };
89  
90          verify(checkConfig, getPath("InputSummaryJavadocPeriod.java"), expected);
91      }
92  
93      @Test
94      public void testNoPeriod() throws Exception {
95          final DefaultConfiguration checkConfig = createModuleConfig(SummaryJavadocCheck.class);
96          checkConfig.addAttribute("period", "");
97          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
98  
99          verify(checkConfig, getPath("InputSummaryJavadocNoPeriod.java"), expected);
100     }
101 
102     @Test
103     public void testDefaultConfiguration() throws Exception {
104         final DefaultConfiguration checkConfig = createModuleConfig(SummaryJavadocCheck.class);
105         final String[] expected = {
106             "14: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
107             "32: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
108             "37: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
109             "53: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
110             "58: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
111             "69: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
112             "103: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
113             "116: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
114             "121: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
115             "126: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
116             "132: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
117         };
118 
119         createChecker(checkConfig);
120         verify(checkConfig, getPath("InputSummaryJavadocIncorrect.java"), expected);
121     }
122 
123     @Test
124     public void testPeriodAtEnd() throws Exception {
125         final DefaultConfiguration checkConfig = createModuleConfig(SummaryJavadocCheck.class);
126         checkConfig.addAttribute("period", ".");
127         final String[] expected = {
128             "10: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
129             "17: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
130             "24: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
131             "31: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
132             "51: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
133         };
134 
135         verify(checkConfig, getPath("InputSummaryJavadocPeriodAtEnd.java"), expected);
136     }
137 }