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