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.EmptyBlockCheck.MSG_KEY_BLOCK_EMPTY;
23  import static com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck.MSG_KEY_BLOCK_NO_STATEMENT;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  import org.junit.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
34  
35  public class EmptyBlockCheckTest
36      extends AbstractModuleTestSupport {
37  
38      @Override
39      protected String getPackageLocation() {
40          return "com/puppycrawl/tools/checkstyle/checks/blocks/emptyblock";
41      }
42  
43      /* Additional test for jacoco, since valueOf()
44       * is generated by javac and jacoco reports that
45       * valueOf() is uncovered.
46       */
47      @Test
48      public void testBlockOptionValueOf() {
49          final BlockOption option = BlockOption.valueOf("TEXT");
50          assertEquals("Invalid valueOf result", BlockOption.TEXT, option);
51      }
52  
53      @Test
54      public void testDefault()
55              throws Exception {
56          final DefaultConfiguration checkConfig =
57              createModuleConfig(EmptyBlockCheck.class);
58          final String[] expected = {
59              "33:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
60              "35:17: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
61              "37:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
62              "40:17: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
63              "63:5: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
64              "71:29: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
65              "73:41: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
66              "84:12: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
67          };
68          verify(checkConfig, getPath("InputEmptyBlockSemantic.java"), expected);
69      }
70  
71      @Test
72      public void testText()
73              throws Exception {
74          final DefaultConfiguration checkConfig =
75              createModuleConfig(EmptyBlockCheck.class);
76          checkConfig.addAttribute("option", BlockOption.TEXT.toString());
77          final String[] expected = {
78              "33:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "try"),
79              "35:17: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "finally"),
80              "63:5: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "INSTANCE_INIT"),
81              "71:29: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "synchronized"),
82              "84:12: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "STATIC_INIT"),
83          };
84          verify(checkConfig, getPath("InputEmptyBlockSemantic.java"), expected);
85      }
86  
87      @Test
88      public void testStatement()
89              throws Exception {
90          final DefaultConfiguration checkConfig =
91              createModuleConfig(EmptyBlockCheck.class);
92          checkConfig.addAttribute("option", BlockOption.STATEMENT.toString());
93          final String[] expected = {
94              "33:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
95              "35:17: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
96              "37:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
97              "40:17: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
98              "63:5: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
99              "71:29: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
100             "73:41: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
101             "84:12: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
102         };
103         verify(checkConfig, getPath("InputEmptyBlockSemantic.java"), expected);
104     }
105 
106     @Test
107     public void allowEmptyLoops() throws Exception {
108         final DefaultConfiguration checkConfig =
109                 createModuleConfig(EmptyBlockCheck.class);
110         checkConfig.addAttribute("option", BlockOption.STATEMENT.toString());
111         checkConfig.addAttribute("tokens", "LITERAL_TRY, LITERAL_CATCH,"
112                 + "LITERAL_FINALLY, LITERAL_DO, LITERAL_IF,"
113                 + "LITERAL_ELSE, INSTANCE_INIT, STATIC_INIT, LITERAL_SWITCH");
114         final String[] expected = {
115             "16:29: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
116             "19:42: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
117             "22:29: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
118             "23:28: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT),
119         };
120         verify(checkConfig, getPath("InputEmptyBlockSemantic2.java"), expected);
121     }
122 
123     @Test
124     public void allowEmptyLoopsText() throws Exception {
125         final DefaultConfiguration checkConfig =
126                 createModuleConfig(EmptyBlockCheck.class);
127         checkConfig.addAttribute("option", BlockOption.TEXT.toString());
128         checkConfig.addAttribute("tokens", "LITERAL_TRY, LITERAL_CATCH,"
129                 + "LITERAL_FINALLY, LITERAL_DO, LITERAL_IF,"
130                 + "LITERAL_ELSE, INSTANCE_INIT, STATIC_INIT, LITERAL_SWITCH");
131         final String[] expected = {
132             "16:29: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "if"),
133             "19:42: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "if"),
134             "22:29: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "if"),
135             "23:28: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "switch"),
136         };
137         verify(checkConfig, getPath("InputEmptyBlockSemantic2.java"), expected);
138     }
139 
140     @Test
141     public void testInvalidOption() throws Exception {
142         final DefaultConfiguration checkConfig = createModuleConfig(EmptyBlockCheck.class);
143         checkConfig.addAttribute("option", "invalid_option");
144 
145         try {
146             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
147 
148             verify(checkConfig, getPath("InputEmptyBlockSemantic.java"), expected);
149             fail("exception expected");
150         }
151         catch (CheckstyleException ex) {
152             final String messageStart =
153                 "cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
154                     + "Cannot set property 'option' to 'invalid_option' in module";
155 
156             assertTrue("Invalid exception message, should start with: " + messageStart,
157                 ex.getMessage().startsWith(messageStart));
158         }
159     }
160 
161     @Test
162     public void testAllowEmptyCaseWithText() throws Exception {
163         final DefaultConfiguration checkConfig = createModuleConfig(EmptyBlockCheck.class);
164         checkConfig.addAttribute("option", BlockOption.TEXT.toString());
165         checkConfig.addAttribute("tokens", "LITERAL_CASE");
166         final String[] expected = {
167             "12:28: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
168             "18:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
169             "29:29: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
170             "31:37: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
171             "32:29: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "case"),
172         };
173         verify(checkConfig, getPath("InputEmptyBlockCase.java"), expected);
174     }
175 
176     @Test
177     public void testForbidCaseWithoutStmt() throws Exception {
178         final DefaultConfiguration checkConfig = createModuleConfig(EmptyBlockCheck.class);
179         checkConfig.addAttribute("option", BlockOption.STATEMENT.toString());
180         checkConfig.addAttribute("tokens", "LITERAL_CASE");
181         final String[] expected = {
182             "12:28: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
183             "18:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
184             "22:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
185             "29:29: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
186             "31:37: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
187             "32:29: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
188             "32:40: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "case"),
189         };
190         verify(checkConfig, getPath("InputEmptyBlockCase.java"), expected);
191     }
192 
193     @Test
194     public void testAllowEmptyDefaultWithText() throws Exception {
195         final DefaultConfiguration checkConfig = createModuleConfig(EmptyBlockCheck.class);
196         checkConfig.addAttribute("option", BlockOption.TEXT.toString());
197         checkConfig.addAttribute("tokens", "LITERAL_DEFAULT");
198         final String[] expected = {
199             "5:30: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
200             "11:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
201             "36:22: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
202             "44:47: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
203             "50:22: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
204             "78:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
205         };
206         verify(checkConfig, getPath("InputEmptyBlockDefault.java"), expected);
207     }
208 
209     @Test
210     public void testForbidDefaultWithoutStatement() throws Exception {
211         final DefaultConfiguration checkConfig = createModuleConfig(EmptyBlockCheck.class);
212         checkConfig.addAttribute("option", BlockOption.STATEMENT.toString());
213         checkConfig.addAttribute("tokens", "LITERAL_DEFAULT");
214         final String[] expected = {
215             "5:30: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
216             "11:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
217             "15:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
218             "26:30: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
219             "36:22: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
220             "44:47: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
221             "50:22: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
222             "65:22: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
223             "78:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STATEMENT, "default"),
224         };
225         verify(checkConfig, getPath("InputEmptyBlockDefault.java"), expected);
226     }
227 
228     @Test
229     public void testAnnotationDefaultKeyword() throws Exception {
230         final DefaultConfiguration checkConfig = createModuleConfig(EmptyBlockCheck.class);
231         checkConfig.addAttribute("option", BlockOption.STATEMENT.toString());
232         checkConfig.addAttribute("tokens", "LITERAL_DEFAULT");
233         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
234         final String path = getPath("InputEmptyBlockAnnotationDefaultKeyword.java");
235         verify(checkConfig, path, expected);
236     }
237 
238 }