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.NoWhitespaceAfterCheck.MSG_KEY;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.fail;
25  
26  import org.junit.Test;
27  
28  import antlr.CommonHiddenStreamToken;
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.api.DetailAST;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  
34  public class NoWhitespaceAfterCheckTest
35      extends AbstractModuleTestSupport {
36      @Override
37      protected String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/whitespace/nowhitespaceafter";
39      }
40  
41      @Test
42      public void testDefault() throws Exception {
43          final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceAfterCheck.class);
44          checkConfig.addAttribute("allowLineBreaks", "false");
45          final String[] expected = {
46              "5:14: " + getCheckMessage(MSG_KEY, "."),
47              "6:12: " + getCheckMessage(MSG_KEY, "."),
48              "29:14: " + getCheckMessage(MSG_KEY, "-"),
49              "29:21: " + getCheckMessage(MSG_KEY, "+"),
50              "31:15: " + getCheckMessage(MSG_KEY, "++"),
51              "31:22: " + getCheckMessage(MSG_KEY, "--"),
52              "111:22: " + getCheckMessage(MSG_KEY, "!"),
53              "112:23: " + getCheckMessage(MSG_KEY, "~"),
54              "129:24: " + getCheckMessage(MSG_KEY, "."),
55              "132:11: " + getCheckMessage(MSG_KEY, "."),
56              "136:12: " + getCheckMessage(MSG_KEY, "."),
57              "264:2: " + getCheckMessage(MSG_KEY, "."),
58              "289:6: " + getCheckMessage(MSG_KEY, "@"),
59              "290:6: " + getCheckMessage(MSG_KEY, "@"),
60              "291:6: " + getCheckMessage(MSG_KEY, "@"),
61              "296:27: " + getCheckMessage(MSG_KEY, "int"),
62          };
63          verify(checkConfig, getPath("InputNoWhitespaceAfter.java"), expected);
64      }
65  
66      @Test
67      public void testDotAllowLineBreaks() throws Exception {
68          final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceAfterCheck.class);
69          checkConfig.addAttribute("tokens", "DOT");
70          final String[] expected = {
71              "5:14: " + getCheckMessage(MSG_KEY, "."),
72              "129:24: " + getCheckMessage(MSG_KEY, "."),
73              "136:12: " + getCheckMessage(MSG_KEY, "."),
74          };
75          verify(checkConfig, getPath("InputNoWhitespaceAfter.java"), expected);
76      }
77  
78      @Test
79      public void testTypecast() throws Exception {
80          final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceAfterCheck.class);
81          checkConfig.addAttribute("tokens", "TYPECAST");
82          final String[] expected = {
83              "87:28: " + getCheckMessage(MSG_KEY, ")"),
84              "89:23: " + getCheckMessage(MSG_KEY, ")"),
85              "241:22: " + getCheckMessage(MSG_KEY, ")"),
86          };
87          verify(checkConfig, getPath("InputNoWhitespaceAfter.java"), expected);
88      }
89  
90      @Test
91      public void testArrayDeclarations() throws Exception {
92          final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceAfterCheck.class);
93          checkConfig.addAttribute("tokens", "ARRAY_DECLARATOR");
94          checkConfig.addAttribute("tokens", "INDEX_OP");
95          final String[] expected = {
96              "6:11: " + getCheckMessage(MSG_KEY, "Object"),
97              "8:22: " + getCheckMessage(MSG_KEY, "someStuff3"),
98              "9:8: " + getCheckMessage(MSG_KEY, "int"),
99              "10:13: " + getCheckMessage(MSG_KEY, "s"),
100             "11:13: " + getCheckMessage(MSG_KEY, "d"),
101             "16:14: " + getCheckMessage(MSG_KEY, "get"),
102             "18:8: " + getCheckMessage(MSG_KEY, "int"),
103             "19:34: " + getCheckMessage(MSG_KEY, "get1"),
104             "28:8: " + getCheckMessage(MSG_KEY, "int"),
105             "29:12: " + getCheckMessage(MSG_KEY, "cba"),
106             "31:26: " + getCheckMessage(MSG_KEY, "String"),
107             "32:27: " + getCheckMessage(MSG_KEY, "String"),
108             "39:11: " + getCheckMessage(MSG_KEY, "ar"),
109             "39:24: " + getCheckMessage(MSG_KEY, "int"),
110             "40:16: " + getCheckMessage(MSG_KEY, "int"),
111             "43:64: " + getCheckMessage(MSG_KEY, "getLongMultiArray"),
112             "47:26: " + getCheckMessage(MSG_KEY, "}"),
113             "49:22: " + getCheckMessage(MSG_KEY, "int"),
114             "50:24: " + getCheckMessage(MSG_KEY, "]"),
115             "51:35: " + getCheckMessage(MSG_KEY, "}"),
116             "52:38: " + getCheckMessage(MSG_KEY, "]"),
117         };
118         verify(checkConfig, getPath("InputNoWhitespaceAfterArrayDeclarations.java"), expected);
119     }
120 
121     @Test
122     public void testArrayDeclarations2() throws Exception {
123         final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceAfterCheck.class);
124         checkConfig.addAttribute("tokens", "ARRAY_DECLARATOR");
125         checkConfig.addAttribute("tokens", "INDEX_OP");
126         final String[] expected = {
127             "12:30: " + getCheckMessage(MSG_KEY, "]"),
128             "17:40: " + getCheckMessage(MSG_KEY, "create"),
129             "18:27: " + getCheckMessage(MSG_KEY, "int"),
130             "29:23: " + getCheckMessage(MSG_KEY, "]"),
131             "30:27: " + getCheckMessage(MSG_KEY, "int"),
132             "30:38: " + getCheckMessage(MSG_KEY, "]"),
133             "30:51: " + getCheckMessage(MSG_KEY, "]"),
134             "35:44: " + getCheckMessage(MSG_KEY, "int"),
135             "35:56: " + getCheckMessage(MSG_KEY, "]"),
136             "36:18: " + getCheckMessage(MSG_KEY, "e"),
137             "36:23: " + getCheckMessage(MSG_KEY, "]"),
138             "36:43: " + getCheckMessage(MSG_KEY, "]"),
139             "37:14: " + getCheckMessage(MSG_KEY, "e"),
140             "37:18: " + getCheckMessage(MSG_KEY, "]"),
141             "42:30: " + getCheckMessage(MSG_KEY, "Integer"),
142             "43:20: " + getCheckMessage(MSG_KEY, "]"),
143             "48:28: " + getCheckMessage(MSG_KEY, ">"),
144             "48:31: " + getCheckMessage(MSG_KEY, "]"),
145             "48:34: " + getCheckMessage(MSG_KEY, "]"),
146             "52:34: " + getCheckMessage(MSG_KEY, "int"),
147             "54:14: " + getCheckMessage(MSG_KEY, "g"),
148             "55:17: " + getCheckMessage(MSG_KEY, "]"),
149             "56:14: " + getCheckMessage(MSG_KEY, "g"),
150             "56:18: " + getCheckMessage(MSG_KEY, "]"),
151             "56:22: " + getCheckMessage(MSG_KEY, "]"),
152             "62:50: " + getCheckMessage(MSG_KEY, "create"),
153             "62:57: " + getCheckMessage(MSG_KEY, "]"),
154             "67:32: " + getCheckMessage(MSG_KEY, "boolean"),
155             "69:46: " + getCheckMessage(MSG_KEY, "String"),
156             "69:50: " + getCheckMessage(MSG_KEY, "]"),
157             "70:36: " + getCheckMessage(MSG_KEY, "String"),
158             "81:40: " + getCheckMessage(MSG_KEY, "Integer"),
159             "85:14: " + getCheckMessage(MSG_KEY, "char"),
160             "86:52: " + getCheckMessage(MSG_KEY, "A"),
161             "87:86: " + getCheckMessage(MSG_KEY, "Object"),
162             "90:41: " + getCheckMessage(MSG_KEY, ")"),
163             "90:49: " + getCheckMessage(MSG_KEY, "]"),
164             "92:35: " + getCheckMessage(MSG_KEY, "Object"),
165             "94:45: " + getCheckMessage(MSG_KEY, ")"),
166             "97:41: " + getCheckMessage(MSG_KEY, "Object"),
167             "100:43: " + getCheckMessage(MSG_KEY, "]"),
168             "108:31: " + getCheckMessage(MSG_KEY, "Object"),
169         };
170         verify(checkConfig, getPath("InputNoWhitespaceAfterArrayDeclarations2.java"), expected);
171     }
172 
173     @Test
174     public void testArrayDeclarations3() throws Exception {
175         final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceAfterCheck.class);
176         checkConfig.addAttribute("tokens", "ARRAY_DECLARATOR");
177         checkConfig.addAttribute("tokens", "INDEX_OP");
178         verify(checkConfig, getPath("InputNoWhitespaceAfterArrayDeclarations3.java"));
179     }
180 
181     @Test
182     public void testSynchronized() throws Exception {
183         final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceAfterCheck.class);
184         checkConfig.addAttribute("tokens", "LITERAL_SYNCHRONIZED");
185         final String[] expected = {
186             "14:21: " + getCheckMessage(MSG_KEY, "synchronized"),
187         };
188         verify(checkConfig, getPath("InputNoWhitespaceAfterSynchronized.java"), expected);
189     }
190 
191     @Test
192     public void testNpe() throws Exception {
193         final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceAfterCheck.class);
194         verify(checkConfig, getPath("InputNoWhitespaceAfterFormerNpe.java"));
195     }
196 
197     @Test
198     public void testMethodReference() throws Exception {
199         final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceAfterCheck.class);
200         final String[] expected = {
201             "9:40: " + getCheckMessage(MSG_KEY, "int"),
202             "10:61: " + getCheckMessage(MSG_KEY, "String"),
203         };
204         verify(checkConfig, getPath("InputNoWhitespaceAfterMethodRef.java"), expected);
205     }
206 
207     @Test
208     public void testMethodReferenceAfter() throws Exception {
209         final DefaultConfiguration checkConfig = createModuleConfig(NoWhitespaceAfterCheck.class);
210         checkConfig.addAttribute("tokens", "METHOD_REF");
211         final String[] expected = {
212             "17:37: " + getCheckMessage(MSG_KEY, "::"),
213             "18:66: " + getCheckMessage(MSG_KEY, "::"),
214         };
215         verify(checkConfig, getPath("InputNoWhitespaceAfterBadMethodRef.java"), expected);
216     }
217 
218     @Test
219     public void testVisitTokenSwitchReflection() {
220         //unexpected parent for ARRAY_DECLARATOR token
221         final DetailAST astImport = mockAST(TokenTypes.IMPORT, "import", "mockfile");
222         final DetailAST astArrayDeclarator = mockAST(TokenTypes.ARRAY_DECLARATOR, "[", "mockfile");
223         final DetailAST astRightBracket = mockAST(TokenTypes.RBRACK, "[", "mockfile");
224         astImport.addChild(astArrayDeclarator);
225         astArrayDeclarator.addChild(astRightBracket);
226 
227         final NoWhitespaceAfterCheck check = new NoWhitespaceAfterCheck();
228         try {
229             check.visitToken(astArrayDeclarator);
230             fail("no intended exception thrown");
231         }
232         catch (IllegalStateException ex) {
233             assertEquals("Invalid exception message",
234                 "unexpected ast syntax import[0x-1]", ex.getMessage());
235         }
236     }
237 
238     /**
239      * Creates MOCK lexical token and returns AST node for this token.
240      * @param tokenType type of token
241      * @param tokenText text of token
242      * @param tokenFileName file name of token
243      * @return AST node for the token
244      */
245     private static DetailAST mockAST(final int tokenType, final String tokenText,
246             final String tokenFileName) {
247         final CommonHiddenStreamToken tokenImportSemi = new CommonHiddenStreamToken();
248         tokenImportSemi.setType(tokenType);
249         tokenImportSemi.setText(tokenText);
250         tokenImportSemi.setFilename(tokenFileName);
251         final DetailAST astSemi = new DetailAST();
252         astSemi.initialize(tokenImportSemi);
253         return astSemi;
254     }
255 }