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.Test;
19
20
21
22
23
24
25
26
27
28 final class CheckstyleUseEnhancedSwitchTest {
29
30
31
32
33 private static final String FILE = "EnhancedSwitch.java";
34
35
36
37
38 private static final String CHECK = "UseEnhancedSwitchCheck";
39
40 @Test
41 void skipsEnhancedSwitchOnSourceEight() throws Exception {
42 MatcherAssert.assertThat(
43 "UseEnhancedSwitch must not fire when source is 8",
44 this.runValidation("8"),
45 Matchers.not(
46 Matchers.hasItem(
47 new ViolationMatcher(
48 "", CheckstyleUseEnhancedSwitchTest.FILE, "",
49 CheckstyleUseEnhancedSwitchTest.CHECK
50 )
51 )
52 )
53 );
54 }
55
56 @Test
57 void skipsEnhancedSwitchOnLegacySourceEight() throws Exception {
58 MatcherAssert.assertThat(
59 "UseEnhancedSwitch must not fire when source is 1.8",
60 this.runValidation("1.8"),
61 Matchers.not(
62 Matchers.hasItem(
63 new ViolationMatcher(
64 "", CheckstyleUseEnhancedSwitchTest.FILE, "",
65 CheckstyleUseEnhancedSwitchTest.CHECK
66 )
67 )
68 )
69 );
70 }
71
72 @Test
73 void skipsEnhancedSwitchWhenSourceUnknown() throws Exception {
74 MatcherAssert.assertThat(
75 "UseEnhancedSwitch must not fire when source is unknown",
76 this.runValidation(null),
77 Matchers.not(
78 Matchers.hasItem(
79 new ViolationMatcher(
80 "", CheckstyleUseEnhancedSwitchTest.FILE, "",
81 CheckstyleUseEnhancedSwitchTest.CHECK
82 )
83 )
84 )
85 );
86 }
87
88 @Test
89 void reportsEnhancedSwitchOnModernSource() throws Exception {
90 MatcherAssert.assertThat(
91 "UseEnhancedSwitch must fire when source is 17",
92 this.runValidation("17"),
93 Matchers.hasItem(
94 new ViolationMatcher(
95 "", CheckstyleUseEnhancedSwitchTest.FILE, "",
96 CheckstyleUseEnhancedSwitchTest.CHECK
97 )
98 )
99 );
100 }
101
102 private Collection<Violation> runValidation(final String source)
103 throws IOException {
104 final Environment.Mock mock = new Environment.Mock();
105 mock.withParam(
106 "license",
107 String.format(
108 "file:%s",
109 new License().savePackageInfo(
110 new File(mock.basedir(), "src/main/java/foo")
111 ).withLines("Hello.")
112 .withEol(String.valueOf('\n')).file()
113 )
114 );
115 if (source != null) {
116 mock.withParam("maven.compiler.source", source);
117 }
118 final Environment env = mock.withFile(
119 String.format(
120 "src/main/java/foo/%s",
121 CheckstyleUseEnhancedSwitchTest.FILE
122 ),
123 new IoCheckedText(
124 new TextOf(
125 new ResourceOf(
126 new FormattedText(
127 "com/qulice/checkstyle/%s",
128 CheckstyleUseEnhancedSwitchTest.FILE
129 )
130 )
131 )
132 ).asString()
133 );
134 return new CheckstyleValidator(env).validate(
135 env.files(CheckstyleUseEnhancedSwitchTest.FILE)
136 );
137 }
138 }