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.indentation;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.indentation.CommentsIndentationCheck.MSG_KEY_BLOCK;
23  import static com.puppycrawl.tools.checkstyle.checks.indentation.CommentsIndentationCheck.MSG_KEY_SINGLE;
24  
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.api.DetailAST;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
33  
34  public class CommentsIndentationCheckTest extends AbstractModuleTestSupport {
35  
36      @Override
37      protected String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/indentation/commentsindentation";
39      }
40  
41      @Test
42      public void testCommentIsAtTheEndOfBlock() throws Exception {
43          final DefaultConfiguration checkConfig =
44              createModuleConfig(CommentsIndentationCheck.class);
45          final String[] expected = {
46              "18: " + getCheckMessage(MSG_KEY_SINGLE, 17, 25, 8),
47              "33: " + getCheckMessage(MSG_KEY_SINGLE, 35, 5, 4),
48              "37: " + getCheckMessage(MSG_KEY_SINGLE, 36, 0, 8),
49              "47: " + getCheckMessage(MSG_KEY_SINGLE, 46, 15, 12),
50              "49: " + getCheckMessage(MSG_KEY_SINGLE, 45, 10, 8),
51              "54: " + getCheckMessage(MSG_KEY_SINGLE, 53, 13, 8),
52              "74: " + getCheckMessage(MSG_KEY_SINGLE, 70, 18, 8),
53              "88: " + getCheckMessage(MSG_KEY_SINGLE, 85, 31, 8),
54              "100: " + getCheckMessage(MSG_KEY_SINGLE, 98, 21, 8),
55              "115: " + getCheckMessage(MSG_KEY_SINGLE, 116, 29, 12),
56              "138: " + getCheckMessage(MSG_KEY_SINGLE, 131, 26, 8),
57              "164: " + getCheckMessage(MSG_KEY_SINGLE, 161, 33, 8),
58              "174: " + getCheckMessage(MSG_KEY_SINGLE, 173, 21, 8),
59              "186: " + getCheckMessage(MSG_KEY_SINGLE, 184, 34, 8),
60              "208: " + getCheckMessage(MSG_KEY_SINGLE, 206, 26, 8),
61              "214: " + getCheckMessage(MSG_KEY_SINGLE, 213, 0, 8),
62              "228: " + getCheckMessage(MSG_KEY_SINGLE, 225, 12, 8),
63              "234: " + getCheckMessage(MSG_KEY_SINGLE, 233, 0, 8),
64              "248: " + getCheckMessage(MSG_KEY_SINGLE, 245, 12, 8),
65              "255: " + getCheckMessage(MSG_KEY_SINGLE, 252, 0, 8),
66              "265: " + getCheckMessage(MSG_KEY_SINGLE, 264, 14, 8),
67              "271: " + getCheckMessage(MSG_KEY_SINGLE, 269, 9, 8),
68              "277: " + getCheckMessage(MSG_KEY_SINGLE, 276, 9, 8),
69              "316: " + getCheckMessage(MSG_KEY_SINGLE, 315, 9, 8),
70              "322: " + getCheckMessage(MSG_KEY_SINGLE, 323, 0, 4),
71              "336: " + getCheckMessage(MSG_KEY_SINGLE, 333, 0, 8),
72              "355: " + getCheckMessage(MSG_KEY_SINGLE, 352, 9, 8),
73              "380: " + getCheckMessage(MSG_KEY_BLOCK, 381, 12, 8),
74              "393: " + getCheckMessage(MSG_KEY_SINGLE, 392, 12, 8),
75              "400: " + getCheckMessage(MSG_KEY_SINGLE, 401, 8, 10),
76              "457: " + getCheckMessage(MSG_KEY_SINGLE, 455, 0, 8),
77              "473: " + getCheckMessage(MSG_KEY_BLOCK, 469, 10, 8),
78              "483: " + getCheckMessage(MSG_KEY_BLOCK, 477, 10, 8),
79              "491: " + getCheckMessage(MSG_KEY_BLOCK, 487, 10, 8),
80              "499: " + getCheckMessage(MSG_KEY_BLOCK, 495, 10, 8),
81              "507: " + getCheckMessage(MSG_KEY_BLOCK, 503, 10, 8),
82              "518: " + getCheckMessage(MSG_KEY_SINGLE, 511, 10, 8),
83              "525: " + getCheckMessage(MSG_KEY_SINGLE, 522, 0, 8),
84              "532: " + getCheckMessage(MSG_KEY_SINGLE, 529, 0, 8),
85              "538: " + getCheckMessage(MSG_KEY_SINGLE, 536, 0, 8),
86              "546: " + getCheckMessage(MSG_KEY_SINGLE, 542, 4, 8),
87              "551: " + getCheckMessage(MSG_KEY_SINGLE, 550, 12, 8),
88              "557: " + getCheckMessage(MSG_KEY_SINGLE, 555, 0, 8),
89              "562: " + getCheckMessage(MSG_KEY_SINGLE, 561, 0, 8),
90          };
91          final String testInputFile = "InputCommentsIndentationCommentIsAtTheEndOfBlock.java";
92          verify(checkConfig, getPath(testInputFile), expected);
93      }
94  
95      @Test
96      public void testCommentIsInsideSwitchBlock() throws Exception {
97          final DefaultConfiguration checkConfig =
98              createModuleConfig(CommentsIndentationCheck.class);
99          final String[] expected = {
100             "19: " + getCheckMessage(MSG_KEY_BLOCK, 20, 12, 16),
101             "25: " + getCheckMessage(MSG_KEY_SINGLE, "24, 26", 19, "16, 12"),
102             "31: " + getCheckMessage(MSG_KEY_SINGLE, "30, 32", 19, "16, 12"),
103             "48: " + getCheckMessage(MSG_KEY_SINGLE, 49, 6, 16),
104             "55: " + getCheckMessage(MSG_KEY_SINGLE, 56, 8, 12),
105             "59: " + getCheckMessage(MSG_KEY_SINGLE, 58, 22, 16),
106             "68: " + getCheckMessage(MSG_KEY_SINGLE, "65, 69", 14, "12, 16"),
107             "88: " + getCheckMessage(MSG_KEY_SINGLE, 89, 24, 20),
108             "113: " + getCheckMessage(MSG_KEY_SINGLE, "112, 114", 15, "17, 12"),
109             "125: " + getCheckMessage(MSG_KEY_SINGLE, 126, 8, 12),
110             "138: " + getCheckMessage(MSG_KEY_SINGLE, 139, 4, 8),
111             "157: " + getCheckMessage(MSG_KEY_SINGLE, "156, 158", 18, "16, 12"),
112             "200: " + getCheckMessage(MSG_KEY_SINGLE, "199, 201", 4, "12, 12"),
113             "203: " + getCheckMessage(MSG_KEY_SINGLE, "202, 206", 22, "16, 12"),
114             "204: " + getCheckMessage(MSG_KEY_SINGLE, "202, 206", 20, "16, 12"),
115             "205: " + getCheckMessage(MSG_KEY_SINGLE, "202, 206", 17, "16, 12"),
116             "229: " + getCheckMessage(MSG_KEY_SINGLE, "228, 230", 6, "12, 12"),
117             "276: " + getCheckMessage(MSG_KEY_BLOCK, "275, 279", 11, "16, 12"),
118             "281: " + getCheckMessage(MSG_KEY_SINGLE, "280, 282", 11, "16, 12"),
119         };
120         final String testInputFile = "InputCommentsIndentationInSwitchBlock.java";
121         verify(checkConfig, getPath(testInputFile), expected);
122     }
123 
124     @Test
125     public void testCommentIsInsideEmptyBlock() throws Exception {
126         final DefaultConfiguration checkConfig =
127             createModuleConfig(CommentsIndentationCheck.class);
128         final String[] expected = {
129             "9: " + getCheckMessage(MSG_KEY_SINGLE, 12, 19, 31),
130             "10: " + getCheckMessage(MSG_KEY_BLOCK, 12, 23, 31),
131             "33: " + getCheckMessage(MSG_KEY_SINGLE, 34, 0, 8),
132             "57: " + getCheckMessage(MSG_KEY_SINGLE, 58, 0, 8),
133             "71: " + getCheckMessage(MSG_KEY_SINGLE, 72, 0, 8),
134             "103: " + getCheckMessage(MSG_KEY_SINGLE, 104, 0, 8),
135             "107: " + getCheckMessage(MSG_KEY_SINGLE, 108, 0, 8),
136         };
137         final String testInputFile = "InputCommentsIndentationInEmptyBlock.java";
138         verify(checkConfig, getPath(testInputFile), expected);
139     }
140 
141     @Test
142     public void testSurroundingCode() throws Exception {
143         final DefaultConfiguration checkConfig =
144             createModuleConfig(CommentsIndentationCheck.class);
145         final String[] expected = {
146             "13: " + getCheckMessage(MSG_KEY_SINGLE, 14, 14, 12),
147             "23: " + getCheckMessage(MSG_KEY_BLOCK, 24, 16, 12),
148             "25: " + getCheckMessage(MSG_KEY_BLOCK, 27, 16, 12),
149             "28: " + getCheckMessage(MSG_KEY_BLOCK, 31, 16, 12),
150             "50: " + getCheckMessage(MSG_KEY_SINGLE, 53, 27, 36),
151             "51: " + getCheckMessage(MSG_KEY_BLOCK, 53, 23, 36),
152             "90: " + getCheckMessage(MSG_KEY_SINGLE, 91, 14, 8),
153             "98: " + getCheckMessage(MSG_KEY_SINGLE, 100, 13, 8),
154             "108: " + getCheckMessage(MSG_KEY_SINGLE, 109, 33, 8),
155             "130: " + getCheckMessage(MSG_KEY_BLOCK, 131, 12, 8),
156             "135: " + getCheckMessage(MSG_KEY_BLOCK, 136, 4, 8),
157             "141: " + getCheckMessage(MSG_KEY_BLOCK, 140, 4, 8),
158         };
159         final String testInputFile = "InputCommentsIndentationSurroundingCode.java";
160         verify(checkConfig, getPath(testInputFile), expected);
161     }
162 
163     @Test
164     public void testNoNpeWhenBlockCommentEndsClassFile() throws Exception {
165         final DefaultConfiguration checkConfig =
166             createModuleConfig(CommentsIndentationCheck.class);
167         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
168         final String testInputFile = "InputCommentsIndentationNoNpe.java";
169         verify(checkConfig, getPath(testInputFile), expected);
170     }
171 
172     @Test
173     public void testCheckOnlySingleLineComments() throws Exception {
174         final DefaultConfiguration checkConfig =
175             createModuleConfig(CommentsIndentationCheck.class);
176         checkConfig.addAttribute("tokens", "SINGLE_LINE_COMMENT");
177         final String[] expected = {
178             "13: " + getCheckMessage(MSG_KEY_SINGLE, 14, 14, 12),
179             "50: " + getCheckMessage(MSG_KEY_SINGLE, 53, 27, 36),
180             "90: " + getCheckMessage(MSG_KEY_SINGLE, 91, 14, 8),
181             "98: " + getCheckMessage(MSG_KEY_SINGLE, 100, 13, 8),
182             "108: " + getCheckMessage(MSG_KEY_SINGLE, 109, 33, 8),
183         };
184         final String testInputFile = "InputCommentsIndentationSurroundingCode.java";
185         verify(checkConfig, getPath(testInputFile), expected);
186     }
187 
188     @Test
189     public void testCheckOnlyBlockComments() throws Exception {
190         final DefaultConfiguration checkConfig =
191             createModuleConfig(CommentsIndentationCheck.class);
192         checkConfig.addAttribute("tokens", "BLOCK_COMMENT_BEGIN");
193         final String[] expected = {
194             "23: " + getCheckMessage(MSG_KEY_BLOCK, 24, 16, 12),
195             "25: " + getCheckMessage(MSG_KEY_BLOCK, 27, 16, 12),
196             "28: " + getCheckMessage(MSG_KEY_BLOCK, 31, 16, 12),
197             "51: " + getCheckMessage(MSG_KEY_BLOCK, 53, 23, 36),
198             "130: " + getCheckMessage(MSG_KEY_BLOCK, 131, 12, 8),
199             "135: " + getCheckMessage(MSG_KEY_BLOCK, 136, 4, 8),
200             "141: " + getCheckMessage(MSG_KEY_BLOCK, 140, 4, 8),
201         };
202         final String testInputFile = "InputCommentsIndentationSurroundingCode.java";
203         verify(checkConfig, getPath(testInputFile), expected);
204     }
205 
206     @Test
207     public void testVisitToken() {
208         final CommentsIndentationCheck check = new CommentsIndentationCheck();
209         final DetailAST methodDef = new DetailAST();
210         methodDef.setType(TokenTypes.METHOD_DEF);
211         methodDef.setText("methodStub");
212         try {
213             check.visitToken(methodDef);
214             Assert.fail("IllegalArgumentException should have been thrown!");
215         }
216         catch (IllegalArgumentException ex) {
217             final String msg = ex.getMessage();
218             Assert.assertEquals("Invalid exception message",
219                     "Unexpected token type: methodStub", msg);
220         }
221     }
222 
223     @Test
224     public void testJavadoc() throws Exception {
225         final DefaultConfiguration checkConfig = createModuleConfig(CommentsIndentationCheck.class);
226         final String[] expected = {
227             "3: " + getCheckMessage(MSG_KEY_BLOCK, 6, 2, 0),
228             "8: " + getCheckMessage(MSG_KEY_BLOCK, 9, 0, 4),
229             "11: " + getCheckMessage(MSG_KEY_BLOCK, 14, 8, 4),
230             "17: " + getCheckMessage(MSG_KEY_BLOCK, 18, 10, 8),
231         };
232         final String testInputFile = "InputCommentsIndentationJavadoc.java";
233         verify(checkConfig, getPath(testInputFile), expected);
234     }
235 
236     @Test
237     public void testMultiblockStructures() throws Exception {
238         final DefaultConfiguration checkConfig = createModuleConfig(CommentsIndentationCheck.class);
239         final String[] expected = {
240             "12: " + getCheckMessage(MSG_KEY_SINGLE, 11, 8, 12),
241             "18: " + getCheckMessage(MSG_KEY_SINGLE, "17, 19", 16, "12, 8"),
242             "23: " + getCheckMessage(MSG_KEY_SINGLE, "22, 24", 0, "12, 8"),
243             "33: " + getCheckMessage(MSG_KEY_SINGLE, 32, 8, 12),
244             "39: " + getCheckMessage(MSG_KEY_SINGLE, "38, 40", 0, "12, 8"),
245             "44: " + getCheckMessage(MSG_KEY_SINGLE, "43, 45", 16, "12, 8"),
246             "54: " + getCheckMessage(MSG_KEY_SINGLE, 53, 8, 12),
247             "60: " + getCheckMessage(MSG_KEY_SINGLE, "59, 61", 0, "12, 8"),
248             "65: " + getCheckMessage(MSG_KEY_SINGLE, "64, 66", 16, "12, 8"),
249             "75: " + getCheckMessage(MSG_KEY_SINGLE, 74, 8, 12),
250             "81: " + getCheckMessage(MSG_KEY_SINGLE, "80, 82", 0, "12, 8"),
251             "86: " + getCheckMessage(MSG_KEY_SINGLE, "85, 87", 16, "12, 8"),
252             "96: " + getCheckMessage(MSG_KEY_SINGLE, 95, 8, 12),
253             "102: " + getCheckMessage(MSG_KEY_SINGLE, "101, 103", 0, "12, 8"),
254             "107: " + getCheckMessage(MSG_KEY_SINGLE, "106, 108", 16, "12, 8"),
255             "117: " + getCheckMessage(MSG_KEY_SINGLE, 116, 8, 12),
256             "123: " + getCheckMessage(MSG_KEY_SINGLE, "122, 124", 16, "12, 8"),
257             "128: " + getCheckMessage(MSG_KEY_SINGLE, "127, 129", 0, "12, 8"),
258         };
259         final String testInputFile = "InputCommentsIndentationInMultiblockStructures.java";
260         verify(checkConfig, getPath(testInputFile), expected);
261     }
262 
263     @Test
264     public void testCommentsAfterAnnotation() throws Exception {
265         final DefaultConfiguration checkConfig = createModuleConfig(CommentsIndentationCheck.class);
266         final String[] expected = {
267             "14: " + getCheckMessage(MSG_KEY_SINGLE, 15, 4, 0),
268             "18: " + getCheckMessage(MSG_KEY_SINGLE, 19, 8, 4),
269             "36: " + getCheckMessage(MSG_KEY_SINGLE, 37, 4, 0),
270             "41: " + getCheckMessage(MSG_KEY_SINGLE, 42, 8, 4),
271             "50: " + getCheckMessage(MSG_KEY_SINGLE, 51, 2, 4),
272         };
273         final String testInputFile = "InputCommentsIndentationAfterAnnotation.java";
274         verify(checkConfig, getPath(testInputFile), expected);
275     }
276 
277 }