View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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 LocalVariableNameCheckTest
33      extends AbstractModuleTestSupport {
34  
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/naming/localvariablename";
38      }
39  
40      @Test
41      public void testGetAcceptableTokens() {
42          final LocalVariableNameCheck localVariableNameCheck = new LocalVariableNameCheck();
43          final int[] expected = {TokenTypes.VARIABLE_DEF};
44  
45          assertArrayEquals("Default acceptable tokens are invalid",
46                  expected, localVariableNameCheck.getAcceptableTokens());
47      }
48  
49      @Test
50      public void testDefault()
51              throws Exception {
52          final DefaultConfiguration checkConfig =
53              createModuleConfig(LocalVariableNameCheck.class);
54  
55          final String pattern = "^[a-z][a-zA-Z0-9]*$";
56  
57          final String[] expected = {
58              "119:13: " + getCheckMessage(MSG_INVALID_PATTERN, "ABC", pattern),
59              "130:18: " + getCheckMessage(MSG_INVALID_PATTERN, "I", pattern),
60              "132:20: " + getCheckMessage(MSG_INVALID_PATTERN, "InnerBlockVariable", pattern),
61              "207:21: " + getCheckMessage(MSG_INVALID_PATTERN, "O", pattern),
62          };
63          verify(checkConfig, getPath("InputLocalVariableName.java"), expected);
64      }
65  
66      @Test
67      public void testInnerClass()
68              throws Exception {
69          final DefaultConfiguration checkConfig =
70              createModuleConfig(LocalVariableNameCheck.class);
71          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
72          verify(checkConfig, getPath("InputLocalVariableNameInnerClass.java"), expected);
73      }
74  
75      @Test
76      public void testLoopVariables()
77              throws Exception {
78          final DefaultConfiguration checkConfig =
79              createModuleConfig(LocalVariableNameCheck.class);
80          checkConfig.addAttribute("format", "^[a-z]{2,}[a-zA-Z0-9]*$");
81          checkConfig.addAttribute("allowOneCharVarInForLoop", "true");
82  
83          final String pattern = "^[a-z]{2,}[a-zA-Z0-9]*$";
84  
85          final String[] expected = {
86              "19:21: " + getCheckMessage(MSG_INVALID_PATTERN, "i", pattern),
87              "25:17: " + getCheckMessage(MSG_INVALID_PATTERN, "Index", pattern),
88          };
89          verify(checkConfig, getPath("InputLocalVariableNameOneCharInitVarName.java"), expected);
90      }
91  
92  }