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.puppycrawl.tools.checkstyle.checks.whitespace;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck.MSG_WS_NOT_FOLLOWED;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceAfterCheck.MSG_WS_TYPECAST;
24  import static org.junit.Assert.assertArrayEquals;
25  
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.utils.CommonUtils;
31  
32  public class WhitespaceAfterCheckTest
33      extends AbstractModuleTestSupport {
34  
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/whitespace/whitespaceafter";
38      }
39  
40      @Test
41      public void testGetRequiredTokens() {
42          final WhitespaceAfterCheck checkObj = new WhitespaceAfterCheck();
43          assertArrayEquals(
44              "WhitespaceAfterCheck#getRequiredTokens should return empty array by default",
45              CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
46      }
47  
48      @Test
49      public void testDefault() throws Exception {
50          final DefaultConfiguration checkConfig = createModuleConfig(WhitespaceAfterCheck.class);
51          final String[] expected = {
52              "42:40: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ","),
53              "71:30: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ","),
54          };
55          verify(checkConfig, getPath("InputWhitespaceAfterDefaultConfig.java"),
56                  expected);
57      }
58  
59      @Test
60      public void testCast() throws Exception {
61          final DefaultConfiguration configurationTestCast =
62                  createModuleConfig(WhitespaceAfterCheck.class);
63          configurationTestCast.addAttribute("tokens", "TYPECAST");
64          final String[] expected = {
65              "88:21: " + getCheckMessage(MSG_WS_TYPECAST),
66          };
67          verify(configurationTestCast, getPath("InputWhitespaceAfterTypeCast.java"),
68                  expected);
69      }
70  
71      @Test
72      public void testMultilineCast() throws Exception {
73          final DefaultConfiguration configurationTestCast =
74                  createModuleConfig(WhitespaceAfterCheck.class);
75          configurationTestCast.addAttribute("tokens", "TYPECAST");
76          final String[] expected = {
77              "7:24: " + getCheckMessage(MSG_WS_TYPECAST),
78          };
79          verify(configurationTestCast, getPath("InputWhitespaceAfterMultilineCast.java"),
80                  expected);
81      }
82  
83      @Test
84      public void testSemi() throws Exception {
85          final DefaultConfiguration configurationTestSemi =
86                  createModuleConfig(WhitespaceAfterCheck.class);
87          configurationTestSemi.addAttribute("tokens", "SEMI");
88          final String[] expected = {
89              "54:23: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
90              "54:29: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
91              "103:19: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
92          };
93          verify(configurationTestSemi, getPath("InputWhitespaceAfterBraces.java"),
94                  expected);
95      }
96  
97      @Test
98      public void testLiteralWhile() throws Exception {
99          final DefaultConfiguration configurationTestLiteralWhile =
100                 createModuleConfig(WhitespaceAfterCheck.class);
101         configurationTestLiteralWhile.addAttribute("tokens", "LITERAL_WHILE");
102         final String[] expected = {
103             "39:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "while"),
104         };
105         verify(configurationTestLiteralWhile, getPath("InputWhitespaceAfterLiteralWhile.java"),
106                 expected);
107     }
108 
109     @Test
110     public void testLiteralIf() throws Exception {
111         final DefaultConfiguration configurationTestLiteralIf =
112                 createModuleConfig(WhitespaceAfterCheck.class);
113         configurationTestLiteralIf.addAttribute("tokens", "LITERAL_IF");
114         final String[] expected = {
115             "18:11: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "if"),
116         };
117         verify(configurationTestLiteralIf, getPath("InputWhitespaceAfterLiteralIf.java"),
118                 expected);
119     }
120 
121     @Test
122     public void testLiteralElse() throws Exception {
123         final DefaultConfiguration configurationTestLiteralElse =
124                 createModuleConfig(WhitespaceAfterCheck.class);
125         configurationTestLiteralElse.addAttribute("tokens", "LITERAL_ELSE");
126         final String[] expected = {
127             "27:15: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "else"),
128         };
129         verify(configurationTestLiteralElse, getPath("InputWhitespaceAfterLiteralElse.java"),
130                 expected);
131     }
132 
133     @Test
134     public void testLiteralFor() throws Exception {
135         final DefaultConfiguration configurationTestLiteralFor =
136                 createModuleConfig(WhitespaceAfterCheck.class);
137         configurationTestLiteralFor.addAttribute("tokens", "LITERAL_FOR");
138         final String[] expected = {
139             "51:12: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "for"),
140         };
141         verify(configurationTestLiteralFor, getPath("InputWhitespaceAfterLiteralFor.java"),
142                 expected);
143     }
144 
145     @Test
146     public void testLiteralDo() throws Exception {
147         final DefaultConfiguration configurationTestLiteralDo =
148                 createModuleConfig(WhitespaceAfterCheck.class);
149         configurationTestLiteralDo.addAttribute("tokens", "LITERAL_DO");
150         final String[] expected = {
151             "63:11: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "do"),
152         };
153         verify(configurationTestLiteralDo, getPath("InputWhitespaceAfterLiteralDo.java"),
154                 expected);
155     }
156 
157     @Test
158     public void testDoWhile() throws Exception {
159         final DefaultConfiguration configurationTestDoWhile =
160                 createModuleConfig(WhitespaceAfterCheck.class);
161         configurationTestDoWhile.addAttribute("tokens", "DO_WHILE");
162         final String[] expected = {
163             "18:16: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "while"),
164         };
165         verify(configurationTestDoWhile, getPath("InputWhitespaceAfterDoWhile.java"),
166                 expected);
167     }
168 
169     @Test
170     public void testEmptyForIterator() throws Exception {
171         final DefaultConfiguration checkConfig = createModuleConfig(WhitespaceAfterCheck.class);
172         final String[] expected = {
173             "14:31: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
174             "17:31: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ";"),
175         };
176         verify(checkConfig, getPath("InputWhitespaceAfterFor.java"),
177                 expected);
178     }
179 
180     @Test
181     public void testTypeArgumentAndParameterCommas() throws Exception {
182         final DefaultConfiguration checkConfig = createModuleConfig(WhitespaceAfterCheck.class);
183         final String[] expected = {
184             "11:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ","),
185             "11:23: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ","),
186             "11:41: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, ","),
187         };
188         verify(checkConfig, getPath("InputWhitespaceAfterGenerics.java"),
189                 expected);
190     }
191 
192     @Test
193     public void test1322879() throws Exception {
194         final DefaultConfiguration checkConfig = createModuleConfig(WhitespaceAfterCheck.class);
195         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
196         verify(checkConfig, getPath("InputWhitespaceAfterAround.java"),
197                expected);
198     }
199 
200 }