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             "17: " + getCheckMessage(MSG_KEY, 75, 86),
121             "21: " + getCheckMessage(MSG_KEY, 75, 76),
122             "29: " + getCheckMessage(MSG_KEY, 75, 77),
123         };
124 
125         verifyWithInlineConfigParser(
126             getPath("InputLineLengthIgnoringPackageStatements.java"), expected);
127     }
128 
129     @Test
130     public void testLineLengthIgnoringImportStatements() throws Exception {
131         final String[] expected = {
132             "18: " + getCheckMessage(MSG_KEY, 75, 81),
133             "22: " + getCheckMessage(MSG_KEY, 75, 84),
134             "30: " + getCheckMessage(MSG_KEY, 75, 77),
135         };
136 
137         verifyWithInlineConfigParser(
138             getPath("InputLineLengthIgnoringImportStatements.java"), expected);
139     }
140 
141     /**
142      * This tests that the check does not fail when the file contains unmappable characters.
143      * Unmappable characters should be replaced with the default replacement character
144      * {@link CodingErrorAction#REPLACE}. For example, the 0x80 (hex.) character is unmappable
145      * in the IBM1098 charset.
146      *
147      * @throws Exception exception
148      */
149     @SuppressForbidden
150     @Test
151     public void testUnmappableCharacters() throws Exception {
152         final String[] expected = {
153             "4: " + getCheckMessage(MSG_KEY, 75, 238),
154         };
155 
156         final DefaultConfiguration checkConfig = createModuleConfig(LineLengthCheck.class);
157         checkConfig.addProperty("max", "75");
158 
159         final DefaultConfiguration checkerConfig = createRootConfig(checkConfig);
160         checkerConfig.addProperty("charset", "IBM1098");
161 
162         verify(checkerConfig, getPath("InputLineLengthUnmappableCharacters.java"), expected);
163     }
164 }