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.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
23  import static com.puppycrawl.tools.checkstyle.checks.naming.TypeNameCheck.DEFAULT_PATTERN;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  
29  public class TypeNameCheckTest
30      extends AbstractModuleTestSupport {
31  
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/naming/typename";
35      }
36  
37      @Test
38      public void testSpecified()
39              throws Exception {
40          final String[] expected = {
41              "25:14: " + getCheckMessage(MSG_INVALID_PATTERN,
42                          "InputTypeName", "^inputHe"),
43          };
44          verifyWithInlineConfigParser(
45                  getPath("InputTypeName.java"), expected);
46      }
47  
48      @Test
49      public void testDefault()
50              throws Exception {
51          final String[] expected = {
52              "15:7: " + getCheckMessage(MSG_INVALID_PATTERN,
53                      "inputHeaderClass2", DEFAULT_PATTERN),
54              "17:22: " + getCheckMessage(MSG_INVALID_PATTERN,
55                      "inputHeaderInterface", DEFAULT_PATTERN),
56              "19:17: " + getCheckMessage(MSG_INVALID_PATTERN,
57                      "inputHeaderEnum", DEFAULT_PATTERN),
58              "21:23: " + getCheckMessage(MSG_INVALID_PATTERN,
59                      "inputHeaderAnnotation", DEFAULT_PATTERN),
60          };
61          verifyWithInlineConfigParser(
62                  getPath("InputTypeName2.java"), expected);
63      }
64  
65      @Test
66      public void testClassSpecific()
67              throws Exception {
68          final String[] expected = {
69              "15:7: " + getCheckMessage(MSG_INVALID_PATTERN,
70                      "inputHeaderClass3", DEFAULT_PATTERN),
71          };
72          verifyWithInlineConfigParser(
73                  getPath("InputTypeName3.java"), expected);
74      }
75  
76      @Test
77      public void testInterfaceSpecific()
78              throws Exception {
79          final String[] expected = {
80              "17:22: " + getCheckMessage(MSG_INVALID_PATTERN,
81                      "inputHeaderInterface", DEFAULT_PATTERN),
82          };
83          verifyWithInlineConfigParser(
84                  getPath("InputTypeName4.java"), expected);
85      }
86  
87      @Test
88      public void testEnumSpecific()
89              throws Exception {
90          final String[] expected = {
91              "19:17: " + getCheckMessage(MSG_INVALID_PATTERN,
92                      "inputHeaderEnum", DEFAULT_PATTERN),
93          };
94          verifyWithInlineConfigParser(
95                  getPath("InputTypeName5.java"), expected);
96      }
97  
98      @Test
99      public void testAnnotationSpecific()
100             throws Exception {
101         final String[] expected = {
102             "21:23: " + getCheckMessage(MSG_INVALID_PATTERN,
103                 "inputHeaderAnnotation", DEFAULT_PATTERN),
104         };
105         verifyWithInlineConfigParser(
106                 getPath("InputTypeName6.java"), expected);
107     }
108 
109     @Test
110     public void testTypeNameRecords() throws Exception {
111 
112         final String[] expected = {
113             "23:10: " + getCheckMessage(MSG_INVALID_PATTERN, "Third_Name", DEFAULT_PATTERN),
114             "25:11: " + getCheckMessage(MSG_INVALID_PATTERN, "FourthName_", DEFAULT_PATTERN),
115             "28:12: " + getCheckMessage(MSG_INVALID_PATTERN, "My_Record", DEFAULT_PATTERN),
116             "29:16: " + getCheckMessage(MSG_INVALID_PATTERN, "Inner__Record", DEFAULT_PATTERN),
117             "34:12: " + getCheckMessage(MSG_INVALID_PATTERN, "MyRecord__", DEFAULT_PATTERN),
118         };
119         verifyWithInlineConfigParser(
120                 getNonCompilablePath("InputTypeNameRecords.java"), expected);
121     }
122 
123 }