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 com.puppycrawl.tools.checkstyle.checks.coding;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.UnusedCatchParameterShouldBeUnnamedCheck.MSG_UNUSED_CATCH_PARAMETER;
24  
25  import java.io.File;
26  import java.util.Collection;
27  import java.util.Optional;
28  
29  import org.junit.jupiter.api.Test;
30  
31  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32  import com.puppycrawl.tools.checkstyle.JavaParser;
33  import com.puppycrawl.tools.checkstyle.api.DetailAST;
34  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
36  
37  public class UnusedCatchParameterShouldBeUnnamedCheckTest extends AbstractModuleTestSupport {
38  
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/coding/unusedcatchparametershouldbeunnamed";
42      }
43  
44      @Test
45      public void testGetRequiredTokens() {
46          final UnusedCatchParameterShouldBeUnnamedCheck checkObj =
47                              new UnusedCatchParameterShouldBeUnnamedCheck();
48          final int[] expected = {
49              TokenTypes.LITERAL_CATCH,
50              TokenTypes.IDENT,
51          };
52          assertWithMessage("Default required tokens are invalid")
53              .that(checkObj.getRequiredTokens())
54              .isEqualTo(expected);
55          assertWithMessage("Default acceptable tokens are invalid")
56              .that(checkObj.getAcceptableTokens())
57              .isEqualTo(expected);
58          assertWithMessage("Default tokens are invalid")
59              .that(checkObj.getDefaultTokens())
60              .isEqualTo(expected);
61      }
62  
63      @Test
64      public void testUnusedCatchParameterShouldBeUnnamed() throws Exception {
65          final String[] expected = {
66              "17:16: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
67              "24:16: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
68              "31:16: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
69              "52:16: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
70              "65:16: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "A"),
71              "72:16: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "A"),
72              "79:16: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
73              "103:16: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
74          };
75          verifyWithInlineConfigParser(
76                  getNonCompilablePath("InputUnusedCatchParameterShouldBeUnnamed.java"),
77                  expected);
78      }
79  
80      @Test
81      public void testUnusedCatchParameterShouldBeUnnamedWithResourceAndFinally() throws Exception {
82          final String[] expected = {
83              "19:18: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
84              "38:18: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
85              "48:18: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
86              "69:18: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
87          };
88          verifyWithInlineConfigParser(
89                  getNonCompilablePath(
90                          "InputUnusedCatchParameterShouldBeUnnamedWithResourceAndFinally.java"),
91                  expected);
92      }
93  
94      @Test
95      public void testUnusedCatchParameterShouldBeUnnamedNested() throws Exception {
96          final String[] expected = {
97              "15:18: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
98              "18:22: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "ex"),
99              "31:22: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "ex"),
100             "42:18: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
101             "87:22: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "ex"),
102         };
103         verifyWithInlineConfigParser(
104                 getNonCompilablePath(
105                         "InputUnusedCatchParameterShouldBeUnnamedNested.java"),
106                 expected);
107     }
108 
109     @Test
110     public void testUnusedCatchParameterShouldBeUnnamedInsideAnonClass() throws Exception {
111         final String[] expected = {
112             "14:18: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
113             "39:28: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
114             "50:18: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
115             "57:28: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
116             "92:30: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "ex"),
117             "103:18: " + getCheckMessage(MSG_UNUSED_CATCH_PARAMETER, "e"),
118         };
119         verifyWithInlineConfigParser(
120                 getNonCompilablePath(
121                         "InputUnusedCatchParameterShouldBeUnnamedInsideAnonClass.java"),
122                 expected);
123     }
124 
125     @Test
126     public void testClearState() throws Exception {
127         final UnusedCatchParameterShouldBeUnnamedCheck check =
128                 new UnusedCatchParameterShouldBeUnnamedCheck();
129 
130         final DetailAST root = JavaParser.parseFile(
131                 new File(getNonCompilablePath(
132                         "InputUnusedCatchParameterShouldBeUnnamed.java")),
133                 JavaParser.Options.WITHOUT_COMMENTS);
134 
135         final Optional<DetailAST> literalCatch = TestUtil.findTokenInAstByPredicate(root,
136             ast -> ast.getType() == TokenTypes.LITERAL_CATCH);
137 
138         assertWithMessage("State is not cleared on beginTree")
139                 .that(TestUtil.isStatefulFieldClearedDuringBeginTree(check,
140                         literalCatch.orElseThrow(),
141                         "catchParameters",
142                         catchParameters -> {
143                             return ((Collection<?>) catchParameters).isEmpty();
144                         }))
145                 .isTrue();
146     }
147 
148 }