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
27 final class CascadeIndentationCheckTest {
28
29 @Test
30 void reportsErrorWhenIndentationIsIncorrect() throws Exception {
31 final String file = "InvalidIndentation.java";
32 MatcherAssert.assertThat(
33 "Invalid cascade indentation must be reported",
34 this.runValidation(file, false),
35 Matchers.hasItem(
36 new ViolationMatcher(
37 "Indentation (14) must be same or less than", file
38 )
39 )
40 );
41 }
42
43 @Test
44 void acceptsValidIndentation() throws Exception {
45 Assertions.assertDoesNotThrow(
46 () -> this.runValidation("ValidIndentation.java", true)
47 );
48 }
49
50 @Test
51 void allowsProperIndentationInAnnotations() throws Exception {
52 Assertions.assertDoesNotThrow(
53 () -> this.runValidation("AnnotationIndentation.java", true)
54 );
55 }
56
57 @Test
58 void rejectsImproperIndentationInAnnotations() throws Exception {
59 Assertions.assertDoesNotThrow(
60 () -> this.runValidation("AnnotationIndentationNegative.java", false)
61 );
62 }
63
64 @Test
65 void rejectsCascadeIndentAfterStandaloneClosingBracket() throws Exception {
66 final String file = "CascadeAfterClosingBracket.java";
67 MatcherAssert.assertThat(
68 "Indented chain after a standalone closing bracket must be reported",
69 this.runValidation(file, false),
70 Matchers.hasItem(
71 new ViolationMatcher(
72 "must not be greater than the closing bracket", file
73 )
74 )
75 );
76 }
77
78 @Test
79 void rejectsTwoClosingBracketsAtTheSameIndentation() throws Exception {
80 final String file = "TwinClosingBrackets.java";
81 MatcherAssert.assertThat(
82 "Two closing brackets at one indentation must be reported",
83 this.runValidation(file, false),
84 Matchers.hasItem(
85 new ViolationMatcher(
86 "closing bracket line must be less than", file
87 )
88 )
89 );
90 }
91
92 @Test
93 void acceptsProperlyNestedClosingBrackets() throws Exception {
94 Assertions.assertDoesNotThrow(
95 () -> this.runValidation("NestedClosingBrackets.java", true)
96 );
97 }
98
99 private Collection<Violation> runValidation(final String file,
100 final boolean passes) throws IOException {
101 final Environment.Mock mock = new Environment.Mock();
102 final Environment env = mock.withParam(
103 "license",
104 String.format(
105 "file:%s",
106 new License().savePackageInfo(
107 new File(mock.basedir(), "src/main/java/foo")
108 ).withLines("Hello.")
109 .withEol(String.valueOf('\n')).file()
110 )
111 ).withFile(
112 String.format("src/main/java/foo/%s", file),
113 new IoCheckedText(
114 new TextOf(
115 new ResourceOf(
116 new FormattedText("com/qulice/checkstyle/%s", file)
117 )
118 )
119 ).asString()
120 );
121 final Collection<Violation> results =
122 new CheckstyleValidator(env).validate(env.files(file));
123 MatcherAssert.assertThat(
124 "validation result should match expected state",
125 results.isEmpty(),
126 Matchers.is(passes)
127 );
128 return results;
129 }
130 }