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.coding;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.coding.UnusedCatchParameterShouldBeUnnamedCheck;
31  
32  public class XpathRegressionUnusedCatchParameterShouldBeUnnamedTest
33          extends AbstractXpathTestSupport {
34  
35      private final String checkName = UnusedCatchParameterShouldBeUnnamedCheck.class
36                                                  .getSimpleName();
37  
38      @Override
39      protected String getCheckName() {
40          return checkName;
41      }
42  
43      @Override
44      protected String getPackageLocation() {
45          return "org/checkstyle/suppressionxpathfilter/coding/unusedcatchparametershouldbeunnamed";
46      }
47  
48      @Test
49      public void testSimple() throws Exception {
50          final File fileToProcess =
51                  new File(getPath(
52                          "InputXpathUnusedCatchParameterShouldBeUnnamedSimple.java"));
53  
54          final DefaultConfiguration moduleConfig =
55                  createModuleConfig(UnusedCatchParameterShouldBeUnnamedCheck.class);
56  
57          final String[] expectedViolation = {
58              "10:16: " + getCheckMessage(UnusedCatchParameterShouldBeUnnamedCheck.class,
59                      UnusedCatchParameterShouldBeUnnamedCheck.MSG_UNUSED_CATCH_PARAMETER,
60                      "e"),
61          };
62  
63          final List<String> expectedXpathQueries = Arrays.asList(
64                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
65                          + "[@text='InputXpathUnusedCatchParameterShouldBeUnnamedSimple']]"
66                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_TRY/"
67                          + "LITERAL_CATCH/PARAMETER_DEF[./IDENT[@text='e']]",
68                  "/COMPILATION_UNIT/CLASS_DEF["
69                          + "./IDENT[@text='InputXpathUnusedCatchParameterShouldBeUnnamedSimple']]"
70                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
71                          + "LITERAL_TRY/LITERAL_CATCH/PARAMETER_DEF[./IDENT[@text='e']]/MODIFIERS",
72                  "/COMPILATION_UNIT/CLASS_DEF["
73                          + "./IDENT[@text='InputXpathUnusedCatchParameterShouldBeUnnamedSimple']]"
74                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_TRY/"
75                          + "LITERAL_CATCH/PARAMETER_DEF[./IDENT[@text='e']]"
76                          + "/TYPE[./IDENT[@text='Exception']]",
77                  "/COMPILATION_UNIT/CLASS_DEF"
78                          + "[./IDENT[@text='InputXpathUnusedCatchParameterShouldBeUnnamedSimple']]"
79                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_TRY"
80                          + "/LITERAL_CATCH/PARAMETER_DEF[./IDENT[@text='e']]"
81                          + "/TYPE/IDENT[@text='Exception']"
82          );
83  
84          runVerifications(moduleConfig, fileToProcess, expectedViolation,
85                  expectedXpathQueries);
86      }
87  
88      @Test
89      public void testNested() throws Exception {
90          final File fileToProcess =
91                  new File(getPath(
92                          "InputXpathUnusedCatchParameterShouldBeUnnamedNested.java"));
93  
94          final DefaultConfiguration moduleConfig =
95                  createModuleConfig(UnusedCatchParameterShouldBeUnnamedCheck.class);
96  
97          final String[] expectedViolation = {
98              "14:20: " + getCheckMessage(UnusedCatchParameterShouldBeUnnamedCheck.class,
99                      UnusedCatchParameterShouldBeUnnamedCheck.MSG_UNUSED_CATCH_PARAMETER,
100                     "exception"),
101         };
102 
103         final List<String> expectedXpathQueries = Arrays.asList(
104                 "/COMPILATION_UNIT/CLASS_DEF["
105                         + "./IDENT[@text='InputXpathUnusedCatchParameterShouldBeUnnamedNested']]"
106                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_TRY/"
107                         + "LITERAL_CATCH/SLIST/LITERAL_TRY/LITERAL_CATCH/PARAMETER_DEF"
108                         + "[./IDENT[@text='exception']]",
109                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
110                         + "[@text='InputXpathUnusedCatchParameterShouldBeUnnamedNested']]"
111                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_TRY"
112                         + "/LITERAL_CATCH/SLIST/LITERAL_TRY/LITERAL_CATCH/PARAMETER_DEF["
113                         + "./IDENT[@text='exception']]/MODIFIERS",
114                 "/COMPILATION_UNIT/CLASS_DEF"
115                         + "[./IDENT[@text='InputXpathUnusedCatchParameterShouldBeUnnamedNested']]"
116                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_TRY/"
117                         + "LITERAL_CATCH/SLIST/LITERAL_TRY/LITERAL_CATCH/PARAMETER_DEF"
118                         + "[./IDENT[@text='exception']]/TYPE[./IDENT[@text='Exception']]",
119                 "/COMPILATION_UNIT/CLASS_DEF"
120                         + "[./IDENT[@text='InputXpathUnusedCatchParameterShouldBeUnnamedNested']]"
121                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_TRY"
122                         + "/LITERAL_CATCH/SLIST/LITERAL_TRY/LITERAL_CATCH/PARAMETER_DEF"
123                         + "[./IDENT[@text='exception']]/TYPE/IDENT[@text='Exception']"
124         );
125         runVerifications(moduleConfig, fileToProcess, expectedViolation,
126                 expectedXpathQueries);
127     }
128 }