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