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