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.google.common.base.Joiner;
8   import com.qulice.spi.Environment;
9   import com.qulice.spi.Violation;
10  import java.io.File;
11  import java.util.Collections;
12  import org.hamcrest.MatcherAssert;
13  import org.hamcrest.Matchers;
14  import org.junit.jupiter.api.Test;
15  
16  /**
17   * Test case for {@link PmdValidator}'s handling of private
18   * constants that are used only from within an inner class —
19   * they must not be reported as unused.
20   * @since 0.25.1
21   */
22  final class PmdInnerClassConstantsTest {
23  
24      @Test
25      void doesNotComplainAboutConstantsInInnerClasses() throws Exception {
26          final String file = "src/main/java/foo/Foo.java";
27          final Environment env = new Environment.Mock().withFile(
28              file,
29              Joiner.on('\n').join(
30                  "package foo;",
31                  "interface Foo {",
32                  "  final class Bar implements Foo {",
33                  "    private static final Pattern TEST =",
34                  "      Pattern.compile(\"hey\");",
35                  "    public String doSomething() {",
36                  "      return Foo.Bar.TEST.toString();",
37                  "    }",
38                  "  }",
39                  "}"
40              )
41          );
42          MatcherAssert.assertThat(
43              "Private constant in inner class is not a violation",
44              new PmdValidator(env).validate(
45                  Collections.singletonList(new File(env.basedir(), file))
46              ),
47              Matchers.<Violation>empty()
48          );
49      }
50  }