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