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