View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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      protected 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              "126:19: " + getCheckMessage(MSG_INVALID_PATTERN, "CDE", pattern),
57          };
58          verifyWithInlineConfigParser(
59                  getPath("InputLocalFinalVariableName.java"), expected);
60      }
61  
62      @Test
63      public void testSet()
64              throws Exception {
65  
66          final String pattern = "[A-Z]+";
67  
68          final String[] expected = {
69              "125:19: " + getCheckMessage(MSG_INVALID_PATTERN, "cde", pattern),
70          };
71          verifyWithInlineConfigParser(
72                  getPath("InputLocalFinalVariableName1.java"), expected);
73      }
74  
75      @Test
76      public void testInnerClass()
77              throws Exception {
78          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
79          verifyWithInlineConfigParser(
80                  getPath("InputLocalFinalVariableNameInnerClass.java"), expected);
81      }
82  
83      @Test
84      public void testGetAcceptableTokens() {
85          final LocalFinalVariableNameCheck localFinalVariableNameCheckObj =
86              new LocalFinalVariableNameCheck();
87          final int[] actual = localFinalVariableNameCheckObj.getAcceptableTokens();
88          final int[] expected = {
89              TokenTypes.VARIABLE_DEF,
90              TokenTypes.PARAMETER_DEF,
91              TokenTypes.RESOURCE,
92          };
93          assertWithMessage("Default acceptable tokens are invalid")
94              .that(actual)
95              .isEqualTo(expected);
96      }
97  
98      @Test
99      public void testTryWithResources() throws Exception {
100 
101         final String pattern = "[A-Z]+";
102 
103         final String[] expected = {
104             "31:30: " + getCheckMessage(MSG_INVALID_PATTERN, "br", pattern),
105             "41:29: " + getCheckMessage(MSG_INVALID_PATTERN, "br", pattern),
106             "61:22: " + getCheckMessage(MSG_INVALID_PATTERN, "zf", pattern),
107             "79:30: " + getCheckMessage(MSG_INVALID_PATTERN, "fis8859_1", pattern),
108             "82:32: " + getCheckMessage(MSG_INVALID_PATTERN, "isrutf8", pattern),
109         };
110         verifyWithInlineConfigParser(
111                 getPath("InputLocalFinalVariableNameTryResources.java"), expected);
112     }
113 
114     @Test
115     public void testTryWithResourcesJava9() throws Exception {
116 
117         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
118         verifyWithInlineConfigParser(
119                 getPath("InputLocalFinalVariableNameTryResourcesJava9.java"), expected);
120     }
121 
122     @Test
123     public void testUnnamedVariables() throws Exception {
124         final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
125 
126         final String[] expected = {
127             "18:18: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
128             "21:32: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
129             "29:24: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
130             "31:24: " + getCheckMessage(MSG_INVALID_PATTERN, "_BAD", pattern),
131             "37:19: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
132             "38:19: " + getCheckMessage(MSG_INVALID_PATTERN, "_BAD", pattern),
133         };
134         verifyWithInlineConfigParser(
135                 getNonCompilablePath("InputLocalFinalVariableNameUnnamedVariables.java"),
136                 expected);
137     }
138 
139 }