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