View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.errorprone;
6   
7   import com.qulice.spi.Environment;
8   import com.qulice.spi.Violation;
9   import java.io.File;
10  import java.util.Collections;
11  import org.hamcrest.MatcherAssert;
12  import org.hamcrest.Matchers;
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * Test case for {@link ErrorProneValidator}.
17   * @since 1.0
18   */
19  final class ErrorProneValidatorTest {
20  
21      @Test
22      void findsViolationInBadJavaFile() throws Exception {
23          final String file = "src/main/java/Bad.java";
24          final Environment env = new Environment.Mock().withFile(
25              file,
26              "class Bad { private int value; void set(int v) { this.value = this.value; } }"
27          );
28          MatcherAssert.assertThat(
29              "ErrorProne must flag the self-assignment in Bad.java",
30              new ErrorProneValidator(env).validate(
31                  Collections.singletonList(new File(env.basedir(), file))
32              ),
33              Matchers.not(Matchers.<Violation>empty())
34          );
35      }
36  
37      @Test
38      void doesNotFlagCleanJavaFile() throws Exception {
39          final String file = "src/main/java/com/qulice/Clean.java";
40          final Environment env = new Environment.Mock().withFile(
41              file,
42              "package com.qulice; final class Clean { int square(final int num) { return num * num; } }"
43          );
44          final java.util.Collection<Violation> violations =
45              new ErrorProneValidator(env).validate(
46                  Collections.singletonList(new File(env.basedir(), file))
47              );
48          MatcherAssert.assertThat(
49              String.format("Clean code must not produce ErrorProne violations: %s", violations),
50              violations,
51              Matchers.<Violation>empty()
52          );
53      }
54  
55      @Test
56      void doesNotFlagCheckstyleJavadocTag() throws Exception {
57          final String file = "src/main/java/com/qulice/Tagged.java";
58          final Environment env = new Environment.Mock().withFile(
59              file,
60              String.join(
61                  System.lineSeparator(),
62                  "package com.qulice;",
63                  "/**",
64                  " * Sample.",
65                  " * @since 1.0",
66                  " * @checkstyle MethodNameCheck (1 line)",
67                  " */",
68                  "final class Tagged {",
69                  "    int square(final int num) { return num * num; }",
70                  "}"
71              )
72          );
73          final java.util.Collection<Violation> violations =
74              new ErrorProneValidator(env).validate(
75                  Collections.singletonList(new File(env.basedir(), file))
76              );
77          MatcherAssert.assertThat(
78              String.format(
79                  "@checkstyle Javadoc tag must not trigger ErrorProne violations: %s",
80                  violations
81              ),
82              violations,
83              Matchers.<Violation>empty()
84          );
85      }
86  }