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;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.LineEndingCheck.MSG_KEY_UNABLE_OPEN;
24 import static com.puppycrawl.tools.checkstyle.checks.LineEndingCheck.MSG_KEY_WRONG_ENDING;
25 import static com.puppycrawl.tools.checkstyle.checks.LineEndingOption.CR;
26 import static com.puppycrawl.tools.checkstyle.checks.LineEndingOption.CRLF;
27 import static com.puppycrawl.tools.checkstyle.checks.LineEndingOption.LF;
28
29 import java.io.File;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Set;
33
34 import org.junit.jupiter.api.Test;
35
36 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
37 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
38 import com.puppycrawl.tools.checkstyle.api.FileText;
39 import com.puppycrawl.tools.checkstyle.api.Violation;
40 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
41
42 public class LineEndingCheckTest extends AbstractModuleTestSupport {
43
44 @Override
45 public String getPackageLocation() {
46 return "com/puppycrawl/tools/checkstyle/checks/lineending";
47 }
48
49 @Test
50 public void testInputLineEndingLfExpected1() throws Exception {
51 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
52
53 verifyWithInlineConfigParser(
54 getPath("InputLineEndingLfExpected1.java"),
55 expected);
56 }
57
58 @Test
59 public void testInputLineEndingLfExpected2() throws Exception {
60 final String[] expected = {
61 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CRLF),
62 "2: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CRLF),
63 "3: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CRLF),
64 "4: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CRLF),
65 "5: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CRLF),
66 "6: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CRLF),
67 "7: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CRLF),
68 };
69
70 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
71
72 verify(checkConfig,
73 getPath("InputLineEndingLfExpected2.java"),
74 expected);
75 }
76
77 @Test
78 public void testInputLineEndingLfExpected3() throws Exception {
79 final String[] expected = {
80 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CR),
81 "2: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CR),
82 "3: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CR),
83 "4: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CR),
84 "5: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CR),
85 "6: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CR),
86 "7: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CR),
87 };
88
89 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
90
91 verify(checkConfig,
92 getPath("InputLineEndingLfExpected3.java"),
93 expected);
94 }
95
96 @Test
97 public void testInputLineEndingCrlfExpected1() throws Exception {
98 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
99
100 verifyWithInlineConfigParser(
101 getPath("InputLineEndingCrlfExpected1.java"),
102 expected);
103 }
104
105 @Test
106 public void testInputLineEndingCrlfExpected2() throws Exception {
107 final String[] expected = {
108 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, LF),
109 "2: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, LF),
110 "3: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, LF),
111 "4: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, LF),
112 "5: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, LF),
113 "6: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, LF),
114 "7: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, LF),
115 };
116
117 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
118 checkConfig.addProperty("lineEnding", "crlf");
119
120 verify(checkConfig,
121 getPath("InputLineEndingCrlfExpected2.java"),
122 expected);
123 }
124
125 @Test
126 public void testInputLineEndingCrlfExpected3() throws Exception {
127 final String[] expected = {
128 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, CR),
129 "2: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, CR),
130 "3: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, CR),
131 "4: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, CR),
132 "5: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, CR),
133 "6: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, CR),
134 "7: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, CR),
135 };
136
137 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
138 checkConfig.addProperty("lineEnding", "crlf");
139
140 verify(checkConfig,
141 getPath("InputLineEndingCrlfExpected3.java"),
142 expected);
143 }
144
145 @Test
146 public void testInputLineEndingCrExpected1() throws Exception {
147 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
148
149 verifyWithInlineConfigParser(
150 getPath("InputLineEndingCrExpected1.java"),
151 expected);
152 }
153
154 @Test
155 public void testInputLineEndingCrExpected2() throws Exception {
156 final String[] expected = {
157 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, LF),
158 "2: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, LF),
159 "3: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, LF),
160 "4: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, LF),
161 "5: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, LF),
162 "6: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, LF),
163 "7: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, LF),
164 };
165
166 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
167 checkConfig.addProperty("lineEnding", "cr");
168
169 verify(checkConfig,
170 getPath("InputLineEndingCrExpected2.java"),
171 expected);
172 }
173
174 @Test
175 public void testInputLineEndingCrExpected3() throws Exception {
176 final String[] expected = {
177 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, CRLF),
178 "2: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, CRLF),
179 "3: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, CRLF),
180 "4: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, CRLF),
181 "5: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, CRLF),
182 "6: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, CRLF),
183 "7: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, CRLF),
184 };
185
186 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
187 checkConfig.addProperty("lineEnding", "cr");
188
189 verify(checkConfig,
190 getPath("InputLineEndingCrExpected3.java"),
191 expected);
192 }
193
194 @Test
195 public void testInputLineEndingOneLineLf1() throws Exception {
196 final String[] expected = {
197 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, LF),
198 };
199
200 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
201 checkConfig.addProperty("lineEnding", "crlf");
202
203 verify(checkConfig,
204 getPath("InputLineEndingOneLineLf.txt"),
205 expected);
206 }
207
208 @Test
209 public void testInputLineEndingOneLineLf2() throws Exception {
210 final String[] expected = {
211 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, LF),
212 };
213
214 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
215 checkConfig.addProperty("lineEnding", "cr");
216
217 verify(checkConfig,
218 getPath("InputLineEndingOneLineLf.txt"),
219 expected);
220 }
221
222 @Test
223 public void testInputLineEndingOneLineCrlf() throws Exception {
224 final String[] expected = {
225 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CRLF),
226 };
227
228 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
229
230 verify(checkConfig,
231 getPath("InputLineEndingOneLineCrlf.txt"),
232 expected);
233 }
234
235 @Test
236 public void testInputLineEndingOneLineCrlf2() throws Exception {
237 final String[] expected = {
238 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, CRLF),
239 };
240
241 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
242 checkConfig.addProperty("lineEnding", "cr");
243
244 verify(checkConfig,
245 getPath("InputLineEndingOneLineCrlf.txt"),
246 expected);
247 }
248
249 @Test
250 public void testInputLineEndingOneLineCr() throws Exception {
251 final String[] expected = {
252 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CR),
253 };
254
255 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
256
257 verify(checkConfig,
258 getPath("InputLineEndingOneLineCr.txt"),
259 expected);
260 }
261
262 @Test
263 public void testInputLineEndingOneLineCr2() throws Exception {
264 final String[] expected = {
265 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, CR),
266 };
267
268 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
269 checkConfig.addProperty("lineEnding", "crlf");
270
271 verify(checkConfig,
272 getPath("InputLineEndingOneLineCr.txt"),
273 expected);
274 }
275
276 @Test
277 public void testInputLineEndingOneLineLf3() throws Exception {
278 final String[] expected = {
279 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, LF),
280 };
281
282 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
283 checkConfig.addProperty("lineEnding", " crlf ");
284
285 verify(checkConfig,
286 getPath("InputLineEndingOneLineLf.txt"),
287 expected);
288 }
289
290 @Test
291 public void testInputLineEndingMultipleEndings1() throws Exception {
292 final String[] expected = {
293 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, LF),
294 "2: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, CR),
295 "4: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, CR),
296 "5: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, LF),
297 "6: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CRLF, CR),
298 };
299
300 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
301 checkConfig.addProperty("lineEnding", "crlf");
302
303 verify(checkConfig,
304 getPath("InputLineEndingMultipleEndings.java"),
305 expected);
306 }
307
308 @Test
309 public void testInputLineEndingMultipleEndings2() throws Exception {
310 final String[] expected = {
311 "2: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CR),
312 "3: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CRLF),
313 "4: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CR),
314 "6: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CR),
315 "7: " + getCheckMessage(MSG_KEY_WRONG_ENDING, LF, CRLF),
316 };
317
318 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
319
320 verify(checkConfig,
321 getPath("InputLineEndingMultipleEndings.java"),
322 expected);
323 }
324
325 @Test
326 public void testInputLineEndingMultipleEndings3() throws Exception {
327 final String[] expected = {
328 "1: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, LF),
329 "3: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, CRLF),
330 "5: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, LF),
331 "7: " + getCheckMessage(MSG_KEY_WRONG_ENDING, CR, CRLF),
332 };
333
334 final DefaultConfiguration checkConfig = createModuleConfig(LineEndingCheck.class);
335 checkConfig.addProperty("lineEnding", "cr");
336
337 verify(checkConfig,
338 getPath("InputLineEndingMultipleEndings.java"),
339 expected);
340 }
341
342 @Test
343 public void testWrongFileWithPath() throws Exception {
344 final DefaultConfiguration checkConfig =
345 createModuleConfig(LineEndingCheck.class);
346 final LineEndingCheck check = new LineEndingCheck();
347 check.configure(checkConfig);
348
349 final List<String> lines = new ArrayList<>(1);
350 lines.add("txt");
351
352 final File impossibleFile =
353 new File("this/file/does/not/exist.txt");
354
355 final FileText fileText = new FileText(impossibleFile, lines);
356 final Set<Violation> violations =
357 check.process(impossibleFile, fileText);
358
359 assertWithMessage("Amount of violations is unexpected")
360 .that(violations)
361 .hasSize(1);
362
363 final Violation violation = violations.iterator().next();
364
365 assertWithMessage("Violation line number differs from expected")
366 .that(violation.getLineNo())
367 .isEqualTo(0);
368
369 assertWithMessage("Violation message differs from expected")
370 .that(violation.getViolation())
371 .isEqualTo(
372 getCheckMessage(
373 MSG_KEY_UNABLE_OPEN,
374 impossibleFile.getPath()
375 )
376 );
377 }
378
379 }