View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.fail;
26  
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
33  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
34  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
35  
36  public class ConstantNameCheckTest
37      extends AbstractModuleTestSupport {
38  
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/naming/constantname";
42      }
43  
44      @Test
45      public void testGetRequiredTokens() {
46          final ConstantNameCheck checkObj = new ConstantNameCheck();
47          final int[] expected = {TokenTypes.VARIABLE_DEF};
48          assertArrayEquals("Default required tokens are invalid",
49              expected, checkObj.getRequiredTokens());
50      }
51  
52      @Test
53      public void testIllegalRegexp()
54              throws Exception {
55          final DefaultConfiguration checkConfig =
56              createModuleConfig(ConstantNameCheck.class);
57          checkConfig.addAttribute("format", "\\");
58          try {
59              createChecker(checkConfig);
60              fail("CheckstyleException is expected");
61          }
62          catch (CheckstyleException ex) {
63              assertEquals("Invalid exception message", "cannot initialize module"
64                      + " com.puppycrawl.tools.checkstyle.TreeWalker - illegal value"
65                      + " '\\' for property 'format' of module"
66                      + " com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck",
67                      ex.getMessage());
68          }
69      }
70  
71      @Test
72      public void testDefault()
73              throws Exception {
74          final DefaultConfiguration checkConfig =
75              createModuleConfig(ConstantNameCheck.class);
76  
77          final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
78  
79          final String[] expected = {
80              "25:29: " + getCheckMessage(MSG_INVALID_PATTERN, "badConstant", pattern),
81              "142:30: " + getCheckMessage(MSG_INVALID_PATTERN, "BAD__NAME", pattern),
82          };
83          verify(checkConfig, getPath("InputConstantNameSimple.java"), expected);
84      }
85  
86      @Test
87      public void testAccessControlTuning()
88              throws Exception {
89          final DefaultConfiguration checkConfig =
90              createModuleConfig(ConstantNameCheck.class);
91          checkConfig.addAttribute("applyToPublic", "false");
92          checkConfig.addAttribute("applyToProtected", "false");
93          checkConfig.addAttribute("applyToPackage", "false");
94  
95          final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
96  
97          final String[] expected = {
98              "142:30: " + getCheckMessage(MSG_INVALID_PATTERN, "BAD__NAME", pattern),
99          };
100         verify(checkConfig, getPath("InputConstantNameSimple.java"), expected);
101     }
102 
103     @Test
104     public void testInterfaceAndAnnotation()
105             throws Exception {
106         final DefaultConfiguration checkConfig =
107             createModuleConfig(ConstantNameCheck.class);
108 
109         final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
110 
111         final String[] expected = {
112             "24:16: " + getCheckMessage(MSG_INVALID_PATTERN, "data", pattern),
113             "64:16: " + getCheckMessage(MSG_INVALID_PATTERN, "data", pattern),
114         };
115         verify(checkConfig, getPath("InputConstantNameInner.java"), expected);
116     }
117 
118     @Test
119     public void testDefault1()
120             throws Exception {
121         final DefaultConfiguration checkConfig =
122             createModuleConfig(ConstantNameCheck.class);
123         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
124         verify(checkConfig, getPath("InputConstantName.java"), expected);
125     }
126 
127     @Test
128     public void testIntoInterface() throws Exception {
129         final DefaultConfiguration checkConfig =
130                 createModuleConfig(ConstantNameCheck.class);
131 
132         final String pattern = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
133 
134         final String[] expected = {
135             "45:16: " + getCheckMessage(MSG_INVALID_PATTERN, "mPublic", pattern),
136             "46:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mProtected", pattern),
137             "47:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPackage", pattern),
138             "48:9: " + getCheckMessage(MSG_INVALID_PATTERN, "mPrivate", pattern),
139             "50:16: " + getCheckMessage(MSG_INVALID_PATTERN, "_public", pattern),
140             "51:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_protected", pattern),
141             "52:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_package", pattern),
142             "53:9: " + getCheckMessage(MSG_INVALID_PATTERN, "_private", pattern),
143         };
144         verify(checkConfig, getPath("InputConstantNameMemberExtended.java"), expected);
145     }
146 
147     @Test
148     public void testStaticMethodInInterface()
149             throws Exception {
150         final DefaultConfiguration checkConfig =
151                 createModuleConfig(ConstantNameCheck.class);
152         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
153         verify(checkConfig, getPath("InputConstantNameStaticModifierInInterface.java"), expected);
154     }
155 
156     @Test
157     public void testGetAcceptableTokens() {
158         final ConstantNameCheck constantNameCheckObj = new ConstantNameCheck();
159         final int[] actual = constantNameCheckObj.getAcceptableTokens();
160         final int[] expected = {
161             TokenTypes.VARIABLE_DEF,
162         };
163         Assert.assertNotNull("Default acceptable should not be null", actual);
164         assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
165     }
166 
167 }