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