View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.JavadocParagraphCheck.MSG_LINE_BEFORE;
24  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocParagraphCheck.MSG_MISPLACED_TAG;
25  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocParagraphCheck.MSG_PRECEDED_BLOCK_TAG;
26  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocParagraphCheck.MSG_REDUNDANT_PARAGRAPH;
27  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocParagraphCheck.MSG_TAG_AFTER;
28  
29  import org.junit.jupiter.api.Test;
30  
31  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
34  
35  public class JavadocParagraphCheckTest extends AbstractModuleTestSupport {
36  
37      @Override
38      protected String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/checks/javadoc/javadocparagraph";
40      }
41  
42      @Test
43      public void testGetRequiredTokens() {
44          final JavadocParagraphCheck checkObj = new JavadocParagraphCheck();
45          final int[] expected = {TokenTypes.BLOCK_COMMENT_BEGIN};
46          assertWithMessage("Default required tokens are invalid")
47              .that(checkObj.getRequiredTokens())
48              .isEqualTo(expected);
49      }
50  
51      @Test
52      public void testCorrect() throws Exception {
53          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
54  
55          verifyWithInlineConfigParser(
56                  getPath("InputJavadocParagraphCorrect.java"), expected);
57      }
58  
59      @Test
60      public void testIncorrect() throws Exception {
61          final String[] expected = {
62              "14:4: " + getCheckMessage(MSG_LINE_BEFORE),
63              "15:19: " + getCheckMessage(MSG_LINE_BEFORE),
64              "22:21: " + getCheckMessage(MSG_LINE_BEFORE),
65              "24:8: " + getCheckMessage(MSG_MISPLACED_TAG),
66              "34:13: " + getCheckMessage(MSG_LINE_BEFORE),
67              "36:8: " + getCheckMessage(MSG_MISPLACED_TAG),
68              "47:8: " + getCheckMessage(MSG_REDUNDANT_PARAGRAPH),
69              "47:24: " + getCheckMessage(MSG_LINE_BEFORE),
70              "48:8: " + getCheckMessage(MSG_LINE_BEFORE),
71              "49:8: " + getCheckMessage(MSG_LINE_BEFORE),
72              "49:11: " + getCheckMessage(MSG_LINE_BEFORE),
73              "50:8: " + getCheckMessage(MSG_MISPLACED_TAG),
74              "50:8: " + getCheckMessage(MSG_LINE_BEFORE),
75              "50:29: " + getCheckMessage(MSG_MISPLACED_TAG),
76              "50:29: " + getCheckMessage(MSG_LINE_BEFORE),
77              "62:25: " + getCheckMessage(MSG_LINE_BEFORE),
78              "69:12: " + getCheckMessage(MSG_REDUNDANT_PARAGRAPH),
79              "75:29: " + getCheckMessage(MSG_LINE_BEFORE),
80              "86:12: " + getCheckMessage(MSG_REDUNDANT_PARAGRAPH),
81              "87:11: " + getCheckMessage(MSG_TAG_AFTER),
82              "97:27: " + getCheckMessage(MSG_LINE_BEFORE),
83              "99:13: " + getCheckMessage(MSG_MISPLACED_TAG),
84              "109:11: " + getCheckMessage(MSG_TAG_AFTER),
85              "110:11: " + getCheckMessage(MSG_TAG_AFTER),
86          };
87          verifyWithInlineConfigParser(
88                  getPath("InputJavadocParagraphIncorrect.java"), expected);
89      }
90  
91      @Test
92      public void testIncorrect2() throws Exception {
93          final String[] expected = {
94              "14:4: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "h1"),
95              "22:7: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "ul"),
96              "37:8: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "table"),
97              "49:8: " + getCheckMessage(MSG_MISPLACED_TAG),
98              "51:8: " + getCheckMessage(MSG_MISPLACED_TAG),
99              "51:8: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "ol"),
100         };
101         verifyWithInlineConfigParser(
102                 getPath("InputJavadocParagraphIncorrect4.java"), expected);
103     }
104 
105     @Test
106     public void testAllowNewlineParagraph() throws Exception {
107         final String[] expected = {
108             "16:4: " + getCheckMessage(MSG_MISPLACED_TAG),
109             "16:4: " + getCheckMessage(MSG_LINE_BEFORE),
110             "17:19: " + getCheckMessage(MSG_MISPLACED_TAG),
111             "17:19: " + getCheckMessage(MSG_LINE_BEFORE),
112             "28:21: " + getCheckMessage(MSG_MISPLACED_TAG),
113             "28:21: " + getCheckMessage(MSG_LINE_BEFORE),
114             "30:8: " + getCheckMessage(MSG_MISPLACED_TAG),
115             "39:13: " + getCheckMessage(MSG_LINE_BEFORE),
116             "41:8: " + getCheckMessage(MSG_MISPLACED_TAG),
117             "56:8: " + getCheckMessage(MSG_REDUNDANT_PARAGRAPH),
118             "56:24: " + getCheckMessage(MSG_MISPLACED_TAG),
119             "56:24: " + getCheckMessage(MSG_LINE_BEFORE),
120             "57:8: " + getCheckMessage(MSG_MISPLACED_TAG),
121             "57:8: " + getCheckMessage(MSG_LINE_BEFORE),
122             "58:8: " + getCheckMessage(MSG_LINE_BEFORE),
123             "58:11: " + getCheckMessage(MSG_MISPLACED_TAG),
124             "58:11: " + getCheckMessage(MSG_LINE_BEFORE),
125             "59:8: " + getCheckMessage(MSG_MISPLACED_TAG),
126             "59:8: " + getCheckMessage(MSG_LINE_BEFORE),
127             "59:29: " + getCheckMessage(MSG_MISPLACED_TAG),
128             "59:29: " + getCheckMessage(MSG_LINE_BEFORE),
129             "75:25: " + getCheckMessage(MSG_MISPLACED_TAG),
130             "75:25: " + getCheckMessage(MSG_LINE_BEFORE),
131             "86:12: " + getCheckMessage(MSG_MISPLACED_TAG),
132             "86:12: " + getCheckMessage(MSG_REDUNDANT_PARAGRAPH),
133             "89:12: " + getCheckMessage(MSG_MISPLACED_TAG),
134             "91:12: " + getCheckMessage(MSG_MISPLACED_TAG),
135             "92:29: " + getCheckMessage(MSG_MISPLACED_TAG),
136             "92:29: " + getCheckMessage(MSG_LINE_BEFORE),
137             "103:12: " + getCheckMessage(MSG_MISPLACED_TAG),
138             "103:12: " + getCheckMessage(MSG_LINE_BEFORE),
139             "103:31: " + getCheckMessage(MSG_MISPLACED_TAG),
140             "103:31: " + getCheckMessage(MSG_LINE_BEFORE),
141         };
142         verifyWithInlineConfigParser(
143                 getPath("InputJavadocParagraphIncorrect2.java"), expected);
144     }
145 
146     @Test
147     public void testAllowNewlineParagraph2() throws Exception {
148         final String[] expected = {
149             "16:12: " + getCheckMessage(MSG_REDUNDANT_PARAGRAPH),
150             "17:11: " + getCheckMessage(MSG_TAG_AFTER),
151             "30:27: " + getCheckMessage(MSG_MISPLACED_TAG),
152             "30:27: " + getCheckMessage(MSG_LINE_BEFORE),
153             "32:13: " + getCheckMessage(MSG_MISPLACED_TAG),
154             "42:11: " + getCheckMessage(MSG_TAG_AFTER),
155             "43:11: " + getCheckMessage(MSG_TAG_AFTER),
156         };
157         verifyWithInlineConfigParser(
158                 getPath("InputJavadocParagraphIncorrect3.java"), expected);
159     }
160 
161     @Test
162     public void testAllowNewlineParagraph3() throws Exception {
163         final String[] expected = {
164             "42:8: " + getCheckMessage(MSG_MISPLACED_TAG),
165             "46:8: " + getCheckMessage(MSG_MISPLACED_TAG),
166             "46:8: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "h1"),
167             "73:8: " + getCheckMessage(MSG_MISPLACED_TAG),
168             "73:8: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "ul"),
169         };
170         verifyWithInlineConfigParser(
171                 getPath("InputJavadocParagraphIncorrect5.java"), expected);
172     }
173 
174     @Test
175     public void testJavadocParagraph() throws Exception {
176         final String[] expected = {
177             "19:4: " + getCheckMessage(MSG_LINE_BEFORE),
178             "28:7: " + getCheckMessage(MSG_REDUNDANT_PARAGRAPH),
179             "31:7: " + getCheckMessage(MSG_LINE_BEFORE),
180             "50:7: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "ul"),
181             "76:8: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "table"),
182             "87:8: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "pre"),
183         };
184 
185         verifyWithInlineConfigParser(
186                 getPath("InputJavadocParagraphCheck1.java"), expected);
187     }
188 
189     @Test
190     public void testJavadocParagraphOpenClosedTag() throws Exception {
191         final String[] expected = {
192             "14:4: " + getCheckMessage(MSG_MISPLACED_TAG),
193             "21:7: " + getCheckMessage(MSG_LINE_BEFORE),
194             "28:20: " + getCheckMessage(MSG_LINE_BEFORE),
195             "29:20: " + getCheckMessage(MSG_LINE_BEFORE),
196             "35:8: " + getCheckMessage(MSG_REDUNDANT_PARAGRAPH),
197             "36:6: " + getCheckMessage(MSG_TAG_AFTER),
198             "38:6: " + getCheckMessage(MSG_TAG_AFTER),
199             "58:7: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "ul"),
200             "73:7: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "h1"),
201             "85:7: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "h1"),
202         };
203 
204         verifyWithInlineConfigParser(
205                 getPath("InputJavadocParagraphIncorrectOpenClosedTag.java"), expected);
206     }
207 
208     @Test
209     public void testJavadocParagraphOpenClosedTag2() throws Exception {
210         final String[] expected = {
211             "14:4: " + getCheckMessage(MSG_MISPLACED_TAG),
212             "21:7: " + getCheckMessage(MSG_LINE_BEFORE),
213             "30:20: " + getCheckMessage(MSG_MISPLACED_TAG),
214             "30:20: " + getCheckMessage(MSG_LINE_BEFORE),
215             "31:20: " + getCheckMessage(MSG_LINE_BEFORE),
216             "37:8: " + getCheckMessage(MSG_REDUNDANT_PARAGRAPH),
217             "38:6: " + getCheckMessage(MSG_TAG_AFTER),
218             "40:6: " + getCheckMessage(MSG_TAG_AFTER),
219             "50:7: " + getCheckMessage(MSG_MISPLACED_TAG),
220             "63:7: " + getCheckMessage(MSG_MISPLACED_TAG),
221             "63:7: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "ul"),
222             "78:7: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "h1"),
223             "87:7: " + getCheckMessage(MSG_MISPLACED_TAG),
224             "91:7: " + getCheckMessage(MSG_MISPLACED_TAG),
225             "91:7: " + getCheckMessage(MSG_PRECEDED_BLOCK_TAG, "h1"),
226         };
227 
228         verifyWithInlineConfigParser(
229                 getPath("InputJavadocParagraphIncorrectOpenClosedTag2.java"), expected);
230     }
231 
232     @Test
233     public void testJavadocParagraphOpenClosedTag3() throws Exception {
234         final String[] expected = {
235             "15:7: " + getCheckMessage(MSG_MISPLACED_TAG),
236             "23:7: " + getCheckMessage(MSG_MISPLACED_TAG),
237             "31:7: " + getCheckMessage(MSG_MISPLACED_TAG),
238         };
239 
240         verifyWithInlineConfigParser(
241                 getPath("InputJavadocParagraphIncorrectOpenClosedTag3.java"), expected);
242     }
243 
244     @Test
245     public void testIncorrect3() throws Exception {
246         final String[] expected = {
247             "15:7: " + getCheckMessage(MSG_MISPLACED_TAG),
248             "23:7: " + getCheckMessage(MSG_MISPLACED_TAG),
249             "31:7: " + getCheckMessage(MSG_MISPLACED_TAG),
250         };
251 
252         verifyWithInlineConfigParser(
253                 getPath("InputJavadocParagraphIncorrect6.java"), expected);
254     }
255 
256     @Test
257     public void testJavadocParagraphNested() throws Exception {
258         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
259 
260         verifyWithInlineConfigParser(
261                 getPath("InputJavadocParagraphNested.java"), expected);
262     }
263 }