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 org.hamcrest.Matchers;
8   import org.junit.jupiter.api.Test;
9   
10  /**
11   * Test case for {@link PmdValidator}'s handling of the
12   * {@code TooManyFields} rule, which skips classes extending
13   * {@code AbstractMojo} but stays active on plain classes, and which lets
14   * {@code UnnecessaryWarningSuppression} report a suppression left on a
15   * Mojo.
16   * Regression test for https://github.com/yegor256/qulice/issues/1767
17   * and https://github.com/yegor256/qulice/issues/1775
18   *
19   * @since 1.0
20   */
21  final class PmdTooManyFieldsTest {
22  
23      @Test
24      void doesNotFireOnMojoWithManyFields() throws Exception {
25          new PmdAssert(
26              "TooManyFieldsInMojo.java",
27              Matchers.any(Boolean.class),
28              Matchers.not(
29                  Matchers.containsString("(TooManyFields)")
30              )
31          ).assertOk();
32      }
33  
34      @Test
35      void firesOnPlainClassWithManyFields() throws Exception {
36          new PmdAssert(
37              "TooManyFieldsInPlainClass.java",
38              Matchers.is(false),
39              Matchers.containsString("(TooManyFields)")
40          ).assertOk();
41      }
42  
43      @Test
44      void ignoresConstants() throws Exception {
45          new PmdAssert(
46              "TooManyFieldsInConstants.java",
47              Matchers.any(Boolean.class),
48              Matchers.not(
49                  Matchers.containsString("(TooManyFields)")
50              )
51          ).assertOk();
52      }
53  
54      @Test
55      void reportsRedundantSuppressionOnMojo() throws Exception {
56          new PmdAssert(
57              "TooManyFieldsSuppressedInMojo.java",
58              Matchers.is(false),
59              Matchers.containsString("(UnnecessaryWarningSuppression)")
60          ).assertOk();
61      }
62  
63      @Test
64      void honoursSuppressionOnPlainClass() throws Exception {
65          new PmdAssert(
66              "TooManyFieldsSuppressedInPlainClass.java",
67              Matchers.any(Boolean.class),
68              Matchers.allOf(
69                  Matchers.not(Matchers.containsString("(TooManyFields)")),
70                  Matchers.not(
71                      Matchers.containsString("(UnnecessaryWarningSuppression)")
72                  )
73              )
74          ).assertOk();
75      }
76  }