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.sizes;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.sizes.MethodLengthCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  public class MethodLengthCheckTest extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/sizes/methodlength";
36      }
37  
38      @Test
39      public void testGetRequiredTokens() {
40          final MethodLengthCheck checkObj = new MethodLengthCheck();
41          assertWithMessage("MethodLengthCheck#getRequiredTokens should return empty array "
42                  + "by default")
43              .that(checkObj.getRequiredTokens())
44              .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
45      }
46  
47      @Test
48      public void testGetAcceptableTokens() {
49          final MethodLengthCheck methodLengthCheckObj =
50              new MethodLengthCheck();
51          final int[] actual = methodLengthCheckObj.getAcceptableTokens();
52          final int[] expected = {
53              TokenTypes.METHOD_DEF,
54              TokenTypes.CTOR_DEF,
55              TokenTypes.COMPACT_CTOR_DEF,
56          };
57  
58          assertWithMessage("Default acceptable tokens are invalid")
59              .that(actual)
60              .isEqualTo(expected);
61      }
62  
63      @Test
64      public void testItOne() throws Exception {
65          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
66          verifyWithInlineConfigParser(
67                  getPath("InputMethodLengthSimpleOne.java"), expected);
68      }
69  
70      @Test
71      public void testItTwo() throws Exception {
72          final String[] expected = {
73              "16:5: " + getCheckMessage(MSG_KEY, 20, 19, "longMethod"),
74          };
75          verifyWithInlineConfigParser(
76                  getPath("InputMethodLengthSimpleTwo.java"), expected);
77      }
78  
79      @Test
80      public void testCountEmptyIsFalse() throws Exception {
81          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
82          verifyWithInlineConfigParser(
83                  getPath("InputMethodLengthCountEmptyIsFalse.java"), expected);
84      }
85  
86      @Test
87      public void testWithComments() throws Exception {
88          final String[] expected = {
89              "34:5: " + getCheckMessage(MSG_KEY, 8, 7, "visit"),
90          };
91          verifyWithInlineConfigParser(
92                  getPath("InputMethodLengthComments.java"), expected);
93      }
94  
95      @Test
96      public void testCountEmpty() throws Exception {
97          final int max = 2;
98          final String[] expected = {
99              "24:5: " + getCheckMessage(MSG_KEY, 3, max, "AA"),
100             "41:5: " + getCheckMessage(MSG_KEY, 3, max, "threeLines"),
101             "45:5: " + getCheckMessage(MSG_KEY, 3, max, "threeLinesAndComments"),
102             "53:5: " + getCheckMessage(MSG_KEY, 3, max, "threeLinesWrap"),
103             "63:5: " + getCheckMessage(MSG_KEY, 10, max, "m2"),
104         };
105         verifyWithInlineConfigParser(
106                 getPath("InputMethodLengthCountEmptySmallSize.java"), expected);
107     }
108 
109     @Test
110     public void testAbstractOne() throws Exception {
111         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
112         verifyWithInlineConfigParser(
113                 getPath("InputMethodLengthModifierOne.java"), expected);
114     }
115 
116     @Test
117     public void testAbstractTwo() throws Exception {
118         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
119         verifyWithInlineConfigParser(
120                 getPath("InputMethodLengthModifierTwo.java"), expected);
121     }
122 
123     @Test
124     public void testTextBlocks() throws Exception {
125         final int max = 2;
126 
127         final String[] expected = {
128             "14:5: " + getCheckMessage(MSG_KEY, 21, max, "longEmptyTextBlock"),
129             "43:5: " + getCheckMessage(MSG_KEY, 10, max, "textBlock2"),
130             "57:5: " + getCheckMessage(MSG_KEY, 8, max, "textBlockWithIndent"),
131             "66:5: " + getCheckMessage(MSG_KEY, 12, max, "textBlockNoIndent"),
132         };
133 
134         verifyWithInlineConfigParser(
135                 getNonCompilablePath("InputMethodLengthTextBlocksCountEmpty.java"),
136                 expected);
137     }
138 
139     @Test
140     public void testRecordsAndCompactCtors() throws Exception {
141 
142         final int max = 2;
143 
144         final String[] expected = {
145             "25:9: " + getCheckMessage(MSG_KEY, 6, max, "MyTestRecord2"),
146             "34:9: " + getCheckMessage(MSG_KEY, 5, max, "foo"),
147             "42:9: " + getCheckMessage(MSG_KEY, 7, max, "MyTestRecord4"),
148             "63:9: " + getCheckMessage(MSG_KEY, 15, max, "m"),
149             "66:17: " + getCheckMessage(MSG_KEY, 8, max, "R76"),
150         };
151 
152         verifyWithInlineConfigParser(
153                 getNonCompilablePath("InputMethodLengthRecordsAndCompactCtors.java"),
154                 expected);
155     }
156 
157     @Test
158     public void testRecordsAndCompactCtorsCountEmpty() throws Exception {
159         final int max = 2;
160 
161         final String[] expected = {
162             "25:9: " + getCheckMessage(MSG_KEY, 3, max, "MyTestRecord2"),
163             "32:9: " + getCheckMessage(MSG_KEY, 3, max, "foo"),
164             "38:9: " + getCheckMessage(MSG_KEY, 3, max, "MyTestRecord4"),
165             "55:9: " + getCheckMessage(MSG_KEY, 13, max, "m"),
166             "58:17: " + getCheckMessage(MSG_KEY, 8, max, "R76"),
167         };
168 
169         verifyWithInlineConfigParser(
170                 getNonCompilablePath("InputMethodLengthCompactCtorsCountEmpty.java"),
171                 expected);
172     }
173 
174 }