View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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 testSimpleOne()
41              throws Exception {
42          final String[] expected = {
43              "22: " + getCheckMessage(MSG_KEY, 80, 81),
44          };
45          verifyWithInlineConfigParser(
46                  getPath("InputLineLengthSimpleOne.java"), expected);
47      }
48  
49      @Test
50      public void testSimpleTwo()
51              throws Exception {
52          final String[] expected = {
53              "88: " + getCheckMessage(MSG_KEY, 80, 83),
54          };
55          verifyWithInlineConfigParser(
56                  getPath("InputLineLengthSimpleTwo.java"), expected);
57      }
58  
59      @Test
60      public void shouldLogActualLineLengthOne()
61              throws Exception {
62          final String[] expected = {
63              "23: 80,81",
64          };
65          verifyWithInlineConfigParser(
66                  getPath("InputLineLengthSimple1One.java"), expected);
67      }
68  
69      @Test
70      public void shouldLogActualLineLengthTwo()
71              throws Exception {
72          final String[] expected = {
73              "89: 80,83",
74          };
75          verifyWithInlineConfigParser(
76                  getPath("InputLineLengthSimple1Two.java"), expected);
77      }
78  
79      @Test
80      public void shouldNotLogLongImportStatements() throws Exception {
81          final String[] expected = {
82              "18: " + getCheckMessage(MSG_KEY, 80, 100),
83          };
84          verifyWithInlineConfigParser(
85                  getPath("InputLineLengthLongImportStatements.java"), expected);
86      }
87  
88      @Test
89      public void shouldNotLogLongPackageStatements() throws Exception {
90          final String[] expected = {
91              "17: " + getCheckMessage(MSG_KEY, 80, 100),
92          };
93          verifyWithInlineConfigParser(
94                  getNonCompilablePath("InputLineLengthLongPackageStatement.java"),
95                  expected);
96      }
97  
98      @Test
99      public void shouldNotLogLongLinks() throws Exception {
100         final String[] expected = {
101             "13: " + getCheckMessage(MSG_KEY, 80, 111),
102         };
103         verifyWithInlineConfigParser(
104                 getPath("InputLineLengthLongLink.java"), expected);
105     }
106 
107     @Test
108     public void countUnicodePointsOnce() throws Exception {
109         final String[] expected = {
110             "15: " + getCheckMessage(MSG_KEY, 100, 149),
111             "16: " + getCheckMessage(MSG_KEY, 100, 149),
112         };
113         verifyWithInlineConfigParser(getPath("InputLineLengthUnicodeChars.java"), expected);
114 
115     }
116 
117     @Test
118     public void testLineLengthIgnoringPackageStatements() throws Exception {
119         final String[] expected = {
120             "13: " + getCheckMessage(MSG_KEY, 75, 76),
121             "22: " + getCheckMessage(MSG_KEY, 75, 86),
122             "26: " + getCheckMessage(MSG_KEY, 75, 76),
123             "34: " + getCheckMessage(MSG_KEY, 75, 77),
124         };
125 
126         verifyWithInlineConfigParser(
127             getNonCompilablePath("InputLineLengthIgnoringPackageStatements.java"), expected);
128     }
129 
130     @Test
131     public void testLineLengthIgnoringImportStatements() throws Exception {
132         final String[] expected = {
133             "12: " + getCheckMessage(MSG_KEY, 75, 79),
134             "21: " + getCheckMessage(MSG_KEY, 75, 81),
135             "25: " + getCheckMessage(MSG_KEY, 75, 84),
136             "33: " + getCheckMessage(MSG_KEY, 75, 77),
137         };
138 
139         verifyWithInlineConfigParser(
140             getNonCompilablePath("InputLineLengthIgnoringImportStatements.java"), expected);
141     }
142 
143     /**
144      * This tests that the check does not fail when the file contains unmappable characters.
145      * Unmappable characters should be replaced with the default replacement character
146      * {@link CodingErrorAction#REPLACE}. For example, the 0x80 (hex.) character is unmappable
147      * in the IBM1098 charset.
148      *
149      * @throws Exception exception
150      */
151     @SuppressForbidden
152     @Test
153     public void testUnmappableCharacters() throws Exception {
154         final String[] expected = {
155             "4: " + getCheckMessage(MSG_KEY, 75, 238),
156         };
157 
158         final DefaultConfiguration checkConfig = createModuleConfig(LineLengthCheck.class);
159         checkConfig.addProperty("max", "75");
160 
161         final DefaultConfiguration checkerConfig = createRootConfig(checkConfig);
162         checkerConfig.addProperty("charset", "IBM1098");
163 
164         verify(checkerConfig, getPath("InputLineLengthUnmappableCharacters.java"), expected);
165     }
166 }