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