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 com.puppycrawl.tools.checkstyle.checks.naming;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  public class CatchParameterNameCheckTest extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/naming/catchparametername";
36      }
37  
38      @Test
39      public void testTokens() {
40          final CatchParameterNameCheck catchParameterNameCheck = new CatchParameterNameCheck();
41          final int[] expected = {TokenTypes.PARAMETER_DEF};
42  
43          assertWithMessage("Default required tokens are invalid")
44              .that(catchParameterNameCheck.getRequiredTokens())
45              .isEqualTo(expected);
46          assertWithMessage("Default acceptable tokens are invalid")
47              .that(catchParameterNameCheck.getAcceptableTokens())
48              .isEqualTo(expected);
49      }
50  
51      @Test
52      public void testDefaultConfigurationOnCorrectFile() throws Exception {
53          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
54  
55          verifyWithInlineConfigParser(
56                  getPath("InputCatchParameterNameSimpleOne1.java"), expected);
57      }
58  
59      @Test
60      public void testDefaultConfigurationOnCorrectFile2() throws Exception {
61          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
62  
63          verifyWithInlineConfigParser(
64                  getPath("InputCatchParameterNameSimple2.java"), expected);
65      }
66  
67      @Test
68      public void testDefaultConfigurationOnFileWithViolations() throws Exception {
69          final String defaultFormat = "^(e|t|ex|[a-z][a-z][a-zA-Z]+|_)$";
70  
71          final String[] expected = {
72              "25:28: " + getCheckMessage(MSG_INVALID_PATTERN, "exception1", defaultFormat),
73              "35:39: " + getCheckMessage(MSG_INVALID_PATTERN, "ie", defaultFormat),
74              "38:28: " + getCheckMessage(MSG_INVALID_PATTERN, "iException", defaultFormat),
75              "41:28: " + getCheckMessage(MSG_INVALID_PATTERN, "ok", defaultFormat),
76              "45:28: " + getCheckMessage(MSG_INVALID_PATTERN, "e1", defaultFormat),
77              "47:32: " + getCheckMessage(MSG_INVALID_PATTERN, "e2", defaultFormat),
78              "51:28: " + getCheckMessage(MSG_INVALID_PATTERN, "t1", defaultFormat),
79              "53:32: " + getCheckMessage(MSG_INVALID_PATTERN, "t2", defaultFormat),
80          };
81  
82          verifyWithInlineConfigParser(
83                  getPath("InputCatchParameterName.java"), expected);
84      }
85  
86      @Test
87      public void testCustomFormatFromJavadoc() throws Exception {
88  
89          final String[] expected = {
90              "13:28: " + getCheckMessage(MSG_INVALID_PATTERN, "e", "^[a-z][a-zA-Z0-9]+$"),
91              "31:28: " + getCheckMessage(MSG_INVALID_PATTERN, "t", "^[a-z][a-zA-Z0-9]+$"),
92          };
93  
94          verifyWithInlineConfigParser(
95                  getPath("InputCatchParameterName2.java"), expected);
96      }
97  
98      @Test
99      public void testCustomFormatWithNoAnchors() throws Exception {
100 
101         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
102 
103         verifyWithInlineConfigParser(
104                 getPath("InputCatchParameterName3.java"), expected);
105     }
106 
107     @Test
108     public void testCatchParameterNameUnnamed() throws Exception {
109         final String defaultFormat = "^(e|t|ex|[a-z][a-z][a-zA-Z]+|_)$";
110 
111         final String[] expected = {
112             "18:28: " + getCheckMessage(MSG_INVALID_PATTERN, "__", defaultFormat),
113             "24:28: " + getCheckMessage(MSG_INVALID_PATTERN, "_BAD", defaultFormat),
114             "27:28: " + getCheckMessage(MSG_INVALID_PATTERN, "BAD__", defaultFormat),
115         };
116 
117         verifyWithInlineConfigParser(
118                 getNonCompilablePath("InputCatchParameterNameUnnamed.java"), expected);
119     }
120 }