View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.whitespace;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceBeforeEmptyBodyCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  
30  public class WhitespaceBeforeEmptyBodyCheckTest extends AbstractModuleTestSupport {
31  
32      @Override
33      public String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/whitespace/whitespacebeforeemptybody";
35      }
36  
37      @Test
38      public void testGetRequiredTokens() {
39          final WhitespaceBeforeEmptyBodyCheck checkObj = new WhitespaceBeforeEmptyBodyCheck();
40          assertWithMessage(
41                  "WhitespaceAroundCheck#getRequiredTokens should return empty array by default")
42                  .that(checkObj.getRequiredTokens())
43                  .isEmpty();
44      }
45  
46      @Test
47      public void testGetAcceptableTokens() {
48          final WhitespaceBeforeEmptyBodyCheck checkObj = new WhitespaceBeforeEmptyBodyCheck();
49          final int[] expected = {
50              TokenTypes.METHOD_DEF,
51              TokenTypes.CTOR_DEF,
52              TokenTypes.COMPACT_CTOR_DEF,
53              TokenTypes.CLASS_DEF,
54              TokenTypes.INTERFACE_DEF,
55              TokenTypes.RECORD_DEF,
56              TokenTypes.ENUM_DEF,
57              TokenTypes.ANNOTATION_DEF,
58              TokenTypes.LITERAL_NEW,
59              TokenTypes.LITERAL_DO,
60              TokenTypes.LITERAL_WHILE,
61              TokenTypes.LITERAL_FOR,
62              TokenTypes.LITERAL_IF,
63              TokenTypes.LITERAL_ELSE,
64              TokenTypes.STATIC_INIT,
65              TokenTypes.LITERAL_TRY,
66              TokenTypes.LITERAL_CATCH,
67              TokenTypes.LITERAL_FINALLY,
68              TokenTypes.LITERAL_SYNCHRONIZED,
69              TokenTypes.LITERAL_SWITCH,
70              TokenTypes.LAMBDA,
71          };
72          assertWithMessage("Acceptable tokens are invalid")
73                  .that(checkObj.getAcceptableTokens())
74                  .isEqualTo(expected);
75      }
76  
77      @Test
78      public void testAnonymousClassWithEmptyBody() throws Exception {
79          final String[] expected = {
80              "22:26: " + getCheckMessage(MSG_KEY, "{"),
81              "26:26: " + getCheckMessage(MSG_KEY, "{"),
82              "29:26: " + getCheckMessage(MSG_KEY, "{"),
83              "32:26: " + getCheckMessage(MSG_KEY, "{"),
84          };
85  
86          verifyWithInlineConfigParser(
87                  getPath("InputWhitespaceBeforeEmptyBodyAnonymousClass.java"),
88                  expected);
89      }
90  
91      @Test
92      public void testConstructorWithEmptyBody() throws Exception {
93          final String[] expected = {
94              "13:48: " + getCheckMessage(MSG_KEY, "{"),
95              "16:53: " + getCheckMessage(MSG_KEY, "{"),
96              "26:12: " + getCheckMessage(MSG_KEY, "{"),
97          };
98  
99          verifyWithInlineConfigParser(
100                 getPath("InputWhitespaceBeforeEmptyBodyConstructor.java"), expected);
101     }
102 
103     @Test
104     public void testDefaultWithEmptyBody() throws Exception {
105         final String[] expected = {
106             "24:36: " + getCheckMessage(MSG_KEY, "{"),
107             "28:12: " + getCheckMessage(MSG_KEY, "{"),
108             "29:30: " + getCheckMessage(MSG_KEY, "{"),
109             "30:18: " + getCheckMessage(MSG_KEY, "{"),
110             "35:28: " + getCheckMessage(MSG_KEY, "{"),
111             "41:19: " + getCheckMessage(MSG_KEY, "{"),
112         };
113 
114         verifyWithInlineConfigParser(
115                 getPath("InputWhitespaceBeforeEmptyBodyDefault.java"), expected);
116     }
117 
118     @Test
119     public void testInitializersWithEmptyBody() throws Exception {
120         final String[] expected = {
121             "12:11: " + getCheckMessage(MSG_KEY, "{"),
122             "15:11: " + getCheckMessage(MSG_KEY, "{"),
123             "17:11: " + getCheckMessage(MSG_KEY, "{"),
124         };
125 
126         verifyWithInlineConfigParser(
127                 getPath("InputWhitespaceBeforeEmptyBodyInitializers.java"), expected);
128     }
129 
130     @Test
131     public void testLambdaWithEmptyBody() throws Exception {
132         final String[] expected = {
133             "21:22: " + getCheckMessage(MSG_KEY, "{"),
134             "25:22: " + getCheckMessage(MSG_KEY, "{"),
135         };
136 
137         verifyWithInlineConfigParser(
138                 getPath("InputWhitespaceBeforeEmptyBodyLambda.java"), expected);
139     }
140 
141     @Test
142     public void testLoopsWithEmptyBody() throws Exception {
143         final String[] expected = {
144             "18:36: " + getCheckMessage(MSG_KEY, "{"),
145             "20:36: " + getCheckMessage(MSG_KEY, "{"),
146             "27:23: " + getCheckMessage(MSG_KEY, "{"),
147             "30:11: " + getCheckMessage(MSG_KEY, "{"),
148         };
149 
150         verifyWithInlineConfigParser(
151                 getPath("InputWhitespaceBeforeEmptyBodyLoops.java"), expected);
152     }
153 
154     @Test
155     public void testMethodWithEmptyBody() throws Exception {
156         final String[] expected = {
157             "14:19: " + getCheckMessage(MSG_KEY, "{"),
158             "17:19: " + getCheckMessage(MSG_KEY, "{"),
159             "29:36: " + getCheckMessage(MSG_KEY, "{"),
160             "31:46: " + getCheckMessage(MSG_KEY, "{"),
161         };
162 
163         verifyWithInlineConfigParser(
164                 getPath("InputWhitespaceBeforeEmptyBodyMethods.java"), expected);
165     }
166 
167     @Test
168     public void testMultiBlockStatementsWithEmptyBody() throws Exception {
169         final String[] expected = {
170             "19:20: " + getCheckMessage(MSG_KEY, "{"),
171             "23:13: " + getCheckMessage(MSG_KEY, "{"),
172             "28:22: " + getCheckMessage(MSG_KEY, "{"),
173             "29:15: " + getCheckMessage(MSG_KEY, "{"),
174             "34:28: " + getCheckMessage(MSG_KEY, "{"),
175             "40:12: " + getCheckMessage(MSG_KEY, "{"),
176             "42:28: " + getCheckMessage(MSG_KEY, "{"),
177             "49:39: " + getCheckMessage(MSG_KEY, "{"),
178             "55:16: " + getCheckMessage(MSG_KEY, "{"),
179         };
180 
181         verifyWithInlineConfigParser(
182                 getPath("InputWhitespaceBeforeEmptyBodyMultiBlockStatements.java"), expected);
183     }
184 
185     @Test
186     public void testSwitchWithEmptyBody() throws Exception {
187         final String[] expected = {
188             "15:19: " + getCheckMessage(MSG_KEY, "{"),
189             "17:19: " + getCheckMessage(MSG_KEY, "{"),
190         };
191 
192         verifyWithInlineConfigParser(
193                 getPath("InputWhitespaceBeforeEmptyBodySwitch.java"), expected);
194     }
195 
196     @Test
197     public void testSynchronizedWithEmptyBody() throws Exception {
198         final String[] expected = {
199             "13:28: " + getCheckMessage(MSG_KEY, "{"),
200             "16:28: " + getCheckMessage(MSG_KEY, "{"),
201         };
202 
203         verifyWithInlineConfigParser(
204                 getPath("InputWhitespaceBeforeEmptyBodySynchronized.java"), expected);
205     }
206 
207     @Test
208     public void testTypesWithEmptyBody() throws Exception {
209         final String[] expected = {
210             "12:12: " + getCheckMessage(MSG_KEY, "{"),
211             "19:38: " + getCheckMessage(MSG_KEY, "{"),
212             "29:16: " + getCheckMessage(MSG_KEY, "{"),
213             "37:25: " + getCheckMessage(MSG_KEY, "{"),
214             "48:11: " + getCheckMessage(MSG_KEY, "{"),
215             "57:19: " + getCheckMessage(MSG_KEY, "{"),
216             "64:14: " + getCheckMessage(MSG_KEY, "{"),
217         };
218 
219         verifyWithInlineConfigParser(
220                 getPath("InputWhitespaceBeforeEmptyBodyTypes.java"), expected);
221     }
222 
223     @Test
224     public void testCompactSourceFile() throws Exception {
225         final String[] expected = {
226             "18:17: " + getCheckMessage(MSG_KEY, "{"),
227             "21:25: " + getCheckMessage(MSG_KEY, "{"),
228             "24:15: " + getCheckMessage(MSG_KEY, "{"),
229             "27:19: " + getCheckMessage(MSG_KEY, "{"),
230         };
231 
232         final String filename = "compact/InputWhitespaceBeforeEmptyBodyCompactSourceFile.java";
233         verifyWithInlineConfigParser(
234                 getNonCompilablePath(filename), expected);
235     }
236 
237 }