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