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;
21  
22  import java.io.File;
23  import java.util.List;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
28  import com.puppycrawl.tools.checkstyle.checks.metrics.ClassFanOutComplexityCheck;
29  
30  public class XpathRegressionClassFanOutComplexityTest extends AbstractXpathTestSupport {
31  
32      private final String checkName = ClassFanOutComplexityCheck.class.getSimpleName();
33  
34      @Override
35      protected String getCheckName() {
36          return checkName;
37      }
38  
39      @Test
40      public void testInClass() throws Exception {
41          final File fileToProcess = new File(
42                  getPath("InputXpathClassFanOutComplexityClass.java")
43          );
44          final DefaultConfiguration moduleConfig =
45                  createModuleConfig(ClassFanOutComplexityCheck.class);
46          moduleConfig.addProperty("max", "0");
47  
48          final String[] expectedViolation = {
49              "6:1: " + getCheckMessage(ClassFanOutComplexityCheck.class,
50              ClassFanOutComplexityCheck.MSG_KEY, 4, 0),
51          };
52  
53          final List<String> expectedXpathQueries = List.of(
54              "/COMPILATION_UNIT/CLASS_DEF"
55              + "[./IDENT[@text='InputXpathClassFanOutComplexityClass']]",
56              "/COMPILATION_UNIT/CLASS_DEF"
57              + "[./IDENT[@text='InputXpathClassFanOutComplexityClass']]/MODIFIERS",
58              "/COMPILATION_UNIT/CLASS_DEF"
59              + "[./IDENT[@text='InputXpathClassFanOutComplexityClass']]/MODIFIERS/LITERAL_PUBLIC"
60  
61          );
62          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
63      }
64  
65      @Test
66      public void testInInterface() throws Exception {
67          final File fileToProcess = new File(
68                  getPath("InputXpathClassFanOutComplexityInterface.java")
69          );
70          final DefaultConfiguration moduleConfig =
71                  createModuleConfig(ClassFanOutComplexityCheck.class);
72          moduleConfig.addProperty("max", "0");
73  
74          final String[] expectedViolation = {
75              "7:1: " + getCheckMessage(ClassFanOutComplexityCheck.class,
76              ClassFanOutComplexityCheck.MSG_KEY, 1, 0),
77          };
78  
79          final List<String> expectedXpathQueries = List.of(
80              "/COMPILATION_UNIT/INTERFACE_DEF"
81              + "[./IDENT[@text='BadInterface']]",
82              "/COMPILATION_UNIT/INTERFACE_DEF"
83              + "[./IDENT[@text='BadInterface']]/MODIFIERS",
84              "/COMPILATION_UNIT/INTERFACE_DEF"
85              + "[./IDENT[@text='BadInterface']]/LITERAL_INTERFACE"
86          );
87          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
88      }
89  
90      @Test
91      public void testInEnum() throws Exception {
92          final File fileToProcess = new File(
93                  getPath("InputXpathClassFanOutComplexityEnum.java")
94          );
95          final DefaultConfiguration moduleConfig =
96                  createModuleConfig(ClassFanOutComplexityCheck.class);
97          moduleConfig.addProperty("max", "0");
98  
99          final String[] expectedViolation = {
100             "9:1: " + getCheckMessage(ClassFanOutComplexityCheck.class,
101             ClassFanOutComplexityCheck.MSG_KEY, 1, 0),
102         };
103 
104         final List<String> expectedXpathQueries = List.of(
105             "/COMPILATION_UNIT/ENUM_DEF"
106             + "[./IDENT[@text='MyEnum']]",
107             "/COMPILATION_UNIT/ENUM_DEF"
108             + "[./IDENT[@text='MyEnum']]/MODIFIERS",
109             "/COMPILATION_UNIT/ENUM_DEF"
110             + "[./IDENT[@text='MyEnum']]/ENUM"
111         );
112         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
113     }
114 }