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