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.google.checkstyle.test.chapter5naming.rule522typenames;
21  
22  import java.util.Map;
23  
24  import org.junit.jupiter.api.Test;
25  
26  import com.google.checkstyle.test.base.AbstractGoogleModuleTestSupport;
27  import com.puppycrawl.tools.checkstyle.api.Configuration;
28  
29  public class TypeNameTest extends AbstractGoogleModuleTestSupport {
30  
31      @Override
32      protected String getPackageLocation() {
33          return "com/google/checkstyle/test/chapter5naming/rule522typenames";
34      }
35  
36      @Test
37      public void testTypeName() throws Exception {
38          final Configuration checkConfig = getModuleConfig("TypeName");
39          final String msgKey = "name.invalidPattern";
40          final String format = "^[A-Z][a-zA-Z0-9]*$";
41          final Map<String, String> messages = checkConfig.getMessages();
42  
43          final String[] expected = {
44              "3:7: " + getCheckMessage(messages, msgKey, "inputHeaderClass", format),
45              "5:22: " + getCheckMessage(messages, msgKey, "InputHeader___Interface", format),
46              "7:17: " + getCheckMessage(messages, msgKey, "inputHeaderEnum", format),
47              "9:11: " + getCheckMessage(messages, msgKey, "NoValid$Name", format),
48              "11:11: " + getCheckMessage(messages, msgKey, "$NoValidName", format),
49              "13:11: " + getCheckMessage(messages, msgKey, "NoValidName$", format),
50              "19:7: " + getCheckMessage(messages, msgKey, "_ValidName", format),
51              "21:7: " + getCheckMessage(messages, msgKey, "Valid_Name", format),
52              "23:7: " + getCheckMessage(messages, msgKey, "ValidName_", format),
53              "27:11: " + getCheckMessage(messages, msgKey, "_Foo", format),
54              "29:11: " + getCheckMessage(messages, msgKey, "Fo_o", format),
55              "31:11: " + getCheckMessage(messages, msgKey, "Foo_", format),
56              "33:11: " + getCheckMessage(messages, msgKey, "$Foo", format),
57              "35:11: " + getCheckMessage(messages, msgKey, "Fo$o", format),
58              "37:11: " + getCheckMessage(messages, msgKey, "Foo$", format),
59              "41:6: " + getCheckMessage(messages, msgKey, "_FooEnum", format),
60              "43:6: " + getCheckMessage(messages, msgKey, "Foo_Enum", format),
61              "45:6: " + getCheckMessage(messages, msgKey, "FooEnum_", format),
62              "47:6: " + getCheckMessage(messages, msgKey, "$FooEnum", format),
63              "49:6: " + getCheckMessage(messages, msgKey, "Foo$Enum", format),
64              "51:6: " + getCheckMessage(messages, msgKey, "FooEnum$", format),
65              "53:7: " + getCheckMessage(messages, msgKey, "aaa", format),
66              "55:11: " + getCheckMessage(messages, msgKey, "bbb", format),
67              "57:6: " + getCheckMessage(messages, msgKey, "ccc", format),
68              "61:12: " + getCheckMessage(messages, msgKey, "_Annotation", format),
69              "63:12: " + getCheckMessage(messages, msgKey, "Annot_ation", format),
70              "65:12: " + getCheckMessage(messages, msgKey, "Annotation_", format),
71              "67:12: " + getCheckMessage(messages, msgKey, "$Annotation", format),
72              "69:12: " + getCheckMessage(messages, msgKey, "Annot$ation", format),
73              "71:12: " + getCheckMessage(messages, msgKey, "Annotation$", format),
74          };
75  
76          final String filePath = getPath("InputTypeName.java");
77  
78          final Integer[] warnList = getLinesWithWarn(filePath);
79          verify(checkConfig, filePath, expected, warnList);
80      }
81  
82  }