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  
31  public class MemberNameCheckTest
32      extends AbstractModuleTestSupport {
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/naming/membername";
36      }
37  
38      @Test
39      public void testGetRequiredTokens() {
40          final MemberNameCheck checkObj = new MemberNameCheck();
41          final int[] expected = {TokenTypes.VARIABLE_DEF};
42          assertArrayEquals("Default required tokens are invalid",
43              expected, checkObj.getRequiredTokens());
44      }
45  
46      @Test
47      public void testSpecified()
48              throws Exception {
49          final DefaultConfiguration checkConfig =
50              createModuleConfig(MemberNameCheck.class);
51          checkConfig.addAttribute("format", "^m[A-Z][a-zA-Z0-9]*$");
52  
53          final String pattern = "^m[A-Z][a-zA-Z0-9]*$";
54  
55          final String[] expected = {
56              "35:17: " + getCheckMessage(MSG_INVALID_PATTERN, "badMember", pattern),
57              "224:17: " + getCheckMessage(MSG_INVALID_PATTERN, "someMember", pattern),
58          };
59          verify(checkConfig, getPath("InputMemberNameSimple.java"), expected);
60      }
61  
62      @Test
63      public void testInnerClass()
64              throws Exception {
65          final DefaultConfiguration checkConfig =
66              createModuleConfig(MemberNameCheck.class);
67  
68          final String pattern = "^[a-z][a-zA-Z0-9]*$";
69  
70          final String[] expected = {
71              "56:25: " + getCheckMessage(MSG_INVALID_PATTERN, "ABC", pattern),
72          };
73          verify(checkConfig, getPath("InputMemberNameInner.java"), expected);
74      }
75  
76      @Test
77      public void testDefaults() throws Exception {
78          final DefaultConfiguration checkConfig =
79              createModuleConfig(MemberNameCheck.class);
80  
81          final String pattern = "^[a-z][a-zA-Z0-9]*$";
82  
83          final String[] expected = {
84              "10:16: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
85              "11:19: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
86              "12:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
87              "13:17: " + getCheckMessage(MSG_INVALID_PATTERN, "_private", pattern),
88          };
89          verify(checkConfig, getPath("InputMemberName.java"), expected);
90      }
91  
92      @Test
93      public void testUnderlined() throws Exception {
94          final DefaultConfiguration checkConfig =
95              createModuleConfig(MemberNameCheck.class);
96          checkConfig.addAttribute("format", "^_[a-z]*$");
97  
98          final String pattern = "^_[a-z]*$";
99  
100         final String[] expected = {
101             "5:16: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
102             "6:19: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
103             "7:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
104             "8:17: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
105         };
106         verify(checkConfig, getPath("InputMemberName.java"), expected);
107     }
108 
109     @Test
110     public void testPublicOnly() throws Exception {
111         final DefaultConfiguration checkConfig =
112             createModuleConfig(MemberNameCheck.class);
113         checkConfig.addAttribute("format", "^_[a-z]*$");
114         checkConfig.addAttribute("applyToProtected", "false");
115         checkConfig.addAttribute("applyToPackage", "false");
116         checkConfig.addAttribute("applyToPrivate", "false");
117 
118         final String pattern = "^_[a-z]*$";
119 
120         final String[] expected = {
121             "5:16: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
122         };
123         verify(checkConfig, getPath("InputMemberName.java"), expected);
124     }
125 
126     @Test
127     public void testProtectedOnly() throws Exception {
128         final DefaultConfiguration checkConfig =
129             createModuleConfig(MemberNameCheck.class);
130         checkConfig.addAttribute("format", "^_[a-z]*$");
131         checkConfig.addAttribute("applyToPublic", "false");
132         checkConfig.addAttribute("applyToPackage", "false");
133         checkConfig.addAttribute("applyToPrivate", "false");
134 
135         final String pattern = "^_[a-z]*$";
136 
137         final String[] expected = {
138             "6:19: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
139         };
140         verify(checkConfig, getPath("InputMemberName.java"), expected);
141     }
142 
143     @Test
144     public void testPackageOnly() throws Exception {
145         final DefaultConfiguration checkConfig =
146             createModuleConfig(MemberNameCheck.class);
147         checkConfig.addAttribute("format", "^_[a-z]*$");
148         checkConfig.addAttribute("applyToPublic", "false");
149         checkConfig.addAttribute("applyToProtected", "false");
150         checkConfig.addAttribute("applyToPrivate", "false");
151 
152         final String pattern = "^_[a-z]*$";
153 
154         final String[] expected = {
155             "7:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
156         };
157         verify(checkConfig, getPath("InputMemberName.java"), expected);
158     }
159 
160     @Test
161     public void testPrivateOnly() throws Exception {
162         final DefaultConfiguration checkConfig =
163             createModuleConfig(MemberNameCheck.class);
164         checkConfig.addAttribute("format", "^_[a-z]*$");
165         checkConfig.addAttribute("applyToPublic", "false");
166         checkConfig.addAttribute("applyToProtected", "false");
167         checkConfig.addAttribute("applyToPackage", "false");
168 
169         final String pattern = "^_[a-z]*$";
170 
171         final String[] expected = {
172             "8:17: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
173         };
174         verify(checkConfig, getPath("InputMemberName.java"), expected);
175     }
176 
177     @Test
178     public void testNotPrivate() throws Exception {
179         final DefaultConfiguration checkConfig =
180             createModuleConfig(MemberNameCheck.class);
181         checkConfig.addAttribute("applyToPrivate", "false");
182 
183         final String pattern = "^[a-z][a-zA-Z0-9]*$";
184 
185         final String[] expected = {
186             "10:16: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
187             "11:19: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
188             "12:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
189         };
190         verify(checkConfig, getPath("InputMemberName.java"), expected);
191     }
192 
193     @Test
194     public void memberNameExtended() throws Exception {
195         final DefaultConfiguration checkConfig =
196             createModuleConfig(MemberNameCheck.class);
197         checkConfig.addAttribute("format", "^[a-z][a-z0-9][a-zA-Z0-9]*$");
198 
199         final String pattern = "^[a-z][a-z0-9][a-zA-Z0-9]*$";
200 
201         final String[] expected = {
202             "8:16: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
203             "9:19: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
204             "10:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
205             "11:17: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
206             "13:16: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
207             "14:19: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
208             "15:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
209             "16:17: " + getCheckMessage(MSG_INVALID_PATTERN, "_private", pattern),
210             "19:20: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
211             "20:23: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
212             "21:13: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
213             "22:21: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
214             "24:20: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
215             "25:23: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
216             "26:13: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
217             "27:21: " + getCheckMessage(MSG_INVALID_PATTERN, "_private", pattern),
218             "31:20: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
219             "32:23: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
220             "33:13: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
221             "34:21: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
222             "36:20: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
223             "37:23: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
224             "38:13: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
225             "39:21: " + getCheckMessage(MSG_INVALID_PATTERN, "_private", pattern),
226             "63:16: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
227             "64:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
228             "65:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
229             "66:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
230             "68:16: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
231             "69:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
232             "70:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
233             "71:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_private", pattern),
234         };
235         verify(checkConfig, getPath("InputMemberNameExtended.java"), expected);
236     }
237 
238     @Test
239     public void testGetAcceptableTokens() {
240         final MemberNameCheck memberNameCheckObj = new MemberNameCheck();
241         final int[] actual = memberNameCheckObj.getAcceptableTokens();
242         final int[] expected = {
243             TokenTypes.VARIABLE_DEF,
244         };
245         assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
246     }
247 }