View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.api.TokenTypes;
29  
30  public class PatternVariableNameCheckTest
31          extends AbstractModuleTestSupport {
32  
33      @Override
34      public String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/naming/patternvariablename";
36      }
37  
38      @Test
39      public void testGetAcceptableTokens() {
40          final PatternVariableNameCheck patternVariableNameCheck = new PatternVariableNameCheck();
41          final int[] expected = {TokenTypes.PATTERN_VARIABLE_DEF};
42  
43          assertWithMessage("Default acceptable tokens are invalid")
44                  .that(patternVariableNameCheck.getAcceptableTokens())
45                  .isEqualTo(expected);
46      }
47  
48      @Test
49      public void testDefault() throws Exception {
50  
51          final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
52  
53          final String[] expected = {
54              "18:39: " + getCheckMessage(MSG_INVALID_PATTERN, "OTHER", pattern),
55              "28:34: " + getCheckMessage(MSG_INVALID_PATTERN, "Count", pattern),
56              "43:36: " + getCheckMessage(MSG_INVALID_PATTERN, "S", pattern),
57              "44:42: " + getCheckMessage(MSG_INVALID_PATTERN, "STRING", pattern),
58              "48:34: " + getCheckMessage(MSG_INVALID_PATTERN, "STRING", pattern),
59              "49:43: " + getCheckMessage(MSG_INVALID_PATTERN, "STRING", pattern),
60              "62:37: " + getCheckMessage(MSG_INVALID_PATTERN, "INTEGER", pattern),
61              "68:43: " + getCheckMessage(MSG_INVALID_PATTERN, "Thing1", pattern),
62              "72:41: " + getCheckMessage(MSG_INVALID_PATTERN, "Thing2", pattern),
63          };
64          verifyWithInlineConfigParser(
65                  getPath(
66                          "InputPatternVariableNameEnhancedInstanceofTestDefault.java"),
67                  expected);
68      }
69  
70      @Test
71      public void testPatternVariableNameNoSingleChar() throws Exception {
72  
73          final String pattern = "^[a-z][a-zA-Z0-9]+$";
74  
75          final String[] expected = {
76              "18:39: " + getCheckMessage(MSG_INVALID_PATTERN, "OTHER", pattern),
77              "23:33: " + getCheckMessage(MSG_INVALID_PATTERN, "s", pattern),
78              "28:34: " + getCheckMessage(MSG_INVALID_PATTERN, "Count", pattern),
79              "43:36: " + getCheckMessage(MSG_INVALID_PATTERN, "S", pattern),
80              "44:42: " + getCheckMessage(MSG_INVALID_PATTERN, "STRING", pattern),
81              "47:34: " + getCheckMessage(MSG_INVALID_PATTERN, "STRING", pattern),
82              "48:43: " + getCheckMessage(MSG_INVALID_PATTERN, "STRING", pattern),
83              "51:57: " + getCheckMessage(MSG_INVALID_PATTERN, "s", pattern),
84              "59:48: " + getCheckMessage(MSG_INVALID_PATTERN, "a", pattern),
85              "60:39: " + getCheckMessage(MSG_INVALID_PATTERN, "x", pattern),
86              "61:43: " + getCheckMessage(MSG_INVALID_PATTERN, "y", pattern),
87              "63:37: " + getCheckMessage(MSG_INVALID_PATTERN, "INTEGER", pattern),
88              "68:43: " + getCheckMessage(MSG_INVALID_PATTERN, "Thing1", pattern),
89              "72:41: " + getCheckMessage(MSG_INVALID_PATTERN, "Thing2", pattern),
90              "77:36: " + getCheckMessage(MSG_INVALID_PATTERN, "j", pattern),
91              "78:34: " + getCheckMessage(MSG_INVALID_PATTERN, "j", pattern),
92              "79:37: " + getCheckMessage(MSG_INVALID_PATTERN, "j", pattern),
93              "86:41: " + getCheckMessage(MSG_INVALID_PATTERN, "s", pattern),
94          };
95          verifyWithInlineConfigParser(
96                  getPath(
97                          "InputPatternVariableNameEnhancedInstanceofNoSingleChar.java"),
98                  expected);
99      }
100 
101     @Test
102     public void testPatternVariableNameUnnamed() throws Exception {
103 
104         final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
105 
106         final String[] expected = {
107             "17:33: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
108             "19:33: " + getCheckMessage(MSG_INVALID_PATTERN, "_s", pattern),
109             "22:33: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
110             "29:25: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
111             "32:25: " + getCheckMessage(MSG_INVALID_PATTERN, "_s", pattern),
112             "40:67: " + getCheckMessage(MSG_INVALID_PATTERN, "_Color", pattern),
113             "45:59: " + getCheckMessage(MSG_INVALID_PATTERN, "_Color", pattern),
114             "51:76: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
115         };
116 
117         verifyWithInlineConfigParser(
118                 getNonCompilablePath(
119                         "InputPatternVariableNameUnnamed.java"),
120                 expected);
121     }
122 
123     @Test
124     public void testPatternVariableNameRecordPattern() throws Exception {
125 
126         final String pattern = "^([a-z][a-zA-Z0-9]*|_)$";
127 
128         final String[] expected = {
129             "15:36: " + getCheckMessage(MSG_INVALID_PATTERN, "XX", pattern),
130             "15:44: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
131             "20:49: " + getCheckMessage(MSG_INVALID_PATTERN, "S", pattern),
132             "25:28: " + getCheckMessage(MSG_INVALID_PATTERN, "XX", pattern),
133             "25:36: " + getCheckMessage(MSG_INVALID_PATTERN, "__", pattern),
134             "31:41: " + getCheckMessage(MSG_INVALID_PATTERN, "S", pattern),
135         };
136 
137         verifyWithInlineConfigParser(
138                 getNonCompilablePath(
139                         "InputPatternVariableNameRecordPattern.java"),
140                 expected);
141     }
142 }