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