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.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck.MSG_KEY;
23  
24  import java.nio.charset.CodingErrorAction;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import de.thetaphi.forbiddenapis.SuppressForbidden;
31  
32  public class LineLengthCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/sizes/linelength";
37      }
38  
39      @Test
40      public void testSimple()
41              throws Exception {
42          final String[] expected = {
43              "22: " + getCheckMessage(MSG_KEY, 80, 81),
44              "149: " + getCheckMessage(MSG_KEY, 80, 83),
45          };
46          verifyWithInlineConfigParser(
47                  getPath("InputLineLengthSimple.java"), expected);
48      }
49  
50      @Test
51      public void shouldLogActualLineLength()
52              throws Exception {
53          final String[] expected = {
54              "23: 80,81",
55              "150: 80,83",
56          };
57          verifyWithInlineConfigParser(
58                  getPath("InputLineLengthSimple1.java"), expected);
59      }
60  
61      @Test
62      public void shouldNotLogLongImportStatements() throws Exception {
63          final String[] expected = {
64              "18: " + getCheckMessage(MSG_KEY, 80, 100),
65          };
66          verifyWithInlineConfigParser(
67                  getPath("InputLineLengthLongImportStatements.java"), expected);
68      }
69  
70      @Test
71      public void shouldNotLogLongPackageStatements() throws Exception {
72          final String[] expected = {
73              "17: " + getCheckMessage(MSG_KEY, 80, 100),
74          };
75          verifyWithInlineConfigParser(
76                  getNonCompilablePath("InputLineLengthLongPackageStatement.java"),
77                  expected);
78      }
79  
80      @Test
81      public void shouldNotLogLongLinks() throws Exception {
82          final String[] expected = {
83              "13: " + getCheckMessage(MSG_KEY, 80, 111),
84          };
85          verifyWithInlineConfigParser(
86                  getPath("InputLineLengthLongLink.java"), expected);
87      }
88  
89      @Test
90      public void countUnicodePointsOnce() throws Exception {
91          final String[] expected = {
92              "15: " + getCheckMessage(MSG_KEY, 100, 149),
93              "16: " + getCheckMessage(MSG_KEY, 100, 149),
94          };
95          verifyWithInlineConfigParser(getPath("InputLineLengthUnicodeChars.java"), expected);
96  
97      }
98  
99      @Test
100     public void testLineLengthIgnoringPackageStatements() throws Exception {
101         final String[] expected = {
102             "13: " + getCheckMessage(MSG_KEY, 75, 76),
103             "22: " + getCheckMessage(MSG_KEY, 75, 86),
104             "26: " + getCheckMessage(MSG_KEY, 75, 76),
105             "34: " + getCheckMessage(MSG_KEY, 75, 77),
106         };
107 
108         verifyWithInlineConfigParser(
109             getNonCompilablePath("InputLineLengthIgnoringPackageStatements.java"), expected);
110     }
111 
112     @Test
113     public void testLineLengthIgnoringImportStatements() throws Exception {
114         final String[] expected = {
115             "12: " + getCheckMessage(MSG_KEY, 75, 79),
116             "21: " + getCheckMessage(MSG_KEY, 75, 81),
117             "25: " + getCheckMessage(MSG_KEY, 75, 84),
118             "33: " + getCheckMessage(MSG_KEY, 75, 77),
119         };
120 
121         verifyWithInlineConfigParser(
122             getNonCompilablePath("InputLineLengthIgnoringImportStatements.java"), expected);
123     }
124 
125     /**
126      * This tests that the check does not fail when the file contains unmappable characters.
127      * Unmappable characters should be replaced with the default replacement character
128      * {@link CodingErrorAction#REPLACE}. For example, the 0x80 (hex.) character is unmappable
129      * in the IBM1098 charset.
130      *
131      * @throws Exception exception
132      */
133     @SuppressForbidden
134     @Test
135     public void testUnmappableCharacters() throws Exception {
136         final String[] expected = {
137             "4: " + getCheckMessage(MSG_KEY, 75, 238),
138         };
139 
140         final DefaultConfiguration checkConfig = createModuleConfig(LineLengthCheck.class);
141         checkConfig.addProperty("max", "75");
142 
143         final DefaultConfiguration checkerConfig = createRootConfig(checkConfig);
144         checkerConfig.addProperty("charset", "IBM1098");
145 
146         verify(checkerConfig, getPath("InputLineLengthUnmappableCharacters.java"), expected);
147     }
148 }