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 com.puppycrawl.tools.checkstyle.checks.naming.MethodNameCheck.MSG_KEY;
24  import static org.junit.Assert.assertArrayEquals;
25  
26  import org.junit.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
32  
33  public class MethodNameCheckTest
34      extends AbstractModuleTestSupport {
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/naming/methodname";
38      }
39  
40      @Test
41      public void testGetRequiredTokens() {
42          final MethodNameCheck checkObj = new MethodNameCheck();
43          final int[] expected = {TokenTypes.METHOD_DEF};
44          assertArrayEquals("Default required tokens are invalid",
45              expected, checkObj.getRequiredTokens());
46      }
47  
48      @Test
49      public void testDefault()
50              throws Exception {
51          final DefaultConfiguration checkConfig =
52              createModuleConfig(MethodNameCheck.class);
53  
54          final String pattern = "^[a-z][a-zA-Z0-9]*$";
55  
56          final String[] expected = {
57              "137:10: " + getCheckMessage(MSG_INVALID_PATTERN, "ALL_UPPERCASE_METHOD", pattern),
58          };
59          verify(checkConfig, getPath("InputMethodNameSimple.java"), expected);
60      }
61  
62      @Test
63      public void testMethodEqClass() throws Exception {
64          final DefaultConfiguration checkConfig =
65              createModuleConfig(MethodNameCheck.class);
66  
67          final String pattern = "^[a-z][a-zA-Z0-9]*$";
68  
69          final String[] expected = {
70              "12:16: " + getCheckMessage(MSG_KEY, "InputMethodNameEqualClassName"),
71              "12:16: " + getCheckMessage(MSG_INVALID_PATTERN,
72                      "InputMethodNameEqualClassName", pattern),
73              "17:17: " + getCheckMessage(MSG_INVALID_PATTERN, "PRIVATEInputMethodNameEqualClassName",
74                  pattern),
75              "23:20: " + getCheckMessage(MSG_KEY, "Inner"),
76              "23:20: " + getCheckMessage(MSG_INVALID_PATTERN, "Inner", pattern),
77              "28:20: " + getCheckMessage(MSG_INVALID_PATTERN,
78                      "InputMethodNameEqualClassName", pattern),
79              "37:24: " + getCheckMessage(MSG_KEY, "InputMethodNameEqualClassName"),
80              "37:24: " + getCheckMessage(MSG_INVALID_PATTERN,
81                      "InputMethodNameEqualClassName", pattern),
82              "47:9: " + getCheckMessage(MSG_KEY, "SweetInterface"),
83              "47:9: " + getCheckMessage(MSG_INVALID_PATTERN, "SweetInterface", pattern),
84              "53:17: " + getCheckMessage(MSG_KEY, "Outer"),
85              "53:17: " + getCheckMessage(MSG_INVALID_PATTERN, "Outer", pattern),
86          };
87  
88          verify(checkConfig, getPath("InputMethodNameEqualClassName.java"), expected);
89      }
90  
91      @Test
92      public void testMethodEqClassAllow() throws Exception {
93          final DefaultConfiguration checkConfig =
94              createModuleConfig(MethodNameCheck.class);
95  
96          // allow method names and class names to equal
97          checkConfig.addAttribute("allowClassName", "true");
98  
99          final String pattern = "^[a-z][a-zA-Z0-9]*$";
100 
101         final String[] expected = {
102             "12:16: " + getCheckMessage(MSG_INVALID_PATTERN,
103                     "InputMethodNameEqualClassName", pattern),
104             "17:17: " + getCheckMessage(MSG_INVALID_PATTERN, "PRIVATEInputMethodNameEqualClassName",
105                 pattern),
106             "23:20: " + getCheckMessage(MSG_INVALID_PATTERN, "Inner", pattern),
107             "28:20: " + getCheckMessage(MSG_INVALID_PATTERN,
108                     "InputMethodNameEqualClassName", pattern),
109             "37:24: " + getCheckMessage(MSG_INVALID_PATTERN,
110                     "InputMethodNameEqualClassName", pattern),
111             "47:9: " + getCheckMessage(MSG_INVALID_PATTERN, "SweetInterface", pattern),
112             "53:17: " + getCheckMessage(MSG_INVALID_PATTERN, "Outer", pattern),
113         };
114 
115         verify(checkConfig, getPath("InputMethodNameEqualClassName.java"), expected);
116     }
117 
118     @Test
119     public void testAccessTuning() throws Exception {
120         final DefaultConfiguration checkConfig =
121             createModuleConfig(MethodNameCheck.class);
122 
123         // allow method names and class names to equal
124         checkConfig.addAttribute("allowClassName", "true");
125 
126         // allow method names and class names to equal
127         checkConfig.addAttribute("applyToPrivate", "false");
128 
129         final String pattern = "^[a-z][a-zA-Z0-9]*$";
130 
131         final String[] expected = {
132             "12:16: " + getCheckMessage(MSG_INVALID_PATTERN,
133                     "InputMethodNameEqualClassName", pattern),
134             "23:20: " + getCheckMessage(MSG_INVALID_PATTERN, "Inner", pattern),
135             "28:20: " + getCheckMessage(MSG_INVALID_PATTERN,
136                     "InputMethodNameEqualClassName", pattern),
137             "37:24: " + getCheckMessage(MSG_INVALID_PATTERN,
138                     "InputMethodNameEqualClassName", pattern),
139             "47:9: " + getCheckMessage(MSG_INVALID_PATTERN, "SweetInterface", pattern),
140             "53:17: " + getCheckMessage(MSG_INVALID_PATTERN, "Outer", pattern),
141         };
142 
143         verify(checkConfig, getPath("InputMethodNameEqualClassName.java"), expected);
144     }
145 
146     @Test
147     public void testForNpe() throws Exception {
148         final DefaultConfiguration checkConfig =
149             createModuleConfig(MethodNameCheck.class);
150 
151         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
152 
153         verify(checkConfig, getPath("InputMethodNameExtra.java"), expected);
154     }
155 
156     @Test
157     public void testOverriddenMethods() throws Exception {
158         final DefaultConfiguration checkConfig =
159             createModuleConfig(MethodNameCheck.class);
160 
161         final String pattern = "^[a-z][a-zA-Z0-9]*$";
162 
163         final String[] expected = {
164             "17:17: " + getCheckMessage(MSG_INVALID_PATTERN, "PUBLICfoo", pattern),
165             "20:20: " + getCheckMessage(MSG_INVALID_PATTERN, "PROTECTEDfoo", pattern),
166         };
167 
168         verify(checkConfig, getPath("InputMethodNameOverriddenMethods.java"), expected);
169     }
170 
171     @Test
172     public void testGetAcceptableTokens() {
173         final MethodNameCheck methodNameCheckObj = new MethodNameCheck();
174         final int[] actual = methodNameCheckObj.getAcceptableTokens();
175         final int[] expected = {
176             TokenTypes.METHOD_DEF,
177         };
178         assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
179     }
180 }