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.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.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
30  
31  public class TypeNameCheckTest
32      extends AbstractModuleTestSupport {
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/naming/typename";
36      }
37  
38      @Test
39      public void testSpecified()
40              throws Exception {
41          final DefaultConfiguration checkConfig =
42                  createModuleConfig(TypeNameCheck.class);
43          checkConfig.addAttribute("format", "^inputHe");
44          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
45          verify(checkConfig, getPath("InputTypeName.java"), expected);
46      }
47  
48      @Test
49      public void testDefault()
50              throws Exception {
51          final DefaultConfiguration checkConfig =
52                  createModuleConfig(TypeNameCheck.class);
53          final String[] expected = {
54              "3:7: " + getCheckMessage(MSG_INVALID_PATTERN,
55                      "inputHeaderClass", DEFAULT_PATTERN),
56              "5:22: " + getCheckMessage(MSG_INVALID_PATTERN,
57                      "inputHeaderInterface", DEFAULT_PATTERN),
58              "7:17: " + getCheckMessage(MSG_INVALID_PATTERN,
59                      "inputHeaderEnum", DEFAULT_PATTERN),
60              "9:23: " + getCheckMessage(MSG_INVALID_PATTERN,
61                      "inputHeaderAnnotation", DEFAULT_PATTERN),
62          };
63          verify(checkConfig, getPath("InputTypeName.java"), expected);
64      }
65  
66      @Test
67      public void testClassSpecific()
68              throws Exception {
69          final DefaultConfiguration checkConfig =
70              createModuleConfig(TypeNameCheck.class);
71          checkConfig.addAttribute("tokens", "CLASS_DEF");
72          final String[] expected = {
73              "3:7: " + getCheckMessage(MSG_INVALID_PATTERN,
74                      "inputHeaderClass", DEFAULT_PATTERN),
75          };
76          verify(checkConfig, getPath("InputTypeName.java"), expected);
77      }
78  
79      @Test
80      public void testInterfaceSpecific()
81              throws Exception {
82          final DefaultConfiguration checkConfig =
83              createModuleConfig(TypeNameCheck.class);
84          checkConfig.addAttribute("tokens", "INTERFACE_DEF");
85          final String[] expected = {
86              "5:22: " + getCheckMessage(MSG_INVALID_PATTERN,
87                      "inputHeaderInterface", DEFAULT_PATTERN),
88          };
89          verify(checkConfig, getPath("InputTypeName.java"), expected);
90      }
91  
92      @Test
93      public void testEnumSpecific()
94              throws Exception {
95          final DefaultConfiguration checkConfig =
96              createModuleConfig(TypeNameCheck.class);
97          checkConfig.addAttribute("tokens", "ENUM_DEF");
98          final String[] expected = {
99              "7:17: " + getCheckMessage(MSG_INVALID_PATTERN,
100                     "inputHeaderEnum", DEFAULT_PATTERN),
101         };
102         verify(checkConfig, getPath("InputTypeName.java"), expected);
103     }
104 
105     @Test
106     public void testAnnotationSpecific()
107             throws Exception {
108         final DefaultConfiguration checkConfig =
109             createModuleConfig(TypeNameCheck.class);
110         checkConfig.addAttribute("tokens", "ANNOTATION_DEF");
111         final String[] expected = {
112             "9:23: " + getCheckMessage(MSG_INVALID_PATTERN,
113                 "inputHeaderAnnotation", DEFAULT_PATTERN),
114         };
115         verify(checkConfig, getPath("InputTypeName.java"), expected);
116     }
117 }