View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.google.checkstyle.test.chapter4formatting.rule4861blockcommentstyle;
21  
22  import org.junit.Test;
23  
24  import com.google.checkstyle.test.base.AbstractModuleTestSupport;
25  import com.puppycrawl.tools.checkstyle.api.Configuration;
26  import com.puppycrawl.tools.checkstyle.checks.indentation.CommentsIndentationCheck;
27  
28  public class CommentsIndentationTest extends AbstractModuleTestSupport {
29  
30      @Override
31      protected String getPackageLocation() {
32          return "com/google/checkstyle/test/chapter4formatting/rule4861blockcommentstyle";
33      }
34  
35      @Test
36      public void testCommentIsAtTheEndOfBlock() throws Exception {
37          final String[] expected = {
38              "18: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
39                  17, 25, 8),
40              "33: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
41                  35, 5, 4),
42              "37: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
43                  36, 0, 8),
44              "47: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
45                  46, 15, 12),
46              "49: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
47                  45, 10, 8),
48              "54: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
49                  53, 13, 8),
50              "74: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
51                  70, 18, 8),
52              "88: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
53                  85, 31, 8),
54              "100: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
55                  98, 21, 8),
56              "115: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
57                  116, 29, 12),
58              "138: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
59                  131, 26, 8),
60              "164: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
61                  161, 33, 8),
62              "174: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
63                  173, 21, 8),
64              "186: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
65                  184, 34, 8),
66              "208: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
67                  206, 26, 8),
68              "214: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
69                  213, 0, 8),
70              "228: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
71                  225, 12, 8),
72              "234: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
73                  233, 0, 8),
74              "248: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
75                  245, 12, 8),
76              "255: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
77                  252, 0, 8),
78              "265: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
79                  264, 14, 8),
80              "271: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
81                  269, 9, 8),
82              "277: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
83                  276, 9, 8),
84              "316: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
85                  315, 9, 8),
86              "322: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
87                  323, 0, 4),
88              "336: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
89                  333, 0, 8),
90              "355: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
91                  352, 9, 8),
92              };
93  
94          final Configuration checkConfig = getModuleConfig("CommentsIndentation");
95          final String filePath =
96              getPath("InputCommentsIndentationCommentIsAtTheEndOfBlock.java");
97  
98          final Integer[] warnList = getLinesWithWarn(filePath);
99          verify(checkConfig, filePath, expected, warnList);
100     }
101 
102     @Test
103     public void testCommentIsInsideSwitchBlock() throws Exception {
104         final String[] expected = {
105             "19: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.block",
106                 20, 12, 16),
107             "25: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
108                 "24, 26", 19, "16, 12"),
109             "31: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
110                 "30, 32", 19, "16, 12"),
111             "48: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
112                 49, 6, 16),
113             "55: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
114                 56, 8, 12),
115             "59: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
116                 58, 22, 16),
117             "68: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
118                 "65, 69", 14, "12, 16"),
119             "88: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
120                 89, 24, 20),
121             "113: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
122                 "112, 114", 15, "17, 12"),
123             "125: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
124                 126, 8, 12),
125             "138: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
126                 139, 4, 8),
127             "157: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
128                 "156, 158", 18, "16, 12"),
129             "200: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
130                 "199, 201", 4, "12, 12"),
131             "203: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
132                 "202, 206", 22, "16, 12"),
133             "204: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
134                 "202, 206", 20, "16, 12"),
135             "205: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
136                 "202, 206", 17, "16, 12"),
137             "229: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
138                 "228, 230", 6, "12, 12"),
139             };
140 
141         final Configuration checkConfig = getModuleConfig("CommentsIndentation");
142         final String filePath =
143             getPath("InputCommentsIndentationInSwitchBlock.java");
144 
145         final Integer[] warnList = getLinesWithWarn(filePath);
146         verify(checkConfig, filePath, expected, warnList);
147     }
148 
149     @Test
150     public void testCommentIsInsideEmptyBlock() throws Exception {
151         final String[] expected = {
152             "9: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
153                 12, 19, 31),
154             "10: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.block",
155                 12, 23, 31),
156             "33: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
157                 34, 0, 8),
158             "57: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
159                 58, 0, 8),
160             "71: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
161                 72, 0, 8),
162             };
163 
164         final Configuration checkConfig = getModuleConfig("CommentsIndentation");
165         final String filePath =
166             getPath("InputCommentsIndentationInEmptyBlock.java");
167 
168         final Integer[] warnList = getLinesWithWarn(filePath);
169         verify(checkConfig, filePath, expected, warnList);
170     }
171 
172     @Test
173     public void testSurroundingCode() throws Exception {
174         final String[] expected = {
175             "13: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
176                 14, 14, 12),
177             "23: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.block",
178                 24, 16, 12),
179             "25: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.block",
180                 27, 16, 12),
181             "28: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.block",
182                 31, 16, 12),
183             "50: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
184                 53, 27, 36),
185             "51: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.block",
186                 53, 23, 36),
187             "90: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
188                 91, 14, 8),
189             "98: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
190                 100, 13, 8),
191             "108: " + getCheckMessage(CommentsIndentationCheck.class, "comments.indentation.single",
192                 109, 33, 8),
193             };
194 
195         final Configuration checkConfig = getModuleConfig("CommentsIndentation");
196         final String filePath =
197             getPath("InputCommentsIndentationSurroundingCode.java");
198 
199         final Integer[] warnList = getLinesWithWarn(filePath);
200         verify(checkConfig, filePath, expected, warnList);
201     }
202 
203 }