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.WriteTagCheck.MSG_MISSING_TAG;
24  import static com.puppycrawl.tools.checkstyle.checks.javadoc.WriteTagCheck.MSG_TAG_FORMAT;
25  import static com.puppycrawl.tools.checkstyle.checks.javadoc.WriteTagCheck.MSG_WRITE_TAG;
26  
27  import java.io.ByteArrayInputStream;
28  import java.io.File;
29  import java.io.InputStreamReader;
30  import java.io.LineNumberReader;
31  import java.nio.charset.StandardCharsets;
32  import java.util.ArrayList;
33  import java.util.Collections;
34  import java.util.List;
35  
36  import org.junit.jupiter.api.Test;
37  
38  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
39  import com.puppycrawl.tools.checkstyle.Checker;
40  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
41  
42  /**
43   * Unit test for WriteTagCheck.
44   */
45  public class WriteTagCheckTest extends AbstractModuleTestSupport {
46  
47      @Override
48      protected String getPackageLocation() {
49          return "com/puppycrawl/tools/checkstyle/checks/javadoc/writetag";
50      }
51  
52      @Test
53      public void testDefaultSettings() throws Exception {
54          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
55          verifyWithInlineConfigParser(getPath("InputWriteTagDefault.java"), expected);
56      }
57  
58      @Test
59      public void testTag() throws Exception {
60          final String[] expected = {
61              "15: " + getCheckMessage(MSG_WRITE_TAG, "@author", "Daniel Grenner"),
62          };
63          verifyWithInlineConfigParser(getPath("InputWriteTag.java"), expected);
64      }
65  
66      @Test
67      public void testMissingFormat() throws Exception {
68          final String[] expected = {
69              "15: " + getCheckMessage(MSG_WRITE_TAG, "@author", "Daniel Grenner"),
70          };
71          verifyWithInlineConfigParser(getPath("InputWriteTagMissingFormat.java"), expected);
72      }
73  
74      @Test
75      public void testTagIncomplete() throws Exception {
76          final String[] expected = {
77              "16: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
78                  "This class needs more code..."),
79          };
80          verifyWithInlineConfigParser(getPath("InputWriteTagIncomplete.java"), expected);
81      }
82  
83      @Test
84      public void testDoubleTag() throws Exception {
85          final String[] expected = {
86              "18: " + getCheckMessage(MSG_WRITE_TAG, "@doubletag", "first text"),
87              "19: " + getCheckMessage(MSG_WRITE_TAG, "@doubletag", "second text"),
88          };
89          verifyWithInlineConfigParser(getPath("InputWriteTagDoubleTag.java"), expected);
90      }
91  
92      @Test
93      public void testEmptyTag() throws Exception {
94          final String[] expected = {
95              "19: " + getCheckMessage(MSG_WRITE_TAG, "@emptytag", ""),
96          };
97          verifyWithInlineConfigParser(getPath("InputWriteTagEmptyTag.java"), expected);
98      }
99  
100     @Test
101     public void testMissingTag() throws Exception {
102         final String[] expected = {
103             "20: " + getCheckMessage(MSG_MISSING_TAG, "@missingtag"),
104         };
105         verifyWithInlineConfigParser(getPath("InputWriteTagMissingTag.java"), expected);
106     }
107 
108     @Test
109     public void testMethod() throws Exception {
110         final String[] expected = {
111             "24: " + getCheckMessage(MSG_WRITE_TAG, "@todo",
112                     "Add a constructor comment"),
113             "36: " + getCheckMessage(MSG_WRITE_TAG, "@todo", "Add a comment"),
114         };
115         verifyWithInlineConfigParser(getPath("InputWriteTagMethod.java"), expected);
116     }
117 
118     @Test
119     public void testSeverity() throws Exception {
120         final String[] expected = {
121             "16: " + getCheckMessage(MSG_WRITE_TAG, "@author", "Daniel Grenner"),
122         };
123         verifyWithInlineConfigParser(getPath("InputWriteTagSeverity.java"), expected);
124     }
125 
126     @Test
127     public void testIgnoreMissing() throws Exception {
128         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
129         verifyWithInlineConfigParser(getPath("InputWriteTagIgnore.java"), expected);
130     }
131 
132     @Test
133     public void testRegularEx() throws Exception {
134         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
135         verifyWithInlineConfigParser(getPath("InputWriteTagRegularExpression.java"), expected);
136     }
137 
138     @Test
139     public void testRegularExError() throws Exception {
140         final String[] expected = {
141             "15: " + getCheckMessage(MSG_TAG_FORMAT, "@author", "ABC"),
142         };
143         verifyWithInlineConfigParser(getPath("InputWriteTagExpressionError.java"), expected);
144     }
145 
146     @Test
147     public void testEnumsAndAnnotations() throws Exception {
148         final String[] expected = {
149             "16: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
150                     "This enum needs more code..."),
151             "21: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
152                     "This enum constant needs more code..."),
153             "28: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
154                     "This annotation needs more code..."),
155             "33: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
156                     "This annotation field needs more code..."),
157         };
158         verifyWithInlineConfigParser(getPath("InputWriteTagEnumsAndAnnotations.java"), expected);
159     }
160 
161     @Test
162     public void testNoJavadocs() throws Exception {
163         final String[] expected = {
164             "13: " + getCheckMessage(MSG_MISSING_TAG, "null"),
165         };
166         verifyWithInlineConfigParser(getPath("InputWriteTagNoJavadoc.java"), expected);
167     }
168 
169     @Test
170     public void testWriteTagRecordsAndCompactCtors() throws Exception {
171         final String[] expected = {
172             "15: " + getCheckMessage(MSG_MISSING_TAG, "@incomplete"),
173             "19: " + getCheckMessage(MSG_TAG_FORMAT, "@incomplete", "\\S"),
174             "26: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
175                     "Failed to recognize 'record' introduced in Java 14."),
176             "33: " + getCheckMessage(MSG_MISSING_TAG, "@incomplete"),
177             "37: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
178                     "Failed to recognize 'record' introduced in Java 14."),
179             "44: " + getCheckMessage(MSG_MISSING_TAG, "@incomplete"),
180             "48: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
181                     "Failed to recognize 'record' introduced in Java 14."),
182             "56: " + getCheckMessage(MSG_MISSING_TAG, "@incomplete"),
183             "58: " + getCheckMessage(MSG_MISSING_TAG, "@incomplete"),
184             "62: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
185                     "Failed to recognize 'record' introduced in Java 14."),
186         };
187         verifyWithInlineConfigParser(
188             getNonCompilablePath("InputWriteTagRecordsAndCompactCtors.java"), expected);
189     }
190 
191     @Override
192     protected void verify(Checker checker,
193                           File[] processedFiles,
194                           String messageFileName,
195                           String... expected)
196             throws Exception {
197         getStream().flush();
198         final List<File> theFiles = new ArrayList<>();
199         Collections.addAll(theFiles, processedFiles);
200         final int errs = checker.process(theFiles);
201 
202         // process each of the lines
203         try (ByteArrayInputStream localStream =
204                 new ByteArrayInputStream(getStream().toByteArray());
205             LineNumberReader lnr = new LineNumberReader(
206                 new InputStreamReader(localStream, StandardCharsets.UTF_8))) {
207             for (int i = 0; i < expected.length; i++) {
208                 final String expectedResult = messageFileName + ":" + expected[i];
209                 final String actual = lnr.readLine();
210                 assertWithMessage("error message " + i)
211                         .that(actual)
212                         .isEqualTo(expectedResult);
213             }
214 
215             assertWithMessage("unexpected output: " + lnr.readLine())
216                     .that(errs)
217                     .isAtMost(expected.length);
218         }
219         checker.destroy();
220     }
221 
222 }