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.blocks;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck.MSG_KEY_LINE_BREAK_AFTER;
23  import static com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck.MSG_KEY_LINE_NEW;
24  import static com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck.MSG_KEY_LINE_PREVIOUS;
25  import static org.junit.Assert.assertArrayEquals;
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertTrue;
28  import static org.junit.Assert.fail;
29  
30  import org.junit.Test;
31  
32  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
33  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
34  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
35  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
36  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
37  
38  public class LeftCurlyCheckTest extends AbstractModuleTestSupport {
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/blocks/leftcurly";
42      }
43  
44      /* Additional test for jacoco, since valueOf()
45       * is generated by javac and jacoco reports that
46       * valueOf() is uncovered.
47       */
48      @Test
49      public void testLeftCurlyOptionValueOf() {
50          final LeftCurlyOption option = LeftCurlyOption.valueOf("NL");
51          assertEquals("Invalid valueOf result", LeftCurlyOption.NL, option);
52      }
53  
54      @Test
55      public void testGetRequiredTokens() {
56          final LeftCurlyCheck checkObj = new LeftCurlyCheck();
57          assertArrayEquals("LeftCurlyCheck#getRequiredTockens should return empty array by default",
58              CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
59  
60      }
61  
62      @Test
63      public void testDefault() throws Exception {
64          final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
65          final String[] expected = {
66              "8:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
67              "10:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
68              "14:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
69              "18:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
70              "22:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
71          };
72          verify(checkConfig, getPath("InputLeftCurlyDefault.java"), expected);
73      }
74  
75      @Test
76      public void testNl() throws Exception {
77          final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
78          checkConfig.addAttribute("option", LeftCurlyOption.NL.toString());
79          final String[] expected = {
80              "27:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
81              "31:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
82              "36:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
83              "40:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
84              "45:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
85              "50:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
86          };
87          verify(checkConfig, getPath("InputLeftCurlyDefault.java"), expected);
88      }
89  
90      @Test
91      public void testNlow() throws Exception {
92          final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
93          checkConfig.addAttribute("option", LeftCurlyOption.NLOW.toString());
94          final String[] expected = {
95              "8:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
96              "10:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
97              "14:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
98              "18:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
99              "22:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
100             "27:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
101             "31:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
102             "36:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
103             "40:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
104             "45:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
105             "50:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
106         };
107         verify(checkConfig, getPath("InputLeftCurlyDefault.java"), expected);
108     }
109 
110     @Test
111     public void testDefault2() throws Exception {
112         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
113         final String[] expected = {
114             "12:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
115             "17:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
116             "24:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
117             "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
118             "31:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
119             "39:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
120             "41:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
121             "46:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
122             "49:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
123             "53:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
124             "65:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
125             "69:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
126             "77:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
127             "80:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
128             "84:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
129         };
130         verify(checkConfig, getPath("InputLeftCurlyMethod.java"), expected);
131     }
132 
133     @Test
134     public void testNewline2() throws Exception {
135         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
136         checkConfig.addAttribute("option", LeftCurlyOption.NL.toString());
137         final String[] expected = {
138             "14:39: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 39),
139             "21:20: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 20),
140             "34:31: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 31),
141             "43:24: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 24),
142             "56:35: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 35),
143             "60:24: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 24),
144             "74:20: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 20),
145             "87:31: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 31),
146         };
147         verify(checkConfig, getPath("InputLeftCurlyMethod.java"), expected);
148     }
149 
150     @Test
151     public void testDefault3() throws Exception {
152         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
153         final String[] expected = {
154             "12:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
155             "15:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
156             "19:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
157             "21:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
158             "23:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
159             "30:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
160             "34:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
161             "42:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
162             "46:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
163             "52:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
164             "54:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
165             "63:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
166             "76:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
167             "83:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
168             "89:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
169             "97:19: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 19),
170             "106:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
171             "109:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
172             "118:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
173             "120:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
174             "129:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
175             "131:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
176             "133:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
177             "148:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
178             "157:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
179             "164:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
180         };
181         verify(checkConfig, getPath("InputLeftCurlyDefault3.java"), expected);
182     }
183 
184     @Test
185     public void testNewline3() throws Exception {
186         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
187         checkConfig.addAttribute("option", LeftCurlyOption.NL.toString());
188         final String[] expected = {
189             "26:33: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 33),
190             "91:19: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 19),
191             "97:19: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 19),
192             "142:37: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 37),
193             "158:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
194             "165:16: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 16),
195         };
196         verify(checkConfig, getPath("InputLeftCurlyDefault3.java"), expected);
197     }
198 
199     @Test
200     public void testMissingBraces() throws Exception {
201         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
202         final String[] expected = {
203             "12:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
204             "15:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
205             "21:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
206             "34:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
207             "51:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
208             "69:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
209             "105:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
210         };
211         verify(checkConfig, getPath("InputLeftCurlyMissingBraces.java"), expected);
212     }
213 
214     @Test
215     public void testDefaultWithAnnotations() throws Exception {
216         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
217         final String[] expected = {
218             "10:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
219             "14:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
220             "21:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
221             "27:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
222             "50:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
223             "58:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
224         };
225         verify(checkConfig, getPath("InputLeftCurlyAnnotations.java"), expected);
226     }
227 
228     @Test
229     public void testNlWithAnnotations() throws Exception {
230         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
231         checkConfig.addAttribute("option", LeftCurlyOption.NL.toString());
232         final String[] expected = {
233             "35:34: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 34),
234             "38:41: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 41),
235             "44:27: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 27),
236             "66:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
237         };
238         verify(checkConfig, getPath("InputLeftCurlyAnnotations.java"), expected);
239     }
240 
241     @Test
242     public void testLineBreakAfter() throws Exception {
243         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
244         checkConfig.addAttribute("option", LeftCurlyOption.EOL.toString());
245         final String[] expected = {
246             "9:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
247             "12:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
248             "16:9: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 9),
249             "18:13: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 13),
250             "20:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
251             "26:22: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 22),
252             "28:17: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 17),
253             "35:33: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 33),
254             "36:21: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 21),
255             "39:29: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 29),
256             "39:34: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 34),
257             "45:37: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 37),
258             "51:12: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 12),
259             "54:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
260             "56:19: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 19),
261             "66:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
262         };
263         verify(checkConfig, getPath("InputLeftCurlyLineBreakAfter.java"), expected);
264     }
265 
266     @Test
267     public void testIgnoreEnumsOptionTrue() throws Exception {
268         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
269         checkConfig.addAttribute("option", LeftCurlyOption.EOL.toString());
270         checkConfig.addAttribute("ignoreEnums", "true");
271         final String[] expectedWhileTrue = CommonUtils.EMPTY_STRING_ARRAY;
272         verify(checkConfig, getPath("InputLeftCurlyIgnoreEnums.java"), expectedWhileTrue);
273     }
274 
275     @Test
276     public void testIgnoreEnumsOptionFalse() throws Exception {
277         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
278         checkConfig.addAttribute("option", LeftCurlyOption.EOL.toString());
279         checkConfig.addAttribute("ignoreEnums", "false");
280         final String[] expectedWhileFalse = {
281             "4:17: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 17),
282         };
283         verify(checkConfig, getPath("InputLeftCurlyIgnoreEnums.java"), expectedWhileFalse);
284     }
285 
286     @Test
287     public void testDefaultLambda() throws Exception {
288         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
289         final String[] expected = {
290             "5:1: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 1),
291             "12:32: " + getCheckMessage(MSG_KEY_LINE_BREAK_AFTER, "{", 32),
292             "15:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
293         };
294         verify(checkConfig, getPath("InputLeftCurlyNewLineOptionWithLambda.java"),
295                 expected);
296     }
297 
298     @Test
299     public void testNewLineOptionWithLambda() throws Exception {
300         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
301         checkConfig.addAttribute("option", LeftCurlyOption.NL.toString());
302         final String[] expected = {
303             "6:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
304             "12:32: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 32),
305         };
306         verify(checkConfig, getPath("InputLeftCurlyNewLineOptionWithLambda.java"),
307                 expected);
308     }
309 
310     @Test
311     public void testGetAcceptableTokens() {
312         final LeftCurlyCheck check = new LeftCurlyCheck();
313         final int[] actual = check.getAcceptableTokens();
314         final int[] expected = {
315             TokenTypes.INTERFACE_DEF,
316             TokenTypes.CLASS_DEF,
317             TokenTypes.ANNOTATION_DEF,
318             TokenTypes.ENUM_DEF,
319             TokenTypes.CTOR_DEF,
320             TokenTypes.METHOD_DEF,
321             TokenTypes.ENUM_CONSTANT_DEF,
322             TokenTypes.LITERAL_WHILE,
323             TokenTypes.LITERAL_TRY,
324             TokenTypes.LITERAL_CATCH,
325             TokenTypes.LITERAL_FINALLY,
326             TokenTypes.LITERAL_SYNCHRONIZED,
327             TokenTypes.LITERAL_SWITCH,
328             TokenTypes.LITERAL_DO,
329             TokenTypes.LITERAL_IF,
330             TokenTypes.LITERAL_ELSE,
331             TokenTypes.LITERAL_FOR,
332             TokenTypes.STATIC_INIT,
333             TokenTypes.OBJBLOCK,
334             TokenTypes.LAMBDA, };
335         assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
336     }
337 
338     @Test
339     public void testFirstLine() throws Exception {
340         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
341         checkConfig.addAttribute("option", LeftCurlyOption.EOL.toString());
342         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
343         verify(checkConfig, getPath("InputLeftCurlyFirstLine.java"), expected);
344     }
345 
346     @Test
347     public void testCoverageIncrease() throws Exception {
348         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
349         checkConfig.addAttribute("option", LeftCurlyOption.NLOW.toString());
350         final String[] expected = {
351             "12:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
352             "21:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
353             "30:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
354             "39:5: " + getCheckMessage(MSG_KEY_LINE_PREVIOUS, "{", 5),
355             "53:14: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 14),
356             "58:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
357             "62:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
358             "67:12: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 12),
359             "72:18: " + getCheckMessage(MSG_KEY_LINE_NEW, "{", 18),
360         };
361         verify(checkConfig, getPath("InputLeftCurlyCoverageIncrease.java"), expected);
362     }
363 
364     @Test
365     public void testInvalidOption() throws Exception {
366         final DefaultConfiguration checkConfig = createModuleConfig(LeftCurlyCheck.class);
367         checkConfig.addAttribute("option", "invalid_option");
368 
369         try {
370             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
371 
372             verify(checkConfig, getPath("InputLeftCurlyDefault.java"), expected);
373             fail("exception expected");
374         }
375         catch (CheckstyleException ex) {
376             final String messageStart =
377                 "cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
378                     + "Cannot set property 'option' to 'invalid_option' in module";
379 
380             assertTrue("Invalid exception message, should start with: " + messageStart,
381                 ex.getMessage().startsWith(messageStart));
382         }
383     }
384 }