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.text.IoCheckedText;
13 import org.cactoos.text.Joined;
14 import org.hamcrest.MatcherAssert;
15 import org.hamcrest.Matchers;
16 import org.junit.jupiter.api.Test;
17
18
19
20
21
22
23
24 final class CheckstyleTextFileTest {
25
26
27
28
29
30
31
32 @Test
33 void rejectsTabInNonJavaTextFile() throws Exception {
34 final String file = "script.js";
35 MatcherAssert.assertThat(
36 "Tab character in .js file is not reported",
37 this.runValidationWithContent(
38 file,
39 "\tconsole.log(\"Hello World\");".concat(String.valueOf('\n'))
40 ),
41 Matchers.hasItem(
42 new ViolationMatcher(
43 "tab", file, "1", "FileTabCharacterCheck"
44 )
45 )
46 );
47 }
48
49
50
51
52
53
54
55 @Test
56 void rejectsMissingNewlineInNonJavaTextFile() throws Exception {
57 final String file = "README.md";
58 MatcherAssert.assertThat(
59 "Missing final newline in .md file is not reported",
60 this.runValidationWithContent(
61 file,
62 "# Title".concat(String.valueOf('\n')).concat("No newline at end")
63 ),
64 Matchers.hasItem(
65 new ViolationMatcher(
66 "File does not end with a newline 'lf'.", file, "",
67 "NewlineAtEndOfFileCheck"
68 )
69 )
70 );
71 }
72
73
74
75
76
77
78
79 @Test
80 void rejectsEmptyLineBeforeClosingBrace() throws Exception {
81 final String file = "EmptyLineBeforeBrace.java";
82 MatcherAssert.assertThat(
83 "Empty line before closing brace is not reported",
84 this.runValidationWithContent(
85 file,
86 new IoCheckedText(
87 new Joined(
88 String.valueOf('\n'),
89 "package foo;",
90 "public final class EmptyLineBeforeBrace {",
91 " public void foo() {",
92 " int x = 1;",
93 "",
94 " }",
95 "}",
96 ""
97 )
98 ).asString()
99 ),
100 Matchers.hasItem(
101 new ViolationMatcher(
102 "Empty line before closing brace is not allowed",
103 file, "", "RegexpMultilineCheck"
104 )
105 )
106 );
107 }
108
109
110
111
112
113
114
115
116 @Test
117 void acceptsNoEmptyLineBeforeClosingBrace() throws Exception {
118 final String file = "NoEmptyLineBeforeBrace.java";
119 MatcherAssert.assertThat(
120 "Absence of empty line before closing brace was reported",
121 this.runValidationWithContent(
122 file,
123 new IoCheckedText(
124 new Joined(
125 String.valueOf('\n'),
126 "package foo;",
127 "public final class NoEmptyLineBeforeBrace {",
128 " public void foo() {",
129 " int x = 1;",
130 " }",
131 "}",
132 ""
133 )
134 ).asString()
135 ),
136 Matchers.not(
137 Matchers.hasItem(
138 new ViolationMatcher(
139 "Empty line before closing brace is not allowed",
140 file, "", "RegexpMultilineCheck"
141 )
142 )
143 )
144 );
145 }
146
147 private Collection<Violation> runValidationWithContent(final String file,
148 final String content) throws IOException {
149 final Environment.Mock mock = new Environment.Mock();
150 final Environment env = mock.withParam(
151 "license",
152 String.format(
153 "file:%s",
154 new License().savePackageInfo(
155 new File(mock.basedir(), "src/main/java/foo")
156 ).withLines("Hello.")
157 .withEol(String.valueOf('\n')).file()
158 )
159 ).withFile(String.format("src/main/resources/%s", file), content);
160 return new CheckstyleValidator(env).validate(env.files(file));
161 }
162 }