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.AbstractParenPadCheck.MSG_WS_FOLLOWED;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.AbstractParenPadCheck.MSG_WS_NOT_FOLLOWED;
24  import static com.puppycrawl.tools.checkstyle.checks.whitespace.AbstractParenPadCheck.MSG_WS_NOT_PRECEDED;
25  import static com.puppycrawl.tools.checkstyle.checks.whitespace.AbstractParenPadCheck.MSG_WS_PRECEDED;
26  import static org.junit.Assert.assertTrue;
27  import static org.junit.Assert.fail;
28  
29  import java.lang.reflect.Method;
30  
31  import org.junit.Test;
32  import org.powermock.reflect.Whitebox;
33  
34  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
36  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
37  import com.puppycrawl.tools.checkstyle.api.DetailAST;
38  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
39  import com.puppycrawl.tools.checkstyle.utils.TokenUtils;
40  
41  public class ParenPadCheckTest
42      extends AbstractModuleTestSupport {
43  
44      @Override
45      protected String getPackageLocation() {
46          return "com/puppycrawl/tools/checkstyle/checks/whitespace/parenpad";
47      }
48  
49      @Test
50      public void testDefault()
51              throws Exception {
52          final DefaultConfiguration checkConfig =
53              createModuleConfig(ParenPadCheck.class);
54          final String[] expected = {
55              "58:12: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
56              "58:36: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
57              "74:13: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
58              "74:18: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
59              "232:27: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
60              "241:24: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
61              "241:30: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
62              "277:18: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
63              "277:23: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
64          };
65          verify(checkConfig, getPath("InputParenPadWhitespace.java"), expected);
66      }
67  
68      @Test
69      public void testSpace()
70              throws Exception {
71          final DefaultConfiguration checkConfig =
72              createModuleConfig(ParenPadCheck.class);
73          checkConfig.addAttribute("option", PadOption.SPACE.toString());
74          final String[] expected = {
75              "29:20: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
76              "29:23: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
77              "37:22: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
78              "37:26: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
79              "41:15: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
80              "41:33: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
81              "76:20: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
82              "76:21: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
83              "97:22: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
84              "97:28: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
85              "98:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
86              "98:18: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
87              "150:28: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
88              "150:32: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
89              "153:16: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
90              "153:20: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
91              "160:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
92              "160:34: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
93              "162:20: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
94              "165:10: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
95              "178:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
96              "178:36: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
97              "225:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
98              "235:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
99              "235:39: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
100             "252:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
101             "252:93: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
102             "273:26: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
103             "273:36: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
104             "275:29: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
105             "275:42: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
106             "276:18: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
107             "276:33: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
108             "287:55: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
109             "287:70: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
110         };
111         verify(checkConfig, getPath("InputParenPadWhitespace.java"), expected);
112     }
113 
114     @Test
115     public void testDefaultForIterator()
116             throws Exception {
117         final DefaultConfiguration checkConfig =
118             createModuleConfig(ParenPadCheck.class);
119         final String[] expected = {
120             "17:34: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
121             "20:35: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
122             "40:14: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
123             "40:36: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
124             "43:14: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
125             "48:27: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
126             "51:26: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
127         };
128         verify(checkConfig, getPath("InputParenPadForWhitespace.java"), expected);
129     }
130 
131     @Test
132     public void testSpaceEmptyForIterator()
133             throws Exception {
134         final DefaultConfiguration checkConfig =
135             createModuleConfig(ParenPadCheck.class);
136         checkConfig.addAttribute("option", PadOption.SPACE.toString());
137         final String[] expected = {
138             "11:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
139             "11:35: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
140             "14:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
141             "14:34: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
142             "17:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
143             "20:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
144             "23:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
145             "27:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
146             "32:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
147         };
148         verify(checkConfig, getPath("InputParenPadForWhitespace.java"), expected);
149     }
150 
151     @Test
152     public void test1322879() throws Exception {
153         final DefaultConfiguration checkConfig =
154             createModuleConfig(ParenPadCheck.class);
155         checkConfig.addAttribute("option", PadOption.SPACE.toString());
156         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
157         verify(checkConfig, getPath("InputParenPadWithSpace.java"),
158                expected);
159     }
160 
161     @Test
162     public void testNospaceWithComplexInput() throws Exception {
163         final DefaultConfiguration checkConfig =
164             createModuleConfig(ParenPadCheck.class);
165         checkConfig.addAttribute("option", PadOption.NOSPACE.toString());
166         final String[] expected = {
167             "44:27: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
168             "44:27: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
169             "45:18: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
170             "48:27: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
171             "49:19: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
172             "49:19: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
173             "52:27: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
174             "53:21: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
175             "54:18: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
176             "54:52: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
177             "54:52: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
178             "57:26: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
179             "58:22: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
180             "59:24: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
181             "60:26: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
182             "60:51: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
183             "60:57: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
184             "61:29: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
185             "62:43: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
186             "63:41: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
187             "65:43: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
188             "78:28: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
189             "78:28: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
190             "79:19: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
191             "82:33: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
192             "83:19: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
193             "83:19: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
194             "86:29: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
195             "87:35: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
196             "88:51: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
197             "88:51: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
198             "88:53: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
199             "90:38: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
200             "91:32: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
201             "92:35: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
202             "93:30: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
203             "94:60: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
204             "94:62: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
205             "94:69: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
206             "95:34: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
207             "96:47: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
208             "97:42: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
209             "99:44: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
210             "112:17: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
211             "113:23: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
212             "113:25: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
213             "113:31: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
214             "114:26: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
215             "114:28: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
216             "114:34: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
217             "114:50: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
218             "115:26: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
219             "115:28: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
220             "115:35: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
221             "115:53: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
222             "115:55: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
223             "119:17: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
224             "119:22: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
225             "123:30: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
226             "123:44: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
227             "126:22: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
228             "126:22: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
229             "130:19: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
230             "130:19: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
231             "139:10: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
232             "139:20: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
233             "145:33: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
234             "145:46: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
235             "153:34: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
236             "154:48: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
237             "155:36: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
238             "155:46: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
239             "159:26: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
240             "159:35: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
241             "160:13: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
242             "160:29: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
243             "160:48: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
244             "160:50: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
245             "163:32: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
246             "163:35: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
247             "163:48: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
248             "163:60: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
249             "166:39: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
250             "167:25: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
251             "167:50: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
252             "173:38: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
253             "174:48: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
254             "175:21: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
255             "175:48: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
256             "185:17: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
257             "185:35: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
258             "186:20: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
259             "186:38: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
260             "190:30: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
261             "190:44: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
262             "191:13: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
263             "191:38: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
264             "192:23: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
265             "192:39: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
266             "200:81: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
267             "200:83: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
268             "201:21: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
269             "202:23: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
270             "203:21: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
271             "203:24: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
272             "206:14: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
273             "206:22: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
274             "206:32: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
275             "207:18: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
276             "207:46: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
277             "210:37: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
278             "210:74: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
279             "210:80: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
280             "210:82: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
281             "211:37: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
282             "212:49: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
283             "212:51: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
284             "212:53: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
285         };
286         verify(checkConfig, getPath("InputParenPadLeftRightAndNoSpace.java"), expected);
287     }
288 
289     @Test
290     public void testConfigureTokens() throws Exception {
291         final DefaultConfiguration checkConfig =
292                 createModuleConfig(ParenPadCheck.class);
293         checkConfig.addAttribute("tokens", "METHOD_CALL");
294         final String[] expected = {
295             "90:38: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
296             "113:23: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
297             "115:53: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
298             "145:33: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
299             "145:46: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
300             "153:34: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
301             "154:48: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
302             "155:36: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
303             "155:46: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
304             "163:32: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
305             "163:35: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
306             "163:48: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
307             "163:60: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
308             "201:21: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
309             "202:23: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
310             "203:21: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
311             "203:24: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
312             "206:32: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
313             "210:37: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
314             "210:74: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
315             "210:80: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
316             "210:82: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
317             "211:37: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
318             "212:49: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
319             "212:51: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
320             "212:53: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
321         };
322         verify(checkConfig, getPath("InputParenPadLeftRightAndNoSpace.java"), expected);
323     }
324 
325     @Test
326     public void testInvalidOption() throws Exception {
327         final DefaultConfiguration checkConfig = createModuleConfig(ParenPadCheck.class);
328         checkConfig.addAttribute("option", "invalid_option");
329 
330         try {
331             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
332 
333             verify(checkConfig, getPath("InputParenPadLeftRightAndNoSpace.java"), expected);
334             fail("exception expected");
335         }
336         catch (CheckstyleException ex) {
337             final String messageStart = "cannot initialize module "
338                 + "com.puppycrawl.tools.checkstyle.TreeWalker - Cannot set property 'option' to "
339                 + "'invalid_option' in module";
340             assertTrue("Invalid exception message, should start with: " + messageStart,
341                 ex.getMessage().startsWith(messageStart));
342         }
343     }
344 
345     @Test
346     public void testLambdaAssignment() throws Exception {
347         final DefaultConfiguration checkConfig = createModuleConfig(ParenPadCheck.class);
348         final String[] expected = {
349             "9:42: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
350             "9:44: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
351             "11:43: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
352             "13:42: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
353             "15:47: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
354             "15:49: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
355             "17:47: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
356             "17:56: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
357             "19:62: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
358             "19:62: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
359             "22:21: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
360             "22:34: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
361         };
362         verify(checkConfig, getPath("InputParenPadLambda.java"), expected);
363     }
364 
365     @Test
366     public void testLambdaAssignmentWithSpace() throws Exception {
367         final DefaultConfiguration checkConfig = createModuleConfig(ParenPadCheck.class);
368         checkConfig.addAttribute("option", PadOption.SPACE.toString());
369         final String[] expected = {
370             "9:42: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
371             "9:43: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
372             "11:42: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
373             "13:44: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
374             "15:48: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
375             "15:49: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
376             "17:48: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
377             "17:56: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
378             "22:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
379             "22:33: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
380         };
381         verify(checkConfig, getPath("InputParenPadLambdaWithSpace.java"), expected);
382     }
383 
384     @Test
385     public void testLambdaCheckDisabled() throws Exception {
386         final DefaultConfiguration checkConfig = createModuleConfig(ParenPadCheck.class);
387         checkConfig.addAttribute("tokens", "EXPR, METHOD_CALL, METHOD_DEF");
388         final String[] expected = {
389             "19:62: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
390             "19:62: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
391             "22:21: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
392             "22:34: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
393         };
394         verify(checkConfig, getPath("InputParenPadWithDisabledLambda.java"), expected);
395     }
396 
397     @Test
398     public void testLambdaCheckDisabledWithSpace() throws Exception {
399         final DefaultConfiguration checkConfig = createModuleConfig(ParenPadCheck.class);
400         checkConfig.addAttribute("option", PadOption.SPACE.toString());
401         checkConfig.addAttribute("tokens", "EXPR, METHOD_CALL, METHOD_DEF");
402         final String[] expected = {
403             "22:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
404             "22:33: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
405         };
406         verify(checkConfig, getPath("InputParenPadWithSpaceAndDisabledLambda.java"), expected);
407     }
408 
409     @Test
410     public void testLambdaCheckOnly() throws Exception {
411         final DefaultConfiguration checkConfig = createModuleConfig(ParenPadCheck.class);
412         checkConfig.addAttribute("tokens", "LAMBDA");
413         final String[] expected = {
414             "9:42: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
415             "9:44: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
416             "11:43: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
417             "13:42: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
418             "15:47: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
419             "15:49: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
420             "17:47: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
421             "17:56: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
422         };
423         verify(checkConfig, getPath("InputParenPadLambdaOnly.java"), expected);
424     }
425 
426     @Test
427     public void testLambdaCheckOnlyWithSpace() throws Exception {
428         final DefaultConfiguration checkConfig = createModuleConfig(ParenPadCheck.class);
429         checkConfig.addAttribute("option", PadOption.SPACE.toString());
430         checkConfig.addAttribute("tokens", "LAMBDA");
431         final String[] expected = {
432             "9:42: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
433             "9:43: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
434             "11:42: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
435             "13:44: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
436             "15:48: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
437             "15:49: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
438             "17:48: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
439             "17:56: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
440         };
441         verify(checkConfig, getPath("InputParenPadLambdaOnlyWithSpace.java"), expected);
442     }
443 
444     @Test
445     public void testLambdaCheckOnlyWithSpace1() throws Exception {
446         final DefaultConfiguration checkConfig = createModuleConfig(ParenPadCheck.class);
447         checkConfig.addAttribute("option", PadOption.SPACE.toString());
448         final String[] expected = {
449             "5:2: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
450         };
451         verify(checkConfig, getPath("InputParenPadStartOfTheLine.java"), expected);
452     }
453 
454     @Test
455     public void testTryWithResources() throws Exception {
456         final DefaultConfiguration checkConfig = createModuleConfig(ParenPadCheck.class);
457         final String[] expected = {
458             "9:36: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
459             "10:60: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
460         };
461         verify(checkConfig, getPath("InputParenPadTryWithResources.java"), expected);
462     }
463 
464     /**
465      * Pitest requires us to specify more concrete lower bound for condition for
466      * ParenPadCheck#isAcceptableToken as nodes of first several types like CTOR_DEF,
467      * METHOD_DEF will never reach this method. It is hard to recreate conditions for
468      * all tokens to go through this method. We do not want to change main code to have
469      * this set ok tokens more exact, because it will not be ease to understand.
470      * So we have to use reflection to be sure all
471      * acceptable tokens pass that check.
472      */
473     @Test
474     public void testIsAcceptableToken() throws Exception {
475         final ParenPadCheck check = new ParenPadCheck();
476         final Method method = Whitebox.getMethod(ParenPadCheck.class,
477             "isAcceptableToken", DetailAST.class);
478         final DetailAST ast = new DetailAST();
479         final String message = "Expected that all acceptable tokens will pass isAcceptableToken "
480             + "method, but some token don't: ";
481 
482         for (int token : check.getAcceptableTokens()) {
483             ast.setType(token);
484             assertTrue(message + TokenUtils.getTokenName(token),
485                     (boolean) method.invoke(check, ast));
486         }
487     }
488 
489 }