View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
23  import static org.junit.Assert.assertArrayEquals;
24  
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.api.Configuration;
30  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
32  
33  public class CatchParameterNameCheckTest extends AbstractModuleTestSupport {
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/naming/catchparametername";
37      }
38  
39      @Test
40      public void testTokens() {
41          final CatchParameterNameCheck catchParameterNameCheck = new CatchParameterNameCheck();
42          final int[] expected = {TokenTypes.PARAMETER_DEF};
43  
44          assertArrayEquals("Default required tokens are invalid",
45              expected, catchParameterNameCheck.getRequiredTokens());
46          assertArrayEquals("Default acceptable tokens are invalid",
47              expected, catchParameterNameCheck.getAcceptableTokens());
48      }
49  
50      @Test
51      public void testDefaultConfigurationOnCorrectFile() throws Exception {
52          final Configuration checkConfig = createModuleConfig(CatchParameterNameCheck.class);
53          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
54  
55          verify(checkConfig, getPath("InputCatchParameterNameSimple.java"), expected);
56      }
57  
58      @Test
59      public void testDefaultConfigurationOnFileWithViolations() throws Exception {
60          final Configuration checkConfig = createModuleConfig(CatchParameterNameCheck.class);
61          final String defaultFormat = "^(e|t|ex|[a-z][a-z][a-zA-Z]+)$";
62  
63          final String[] expected = {
64              "18:28: " + getCheckMessage(MSG_INVALID_PATTERN, "exception1", defaultFormat),
65              "28:39: " + getCheckMessage(MSG_INVALID_PATTERN, "ie", defaultFormat),
66              "31:28: " + getCheckMessage(MSG_INVALID_PATTERN, "iException", defaultFormat),
67              "34:28: " + getCheckMessage(MSG_INVALID_PATTERN, "ok", defaultFormat),
68              "38:28: " + getCheckMessage(MSG_INVALID_PATTERN, "e1", defaultFormat),
69              "40:32: " + getCheckMessage(MSG_INVALID_PATTERN, "e2", defaultFormat),
70              "44:28: " + getCheckMessage(MSG_INVALID_PATTERN, "t1", defaultFormat),
71              "46:32: " + getCheckMessage(MSG_INVALID_PATTERN, "t2", defaultFormat),
72          };
73  
74          verify(checkConfig, getPath("InputCatchParameterName.java"), expected);
75      }
76  
77      @Test
78      public void testCustomFormatFromJavadoc() throws Exception {
79          final DefaultConfiguration checkConfig = createModuleConfig(CatchParameterNameCheck.class);
80          checkConfig.addAttribute("format", "^[a-z][a-zA-Z0-9]+$");
81  
82          final String[] expected = {
83              "6:28: " + getCheckMessage(MSG_INVALID_PATTERN, "e", "^[a-z][a-zA-Z0-9]+$"),
84              "24:28: " + getCheckMessage(MSG_INVALID_PATTERN, "t", "^[a-z][a-zA-Z0-9]+$"),
85          };
86  
87          verify(checkConfig, getPath("InputCatchParameterName.java"), expected);
88      }
89  
90      @Test
91      public void testCustomFormatWithNoAnchors() throws Exception {
92          final DefaultConfiguration checkConfig = createModuleConfig(CatchParameterNameCheck.class);
93          checkConfig.addAttribute("format", "[a-z]");
94  
95          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
96  
97          verify(checkConfig, getPath("InputCatchParameterName.java"), expected);
98      }
99  }