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.api.TokenTypes;
29  
30  public class RecordComponentNameCheckTest extends AbstractModuleTestSupport {
31  
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname";
35      }
36  
37      @Test
38      public void testGetClassRequiredTokens() {
39          final RecordComponentNameCheck checkObj =
40                  new RecordComponentNameCheck();
41          final int[] expected = {TokenTypes.RECORD_COMPONENT_DEF};
42          assertWithMessage("Default required tokens are invalid")
43                  .that(checkObj.getRequiredTokens())
44                  .isEqualTo(expected);
45      }
46  
47      @Test
48      public void testRecordDefault()
49              throws Exception {
50  
51          final String pattern = "^[a-z][a-zA-Z0-9]*$";
52  
53          final String[] expected = {
54              "19:34: " + getCheckMessage(MSG_INVALID_PATTERN, "value_123", pattern),
55              "20:15: " + getCheckMessage(MSG_INVALID_PATTERN, "Values", pattern),
56              "23:35: " + getCheckMessage(MSG_INVALID_PATTERN, "_value123", pattern),
57              "24:9: " + getCheckMessage(MSG_INVALID_PATTERN, "$age", pattern),
58          };
59          verifyWithInlineConfigParser(
60                  getNonCompilablePath("InputRecordComponentNameDefault.java"), expected);
61      }
62  
63      @Test
64      public void testClassFooName()
65              throws Exception {
66  
67          final String pattern = "^[a-z0-9]+$";
68  
69          final String[] expected = {
70              "19:36: " + getCheckMessage(MSG_INVALID_PATTERN, "value_123", pattern),
71              "20:15: " + getCheckMessage(MSG_INVALID_PATTERN, "Values", pattern),
72              "23:37: " + getCheckMessage(MSG_INVALID_PATTERN, "V", pattern),
73              "24:9: " + getCheckMessage(MSG_INVALID_PATTERN, "$age", pattern),
74          };
75          verifyWithInlineConfigParser(
76                  getNonCompilablePath("InputRecordComponentNameLowercase.java"), expected);
77      }
78  
79      @Test
80      public void testGetAcceptableTokens() {
81          final RecordComponentNameCheck typeParameterNameCheckObj =
82                  new RecordComponentNameCheck();
83          final int[] actual = typeParameterNameCheckObj.getAcceptableTokens();
84          final int[] expected = {
85              TokenTypes.RECORD_COMPONENT_DEF,
86          };
87          assertWithMessage("Default acceptable tokens are invalid")
88                  .that(actual)
89                  .isEqualTo(expected);
90      }
91  }