View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.ReturnCountCheck.MSG_KEY;
24  import static com.puppycrawl.tools.checkstyle.checks.coding.ReturnCountCheck.MSG_KEY_VOID;
25  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
26  
27  import java.io.File;
28  import java.util.Collection;
29  import java.util.Optional;
30  import java.util.Set;
31  
32  import org.junit.jupiter.api.Test;
33  
34  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35  import com.puppycrawl.tools.checkstyle.DetailAstImpl;
36  import com.puppycrawl.tools.checkstyle.JavaParser;
37  import com.puppycrawl.tools.checkstyle.api.DetailAST;
38  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
39  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
40  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
41  
42  public class ReturnCountCheckTest extends AbstractModuleTestSupport {
43  
44      @Override
45      protected String getPackageLocation() {
46          return "com/puppycrawl/tools/checkstyle/checks/coding/returncount";
47      }
48  
49      @Test
50      public void testDefault() throws Exception {
51          final String[] expected = {
52              "28:5: " + getCheckMessage(MSG_KEY_VOID, 7, 1),
53              "40:5: " + getCheckMessage(MSG_KEY_VOID, 2, 1),
54              "45:17: " + getCheckMessage(MSG_KEY_VOID, 6, 1),
55              "59:5: " + getCheckMessage(MSG_KEY, 7, 2),
56          };
57          verifyWithInlineConfigParser(
58                  getPath("InputReturnCountSwitches.java"), expected);
59      }
60  
61      @Test
62      public void testFormat() throws Exception {
63          final String[] expected = {
64              "15:5: " + getCheckMessage(MSG_KEY, 7, 2),
65              "28:5: " + getCheckMessage(MSG_KEY_VOID, 7, 1),
66              "40:5: " + getCheckMessage(MSG_KEY_VOID, 2, 1),
67              "45:17: " + getCheckMessage(MSG_KEY_VOID, 6, 1),
68              "59:5: " + getCheckMessage(MSG_KEY, 7, 2),
69          };
70          verifyWithInlineConfigParser(
71                  getPath("InputReturnCountSwitches2.java"), expected);
72      }
73  
74      @Test
75      public void testMethodsAndLambdas() throws Exception {
76          final String[] expected = {
77              "25:55: " + getCheckMessage(MSG_KEY, 2, 1),
78              "37:49: " + getCheckMessage(MSG_KEY, 2, 1),
79              "44:42: " + getCheckMessage(MSG_KEY, 3, 1),
80              "51:5: " + getCheckMessage(MSG_KEY, 2, 1),
81              "59:57: " + getCheckMessage(MSG_KEY, 2, 1),
82          };
83          verifyWithInlineConfigParser(
84                  getPath("InputReturnCountLambda.java"), expected);
85      }
86  
87      @Test
88      public void testLambdasOnly() throws Exception {
89          final String[] expected = {
90              "43:42: " + getCheckMessage(MSG_KEY, 3, 2),
91          };
92          verifyWithInlineConfigParser(
93                  getPath("InputReturnCountLambda2.java"), expected);
94      }
95  
96      @Test
97      public void testMethodsOnly() throws Exception {
98          final String[] expected = {
99              "35:5: " + getCheckMessage(MSG_KEY, 3, 2),
100             "42:5: " + getCheckMessage(MSG_KEY, 4, 2),
101             "50:5: " + getCheckMessage(MSG_KEY, 4, 2),
102             "65:5: " + getCheckMessage(MSG_KEY, 3, 2),
103         };
104         verifyWithInlineConfigParser(
105                 getPath("InputReturnCountLambda3.java"), expected);
106     }
107 
108     @Test
109     public void testWithReturnOnlyAsTokens() throws Exception {
110         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
111         verifyWithInlineConfigParser(
112                 getPath("InputReturnCountLambda4.java"), expected);
113     }
114 
115     @Test
116     public void testImproperToken() {
117         final ReturnCountCheck check = new ReturnCountCheck();
118 
119         final DetailAstImpl classDefAst = new DetailAstImpl();
120         classDefAst.setType(TokenTypes.CLASS_DEF);
121 
122         try {
123             check.visitToken(classDefAst);
124             assertWithMessage("IllegalStateException is expected").fail();
125         }
126         catch (IllegalStateException ex) {
127             // it is OK
128         }
129 
130         try {
131             check.leaveToken(classDefAst);
132             assertWithMessage("IllegalStateException is expected").fail();
133         }
134         catch (IllegalStateException ex) {
135             // it is OK
136         }
137     }
138 
139     @Test
140     public void testMaxForVoid() throws Exception {
141         final String[] expected = {
142             "14:5: " + getCheckMessage(MSG_KEY_VOID, 1, 0),
143             "18:5: " + getCheckMessage(MSG_KEY_VOID, 1, 0),
144             "24:5: " + getCheckMessage(MSG_KEY_VOID, 2, 0),
145             "40:5: " + getCheckMessage(MSG_KEY, 3, 2),
146             "51:5: " + getCheckMessage(MSG_KEY_VOID, 2, 0),
147         };
148         verifyWithInlineConfigParser(
149                 getPath("InputReturnCountVoid.java"), expected);
150     }
151 
152     /**
153      * We cannot reproduce situation when visitToken is called and leaveToken is not.
154      * So, we have to use reflection to be sure that even in such situation
155      * state of the field will be cleared.
156      *
157      * @throws Exception when code tested throws exception
158      */
159     @Test
160     @SuppressWarnings("unchecked")
161     public void testClearState() throws Exception {
162         final ReturnCountCheck check = new ReturnCountCheck();
163         final Optional<DetailAST> methodDef = TestUtil.findTokenInAstByPredicate(
164             JavaParser.parseFile(new File(getPath("InputReturnCountVoid.java")),
165                 JavaParser.Options.WITHOUT_COMMENTS),
166             ast -> ast.getType() == TokenTypes.METHOD_DEF);
167 
168         assertWithMessage("Ast should contain METHOD_DEF")
169                 .that(methodDef.isPresent())
170                 .isTrue();
171         assertWithMessage("State is not cleared on beginTree")
172                 .that(TestUtil.isStatefulFieldClearedDuringBeginTree(check,
173                         methodDef.orElseThrow(), "contextStack",
174                         contextStack -> ((Collection<Set<String>>) contextStack).isEmpty()))
175                 .isTrue();
176     }
177 
178     /**
179      * Tries to reproduce system failure to call Check on not acceptable token.
180      * It can not be reproduced by Input files. Maintainers thinks that keeping
181      * exception on unknown token is beneficial.
182      *
183      */
184     @Test
185     public void testImproperVisitToken() {
186         final ReturnCountCheck check = new ReturnCountCheck();
187         final DetailAstImpl classDefAst = new DetailAstImpl();
188         classDefAst.setType(TokenTypes.CLASS_DEF);
189         final IllegalStateException exception = getExpectedThrowable(IllegalStateException.class,
190                 () -> check.visitToken(classDefAst), "IllegalStateException was expected");
191 
192         assertWithMessage("Message doesn't contain ast")
193                 .that(exception.getMessage())
194                 .isEqualTo(classDefAst.toString());
195     }
196 
197     /**
198      * Tries to reproduce system failure to call Check on not acceptable token.
199      * It can not be reproduced by Input files. Maintainers thinks that keeping
200      * exception on unknown token is beneficial.
201      *
202      */
203     @Test
204     public void testImproperLeaveToken() {
205         final ReturnCountCheck check = new ReturnCountCheck();
206         final DetailAstImpl classDefAst = new DetailAstImpl();
207         classDefAst.setType(TokenTypes.CLASS_DEF);
208         final IllegalStateException exception = getExpectedThrowable(IllegalStateException.class,
209                 () -> check.leaveToken(classDefAst), "IllegalStateException was expected");
210 
211         assertWithMessage("Message doesn't contain ast")
212                 .that(exception.getMessage())
213                 .isEqualTo(classDefAst.toString());
214     }
215 
216 }