1
2
3
4
5 package com.qulice.checkstyle;
6
7 import com.qulice.spi.Environment;
8 import com.qulice.spi.Violation;
9 import java.io.File;
10 import java.io.IOException;
11 import java.util.Collection;
12 import org.cactoos.io.ResourceOf;
13 import org.cactoos.text.FormattedText;
14 import org.cactoos.text.IoCheckedText;
15 import org.cactoos.text.TextOf;
16 import org.hamcrest.MatcherAssert;
17 import org.hamcrest.Matchers;
18 import org.junit.jupiter.api.Assertions;
19 import org.junit.jupiter.api.Test;
20
21
22
23
24
25
26 final class CheckstyleLineLengthTest {
27
28 @Test
29 @SuppressWarnings("unchecked")
30 void reportsErrorWhenCommentOrJavadocIsTooLong() throws Exception {
31 MatcherAssert.assertThat(
32 "Two long lines should be found",
33 this.runValidation("TooLongLines.java", false),
34 Matchers.hasItems(
35 new ViolationMatcher(
36 "Line is longer than 100 characters (found 104)", ""
37 ),
38 new ViolationMatcher(
39 "Line is longer than 100 characters (found 103)", ""
40 )
41 )
42 );
43 }
44
45 @Test
46 void suppressesLineLengthInCommentsViaNearbyComment() throws Exception {
47 Assertions.assertDoesNotThrow(
48 () -> this.runValidation("SuppressLineLengthInComment.java", true)
49 );
50 }
51
52 @Test
53 void doesNotRejectUrlsInLongLines() throws Exception {
54 Assertions.assertDoesNotThrow(
55 () -> this.runValidation("UrlInLongLine.java", true)
56 );
57 }
58
59 @Test
60 void doesNotDoubleCountNonAsciiChars() throws Exception {
61 Assertions.assertDoesNotThrow(
62 () -> this.runValidation("NonAsciiLineLength.java", true)
63 );
64 }
65
66 private Collection<Violation> runValidation(final String file,
67 final boolean passes) throws IOException {
68 final Environment.Mock mock = new Environment.Mock();
69 final Environment env = mock.withParam(
70 "license",
71 String.format(
72 "file:%s",
73 new License().savePackageInfo(
74 new File(mock.basedir(), "src/main/java/foo")
75 ).withLines("Hello.")
76 .withEol(String.valueOf('\n')).file()
77 )
78 ).withFile(
79 String.format("src/main/java/foo/%s", file),
80 new IoCheckedText(
81 new TextOf(
82 new ResourceOf(
83 new FormattedText("com/qulice/checkstyle/%s", file)
84 )
85 )
86 ).asString()
87 );
88 final Collection<Violation> results =
89 new CheckstyleValidator(env).validate(env.files(file));
90 MatcherAssert.assertThat(
91 "validation result should match expected state",
92 results.isEmpty(),
93 Matchers.is(passes)
94 );
95 return results;
96 }
97 }