1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
145
146
147
148
149
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 }