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.blocks;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck.MSG_KEY_NEED_BRACES;
23  
24  import org.junit.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
27  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
28  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
29  
30  public class NeedBracesCheckTest extends AbstractModuleTestSupport {
31  
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/blocks/needbraces";
35      }
36  
37      @Test
38      public void testIt() throws Exception {
39          final DefaultConfiguration checkConfig =
40              createModuleConfig(NeedBracesCheck.class);
41          final String[] expected = {
42              "29: " + getCheckMessage(MSG_KEY_NEED_BRACES, "do"),
43              "41: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"),
44              "42: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"),
45              "44: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"),
46              "45: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
47              "58: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"),
48              "59: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"),
49              "61: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"),
50              "63: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
51              "82: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
52              "83: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
53              "85: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
54              "87: " + getCheckMessage(MSG_KEY_NEED_BRACES, "else"),
55              "89: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
56              "97: " + getCheckMessage(MSG_KEY_NEED_BRACES, "else"),
57              "99: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
58              "100: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
59          };
60          verify(checkConfig, getPath("InputNeedBraces.java"), expected);
61      }
62  
63      @Test
64      public void testSingleLineStatements() throws Exception {
65          final DefaultConfiguration checkConfig =
66              createModuleConfig(NeedBracesCheck.class);
67          checkConfig.addAttribute("allowSingleLineStatement", "true");
68          final String[] expected = {
69              "23: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
70              "29: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
71              "38: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
72              "46: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"),
73              "53: " + getCheckMessage(MSG_KEY_NEED_BRACES, "do"),
74              "56: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"),
75              "62: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"),
76              "91: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
77              "95: " + getCheckMessage(MSG_KEY_NEED_BRACES, "else"),
78              "107: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
79              "114: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"),
80          };
81          verify(checkConfig, getPath("InputNeedBracesSingleLineStatements.java"), expected);
82      }
83  
84      @Test
85      public void testSingleLineLambda() throws Exception {
86          final DefaultConfiguration checkConfig =
87              createModuleConfig(NeedBracesCheck.class);
88          checkConfig.addAttribute("tokens", "LAMBDA");
89          checkConfig.addAttribute("allowSingleLineStatement", "true");
90          final String[] expected = {
91              "7: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"),
92          };
93          verify(checkConfig, getPath("InputNeedBracesSingleLineLambda.java"), expected);
94      }
95  
96      @Test
97      public void testSingleLineCaseDefault() throws Exception {
98          final DefaultConfiguration checkConfig =
99              createModuleConfig(NeedBracesCheck.class);
100         checkConfig.addAttribute("tokens", "LITERAL_CASE, LITERAL_DEFAULT");
101         checkConfig.addAttribute("allowSingleLineStatement", "true");
102         final String[] expected = {
103             "72: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"),
104             "75: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"),
105         };
106         verify(checkConfig, getPath("InputNeedBracesSingleLineStatements.java"), expected);
107     }
108 
109     @Test
110     public void testSingleLineCaseDefault2() throws Exception {
111         final DefaultConfiguration checkConfig =
112                 createModuleConfig(NeedBracesCheck.class);
113         checkConfig.addAttribute("tokens", "LITERAL_CASE, LITERAL_DEFAULT");
114         checkConfig.addAttribute("allowSingleLineStatement", "true");
115         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
116         verify(checkConfig, getPath("InputNeedBracesEmptyDefault.java"), expected);
117     }
118 
119     @Test
120     public void testCycles() throws Exception {
121         final DefaultConfiguration checkConfig = createModuleConfig(NeedBracesCheck.class);
122         checkConfig.addAttribute("tokens", "LITERAL_WHILE, LITERAL_DO, LITERAL_FOR");
123         checkConfig.addAttribute("allowSingleLineStatement", "true");
124         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
125         verify(checkConfig, getPath("InputNeedBracesConditional.java"), expected);
126     }
127 
128     @Test
129     public void testConditions() throws Exception {
130         final DefaultConfiguration checkConfig = createModuleConfig(NeedBracesCheck.class);
131         checkConfig.addAttribute("tokens", "LITERAL_ELSE, LITERAL_CASE, LITERAL_DEFAULT");
132         checkConfig.addAttribute("allowSingleLineStatement", "true");
133         final String[] expected = {
134             "29: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"),
135             "35: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"),
136             "36: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"),
137             "38: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"),
138             "41: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"),
139             "44: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"),
140             "49: " + getCheckMessage(MSG_KEY_NEED_BRACES, "default"),
141             "56: " + getCheckMessage(MSG_KEY_NEED_BRACES, "default"),
142         };
143         verify(checkConfig, getPath("InputNeedBracesConditional.java"), expected);
144     }
145 
146     @Test
147     public void testAllowEmptyLoopBodyTrue() throws Exception {
148         final DefaultConfiguration checkConfig =
149             createModuleConfig(NeedBracesCheck.class);
150         checkConfig.addAttribute("allowEmptyLoopBody", "true");
151         final String[] expected = {
152             "97: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
153         };
154         verify(checkConfig, getPath("InputNeedBracesNoBodyLoops.java"), expected);
155     }
156 
157     @Test
158     public void testAllowEmptyLoopBodyFalse() throws Exception {
159         final DefaultConfiguration checkConfig =
160             createModuleConfig(NeedBracesCheck.class);
161         final String[] expected = {
162             "10: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"),
163             "14: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"),
164             "18: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"),
165             "19: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"),
166             "23: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"),
167             "28: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"),
168             "33: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"),
169             "39: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"),
170             "45: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"),
171             "50: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"),
172             "54: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"),
173             "60: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"),
174             "67: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"),
175             "89: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"),
176             "93: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"),
177             "97: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"),
178             "108: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"),
179         };
180         verify(checkConfig, getPath("InputNeedBracesNoBodyLoops.java"), expected);
181     }
182 
183     @Test
184     public void testEmptySingleLineDefaultStmt() throws Exception {
185         final DefaultConfiguration checkConfig = createModuleConfig(NeedBracesCheck.class);
186         checkConfig.addAttribute("tokens", "LITERAL_DEFAULT");
187         checkConfig.addAttribute("allowSingleLineStatement", "true");
188         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
189         verify(checkConfig, getPath("InputNeedBracesEmptyDefault.java"), expected);
190     }
191 
192 }