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 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
127
128
129
130
131
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 }