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 LocalFinalVariableNameCheckTest
33      extends AbstractModuleTestSupport {
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          assertArrayEquals(
44              "LocalFinalVariableNameCheck#getRequiredTockens should return empty array by default",
45              CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
46      }
47  
48      @Test
49      public void testDefault()
50              throws Exception {
51          final DefaultConfiguration checkConfig =
52              createModuleConfig(LocalFinalVariableNameCheck.class);
53  
54          final String pattern = "^[a-z][a-zA-Z0-9]*$";
55  
56          final String[] expected = {
57              "123:19: " + getCheckMessage(MSG_INVALID_PATTERN, "CDE", pattern),
58          };
59          verify(checkConfig, getPath("InputLocalFinalVariableName.java"), expected);
60      }
61  
62      @Test
63      public void testSet()
64              throws Exception {
65          final DefaultConfiguration checkConfig =
66              createModuleConfig(LocalFinalVariableNameCheck.class);
67          checkConfig.addAttribute("format", "[A-Z]+");
68  
69          final String pattern = "[A-Z]+";
70  
71          final String[] expected = {
72              "122:19: " + getCheckMessage(MSG_INVALID_PATTERN, "cde", pattern),
73          };
74          verify(checkConfig, getPath("InputLocalFinalVariableName.java"), expected);
75      }
76  
77      @Test
78      public void testInnerClass()
79              throws Exception {
80          final DefaultConfiguration checkConfig =
81              createModuleConfig(LocalFinalVariableNameCheck.class);
82          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
83          verify(checkConfig, getPath("InputLocalFinalVariableNameInnerClass.java"), expected);
84      }
85  
86      @Test
87      public void testGetAcceptableTokens() {
88          final LocalFinalVariableNameCheck localFinalVariableNameCheckObj =
89              new LocalFinalVariableNameCheck();
90          final int[] actual = localFinalVariableNameCheckObj.getAcceptableTokens();
91          final int[] expected = {
92              TokenTypes.VARIABLE_DEF,
93              TokenTypes.PARAMETER_DEF,
94              TokenTypes.RESOURCE,
95          };
96          assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
97      }
98  
99      @Test
100     public void testTryWithResources() throws Exception {
101         final DefaultConfiguration checkConfig =
102             createModuleConfig(LocalFinalVariableNameCheck.class);
103         checkConfig.addAttribute("format", "[A-Z]+");
104 
105         final String pattern = "[A-Z]+";
106 
107         final String[] expected = {
108             "23:30: " + getCheckMessage(MSG_INVALID_PATTERN, "br", pattern),
109             "33:29: " + getCheckMessage(MSG_INVALID_PATTERN, "br", pattern),
110             "53:22: " + getCheckMessage(MSG_INVALID_PATTERN, "zf", pattern),
111             "71:30: " + getCheckMessage(MSG_INVALID_PATTERN, "fis8859_1", pattern),
112             "73:32: " + getCheckMessage(MSG_INVALID_PATTERN, "isrutf8", pattern),
113         };
114         verify(checkConfig, getPath("InputLocalFinalVariableNameTryResources.java"), expected);
115     }
116 }