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.TypeBodyPaddingCheck.MSG_AFTER_LCURLY;
24  import static com.puppycrawl.tools.checkstyle.checks.whitespace.TypeBodyPaddingCheck.MSG_BEFORE_RCURLY;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31  
32  public class TypeBodyPaddingCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      public String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/whitespace/typebodypadding";
37      }
38  
39      @Test
40      public void testDefault() throws Exception {
41          final String[] expected = {
42              "17:1: " + getCheckMessage(MSG_BEFORE_RCURLY),
43              "48:20: " + getCheckMessage(MSG_AFTER_LCURLY),
44              "56:1: " + getCheckMessage(MSG_BEFORE_RCURLY),
45              "58:19: " + getCheckMessage(MSG_AFTER_LCURLY),
46              "60:1: " + getCheckMessage(MSG_BEFORE_RCURLY),
47              "62:19: " + getCheckMessage(MSG_AFTER_LCURLY),
48              "72:1: " + getCheckMessage(MSG_BEFORE_RCURLY),
49              "81:32: " + getCheckMessage(MSG_AFTER_LCURLY),
50              "83:1: " + getCheckMessage(MSG_BEFORE_RCURLY),
51              "93:22: " + getCheckMessage(MSG_AFTER_LCURLY),
52              "96:1: " + getCheckMessage(MSG_BEFORE_RCURLY),
53              "105:33: " + getCheckMessage(MSG_AFTER_LCURLY),
54              "107:1: " + getCheckMessage(MSG_BEFORE_RCURLY),
55              "116:34: " + getCheckMessage(MSG_AFTER_LCURLY),
56              "118:1: " + getCheckMessage(MSG_BEFORE_RCURLY),
57          };
58          verifyWithInlineConfigParser(
59                  getPath("InputTypeBodyPaddingDefault.java"), expected);
60  
61          final String[] expected2 = CommonUtil.EMPTY_STRING_ARRAY;
62          verifyWithInlineConfigParser(
63                  getPath("InputTypeBodyPaddingDefault2.java"), expected2);
64      }
65  
66      @Test
67      public void testAllowEmptyFalse() throws Exception {
68          final String[] expected = {
69              "15:38: " + getCheckMessage(MSG_AFTER_LCURLY),
70              "15:39: " + getCheckMessage(MSG_BEFORE_RCURLY),
71              "19:24: " + getCheckMessage(MSG_AFTER_LCURLY),
72              "20:1: " + getCheckMessage(MSG_BEFORE_RCURLY),
73              "23:1: " + getCheckMessage(MSG_AFTER_LCURLY),
74              "23:2: " + getCheckMessage(MSG_BEFORE_RCURLY),
75              "27:1: " + getCheckMessage(MSG_AFTER_LCURLY),
76              "28:1: " + getCheckMessage(MSG_BEFORE_RCURLY),
77              "43:64: " + getCheckMessage(MSG_AFTER_LCURLY),
78              "43:65: " + getCheckMessage(MSG_BEFORE_RCURLY),
79              "49:36: " + getCheckMessage(MSG_AFTER_LCURLY),
80              "49:37: " + getCheckMessage(MSG_BEFORE_RCURLY),
81          };
82          verifyWithInlineConfigParser(
83                  getPath("InputTypeBodyPaddingAllowEmpty.java"), expected);
84      }
85  
86      @Test
87      public void testStartingEndingFalse() throws Exception {
88          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
89          verifyWithInlineConfigParser(
90                  getPath("InputTypeBodyPaddingFalse.java"), expected);
91      }
92  
93      @Test
94      public void testSkipInnerFalse() throws Exception {
95          final String[] expected = {
96              "17:22: " + getCheckMessage(MSG_AFTER_LCURLY),
97              "19:5: " + getCheckMessage(MSG_BEFORE_RCURLY),
98              "22:30: " + getCheckMessage(MSG_AFTER_LCURLY),
99              "24:5: " + getCheckMessage(MSG_BEFORE_RCURLY),
100             "27:20: " + getCheckMessage(MSG_AFTER_LCURLY),
101             "29:5: " + getCheckMessage(MSG_BEFORE_RCURLY),
102             "32:31: " + getCheckMessage(MSG_AFTER_LCURLY),
103             "34:5: " + getCheckMessage(MSG_BEFORE_RCURLY),
104         };
105         verifyWithInlineConfigParser(
106                 getPath("InputTypeBodyPaddingSkipInnerFalse.java"), expected);
107     }
108 
109     @Test
110     public void testSkipLocalFalse() throws Exception {
111         final String[] expected = {
112             "35:26: " + getCheckMessage(MSG_AFTER_LCURLY),
113             "37:9: " + getCheckMessage(MSG_BEFORE_RCURLY),
114             "42:32: " + getCheckMessage(MSG_AFTER_LCURLY),
115             "44:9: " + getCheckMessage(MSG_BEFORE_RCURLY),
116             "49:38: " + getCheckMessage(MSG_AFTER_LCURLY),
117             "51:9: " + getCheckMessage(MSG_BEFORE_RCURLY),
118             "56:40: " + getCheckMessage(MSG_AFTER_LCURLY),
119             "58:9: " + getCheckMessage(MSG_BEFORE_RCURLY),
120             "63:34: " + getCheckMessage(MSG_AFTER_LCURLY),
121             "65:9: " + getCheckMessage(MSG_BEFORE_RCURLY),
122         };
123         verifyWithInlineConfigParser(
124                 getPath("InputTypeBodyPaddingSkipLocalFalse.java"), expected);
125     }
126 
127     @Test
128     public void testGetAcceptableTokens() {
129         final TypeBodyPaddingCheck check = new TypeBodyPaddingCheck();
130         final int[] actual = check.getAcceptableTokens();
131         final int[] expected = {
132             TokenTypes.CLASS_DEF,
133             TokenTypes.INTERFACE_DEF,
134             TokenTypes.ENUM_DEF,
135             TokenTypes.RECORD_DEF,
136             TokenTypes.ANNOTATION_DEF,
137         };
138         assertWithMessage("Invalid acceptable tokens")
139             .that(actual)
140             .isEqualTo(expected);
141     }
142 
143     @Test
144     public void testGetDefaultTokens() {
145         final TypeBodyPaddingCheck check = new TypeBodyPaddingCheck();
146         final int[] actual = check.getDefaultTokens();
147         final int[] expected = {
148             TokenTypes.CLASS_DEF,
149             TokenTypes.INTERFACE_DEF,
150             TokenTypes.ENUM_DEF,
151             TokenTypes.RECORD_DEF,
152             TokenTypes.ANNOTATION_DEF,
153         };
154         assertWithMessage("Invalid default tokens")
155             .that(actual)
156             .isEqualTo(expected);
157     }
158 
159     @Test
160     public void testGetRequiredTokens() {
161         final TypeBodyPaddingCheck check = new TypeBodyPaddingCheck();
162         final int[] actual = check.getRequiredTokens();
163         assertWithMessage("Invalid required tokens")
164             .that(actual)
165             .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
166     }
167 
168 }