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