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.List;
25  
26  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.metrics.CyclomaticComplexityCheck;
31  
32  public class XpathRegressionCyclomaticComplexityTest extends AbstractXpathTestSupport {
33  
34      private final String checkName = CyclomaticComplexityCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Override
42      protected String getPackageLocation() {
43          return "org/checkstyle/suppressionxpathfilter/metrics/cyclomaticcomplexity";
44      }
45  
46      @Test
47      public void testConditionals() throws Exception {
48  
49          final File fileToProcess =
50                  new File(getPath("InputXpathCyclomaticComplexityConditionals.java"));
51  
52          final DefaultConfiguration moduleConfig =
53                  createModuleConfig(CyclomaticComplexityCheck.class);
54          moduleConfig.addProperty("max", "0");
55  
56          final String[] expectedViolation = {
57              "4:5: " + getCheckMessage(CyclomaticComplexityCheck.class,
58                      CyclomaticComplexityCheck.MSG_KEY, 2, 0),
59          };
60  
61          final List<String> expectedXpathQueries = Arrays.asList(
62              "/COMPILATION_UNIT/CLASS_DEF"
63                  + "[./IDENT[@text='InputXpathCyclomaticComplexityConditionals']]"
64                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]",
65              "/COMPILATION_UNIT/CLASS_DEF"
66                  + "[./IDENT[@text='InputXpathCyclomaticComplexityConditionals']]"
67                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS",
68              "/COMPILATION_UNIT/CLASS_DEF"
69                  + "[./IDENT[@text='InputXpathCyclomaticComplexityConditionals']]"
70                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS/LITERAL_PUBLIC"
71                  );
72  
73          runVerifications(moduleConfig, fileToProcess, expectedViolation,
74                  expectedXpathQueries);
75      }
76  
77      @Test
78      public void testSwitchBlock() throws Exception {
79          final File fileToProcess =
80                  new File(getPath("InputXpathCyclomaticComplexitySwitchBlock.java"));
81  
82          final DefaultConfiguration moduleConfig =
83                  createModuleConfig(CyclomaticComplexityCheck.class);
84          moduleConfig.addProperty("max", "0");
85  
86          final String[] expectedViolation = {
87              "6:5: " + getCheckMessage(CyclomaticComplexityCheck.class,
88                      CyclomaticComplexityCheck.MSG_KEY, 5, 0),
89          };
90  
91          final List<String> expectedXpathQueries = Arrays.asList(
92              "/COMPILATION_UNIT/CLASS_DEF"
93                      + "[./IDENT[@text='InputXpathCyclomaticComplexitySwitchBlock']]"
94                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo2']]",
95              "/COMPILATION_UNIT/CLASS_DEF"
96                      + "[./IDENT[@text='InputXpathCyclomaticComplexitySwitchBlock']]"
97                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo2']]/MODIFIERS",
98              "/COMPILATION_UNIT/CLASS_DEF"
99                      + "[./IDENT[@text='InputXpathCyclomaticComplexitySwitchBlock']]"
100                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo2']]/MODIFIERS/LITERAL_PUBLIC"
101         );
102 
103         runVerifications(moduleConfig, fileToProcess, expectedViolation,
104                 expectedXpathQueries);
105     }
106 }