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.coding;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.coding.ReturnCountCheck.MSG_KEY;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.ReturnCountCheck.MSG_KEY_VOID;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.File;
27  import java.util.Collection;
28  import java.util.Optional;
29  import java.util.Set;
30  
31  import org.junit.Assert;
32  import org.junit.Test;
33  
34  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
36  import com.puppycrawl.tools.checkstyle.api.DetailAST;
37  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
38  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
39  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
40  
41  public class ReturnCountCheckTest extends AbstractModuleTestSupport {
42      @Override
43      protected String getPackageLocation() {
44          return "com/puppycrawl/tools/checkstyle/checks/coding/returncount";
45      }
46  
47      @Test
48      public void testDefault() throws Exception {
49          final DefaultConfiguration checkConfig =
50              createModuleConfig(ReturnCountCheck.class);
51          final String[] expected = {
52              "18:5: " + getCheckMessage(MSG_KEY_VOID, 7, 1),
53              "30:5: " + getCheckMessage(MSG_KEY_VOID, 2, 1),
54              "35:17: " + getCheckMessage(MSG_KEY_VOID, 6, 1),
55              "49:5: " + getCheckMessage(MSG_KEY, 7, 2),
56          };
57          verify(checkConfig, getPath("InputReturnCountSwitches.java"), expected);
58      }
59  
60      @Test
61      public void testFormat() throws Exception {
62          final DefaultConfiguration checkConfig =
63              createModuleConfig(ReturnCountCheck.class);
64          checkConfig.addAttribute("format", "^$");
65          final String[] expected = {
66              "5:5: " + getCheckMessage(MSG_KEY, 7, 2),
67              "18:5: " + getCheckMessage(MSG_KEY_VOID, 7, 1),
68              "30:5: " + getCheckMessage(MSG_KEY_VOID, 2, 1),
69              "35:17: " + getCheckMessage(MSG_KEY_VOID, 6, 1),
70              "49:5: " + getCheckMessage(MSG_KEY, 7, 2),
71          };
72          verify(checkConfig, getPath("InputReturnCountSwitches.java"), expected);
73      }
74  
75      @Test
76      public void testMethodsAndLambdas() throws Exception {
77          final DefaultConfiguration checkConfig = createModuleConfig(ReturnCountCheck.class);
78          checkConfig.addAttribute("max", "1");
79          final String[] expected = {
80              "15:55: " + getCheckMessage(MSG_KEY, 2, 1),
81              "27:49: " + getCheckMessage(MSG_KEY, 2, 1),
82              "34:42: " + getCheckMessage(MSG_KEY, 3, 1),
83              "41:5: " + getCheckMessage(MSG_KEY, 2, 1),
84              "49:57: " + getCheckMessage(MSG_KEY, 2, 1),
85          };
86          verify(checkConfig, getPath("InputReturnCountLambda.java"), expected);
87      }
88  
89      @Test
90      public void testLambdasOnly() throws Exception {
91          final DefaultConfiguration checkConfig = createModuleConfig(ReturnCountCheck.class);
92          checkConfig.addAttribute("tokens", "LAMBDA");
93          final String[] expected = {
94              "34:42: " + getCheckMessage(MSG_KEY, 3, 2),
95          };
96          verify(checkConfig, getPath("InputReturnCountLambda.java"), expected);
97      }
98  
99      @Test
100     public void testMethodsOnly() throws Exception {
101         final DefaultConfiguration checkConfig = createModuleConfig(ReturnCountCheck.class);
102         checkConfig.addAttribute("tokens", "METHOD_DEF");
103         final String[] expected = {
104             "26:5: " + getCheckMessage(MSG_KEY, 3, 2),
105             "33:5: " + getCheckMessage(MSG_KEY, 4, 2),
106             "41:5: " + getCheckMessage(MSG_KEY, 4, 2),
107             "56:5: " + getCheckMessage(MSG_KEY, 3, 2),
108         };
109         verify(checkConfig, getPath("InputReturnCountLambda.java"), expected);
110     }
111 
112     @Test
113     public void testWithReturnOnlyAsTokens() throws Exception {
114         final DefaultConfiguration checkConfig = createModuleConfig(ReturnCountCheck.class);
115         checkConfig.addAttribute("tokens", "LITERAL_RETURN");
116         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
117         verify(checkConfig, getPath("InputReturnCountLambda.java"), expected);
118     }
119 
120     @Test
121     public void testImproperToken() {
122         final ReturnCountCheck check = new ReturnCountCheck();
123 
124         final DetailAST classDefAst = new DetailAST();
125         classDefAst.setType(TokenTypes.CLASS_DEF);
126 
127         try {
128             check.visitToken(classDefAst);
129             Assert.fail("IllegalStateException is expected");
130         }
131         catch (IllegalStateException ex) {
132             // it is OK
133         }
134 
135         try {
136             check.leaveToken(classDefAst);
137             Assert.fail("IllegalStateException is expected");
138         }
139         catch (IllegalStateException ex) {
140             // it is OK
141         }
142     }
143 
144     @Test
145     public void testMaxForVoid() throws Exception {
146         final DefaultConfiguration checkConfig = createModuleConfig(ReturnCountCheck.class);
147         checkConfig.addAttribute("max", "2");
148         checkConfig.addAttribute("maxForVoid", "0");
149         final String[] expected = {
150             "4:5: " + getCheckMessage(MSG_KEY_VOID, 1, 0),
151             "8:5: " + getCheckMessage(MSG_KEY_VOID, 1, 0),
152             "14:5: " + getCheckMessage(MSG_KEY_VOID, 2, 0),
153             "30:5: " + getCheckMessage(MSG_KEY, 3, 2),
154             "41:5: " + getCheckMessage(MSG_KEY_VOID, 2, 0),
155         };
156         verify(checkConfig, getPath("InputReturnCountVoid.java"), expected);
157     }
158 
159     /**
160      * We cannot reproduce situation when visitToken is called and leaveToken is not.
161      * So, we have to use reflection to be sure that even in such situation
162      * state of the field will be cleared.
163      *
164      * @throws Exception when code tested throws exception
165      */
166     @Test
167     @SuppressWarnings("unchecked")
168     public void testClearState() throws Exception {
169         final ReturnCountCheck check = new ReturnCountCheck();
170         final Optional<DetailAST> methodDef = TestUtil.findTokenInAstByPredicate(
171             TestUtil.parseFile(new File(getPath("InputReturnCountVoid.java"))),
172             ast -> ast.getType() == TokenTypes.METHOD_DEF);
173 
174         assertTrue("Ast should contain METHOD_DEF", methodDef.isPresent());
175         assertTrue("State is not cleared on beginTree",
176             TestUtil.isStatefulFieldClearedDuringBeginTree(check, methodDef.get(),
177                 "contextStack",
178                 contextStack -> ((Collection<Set<String>>) contextStack).isEmpty()));
179     }
180 }