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.google.checkstyle.test.chapter5naming.rule527localvariablenames;
21  
22  import org.junit.Test;
23  
24  import com.google.checkstyle.test.base.AbstractModuleTestSupport;
25  import com.puppycrawl.tools.checkstyle.api.Configuration;
26  
27  public class LocalVariableNameTest extends AbstractModuleTestSupport {
28  
29      private static final String MSG_KEY = "name.invalidPattern";
30  
31      @Override
32      protected String getPackageLocation() {
33          return "com/google/checkstyle/test/chapter5naming/rule527localvariablenames";
34      }
35  
36      @Test
37      public void testLocalVariableName() throws Exception {
38  
39          final Configuration checkConfig = getModuleConfig("LocalVariableName");
40          final String format = checkConfig.getAttribute("format");
41          final String[] expected = {
42              "27:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aA", format),
43              "28:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "a1_a", format),
44              "29:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "A_A", format),
45              "30:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aa2_a", format),
46              "31:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "_a", format),
47              "32:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "_aa", format),
48              "33:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aa_", format),
49              "34:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aaa$aaa", format),
50              "35:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "$aaaaaa", format),
51              "36:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "aaaaaa$", format),
52          };
53  
54          final String filePath = getPath("InputLocalVariableNameSimple.java");
55  
56          final Integer[] warnList = getLinesWithWarn(filePath);
57          verify(checkConfig, filePath, expected, warnList);
58      }
59  
60      @Test
61      public void testOneChar() throws Exception {
62  
63          final Configuration checkConfig = getModuleConfig("LocalVariableName");
64          final String format = checkConfig.getAttribute("format");
65          final String[] expected = {
66              "21:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "I_ndex", format),
67              "45:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "i_ndex", format),
68              "49:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "ii_i1", format),
69              "53:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "$index", format),
70              "57:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "in$dex", format),
71              "61:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "index$", format),
72          };
73  
74          final String filePath = getPath("InputLocalVariableNameOneCharVarName.java");
75  
76          final Integer[] warnList = getLinesWithWarn(filePath);
77          verify(checkConfig, filePath, expected, warnList);
78      }
79  }