View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck.MSG_SUMMARY_FIRST_SENTENCE;
24  import static com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck.MSG_SUMMARY_JAVADOC;
25  import static com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck.MSG_SUMMARY_JAVADOC_MISSING;
26  import static com.puppycrawl.tools.checkstyle.checks.javadoc.SummaryJavadocCheck.MSG_SUMMARY_MISSING_PERIOD;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
33  
34  public class SummaryJavadocCheckTest extends AbstractModuleTestSupport {
35  
36      @Override
37      protected String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/javadoc/summaryjavadoc";
39      }
40  
41      @Test
42      public void testGetRequiredTokens() {
43          final SummaryJavadocCheck checkObj = new SummaryJavadocCheck();
44          final int[] expected = {TokenTypes.BLOCK_COMMENT_BEGIN };
45          assertWithMessage("Default required tokens are invalid")
46                  .that(checkObj.getRequiredTokens())
47                  .isEqualTo(expected);
48      }
49  
50      @Test
51      public void testCorrect() throws Exception {
52          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
53  
54          verifyWithInlineConfigParser(
55                  getPath("InputSummaryJavadocCorrect.java"), expected);
56      }
57  
58      @Test
59      public void testInlineCorrect() throws Exception {
60          final String[] expected = {
61              "112: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
62          };
63  
64          verifyWithInlineConfigParser(
65                  getPath("InputSummaryJavadocInlineCorrect.java"), expected);
66      }
67  
68      @Test
69      public void testIncorrect() throws Exception {
70          final String[] expected = {
71              "20: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
72              "25: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
73              "43: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
74              "48: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
75              "58: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
76              "64: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
77              "69: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
78              "80: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
79              "94: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
80              "114: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
81              "127: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
82              "132: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
83              "137: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
84              "143: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
85              "148: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
86              "151: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
87          };
88          verifyWithInlineConfigParser(
89                  getPath("InputSummaryJavadocIncorrect.java"), expected);
90      }
91  
92      @Test
93      public void testInlineForbidden() throws Exception {
94          final String[] expected = {
95              "26: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
96              "31: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
97              "36: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
98              "41: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
99              "45: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
100             "49: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
101             "59: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
102             "80: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
103             "94: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
104             "108: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
105             "114: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
106         };
107         verifyWithInlineConfigParser(
108                 getPath("InputSummaryJavadocInlineForbidden.java"), expected);
109     }
110 
111     @Test
112     public void testPeriod() throws Exception {
113         final String[] expected = {
114             "14: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
115             "19: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
116             "37: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
117         };
118 
119         verifyWithInlineConfigParser(
120                 getPath("InputSummaryJavadocPeriod.java"), expected);
121     }
122 
123     @Test
124     public void testNoPeriod() throws Exception {
125         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
126 
127         verifyWithInlineConfigParser(
128                 getPath("InputSummaryJavadocNoPeriod.java"), expected);
129     }
130 
131     @Test
132     public void testDefaultConfiguration() throws Exception {
133         final String[] expected = {
134             "19: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
135             "24: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
136             "42: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
137             "47: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
138             "63: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
139             "68: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
140             "79: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
141             "113: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
142             "126: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
143             "131: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
144             "136: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
145             "142: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
146             "147: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
147             "150: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
148         };
149 
150         verifyWithInlineConfigParser(
151                 getPath("InputSummaryJavadocIncorrect2.java"), expected);
152     }
153 
154     @Test
155     public void testIncorrectUsageOfSummaryTag() throws Exception {
156         final String[] expected = {
157             "34: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
158             "41: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
159             "49: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
160             "57: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
161             "64: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
162             "74: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
163             // Until https://github.com/checkstyle/checkstyle/issues/11425
164             "82: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
165             "93: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
166         };
167 
168         verifyWithInlineConfigParser(
169             getPath("InputSummaryJavadocIncorrect3.java"), expected);
170     }
171 
172     @Test
173     public void testInlineDefaultConfiguration() throws Exception {
174         final String[] expected = {
175             "22: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
176             "26: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
177             "30: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
178             "40: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
179             "44: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
180             "56: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
181             "60: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
182             "116: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
183             "120: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
184             "125: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
185             "136: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
186             "153: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
187             "157: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
188             "179: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
189         };
190 
191         verifyWithInlineConfigParser(
192                 getPath("InputSummaryJavadocInlineDefault.java"), expected);
193     }
194 
195     @Test
196     public void testInlineReturn() throws Exception {
197         final String[] expected = {
198             "74: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
199         };
200 
201         verifyWithInlineConfigParser(
202                 getPath("InputSummaryJavadocInlineReturn.java"), expected);
203     }
204 
205     @Test
206     public void testInlineReturn2() throws Exception {
207         final String[] expected = {
208             "15: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
209         };
210 
211         verifyWithInlineConfigParser(
212                 getPath("InputSummaryJavadocInlineReturn2.java"), expected);
213     }
214 
215     @Test
216     public void testInlineReturnForbidden() throws Exception {
217         final String[] expected = {
218             "14: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
219             "21: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
220             "28: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
221         };
222 
223         verifyWithInlineConfigParser(
224                 getPath("InputSummaryJavadocInlineReturnForbidden.java"), expected);
225     }
226 
227     @Test
228     public void testPeriodAtEnd() throws Exception {
229         final String[] expected = {
230             "19: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
231             "26: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
232             "33: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
233             "40: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
234             "60: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
235             "70: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
236         };
237 
238         verifyWithInlineConfigParser(
239                 getPath("InputSummaryJavadocPeriodAtEnd.java"), expected);
240     }
241 
242     @Test
243     public void testForbiddenFragmentRelativeToPeriod() throws Exception {
244         final String[] expected = {
245             "23: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
246         };
247 
248         verifyWithInlineConfigParser(
249                 getPath("InputSummaryJavadocForbiddenFragmentRelativeToPeriod.java"), expected);
250     }
251 
252     @Test
253     public void testJapanesePeriod() throws Exception {
254         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
255 
256         verifyWithInlineConfigParser(
257                 getPath("InputSummaryJavadocJapanesePeriod.java"), expected);
258     }
259 
260     @Test
261     public void testHtmlFormatSummary() throws Exception {
262         final String[] expected = {
263             "22: " + getCheckMessage(MSG_SUMMARY_MISSING_PERIOD),
264             "36: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
265             "41: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
266         };
267 
268         verifyWithInlineConfigParser(
269                 getPath("InputSummaryJavadocHtmlFormat.java"), expected);
270     }
271 
272     @Test
273     public void testPackageInfo() throws Exception {
274         final String[] expected = {
275             "10: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
276         };
277 
278         verifyWithInlineConfigParser(
279                 getPath("package-info.java"), expected);
280     }
281 
282     @Test
283     public void testPackageInfoWithAnnotation() throws Exception {
284         final String[] expected = {
285             "10: " + getCheckMessage(MSG_SUMMARY_JAVADOC_MISSING),
286         };
287 
288         verifyWithInlineConfigParser(
289                 getPath("inputs/package-info.java"), expected);
290     }
291 
292     @Test
293     public void testForbidden() throws Exception {
294         final String[] expected = {
295             "14: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
296         };
297 
298         verifyWithInlineConfigParser(
299                 getPath("InputSummaryJavadocTestForbiddenFragments.java"), expected);
300     }
301 
302     @Test
303     public void testEmptyPeriod() throws Exception {
304         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
305 
306         verifyWithInlineConfigParser(
307                 getPath("InputSummaryJavadocEmptyPeriod.java"), expected);
308     }
309 
310     @Test
311     public void testForbidden3() throws Exception {
312         final String[] expected = {
313             "14: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
314         };
315 
316         verifyWithInlineConfigParser(
317                 getPath("InputSummaryJavadocTestForbiddenFragments3.java"), expected);
318     }
319 
320     @Test
321     public void testForbidden2() throws Exception {
322         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
323 
324         verifyWithInlineConfigParser(
325                 getPath("InputSummaryJavadocTestForbiddenFragments2.java"), expected);
326     }
327 
328     @Test
329     public void testSummaryJavaDoc() throws Exception {
330         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
331 
332         verifyWithInlineConfigParser(
333                 getPath("InputSummaryJavadoc1.java"), expected);
334     }
335 
336     @Test
337     public void testSummaryJavaDoc2() throws Exception {
338         final String[] expected = {
339             "15: " + getCheckMessage(MSG_SUMMARY_JAVADOC),
340         };
341 
342         verifyWithInlineConfigParser(
343                 getPath("InputSummaryJavadoc2.java"), expected);
344     }
345 
346     @Test
347     public void testInheritDoc() throws Exception {
348         final String[] expected = {
349             "14: " + getCheckMessage(MSG_SUMMARY_FIRST_SENTENCE),
350         };
351 
352         verifyWithInlineConfigParser(
353                 getPath("InputSummaryJavadocInheritDoc.java"), expected);
354     }
355 }