View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.pmd;
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 java.util.stream.Collectors;
12  import org.hamcrest.MatcherAssert;
13  import org.hamcrest.Matchers;
14  import org.junit.jupiter.api.Test;
15  
16  /**
17   * Test case for general {@link PmdValidator} behavior that is
18   * not tied to a single PMD rule. Per-rule coverage lives in the
19   * dedicated {@code Pmd*Test.java} files in this package.
20   *
21   * @since 0.3
22   */
23  final class PmdValidatorTest {
24  
25      @Test
26      void findsProblemsInJavaFiles() throws Exception {
27          final String file = "src/main/java/Main.java";
28          final Environment env = new Environment.Mock()
29              .withFile(file, "class Main { int x = 0; }");
30          MatcherAssert.assertThat(
31              "Violations should be found",
32              new PmdValidator(env).validate(
33                  Collections.singletonList(new File(env.basedir(), file))
34              ),
35              Matchers.not(Matchers.<Violation>empty())
36          );
37      }
38  
39      @Test
40      void countsRulesItApplies() throws Exception {
41          MatcherAssert.assertThat(
42              "PMD cannot stay silent about how many rules it applies",
43              new PmdValidator(new Environment.Mock()).rules(),
44              Matchers.greaterThan(100)
45          );
46      }
47  
48      @Test
49      void doesNotCrashOnNonAsciiIdentifier() throws Exception {
50          final String file = "src/main/java/Main.java";
51          final Environment env = new Environment.Mock().withFile(
52              file, "final class Main { private final Phi.Φ obj = null; }"
53          );
54          MatcherAssert.assertThat(
55              "PMD must not choke on a non-ASCII Java identifier",
56              new PmdValidator(env).validate(
57                  Collections.singletonList(new File(env.basedir(), file))
58              ).stream().map(Violation::name).collect(Collectors.toList()),
59              Matchers.not(Matchers.hasItem("ProcessingError"))
60          );
61      }
62  
63      @Test
64      void acceptsJavaFilesWithUppercaseExtension() throws Exception {
65          final String file = "src/main/java/Main.JAVA";
66          final Environment env = new Environment.Mock()
67              .withFile(file, "class Main { int x = 0; }");
68          MatcherAssert.assertThat(
69              "File with uppercase .JAVA extension should not be filtered out",
70              new PmdValidator(env).getNonExcludedFiles(
71                  Collections.singletonList(new File(env.basedir(), file))
72              ),
73              Matchers.not(Matchers.empty())
74          );
75      }
76  }