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 "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
143
144
145
146
147
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 }