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   import org.junit.jupiter.params.ParameterizedTest;
10  import org.junit.jupiter.params.provider.ValueSource;
11  
12  /**
13   * Test case for {@link PmdValidator}'s handling of the
14   * {@code TooManyMethods} rule, which is disabled for test
15   * classes so that splitting asserts into separate {@code @Test}
16   * methods (required by {@code UnitTestContainsTooManyAsserts})
17   * does not push the class past the threshold, and which counts
18   * only public and protected methods that the class itself
19   * introduces.
20   * Regression test for https://github.com/yegor256/qulice/issues/1605,
21   * https://github.com/yegor256/qulice/issues/1647,
22   * https://github.com/yegor256/qulice/issues/1656
23   * and https://github.com/yegor256/qulice/issues/1667
24   *
25   * @since 1.0
26   */
27  final class PmdTooManyMethodsTest {
28  
29      @ParameterizedTest
30      @ValueSource(
31          strings = {
32              "ManyMethodsTest.java",
33              "ManyMethodsIT.java",
34              "ManyMethodsNestedTest.java",
35              "ManyMethodsChecks.java",
36              "ManyPublicMethodsTest.java"
37          }
38      )
39      void allowsManyMethodsInTestClass(final String file) throws Exception {
40          new PmdAssert(
41              file,
42              Matchers.any(Boolean.class),
43              Matchers.not(
44                  Matchers.containsString("TooManyMethods")
45              )
46          ).assertOk();
47      }
48  
49      @Test
50      void reportsTooManyMethodsInNonTestClass() throws Exception {
51          new PmdAssert(
52              "ManyMethods.java",
53              Matchers.is(false),
54              Matchers.containsString("TooManyMethods")
55          ).assertOk();
56      }
57  
58      @Test
59      void reportsTooManyProtectedMethods() throws Exception {
60          new PmdAssert(
61              "ManyProtectedMethods.java",
62              Matchers.is(false),
63              Matchers.containsString("TooManyMethods")
64          ).assertOk();
65      }
66  
67      @Test
68      void ignoresPrivateMethods() throws Exception {
69          new PmdAssert(
70              "ManyPrivateMethods.java",
71              Matchers.any(Boolean.class),
72              Matchers.not(
73                  Matchers.containsString("TooManyMethods")
74              )
75          ).assertOk();
76      }
77  
78      @Test
79      void ignoresOverriddenMethods() throws Exception {
80          new PmdAssert(
81              "ManyOverriddenMethods.java",
82              Matchers.any(Boolean.class),
83              Matchers.not(
84                  Matchers.containsString("TooManyMethods")
85              )
86          ).assertOk();
87      }
88  
89      @ParameterizedTest
90      @ValueSource(
91          strings = {
92              "JnaLibrary.java",
93              "JnaQualifiedLibrary.java",
94              "JnaDirectLibrary.java",
95              "StdCallLibraryMethods.java"
96          }
97      )
98      void allowsManyMethodsInJnaBinding(final String file) throws Exception {
99          new PmdAssert(
100             file,
101             Matchers.any(Boolean.class),
102             Matchers.not(
103                 Matchers.containsString("TooManyMethods")
104             )
105         ).assertOk();
106     }
107 
108     @Test
109     void reportsTooManyMethodsInPlainInterface() throws Exception {
110         new PmdAssert(
111             "ManyMethodsInterface.java",
112             Matchers.is(false),
113             Matchers.containsString("TooManyMethods")
114         ).assertOk();
115     }
116 
117     @Test
118     void reportsRedundantSuppressionInTestClass() throws Exception {
119         new PmdAssert(
120             "ManyMethodsSuppressedTest.java",
121             Matchers.is(false),
122             Matchers.containsString("UnnecessaryWarningSuppression")
123         ).assertOk();
124     }
125 }