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