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 EnumValueNameCheckTest {
28
29 @Test
30 void acceptsProperlyNamedEnumValues() throws Exception {
31 Assertions.assertDoesNotThrow(
32 () -> this.runValidation("ValidEnumValues.java", true)
33 );
34 }
35
36 @Test
37 void rejectsImproperlyNamedEnumValues() throws Exception {
38 final String file = "InvalidEnumValues.java";
39 final String name = "EnumValueNameCheck";
40 MatcherAssert.assertThat(
41 "All three enum value naming violations should be reported",
42 this.runValidation(file, false),
43 Matchers.hasItems(
44 new ViolationMatcher(
45 "Enum value anyName must match pattern",
46 file, "17", name
47 ),
48 new ViolationMatcher(
49 "Enum value MixedCase must match pattern",
50 file, "22", name
51 ),
52 new ViolationMatcher(
53 "Enum value lowercase must match pattern",
54 file, "27", name
55 )
56 )
57 );
58 }
59
60 private Collection<Violation> runValidation(final String file,
61 final boolean passes) throws IOException {
62 final Environment.Mock mock = new Environment.Mock();
63 final Environment env = mock.withParam(
64 "license",
65 String.format(
66 "file:%s",
67 new License().savePackageInfo(
68 new File(mock.basedir(), "src/main/java/foo")
69 ).withLines("Hello.")
70 .withEol(String.valueOf('\n')).file()
71 )
72 ).withFile(
73 String.format("src/main/java/foo/%s", file),
74 new IoCheckedText(
75 new TextOf(
76 new ResourceOf(
77 new FormattedText("com/qulice/checkstyle/%s", file)
78 )
79 )
80 ).asString()
81 );
82 final Collection<Violation> results =
83 new CheckstyleValidator(env).validate(env.files(file));
84 MatcherAssert.assertThat(
85 "validation result should match expected state",
86 results.isEmpty(),
87 Matchers.is(passes)
88 );
89 return results;
90 }
91 }