View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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 LocalFinalVariableNameCheckTest
32      extends AbstractModuleTestSupport {
33  
34      @Override
35      public String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename";
37      }
38  
39      @Test
40      public void testGetRequiredTokens() {
41          final LocalFinalVariableNameCheck checkObj =
42              new LocalFinalVariableNameCheck();
43          assertWithMessage("LocalFinalVariableNameCheck#getRequiredTokens should return empty array "
44                  + "by default")
45              .that(checkObj.getRequiredTokens())
46              .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
47      }
48  
49      @Test
50      public void testDefault()
51              throws Exception {
52  
53          final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
54  
55          final String[] expected = {
56              "24:19: " + getCheckMessage(MSG_INVALID_PATTERN, "CDE", pattern),
57          };
58          verifyWithInlineConfigParser(
59                  getPath("InputLocalFinalVariableName.java"), expected);
60      }
61  
62      @Test
63      public void testDefaultMisc()
64              throws Exception {
65          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
66          verifyWithInlineConfigParser(getPath("InputLocalFinalVariableNameMisc.java"), expected);
67      }
68  
69      @Test
70      public void testDefaultMisc2()
71              throws Exception {
72          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
73          verifyWithInlineConfigParser(getPath("InputLocalFinalVariableNameMisc2.java"), expected);
74      }
75  
76      @Test
77      public void testDefaultMisc3()
78              throws Exception {
79          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
80          verifyWithInlineConfigParser(getPath("InputLocalFinalVariableNameMisc3.java"), expected);
81      }
82  
83      @Test
84      public void testDefaultForEachClause()
85              throws Exception {
86          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
87          verifyWithInlineConfigParser(getPath("InputLocalFinalVariableName2.java"), expected);
88      }
89  
90      @Test
91      public void testDefaultEnum()
92              throws Exception {
93          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
94          verifyWithInlineConfigParser(getPath("InputLocalFinalVariableNameEnum1.java"), expected);
95      }
96  
97      @Test
98      public void testSet()
99              throws Exception {
100 
101         final String pattern = "[A-Z]+";
102 
103         final String[] expected = {
104             "21:19: " + getCheckMessage(MSG_INVALID_PATTERN, "cde", pattern),
105         };
106         verifyWithInlineConfigParser(
107                 getPath("InputLocalFinalVariableName1.java"), expected);
108     }
109 
110     @Test
111     public void testSetMisc()
112             throws Exception {
113         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
114         verifyWithInlineConfigParser(getPath("InputLocalFinalVariableName1Misc.java"), expected);
115     }
116 
117     @Test
118     public void testSetMisc2()
119             throws Exception {
120         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
121         verifyWithInlineConfigParser(getPath("InputLocalFinalVariableName1Misc2.java"), expected);
122     }
123 
124     @Test
125     public void testSetMisc3()
126             throws Exception {
127         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
128         verifyWithInlineConfigParser(getPath("InputLocalFinalVariableName1Misc3.java"), expected);
129     }
130 
131     @Test
132     public void testSetForEachClause()
133             throws Exception {
134         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
135         verifyWithInlineConfigParser(getPath("InputLocalFinalVariableName3.java"), expected);
136     }
137 
138     @Test
139     public void testSetEnum()
140             throws Exception {
141         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
142         verifyWithInlineConfigParser(getPath("InputLocalFinalVariableNameEnum2.java"), expected);
143     }
144 
145     @Test
146     public void testInnerClass()
147             throws Exception {
148         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
149         verifyWithInlineConfigParser(
150                 getPath("InputLocalFinalVariableNameInnerClass.java"), expected);
151     }
152 
153     @Test
154     public void testGetAcceptableTokens() {
155         final LocalFinalVariableNameCheck localFinalVariableNameCheckObj =
156             new LocalFinalVariableNameCheck();
157         final int[] actual = localFinalVariableNameCheckObj.getAcceptableTokens();
158         final int[] expected = {
159             TokenTypes.VARIABLE_DEF,
160             TokenTypes.PARAMETER_DEF,
161             TokenTypes.RESOURCE,
162         };
163         assertWithMessage("Default acceptable tokens are invalid")
164             .that(actual)
165             .isEqualTo(expected);
166     }
167 
168     @Test
169     public void testTryWithResources() throws Exception {
170 
171         final String pattern = "[A-Z]+";
172 
173         final String[] expected = {
174             "32:30: " + getCheckMessage(MSG_INVALID_PATTERN, "br", pattern),
175             "43:29: " + getCheckMessage(MSG_INVALID_PATTERN, "br", pattern),
176             "63:22: " + getCheckMessage(MSG_INVALID_PATTERN, "zf", pattern),
177             "82:30: " + getCheckMessage(MSG_INVALID_PATTERN, "fis8859_1", pattern),
178             "86:32: " + getCheckMessage(MSG_INVALID_PATTERN, "streamreader", pattern),
179         };
180         verifyWithInlineConfigParser(
181                 getPath("InputLocalFinalVariableNameTryResources.java"), expected);
182     }
183 
184     @Test
185     public void testTryWithResourcesJava9() throws Exception {
186 
187         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
188         verifyWithInlineConfigParser(
189                 getPath("InputLocalFinalVariableNameTryResourcesJava9.java"), expected);
190     }
191 
192     @Test
193     public void testUnnamedVariables() throws Exception {
194         final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
195 
196         final String[] expected = {
197             "21:18: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
198             "24:32: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
199             "32:24: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
200             "34:24: " + getCheckMessage(MSG_INVALID_PATTERN, "_BAD", pattern),
201             "40:19: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
202             "41:19: " + getCheckMessage(MSG_INVALID_PATTERN, "_BAD", pattern),
203         };
204         verifyWithInlineConfigParser(
205                 getNonCompilablePath("InputLocalFinalVariableNameUnnamedVariables.java"),
206                 expected);
207     }
208 
209 }