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.google.checkstyle.test.chapter4formatting.rule413emptyblocks;
21  
22  import org.junit.Test;
23  
24  import com.google.checkstyle.test.base.AbstractModuleTestSupport;
25  import com.puppycrawl.tools.checkstyle.api.Configuration;
26  import com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck;
27  
28  public class EmptyBlockTest extends AbstractModuleTestSupport {
29  
30      @Override
31      protected String getPackageLocation() {
32          return "com/google/checkstyle/test/chapter4formatting/rule413emptyblocks";
33      }
34  
35      @Test
36      public void testEmptyBlock() throws Exception {
37          final String[] expected = {
38              "19:21: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
39              "22:34: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
40              "26:21: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
41              "28:20: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "switch"),
42              "68:25: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
43              "71:38: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
44              "75:25: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
45              "77:24: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "switch"),
46              "98:29: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
47              "101:42: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
48              "105:29: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
49              "107:28: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "switch"),
50              "126:16: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "else"),
51              "172:28: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
52              "173:14: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "else"),
53              "175:21: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
54              "179:14: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "else"),
55              "181:21: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
56              "182:26: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
57              "195:20: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "else"),
58              "241:32: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
59              "242:18: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "else"),
60              "244:25: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
61              "248:18: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "else"),
62              "250:25: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
63              "251:30: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
64              "264:24: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "else"),
65              "310:36: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
66              "311:22: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "else"),
67              "313:29: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
68              "317:22: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "else"),
69              "319:29: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
70              "320:34: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "if"),
71          };
72  
73          final Configuration checkConfig = getModuleConfig("EmptyBlock");
74          final String filePath = getPath("InputEmptyBlockBasic.java");
75  
76          final Integer[] warnList = getLinesWithWarn(filePath);
77          verify(checkConfig, filePath, expected, warnList);
78      }
79  
80      @Test
81      public void testEmptyBlockCatch() throws Exception {
82          final String[] expected = {
83              "29:17: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "finally"),
84              "50:21: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "finally"),
85              "72:21: " + getCheckMessage(EmptyBlockCheck.class, "block.empty", "finally"),
86          };
87  
88          final Configuration checkConfig = getModuleConfig("EmptyBlock");
89          final String filePath = getPath("InputEmptyBlock.java");
90  
91          final Integer[] warnList = getLinesWithWarn(filePath);
92          verify(checkConfig, filePath, expected, warnList);
93      }
94  
95  }