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.LineLengthCheck.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.utils.CommonUtils;
30  
31  public class LineLengthCheckTest extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/sizes/linelength";
36      }
37  
38      @Test
39      public void testGetRequiredTokens() {
40          final LineLengthCheck checkObj = new LineLengthCheck();
41          assertArrayEquals(
42              "LineLengthCheck#getRequiredTokens should return empty array by default",
43              CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
44      }
45  
46      @Test
47      public void testGetAcceptableTokens() {
48          final LineLengthCheck checkObj = new LineLengthCheck();
49          assertArrayEquals(
50              "LineLengthCheck#getAcceptabletokens should return empty array by default",
51              CommonUtils.EMPTY_INT_ARRAY, checkObj.getAcceptableTokens());
52      }
53  
54      @Test
55      public void testSimple()
56              throws Exception {
57          final DefaultConfiguration checkConfig =
58              createModuleConfig(LineLengthCheck.class);
59          checkConfig.addAttribute("max", "80");
60          checkConfig.addAttribute("ignorePattern", "^.*is OK.*regexp.*$");
61          final String[] expected = {
62              "18: " + getCheckMessage(MSG_KEY, 80, 81),
63              "145: " + getCheckMessage(MSG_KEY, 80, 83),
64          };
65          verify(checkConfig, getPath("InputLineLengthSimple.java"), expected);
66      }
67  
68      @Test
69      public void shouldLogActualLineLength()
70              throws Exception {
71          final DefaultConfiguration checkConfig =
72              createModuleConfig(LineLengthCheck.class);
73          checkConfig.addAttribute("max", "80");
74          checkConfig.addAttribute("ignorePattern", "^.*is OK.*regexp.*$");
75          checkConfig.addMessage("maxLineLen", "{0},{1}");
76          final String[] expected = {
77              "18: 80,81",
78              "145: 80,83",
79          };
80          verify(checkConfig, getPath("InputLineLengthSimple.java"), expected);
81      }
82  
83      @Test
84      public void shouldNotLogLongImportStatements() throws Exception {
85          final DefaultConfiguration checkConfig =
86              createModuleConfig(LineLengthCheck.class);
87          checkConfig.addAttribute("max", "80");
88          final String[] expected = {
89              "9: " + getCheckMessage(MSG_KEY, 80, 87),
90          };
91          verify(checkConfig, getPath("InputLineLengthLongImportStatements.java"), expected);
92      }
93  
94      @Test
95      public void shouldNotLogLongPackageStatements() throws Exception {
96          final DefaultConfiguration checkConfig =
97              createModuleConfig(LineLengthCheck.class);
98          checkConfig.addAttribute("max", "80");
99          final String[] expected = {
100             "6: " + getCheckMessage(MSG_KEY, 80, 88),
101         };
102         verify(checkConfig, getNonCompilablePath("InputLineLengthLongPackageStatement.java"),
103                 expected);
104     }
105 
106 }