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 LocalVariableNameCheckTest
32      extends AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/naming/localvariablename";
37      }
38  
39      @Test
40      public void testGetAcceptableTokens() {
41          final LocalVariableNameCheck localVariableNameCheck = new LocalVariableNameCheck();
42          final int[] expected = {TokenTypes.VARIABLE_DEF};
43  
44          assertWithMessage("Default acceptable tokens are invalid")
45              .that(localVariableNameCheck.getAcceptableTokens())
46              .isEqualTo(expected);
47      }
48  
49      @Test
50      public void testDefaultOne()
51              throws Exception {
52  
53          final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
54  
55          final String[] expected = {
56              "39:13: " + getCheckMessage(MSG_INVALID_PATTERN, "ABC", pattern),
57              "50:18: " + getCheckMessage(MSG_INVALID_PATTERN, "I", pattern),
58              "52:20: " + getCheckMessage(MSG_INVALID_PATTERN, "InnerBlockVariable", pattern),
59          };
60          verifyWithInlineConfigParser(
61                  getPath("InputLocalVariableName1two.java"), expected);
62      }
63  
64      @Test
65      public void testDefaultTwo()
66              throws Exception {
67  
68          final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
69  
70          final String[] expected = {
71              "27:21: " + getCheckMessage(MSG_INVALID_PATTERN, "O", pattern),
72          };
73          verifyWithInlineConfigParser(
74                  getPath("InputLocalVariableName2one.java"), expected);
75      }
76  
77      @Test
78      public void testInnerClass()
79              throws Exception {
80          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
81          verifyWithInlineConfigParser(
82                  getPath("InputLocalVariableNameInnerClass.java"), expected);
83      }
84  
85      @Test
86      public void testLoopVariables()
87              throws Exception {
88  
89          final String pattern = "^[a-z]{2,}[a-zA-Z0-9]*$";
90  
91          final String[] expected = {
92              "19:29: " + getCheckMessage(MSG_INVALID_PATTERN, "j", pattern),
93              "22:17: " + getCheckMessage(MSG_INVALID_PATTERN, "A", pattern),
94              "24:21: " + getCheckMessage(MSG_INVALID_PATTERN, "i", pattern),
95              "30:17: " + getCheckMessage(MSG_INVALID_PATTERN, "Index", pattern),
96              "47:32: " + getCheckMessage(MSG_INVALID_PATTERN, "a", pattern),
97              "50:32: " + getCheckMessage(MSG_INVALID_PATTERN, "B", pattern),
98          };
99          verifyWithInlineConfigParser(
100                 getPath("InputLocalVariableNameOneCharInitVarName.java"), expected);
101     }
102 
103     @Test
104     public void testUnnamedVariables() throws Exception {
105         final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
106 
107         final String[] expected = {
108             "16:13: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
109             "17:13: " + getCheckMessage(MSG_INVALID_PATTERN, "_result", pattern),
110             "33:22: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
111         };
112         verifyWithInlineConfigParser(
113                 getNonCompilablePath("InputLocalVariableNameUnnamedVariables.java"), expected);
114 
115     }
116 
117 }