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