View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.metrics;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.checks.metrics.NPathComplexityCheck;
32  
33  // -@cs[AbbreviationAsWordInName] Test should be named as its main class.
34  public class XpathRegressionNPathComplexityTest extends AbstractXpathTestSupport {
35  
36      private final String checkName = NPathComplexityCheck.class.getSimpleName();
37  
38      @Override
39      protected String getCheckName() {
40          return checkName;
41      }
42  
43      @Override
44      protected String getPackageLocation() {
45          return "org/checkstyle/suppressionxpathfilter/metrics/npathcomplexity";
46      }
47  
48      @Test
49      public void testMethod() throws Exception {
50          final File fileToProcess =
51                  new File(getPath("InputXpathNPathComplexityMethod.java"));
52  
53          final DefaultConfiguration moduleConfig =
54                  createModuleConfig(NPathComplexityCheck.class);
55          moduleConfig.addProperty("max", "0");
56  
57          final String[] expectedViolation = {
58              "4:5: " + getCheckMessage(NPathComplexityCheck.class,
59                  NPathComplexityCheck.MSG_KEY, 3, 0),
60          };
61  
62          final List<String> expectedXpathQueries = Arrays.asList(
63              "/COMPILATION_UNIT/CLASS_DEF"
64                  + "[./IDENT[@text='InputXpathNPathComplexityMethod']]/OBJBLOCK"
65                  + "/METHOD_DEF[./IDENT[@text='test']]",
66              "/COMPILATION_UNIT/CLASS_DEF"
67                  + "[./IDENT[@text='InputXpathNPathComplexityMethod']]/OBJBLOCK"
68                  + "/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS",
69              "/COMPILATION_UNIT/CLASS_DEF"
70                  + "[./IDENT[@text='InputXpathNPathComplexityMethod']]/OBJBLOCK"
71                  + "/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS/LITERAL_PUBLIC"
72          );
73  
74          runVerifications(moduleConfig, fileToProcess, expectedViolation,
75                  expectedXpathQueries);
76      }
77  
78      @Test
79      public void testStaticBlock() throws Exception {
80          final File fileToProcess =
81                  new File(getPath("InputXpathNPathComplexityStaticBlock.java"));
82  
83          final DefaultConfiguration moduleConfig =
84                  createModuleConfig(NPathComplexityCheck.class);
85          moduleConfig.addProperty("max", "0");
86  
87          final String[] expectedViolation = {
88              "4:5: " + getCheckMessage(NPathComplexityCheck.class,
89                  NPathComplexityCheck.MSG_KEY, 3, 0),
90          };
91  
92          final List<String> expectedXpathQueries = Collections.singletonList(
93              "/COMPILATION_UNIT/CLASS_DEF"
94                  + "[./IDENT[@text='InputXpathNPathComplexityStaticBlock']]"
95                  + "/OBJBLOCK/STATIC_INIT"
96          );
97  
98          runVerifications(moduleConfig, fileToProcess, expectedViolation,
99                  expectedXpathQueries);
100     }
101 }