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