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.JavaNCSSCheck;
30  
31  // -@cs[AbbreviationAsWordInName] Test should be named as its main class.
32  public class XpathRegressionJavaNCSSTest extends AbstractXpathTestSupport {
33  
34      private final String checkName = JavaNCSSCheck.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/javancss";
44      }
45  
46      @Test
47      public void testOne() throws Exception {
48          final File fileToProcess =
49                  new File(getPath("InputXpathJavaNCSSOne.java"));
50  
51          final DefaultConfiguration moduleConfig =
52                  createModuleConfig(JavaNCSSCheck.class);
53  
54          final String[] expectedViolation = {
55              "5:5: " + getCheckMessage(JavaNCSSCheck.class,
56                      JavaNCSSCheck.MSG_METHOD, 51, 50),
57          };
58  
59          final List<String> expectedXpathQueries = List.of(
60                  "/COMPILATION_UNIT/CLASS_DEF"
61                  + "[./IDENT[@text='InputXpathJavaNCSSOne']]"
62                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]",
63  
64                  "/COMPILATION_UNIT/CLASS_DEF"
65                  + "[./IDENT[@text='InputXpathJavaNCSSOne']]"
66                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS",
67  
68                  "/COMPILATION_UNIT/CLASS_DEF"
69                  + "[./IDENT[@text='InputXpathJavaNCSSOne']]"
70                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS/LITERAL_PUBLIC"
71          );
72  
73          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
74      }
75  
76      @Test
77      public void testTwo() throws Exception {
78          final File fileToProcess =
79                  new File(getPath("InputXpathJavaNCSSTwo.java"));
80  
81          final DefaultConfiguration moduleConfig =
82                  createModuleConfig(JavaNCSSCheck.class);
83  
84          moduleConfig.addProperty("classMaximum", "50");
85  
86          final String[] expectedViolation = {
87              "3:1: " + getCheckMessage(JavaNCSSCheck.class,
88                      JavaNCSSCheck.MSG_CLASS, 51, 50),
89          };
90  
91          final List<String> expectedXpathQueries = List.of(
92                  "/COMPILATION_UNIT/CLASS_DEF"
93                  + "[./IDENT[@text='InputXpathJavaNCSSTwo']]",
94  
95                  "/COMPILATION_UNIT/CLASS_DEF"
96                  + "[./IDENT[@text='InputXpathJavaNCSSTwo']]/MODIFIERS",
97  
98                  "/COMPILATION_UNIT/CLASS_DEF"
99                  + "[./IDENT[@text='InputXpathJavaNCSSTwo']]"
100                 + "/MODIFIERS/LITERAL_PUBLIC"
101         );
102 
103         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
104     }
105 
106     @Test
107     public void testThree() throws Exception {
108         final File fileToProcess =
109                 new File(getPath("InputXpathJavaNCSSThree.java"));
110 
111         final DefaultConfiguration moduleConfig =
112                 createModuleConfig(JavaNCSSCheck.class);
113 
114         moduleConfig.addProperty("fileMaximum", "50");
115 
116         final String[] expectedViolation = {
117             "1:1: " + getCheckMessage(JavaNCSSCheck.class,
118                     JavaNCSSCheck.MSG_FILE, 51, 50),
119         };
120 
121         final List<String> expectedXpathQueries = List.of(
122                 "/COMPILATION_UNIT",
123                 "/COMPILATION_UNIT/PACKAGE_DEF"
124         );
125 
126         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
127     }
128 }