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 static com.puppycrawl.tools.checkstyle.checks.sizes.ExecutableStatementCountCheck.MSG_KEY;
23  
24  import java.io.File;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.checks.sizes.ExecutableStatementCountCheck;
32  
33  public class XpathRegressionExecutableStatementCountTest extends AbstractXpathTestSupport {
34  
35      @Override
36      protected String getCheckName() {
37          return ExecutableStatementCountCheck.class.getSimpleName();
38      }
39  
40      @Test
41      public void testDefaultConfig() throws Exception {
42          final String filePath =
43                  getPath("InputXpathExecutableStatementCountDefault.java");
44          final File fileToProcess = new File(filePath);
45  
46          final DefaultConfiguration moduleConfig =
47                  createModuleConfig(ExecutableStatementCountCheck.class);
48  
49          moduleConfig.addProperty("max", "0");
50  
51          final String[] expectedViolations = {
52              "4:5: " + getCheckMessage(ExecutableStatementCountCheck.class, MSG_KEY, 3, 0),
53          };
54  
55          final List<String> expectedXpathQueries = Arrays.asList(
56              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
57              + "@text='InputXpathExecutableStatementCountDefault']]"
58              + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='ElseIfLadder']]",
59              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
60              + "@text='InputXpathExecutableStatementCountDefault']]"
61              + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='ElseIfLadder']]"
62              + "/MODIFIERS",
63              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
64              + "@text='InputXpathExecutableStatementCountDefault']]"
65              + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='ElseIfLadder']]"
66              + "/MODIFIERS/LITERAL_PUBLIC"
67          );
68  
69          runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
70  
71      }
72  
73      @Test
74      public void testCustomMax() throws Exception {
75          final String filePath =
76                  getPath("InputXpathExecutableStatementCountCustomMax.java");
77          final File fileToProcess = new File(filePath);
78  
79          final DefaultConfiguration moduleConfig =
80                  createModuleConfig(ExecutableStatementCountCheck.class);
81  
82          moduleConfig.addProperty("max", "0");
83          moduleConfig.addProperty("tokens", "CTOR_DEF");
84  
85          final String[] expectedViolations = {
86              "4:5: " + getCheckMessage(ExecutableStatementCountCheck.class, MSG_KEY, 2, 0),
87          };
88  
89          final List<String> expectedXpathQueries = Arrays.asList(
90              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
91              + "@text='InputXpathExecutableStatementCountCustomMax']]"
92              + "/OBJBLOCK/CTOR_DEF[./IDENT["
93              + "@text='InputXpathExecutableStatementCountCustomMax']]",
94              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
95              + "@text='InputXpathExecutableStatementCountCustomMax']]"
96              + "/OBJBLOCK/CTOR_DEF[./IDENT["
97              + "@text='InputXpathExecutableStatementCountCustomMax']]"
98              + "/MODIFIERS",
99              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
100             + "@text='InputXpathExecutableStatementCountCustomMax']]"
101             + "/OBJBLOCK/CTOR_DEF[./IDENT["
102             + "@text='InputXpathExecutableStatementCountCustomMax']]"
103             + "/MODIFIERS/LITERAL_PUBLIC"
104         );
105 
106         runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
107     }
108 
109     @Test
110     public void testLambdas() throws Exception {
111         final String filePath =
112                 getPath("InputXpathExecutableStatementCountLambdas.java");
113         final File fileToProcess = new File(filePath);
114 
115         final DefaultConfiguration moduleConfig =
116                 createModuleConfig(ExecutableStatementCountCheck.class);
117 
118         moduleConfig.addProperty("max", "1");
119         moduleConfig.addProperty("tokens", "LAMBDA");
120 
121         final String[] expectedViolations = {
122             "7:22: " + getCheckMessage(ExecutableStatementCountCheck.class, MSG_KEY, 2, 1),
123         };
124 
125         final List<String> expectedXpathQueries = List.of(
126                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
127                         + "[@text='InputXpathExecutableStatementCountLambdas']]"
128                         + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='c']]/ASSIGN/LAMBDA"
129         );
130 
131         runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
132     }
133 
134 }