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.sizes;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.sizes.ExecutableStatementCountCheck.MSG_KEY;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.fail;
25  
26  import java.util.Collection;
27  
28  import org.junit.Assert;
29  import org.junit.Test;
30  
31  import antlr.CommonHiddenStreamToken;
32  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
33  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
34  import com.puppycrawl.tools.checkstyle.api.Context;
35  import com.puppycrawl.tools.checkstyle.api.DetailAST;
36  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
37  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
38  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
39  
40  public class ExecutableStatementCountCheckTest
41      extends AbstractModuleTestSupport {
42      @Override
43      protected String getPackageLocation() {
44          return "com/puppycrawl/tools/checkstyle/checks/sizes/executablestatementcount";
45      }
46  
47      @Test
48      @SuppressWarnings("unchecked")
49      public void testStatefulFieldsClearedOnBeginTree() throws Exception {
50          final DetailAST ast = new DetailAST();
51          ast.setType(TokenTypes.STATIC_INIT);
52          final ExecutableStatementCountCheck check = new ExecutableStatementCountCheck();
53          Assert.assertTrue("Stateful field is not cleared after beginTree",
54                  TestUtil.isStatefulFieldClearedDuringBeginTree(check, ast, "contextStack",
55                      contextStack -> ((Collection<Context>) contextStack).isEmpty()));
56      }
57  
58      @Test
59      public void testMaxZero() throws Exception {
60          final DefaultConfiguration checkConfig =
61              createModuleConfig(ExecutableStatementCountCheck.class);
62  
63          checkConfig.addAttribute("max", "0");
64  
65          final String[] expected = {
66              "4:5: " + getCheckMessage(MSG_KEY, 3, 0),
67              "7:17: " + getCheckMessage(MSG_KEY, 1, 0),
68              "17:5: " + getCheckMessage(MSG_KEY, 2, 0),
69              "27:5: " + getCheckMessage(MSG_KEY, 1, 0),
70              "34:5: " + getCheckMessage(MSG_KEY, 3, 0),
71              "48:5: " + getCheckMessage(MSG_KEY, 2, 0),
72              "58:5: " + getCheckMessage(MSG_KEY, 2, 0),
73              "67:5: " + getCheckMessage(MSG_KEY, 2, 0),
74              "76:5: " + getCheckMessage(MSG_KEY, 2, 0),
75              "79:13: " + getCheckMessage(MSG_KEY, 1, 0),
76          };
77  
78          verify(checkConfig, getPath("InputExecutableStatementCount.java"), expected);
79      }
80  
81      @Test
82      public void testMethodDef() throws Exception {
83          final DefaultConfiguration checkConfig =
84              createModuleConfig(ExecutableStatementCountCheck.class);
85  
86          checkConfig.addAttribute("max", "0");
87          checkConfig.addAttribute("tokens", "METHOD_DEF");
88  
89          final String[] expected = {
90              "4:5: " + getCheckMessage(MSG_KEY, 3, 0),
91              "7:17: " + getCheckMessage(MSG_KEY, 1, 0),
92              "17:5: " + getCheckMessage(MSG_KEY, 2, 0),
93              "27:5: " + getCheckMessage(MSG_KEY, 1, 0),
94              "34:5: " + getCheckMessage(MSG_KEY, 3, 0),
95              "79:13: " + getCheckMessage(MSG_KEY, 1, 0),
96          };
97  
98          verify(checkConfig, getPath("InputExecutableStatementCount.java"), expected);
99      }
100 
101     @Test
102     public void testCtorDef() throws Exception {
103         final DefaultConfiguration checkConfig =
104             createModuleConfig(ExecutableStatementCountCheck.class);
105 
106         checkConfig.addAttribute("max", "0");
107         checkConfig.addAttribute("tokens", "CTOR_DEF");
108 
109         final String[] expected = {
110             "48:5: " + getCheckMessage(MSG_KEY, 2, 0),
111             "76:5: " + getCheckMessage(MSG_KEY, 2, 0),
112         };
113 
114         verify(checkConfig, getPath("InputExecutableStatementCount.java"), expected);
115     }
116 
117     @Test
118     public void testStaticInit() throws Exception {
119         final DefaultConfiguration checkConfig =
120             createModuleConfig(ExecutableStatementCountCheck.class);
121 
122         checkConfig.addAttribute("max", "0");
123         checkConfig.addAttribute("tokens", "STATIC_INIT");
124 
125         final String[] expected = {
126             "58:5: " + getCheckMessage(MSG_KEY, 2, 0),
127         };
128 
129         verify(checkConfig, getPath("InputExecutableStatementCount.java"), expected);
130     }
131 
132     @Test
133     public void testInstanceInit() throws Exception {
134         final DefaultConfiguration checkConfig =
135             createModuleConfig(ExecutableStatementCountCheck.class);
136 
137         checkConfig.addAttribute("max", "0");
138         checkConfig.addAttribute("tokens", "INSTANCE_INIT");
139 
140         final String[] expected = {
141             "67:5: " + getCheckMessage(MSG_KEY, 2, 0),
142         };
143 
144         verify(checkConfig, getPath("InputExecutableStatementCount.java"), expected);
145     }
146 
147     @Test
148     public void testVisitTokenWithWrongTokenType() {
149         final ExecutableStatementCountCheck checkObj =
150             new ExecutableStatementCountCheck();
151         final DetailAST ast = new DetailAST();
152         ast.initialize(
153             new CommonHiddenStreamToken(TokenTypes.ENUM, "ENUM"));
154         try {
155             checkObj.visitToken(ast);
156             fail("exception expected");
157         }
158         catch (IllegalStateException ex) {
159             assertEquals("Invalid exception message", "ENUM[0x-1]", ex.getMessage());
160         }
161     }
162 
163     @Test
164     public void testLeaveTokenWithWrongTokenType() {
165         final ExecutableStatementCountCheck checkObj =
166             new ExecutableStatementCountCheck();
167         final DetailAST ast = new DetailAST();
168         ast.initialize(
169             new CommonHiddenStreamToken(TokenTypes.ENUM, "ENUM"));
170         try {
171             checkObj.leaveToken(ast);
172             fail("exception expected");
173         }
174         catch (IllegalStateException ex) {
175             assertEquals("Invalid exception message", "ENUM[0x-1]", ex.getMessage());
176         }
177     }
178 
179     @Test
180     public void testDefaultConfiguration() throws Exception {
181         final DefaultConfiguration checkConfig =
182             createModuleConfig(ExecutableStatementCountCheck.class);
183 
184         createChecker(checkConfig);
185         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
186         verify(checkConfig, getPath("InputExecutableStatementCount.java"), expected);
187     }
188 }