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.rule525nonconstantfieldnames;
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 MemberNameTest 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/rule525nonconstantfieldnames";
34      }
35  
36      @Test
37      public void testMemberName() throws Exception {
38  
39          final Configuration checkConfig = getModuleConfig("MemberName");
40          final String format = checkConfig.getAttribute("format");
41          final String[] expected = {
42              "5:16: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mPublic", format),
43              "6:19: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mProtected", format),
44              "7:9: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mPackage", format),
45              "8:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mPrivate", format),
46              "10:16: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "_public", format),
47              "11:19: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "prot_ected", format),
48              "12:9: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "package_", format),
49              "13:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "priva$te", format),
50              "20:9: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "ABC", format),
51              "21:15: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "C_D_E", format),
52              "23:16: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "$mPublic", format),
53              "24:19: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mPro$tected", format),
54              "25:9: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mPackage$", format),
55          };
56  
57          final String filePath = getPath("InputMemberNameBasic.java");
58  
59          final Integer[] warnList = getLinesWithWarn(filePath);
60          verify(checkConfig, filePath, expected, warnList);
61      }
62  
63      @Test
64      public void testSimple() throws Exception {
65  
66          final Configuration checkConfig = getModuleConfig("MemberName");
67          final String format = checkConfig.getAttribute("format");
68          final String[] expected = {
69              "12:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "bad$Static", format),
70              "17:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "bad_Member", format),
71              "19:17: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "m", format),
72              "21:19: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "m_M", format),
73              "24:19: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "m$nts", format),
74              "35:9: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mTest1", format),
75              "37:16: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mTest2", format),
76              "39:16: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "$mTest2", format),
77              "41:16: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mTes$t2", format),
78              "43:16: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mTest2$", format),
79              "77:21: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "bad$Static", format),
80              "79:22: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "sum_Created", format),
81              "82:21: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "bad_Member", format),
82              "84:21: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "m", format),
83              "86:23: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "m_M", format),
84              "89:23: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "m$nts", format),
85              "93:13: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mTest1", format),
86              "95:20: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mTest2", format),
87              "97:20: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "$mTest2", format),
88              "99:20: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mTes$t2", format),
89              "101:20: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mTest2$", format),
90              "107:25: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "bad$Static", format),
91              "109:25: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "sum_Created", format),
92              "112:25: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "bad_Member", format),
93              "114:25: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "m", format),
94              "116:25: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "m_M", format),
95              "119:27: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "m$nts", format),
96              "123:25: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mTest1", format),
97              "125:25: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mTest2", format),
98              "127:25: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "$mTest2", format),
99              "129:25: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mTes$t2", format),
100             "131:25: " + getCheckMessage(checkConfig.getMessages(), MSG_KEY, "mTest2$", format),
101         };
102 
103         final String filePath = getPath("InputMemberNameSimple.java");
104 
105         final Integer[] warnList = getLinesWithWarn(filePath);
106         verify(checkConfig, filePath, expected, warnList);
107     }
108 }