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.MethodLengthCheck.MSG_KEY;
23  import static org.junit.Assert.assertArrayEquals;
24  
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
31  
32  public class MethodLengthCheckTest extends AbstractModuleTestSupport {
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          assertArrayEquals(
42              "MethodLengthCheck#getRequiredTockens should return empty array by default",
43              CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
44      }
45  
46      @Test
47      public void testGetAcceptableTokens() {
48          final MethodLengthCheck methodLengthCheckObj =
49              new MethodLengthCheck();
50          final int[] actual = methodLengthCheckObj.getAcceptableTokens();
51          final int[] expected = {
52              TokenTypes.METHOD_DEF,
53              TokenTypes.CTOR_DEF,
54          };
55  
56          assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
57      }
58  
59      @Test
60      public void testIt() throws Exception {
61          final DefaultConfiguration checkConfig =
62              createModuleConfig(MethodLengthCheck.class);
63          checkConfig.addAttribute("max", "19");
64          final String[] expected = {
65              "79:5: " + getCheckMessage(MSG_KEY, 20, 19),
66          };
67          verify(checkConfig, getPath("InputMethodLengthSimple.java"), expected);
68      }
69  
70      @Test
71      public void testCountEmpty() throws Exception {
72          final DefaultConfiguration checkConfig =
73              createModuleConfig(MethodLengthCheck.class);
74          checkConfig.addAttribute("max", "19");
75          checkConfig.addAttribute("countEmpty", "false");
76          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
77          verify(checkConfig, getPath("InputMethodLengthSimple.java"), expected);
78      }
79  
80      @Test
81      public void testWithComments() throws Exception {
82          final DefaultConfiguration checkConfig =
83                  createModuleConfig(MethodLengthCheck.class);
84          checkConfig.addAttribute("max", "7");
85          checkConfig.addAttribute("countEmpty", "false");
86          final String[] expected = {
87              "18:5: " + getCheckMessage(MSG_KEY, 8, 7),
88          };
89          verify(checkConfig, getPath("InputMethodLengthComments.java"), expected);
90      }
91  
92      @Test
93      public void testAbstract() throws Exception {
94          final DefaultConfiguration checkConfig =
95              createModuleConfig(MethodLengthCheck.class);
96          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
97          verify(checkConfig, getPath("InputMethodLengthModifier.java"), expected);
98      }
99  }