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.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
31  
32  public class ParameterNameCheckTest
33      extends AbstractModuleTestSupport {
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/naming/parametername";
37      }
38  
39      @Test
40      public void testGetRequiredTokens() {
41          final ParameterNameCheck checkObj = new ParameterNameCheck();
42          final int[] expected = {TokenTypes.PARAMETER_DEF};
43          assertArrayEquals("Default required tokens are invalid",
44              expected, checkObj.getRequiredTokens());
45      }
46  
47      @Test
48      public void testCatch()
49              throws Exception {
50          final DefaultConfiguration checkConfig =
51              createModuleConfig(ParameterNameCheck.class);
52          checkConfig.addAttribute("format", "^NO_WAY_MATEY$");
53          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
54          verify(checkConfig, getPath("InputParameterNameCatchOnly.java"), expected);
55      }
56  
57      @Test
58      public void testSpecified()
59              throws Exception {
60          final DefaultConfiguration checkConfig =
61              createModuleConfig(ParameterNameCheck.class);
62          checkConfig.addAttribute("format", "^a[A-Z][a-zA-Z0-9]*$");
63  
64          final String pattern = "^a[A-Z][a-zA-Z0-9]*$";
65  
66          final String[] expected = {
67              "71:19: " + getCheckMessage(MSG_INVALID_PATTERN, "badFormat1", pattern),
68              "71:34: " + getCheckMessage(MSG_INVALID_PATTERN, "badFormat2", pattern),
69              "72:25: " + getCheckMessage(MSG_INVALID_PATTERN, "badFormat3", pattern),
70          };
71          verify(checkConfig, getPath("InputParameterName.java"), expected);
72      }
73  
74      @Test
75      public void testDefault()
76              throws Exception {
77          final DefaultConfiguration checkConfig =
78              createModuleConfig(ParameterNameCheck.class);
79          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
80          verify(checkConfig, getPath("InputParameterName.java"), expected);
81      }
82  
83      @Test
84      public void testGetAcceptableTokens() {
85          final ParameterNameCheck parameterNameCheckObj = new ParameterNameCheck();
86          final int[] actual = parameterNameCheckObj.getAcceptableTokens();
87          final int[] expected = {
88              TokenTypes.PARAMETER_DEF,
89          };
90          assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
91      }
92  
93      @Test
94      public void testSkipMethodsWithOverrideAnnotationTrue()
95              throws Exception {
96          final DefaultConfiguration checkConfig =
97              createModuleConfig(ParameterNameCheck.class);
98          checkConfig.addAttribute("format", "^h$");
99          checkConfig.addAttribute("ignoreOverridden", "true");
100 
101         final String pattern = "^h$";
102 
103         final String[] expected = {
104             "11:28: " + getCheckMessage(MSG_INVALID_PATTERN, "object", pattern),
105             "15:30: " + getCheckMessage(MSG_INVALID_PATTERN, "aaaa", pattern),
106             "19:19: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern),
107             "19:28: " + getCheckMessage(MSG_INVALID_PATTERN, "bd", pattern),
108             "21:18: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern),
109             "28:46: " + getCheckMessage(MSG_INVALID_PATTERN, "field", pattern),
110             "28:75: " + getCheckMessage(MSG_INVALID_PATTERN, "packageNames", pattern),
111             };
112         verify(checkConfig, getPath("InputParameterNameOverrideAnnotation.java"), expected);
113     }
114 
115     @Test
116     public void testSkipMethodsWithOverrideAnnotationFalse()
117             throws Exception {
118         final DefaultConfiguration checkConfig =
119             createModuleConfig(ParameterNameCheck.class);
120         checkConfig.addAttribute("format", "^h$");
121         checkConfig.addAttribute("ignoreOverridden", "false");
122 
123         final String pattern = "^h$";
124 
125         final String[] expected = {
126             "6:34: " + getCheckMessage(MSG_INVALID_PATTERN, "o", pattern),
127             "11:28: " + getCheckMessage(MSG_INVALID_PATTERN, "object", pattern),
128             "15:30: " + getCheckMessage(MSG_INVALID_PATTERN, "aaaa", pattern),
129             "19:19: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern),
130             "19:28: " + getCheckMessage(MSG_INVALID_PATTERN, "bd", pattern),
131             "21:18: " + getCheckMessage(MSG_INVALID_PATTERN, "abc", pattern),
132             "28:46: " + getCheckMessage(MSG_INVALID_PATTERN, "field", pattern),
133             "28:75: " + getCheckMessage(MSG_INVALID_PATTERN, "packageNames", pattern),
134             };
135         verify(checkConfig, getPath("InputParameterNameOverrideAnnotation.java"), expected);
136     }
137 
138     @Test
139     public void testPublicAccessModifier()
140             throws Exception {
141         final DefaultConfiguration checkConfig =
142             createModuleConfig(ParameterNameCheck.class);
143         checkConfig.addAttribute("format", "^h$");
144         checkConfig.addAttribute("accessModifiers", AccessModifier.PUBLIC.toString());
145 
146         final String pattern = "^h$";
147 
148         final String[] expected = {
149             "5:49: " + getCheckMessage(MSG_INVALID_PATTERN, "pubconstr", pattern),
150             "9:31: " + getCheckMessage(MSG_INVALID_PATTERN, "inner", pattern),
151             "19:24: " + getCheckMessage(MSG_INVALID_PATTERN, "pubpub", pattern),
152             "30:21: " + getCheckMessage(MSG_INVALID_PATTERN, "pubifc", pattern),
153             "44:24: " + getCheckMessage(MSG_INVALID_PATTERN, "packpub", pattern),
154             "60:21: " + getCheckMessage(MSG_INVALID_PATTERN, "packifc", pattern),
155             };
156         verify(checkConfig, getPath("InputParameterNameAccessModifier.java"), expected);
157     }
158 
159     @Test
160     public void testIsOverriddenNoNullPointerException()
161             throws Exception {
162         final DefaultConfiguration checkConfig = createModuleConfig(ParameterNameCheck.class);
163         checkConfig.addAttribute("format", "^[a-z][a-zA-Z0-9]*$");
164         checkConfig.addAttribute("ignoreOverridden", "true");
165         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
166         verify(checkConfig, getPath("InputParameterNameOverrideAnnotationNoNPE.java"), expected);
167     }
168 
169     @Test
170     public void testReceiverParameter() throws Exception {
171         final DefaultConfiguration checkConfig = createModuleConfig(ParameterNameCheck.class);
172         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
173         verify(checkConfig, getPath("InputParameterNameReceiver.java"), expected);
174     }
175 }