View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package org.checkstyle.suppressionxpathfilter;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.sizes.MethodCountCheck;
30  
31  public class XpathRegressionMethodCountTest extends AbstractXpathTestSupport {
32  
33      @Override
34      protected String getCheckName() {
35          return MethodCountCheck.class.getSimpleName();
36      }
37  
38      @Test
39      public void testDefault() throws Exception {
40          final File fileToProcess = new File(
41              getPath("InputXpathMethodCountDefault.java")
42          );
43  
44          final DefaultConfiguration moduleConfig =
45              createModuleConfig(MethodCountCheck.class);
46          moduleConfig.addProperty("maxTotal", "1");
47  
48          final String[] expectedViolation = {
49              "3:1: " + getCheckMessage(MethodCountCheck.class,
50                  MethodCountCheck.MSG_MANY_METHODS, 2, 1),
51          };
52  
53          final List<String> expectedXpathQueries = Arrays.asList(
54                  "/COMPILATION_UNIT/CLASS_DEF"
55                          + "[./IDENT[@text='InputXpathMethodCountDefault']]",
56  
57                  "/COMPILATION_UNIT/CLASS_DEF"
58                          + "[./IDENT[@text='InputXpathMethodCountDefault']]"
59                      + "/MODIFIERS",
60  
61                  "/COMPILATION_UNIT/CLASS_DEF"
62                          + "[./IDENT[@text='InputXpathMethodCountDefault']]"
63                      + "/LITERAL_CLASS"
64          );
65  
66          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
67      }
68  
69      @Test
70      public void testPrivate() throws Exception {
71          final File fileToProcess = new File(
72              getPath("InputXpathMethodCountPrivate.java")
73          );
74  
75          final DefaultConfiguration moduleConfig =
76              createModuleConfig(MethodCountCheck.class);
77          moduleConfig.addProperty("maxPrivate", "1");
78  
79          final String[] expectedViolation = {
80              "3:1: " + getCheckMessage(MethodCountCheck.class,
81                  MethodCountCheck.MSG_PRIVATE_METHODS, 2, 1),
82          };
83  
84          final List<String> expectedXpathQueries = Arrays.asList(
85                  "/COMPILATION_UNIT/CLASS_DEF"
86                      + "[./IDENT[@text='InputXpathMethodCountPrivate']]",
87  
88                  "/COMPILATION_UNIT/CLASS_DEF"
89                      + "[./IDENT[@text='InputXpathMethodCountPrivate']]"
90                      + "/MODIFIERS",
91  
92                  "/COMPILATION_UNIT/CLASS_DEF"
93                      + "[./IDENT[@text='InputXpathMethodCountPrivate']]"
94                      + "/LITERAL_CLASS"
95          );
96  
97          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
98      }
99  
100     @Test
101     public void testPackage() throws Exception {
102         final File fileToProcess = new File(
103             getPath("InputXpathMethodCountDefault.java")
104         );
105 
106         final DefaultConfiguration moduleConfig =
107             createModuleConfig(MethodCountCheck.class);
108         moduleConfig.addProperty("maxPackage", "1");
109 
110         final String[] expectedViolation = {
111             "3:1: " + getCheckMessage(MethodCountCheck.class,
112                 MethodCountCheck.MSG_PACKAGE_METHODS, 2, 1),
113         };
114 
115         final List<String> expectedXpathQueries = Arrays.asList(
116                 "/COMPILATION_UNIT/CLASS_DEF"
117                     + "[./IDENT[@text='InputXpathMethodCountDefault']]",
118 
119                 "/COMPILATION_UNIT/CLASS_DEF"
120                     + "[./IDENT[@text='InputXpathMethodCountDefault']]"
121                     + "/MODIFIERS",
122 
123                 "/COMPILATION_UNIT/CLASS_DEF"
124                     + "[./IDENT[@text='InputXpathMethodCountDefault']]"
125                     + "/LITERAL_CLASS"
126         );
127 
128         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
129     }
130 
131     @Test
132     public void testProtected() throws Exception {
133         final File fileToProcess = new File(
134             getPath("InputXpathMethodCountProtected.java")
135         );
136 
137         final DefaultConfiguration moduleConfig =
138             createModuleConfig(MethodCountCheck.class);
139         moduleConfig.addProperty("maxProtected", "1");
140 
141         final String[] expectedViolation = {
142             "3:1: " + getCheckMessage(MethodCountCheck.class,
143                 MethodCountCheck.MSG_PROTECTED_METHODS, 2, 1),
144         };
145 
146         final List<String> expectedXpathQueries = Arrays.asList(
147                 "/COMPILATION_UNIT/CLASS_DEF"
148                     + "[./IDENT[@text='InputXpathMethodCountProtected']]",
149 
150                 "/COMPILATION_UNIT/CLASS_DEF"
151                     + "[./IDENT[@text='InputXpathMethodCountProtected']]"
152                     + "/MODIFIERS",
153 
154                 "/COMPILATION_UNIT/CLASS_DEF"
155                     + "[./IDENT[@text='InputXpathMethodCountProtected']]"
156                     + "/LITERAL_CLASS"
157         );
158 
159         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
160     }
161 
162     @Test
163     public void testPublic() throws Exception {
164         final File fileToProcess = new File(
165             getPath("InputXpathMethodCountPublic.java")
166         );
167 
168         final DefaultConfiguration moduleConfig =
169             createModuleConfig(MethodCountCheck.class);
170         moduleConfig.addProperty("maxPublic", "1");
171 
172         final String[] expectedViolation = {
173             "3:1: " + getCheckMessage(MethodCountCheck.class,
174                 MethodCountCheck.MSG_PUBLIC_METHODS, 2, 1),
175         };
176 
177         final List<String> expectedXpathQueries = Arrays.asList(
178                 "/COMPILATION_UNIT/CLASS_DEF"
179                     + "[./IDENT[@text='InputXpathMethodCountPublic']]",
180 
181                 "/COMPILATION_UNIT/CLASS_DEF"
182                     + "[./IDENT[@text='InputXpathMethodCountPublic']]"
183                     + "/MODIFIERS",
184 
185                 "/COMPILATION_UNIT/CLASS_DEF"
186                     + "[./IDENT[@text='InputXpathMethodCountPublic']]"
187                     + "/LITERAL_CLASS"
188         );
189 
190         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
191     }
192 }