1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 verifyWithInlineConfigParserTwice(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 verifyWithInlineConfigParserTwice(
72 getPath("InputWriteTagMissingFormat.java"), expected);
73 }
74
75 @Test
76 public void testTagIncomplete() throws Exception {
77 final String[] expected = {
78 "16: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
79 "This class needs more code..."),
80 };
81 verifyWithInlineConfigParserTwice(
82 getPath("InputWriteTagIncomplete.java"), expected);
83 }
84
85 @Test
86 public void testDoubleTag() throws Exception {
87 final String[] expected = {
88 "18: " + getCheckMessage(MSG_WRITE_TAG, "@doubletag", "first text"),
89 "19: " + getCheckMessage(MSG_WRITE_TAG, "@doubletag", "second text"),
90 };
91 verifyWithInlineConfigParserTwice(
92 getPath("InputWriteTagDoubleTag.java"), expected);
93 }
94
95 @Test
96 public void testEmptyTag() throws Exception {
97 final String[] expected = {
98 "19: " + getCheckMessage(MSG_WRITE_TAG, "@emptytag", ""),
99 };
100 verifyWithInlineConfigParserTwice(
101 getPath("InputWriteTagEmptyTag.java"), expected);
102 }
103
104 @Test
105 public void testMissingTag() throws Exception {
106 final String[] expected = {
107 "20: " + getCheckMessage(MSG_MISSING_TAG, "@missingtag"),
108 };
109 verifyWithInlineConfigParserTwice(
110 getPath("InputWriteTagMissingTag.java"), expected);
111 }
112
113 @Test
114 public void testMethod() throws Exception {
115 final String[] expected = {
116 "24: " + getCheckMessage(MSG_WRITE_TAG, "@todo",
117 "Add a constructor comment"),
118 "36: " + getCheckMessage(MSG_WRITE_TAG, "@todo", "Add a comment"),
119 };
120 verifyWithInlineConfigParserTwice(
121 getPath("InputWriteTagMethod.java"), expected);
122 }
123
124 @Test
125 public void testSeverity() throws Exception {
126 final String[] expected = {
127 "16: " + getCheckMessage(MSG_WRITE_TAG, "@author", "Daniel Grenner"),
128 };
129 verifyWithInlineConfigParserTwice(
130 getPath("InputWriteTagSeverity.java"), expected);
131 }
132
133 @Test
134 public void testIgnoreMissing() throws Exception {
135 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
136 verifyWithInlineConfigParserTwice(getPath("InputWriteTagIgnore.java"), expected);
137 }
138
139 @Test
140 public void testRegularEx() throws Exception {
141 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
142 verifyWithInlineConfigParserTwice(
143 getPath("InputWriteTagRegularExpression.java"), expected);
144 }
145
146 @Test
147 public void testRegularExError() throws Exception {
148 final String[] expected = {
149 "15: " + getCheckMessage(MSG_TAG_FORMAT, "@author", "ABC"),
150 };
151 verifyWithInlineConfigParserTwice(
152 getPath("InputWriteTagExpressionError.java"), expected);
153 }
154
155 @Test
156 public void testEnumsAndAnnotations() throws Exception {
157 final String[] expected = {
158 "16: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
159 "This enum needs more code..."),
160 "21: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
161 "This enum constant needs more code..."),
162 "28: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
163 "This annotation needs more code..."),
164 "33: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
165 "This annotation field needs more code..."),
166 };
167 verifyWithInlineConfigParserTwice(
168 getPath("InputWriteTagEnumsAndAnnotations.java"), expected);
169 }
170
171 @Test
172 public void testNoJavadocs() throws Exception {
173 final String[] expected = {
174 "13: " + getCheckMessage(MSG_MISSING_TAG, "null"),
175 };
176 verifyWithInlineConfigParserTwice(getPath("InputWriteTagNoJavadoc.java"), expected);
177 }
178
179 @Test
180 public void testWriteTagRecordsAndCompactCtors() throws Exception {
181 final String[] expected = {
182 "15: " + getCheckMessage(MSG_MISSING_TAG, "@incomplete"),
183 "19: " + getCheckMessage(MSG_TAG_FORMAT, "@incomplete", "\\S"),
184 "26: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
185 "Failed to recognize 'record' introduced in Java 14."),
186 "33: " + getCheckMessage(MSG_MISSING_TAG, "@incomplete"),
187 "37: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
188 "Failed to recognize 'record' introduced in Java 14."),
189 "44: " + getCheckMessage(MSG_MISSING_TAG, "@incomplete"),
190 "48: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
191 "Failed to recognize 'record' introduced in Java 14."),
192 "56: " + getCheckMessage(MSG_MISSING_TAG, "@incomplete"),
193 "58: " + getCheckMessage(MSG_MISSING_TAG, "@incomplete"),
194 "62: " + getCheckMessage(MSG_WRITE_TAG, "@incomplete",
195 "Failed to recognize 'record' introduced in Java 14."),
196 };
197 verifyWithInlineConfigParserTwice(
198 getNonCompilablePath("InputWriteTagRecordsAndCompactCtors.java"), expected);
199 }
200
201 @Override
202 protected void verify(Checker checker,
203 File[] processedFiles,
204 String messageFileName,
205 String... expected)
206 throws Exception {
207 getStream().flush();
208 final List<File> theFiles = new ArrayList<>();
209 Collections.addAll(theFiles, processedFiles);
210 final int errs = checker.process(theFiles);
211
212
213 try (ByteArrayInputStream localStream =
214 new ByteArrayInputStream(getStream().toByteArray());
215 LineNumberReader lnr = new LineNumberReader(
216 new InputStreamReader(localStream, StandardCharsets.UTF_8))) {
217 for (int i = 0; i < expected.length; i++) {
218 final String expectedResult = messageFileName + ":" + expected[i];
219 final String actual = lnr.readLine();
220 assertWithMessage("error message " + i)
221 .that(actual)
222 .isEqualTo(expectedResult);
223 }
224
225 assertWithMessage("unexpected output: " + lnr.readLine())
226 .that(errs)
227 .isAtMost(expected.length);
228 }
229 checker.destroy();
230 }
231
232 }