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.Collections;
24  import java.util.List;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.design.ThrowsCountCheck;
30  
31  public class XpathRegressionThrowsCountTest extends AbstractXpathTestSupport {
32  
33      private final String checkName = ThrowsCountCheck.class.getSimpleName();
34  
35      @Override
36      protected String getCheckName() {
37          return checkName;
38      }
39  
40      @Test
41      public void testDefault() throws Exception {
42          final File fileToProcess =
43                  new File(getPath("InputXpathThrowsCountDefault.java"));
44          final DefaultConfiguration moduleConfig =
45                  createModuleConfig(ThrowsCountCheck.class);
46          final String[] expectedViolation = {
47              "4:30: " + getCheckMessage(ThrowsCountCheck.class,
48                          ThrowsCountCheck.MSG_KEY, 5, 4),
49          };
50          final List<String> expectedXpathQueries = Collections.singletonList(
51                  "/COMPILATION_UNIT"
52                      + "/CLASS_DEF[./IDENT[@text='InputXpathThrowsCountDefault']]"
53                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunction']]"
54                      + "/LITERAL_THROWS[./IDENT[@text='CloneNotSupportedException']]"
55  
56          );
57  
58          runVerifications(moduleConfig, fileToProcess, expectedViolation,
59                  expectedXpathQueries);
60      }
61  
62      @Test
63      public void testCustomMax() throws Exception {
64          final File fileToProcess =
65                  new File(getPath("InputXpathThrowsCountCustomMax.java"));
66          final DefaultConfiguration moduleConfig =
67                  createModuleConfig(ThrowsCountCheck.class);
68  
69          moduleConfig.addProperty("max", "2");
70  
71          final String[] expectedViolation = {
72              "4:30: " + getCheckMessage(ThrowsCountCheck.class,
73                          ThrowsCountCheck.MSG_KEY, 3, 2),
74          };
75          final List<String> expectedXpathQueries = Collections.singletonList(
76                  "/COMPILATION_UNIT"
77                      + "/INTERFACE_DEF[./IDENT[@text='InputXpathThrowsCountCustomMax']]"
78                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunction']]"
79                      + "/LITERAL_THROWS[./IDENT[@text='IllegalStateException']]"
80  
81          );
82  
83          runVerifications(moduleConfig, fileToProcess, expectedViolation,
84                  expectedXpathQueries);
85      }
86  
87      @Test
88      public void testPrivateMethods() throws Exception {
89          final File fileToProcess =
90                  new File(getPath("InputXpathThrowsCountPrivateMethods.java"));
91          final DefaultConfiguration moduleConfig =
92                  createModuleConfig(ThrowsCountCheck.class);
93  
94          moduleConfig.addProperty("ignorePrivateMethods", "false");
95  
96          final String[] expectedViolation = {
97              "9:40: " + getCheckMessage(ThrowsCountCheck.class,
98                          ThrowsCountCheck.MSG_KEY, 5, 4),
99          };
100         final List<String> expectedXpathQueries = Collections.singletonList(
101                 "/COMPILATION_UNIT"
102                     + "/CLASS_DEF[./IDENT[@text='InputXpathThrowsCountPrivateMethods']]"
103                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunc']]"
104                     + "/SLIST/VARIABLE_DEF[./IDENT[@text='foo']]"
105                     + "/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='myClass']]"
106                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='privateFunc']]"
107                     + "/LITERAL_THROWS[./IDENT[@text='CloneNotSupportedException']]"
108 
109         );
110 
111         runVerifications(moduleConfig, fileToProcess, expectedViolation,
112                 expectedXpathQueries);
113     }
114 }