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.ArrayBracketNoWhitespaceCheck.MSG_WS_FOLLOWED;
24  import static com.puppycrawl.tools.checkstyle.checks.whitespace.ArrayBracketNoWhitespaceCheck.MSG_WS_NOT_FOLLOWED;
25  import static com.puppycrawl.tools.checkstyle.checks.whitespace.ArrayBracketNoWhitespaceCheck.MSG_WS_NOT_PRECEDED;
26  import static com.puppycrawl.tools.checkstyle.checks.whitespace.ArrayBracketNoWhitespaceCheck.MSG_WS_PRECEDED;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  
32  public class ArrayBracketNoWhitespaceCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      public String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/whitespace/arraybracketnowhitespace";
37      }
38  
39      @Test
40      public void testCreation() throws Exception {
41          final String[] expected = {
42              "8:25: " + getCheckMessage(MSG_WS_PRECEDED, "["),
43              "11:30: " + getCheckMessage(MSG_WS_PRECEDED, "["),
44              "12:29: " + getCheckMessage(MSG_WS_FOLLOWED, "["),
45              "13:33: " + getCheckMessage(MSG_WS_PRECEDED, "]"),
46              "14:13: " + getCheckMessage(MSG_WS_PRECEDED, "["),
47              "14:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
48              "18:29: " + getCheckMessage(MSG_WS_FOLLOWED, "["),
49              "18:34: " + getCheckMessage(MSG_WS_PRECEDED, "]"),
50              "26:22: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
51              "27:21: " + getCheckMessage(MSG_WS_PRECEDED, "["),
52              "28:20: " + getCheckMessage(MSG_WS_FOLLOWED, "["),
53              "29:23: " + getCheckMessage(MSG_WS_PRECEDED, "]"),
54              "30:20: " + getCheckMessage(MSG_WS_FOLLOWED, "["),
55              "30:24: " + getCheckMessage(MSG_WS_PRECEDED, "]"),
56              "38:13: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
57              "38:15: " + getCheckMessage(MSG_WS_PRECEDED, "["),
58              "42:20: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
59              "42:22: " + getCheckMessage(MSG_WS_PRECEDED, "["),
60              "47:24: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
61              "47:26: " + getCheckMessage(MSG_WS_PRECEDED, "]"),
62          };
63          verifyWithInlineConfigParser(
64              getPath("InputArrayBracketNoWhitespaceCreation.java"), expected);
65      }
66  
67      @Test
68      public void testMultiline() throws Exception {
69          final String[] expected = {
70              "23:2: " + getCheckMessage(MSG_WS_PRECEDED, "["),
71          };
72          verifyWithInlineConfigParser(
73              getPath("InputArrayBracketNoWhitespaceMultiline.java"), expected);
74      }
75  
76      @Test
77      public void testMemberAndOperators() throws Exception {
78          final String[] expected = {
79              "16:34: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
80              "22:22: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
81              "23:14: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
82              "25:14: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
83              "27:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
84              "29:22: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
85              "31:22: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
86              "36:25: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
87              "37:37: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
88              "42:32: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
89              "44:32: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
90              "46:28: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
91              "48:28: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
92              "50:32: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
93          };
94          verifyWithInlineConfigParser(
95              getPath("InputArrayBracketNoWhitespaceMemberAndOperators.java"), expected);
96      }
97  
98      @Test
99      public void testMisc() throws Exception {
100         final String[] expected = {
101             "10:22: " + getCheckMessage(MSG_WS_PRECEDED, "["),
102             "11:30: " + getCheckMessage(MSG_WS_PRECEDED, "["),
103             "15:30: " + getCheckMessage(MSG_WS_PRECEDED, "["),
104         };
105         verifyWithInlineConfigParser(
106             getPath("InputArrayBracketNoWhitespaceMisc.java"), expected);
107     }
108 
109     @Test
110     public void testDeclarations() throws Exception {
111         final String[] expected = {
112             "10:9: " + getCheckMessage(MSG_WS_PRECEDED, "["),
113             "11:9: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
114             "14:9: " + getCheckMessage(MSG_WS_PRECEDED, "["),
115             "15:9: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
116             "15:11: " + getCheckMessage(MSG_WS_PRECEDED, "["),
117             "22:22: " + getCheckMessage(MSG_WS_PRECEDED, "["),
118             "25:9: " + getCheckMessage(MSG_WS_PRECEDED, "["),
119             "28:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
120             "29:24: " + getCheckMessage(MSG_WS_PRECEDED, "["),
121             "31:51: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
122             "36:13: " + getCheckMessage(MSG_WS_PRECEDED, "["),
123             "42:30: " + getCheckMessage(MSG_WS_PRECEDED, "["),
124             "43:29: " + getCheckMessage(MSG_WS_FOLLOWED, "["),
125             "43:31: " + getCheckMessage(MSG_WS_PRECEDED, "]"),
126             "52:13: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
127             "53:27: " + getCheckMessage(MSG_WS_PRECEDED, "["),
128             "58:41: " + getCheckMessage(MSG_WS_PRECEDED, "["),
129             "63:29: " + getCheckMessage(MSG_WS_PRECEDED, "["),
130             "68:42: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
131             "76:13: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "["),
132             "80:14: " + getCheckMessage(MSG_WS_FOLLOWED, "["),
133             "80:16: " + getCheckMessage(MSG_WS_PRECEDED, "]"),
134         };
135         verifyWithInlineConfigParser(
136             getPath("InputArrayBracketNoWhitespaceDeclarations.java"), expected);
137     }
138 
139     @Test
140     public void testExpressions() throws Exception {
141         final String[] expected = {
142             "14:29: " + getCheckMessage(MSG_WS_FOLLOWED, "["),
143             "14:45: " + getCheckMessage(MSG_WS_PRECEDED, "]"),
144             "73:22: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
145         };
146         verifyWithInlineConfigParser(
147             getPath("InputArrayBracketNoWhitespaceExpressions.java"), expected);
148     }
149 
150     @Test
151     public void testNextToken() throws Exception {
152         final String[] expected = {
153             "9:22: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
154             "10:22: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
155             "11:22: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
156             "16:46: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "]"),
157             "26:19: " + getCheckMessage(MSG_WS_PRECEDED, "]"),
158             "28:21: " + getCheckMessage(MSG_WS_PRECEDED, "["),
159             "29:27: " + getCheckMessage(MSG_WS_PRECEDED, "["),
160             "29:28: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
161             "34:34: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
162             "77:25: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
163             "86:31: " + getCheckMessage(MSG_WS_FOLLOWED, "]"),
164         };
165         verifyWithInlineConfigParser(
166             getPath("InputArrayBracketNoWhitespaceNextToken.java"), expected);
167     }
168 
169     @Test
170     public void testGetAcceptableTokens() {
171 
172         final ArrayBracketNoWhitespaceCheck check = new ArrayBracketNoWhitespaceCheck();
173         assertWithMessage("Acceptable tokens and required tokens must match")
174             .that(check.getRequiredTokens())
175             .isEqualTo(check.getAcceptableTokens());
176     }
177 
178     @Test
179     public void testGetDefaultTokens() {
180         final ArrayBracketNoWhitespaceCheck check = new ArrayBracketNoWhitespaceCheck();
181         assertWithMessage("Default tokens and required tokens must match")
182             .that(check.getRequiredTokens())
183             .isEqualTo(check.getDefaultTokens());
184     }
185 
186 }