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.AbstractClassNameCheck.MSG_ILLEGAL_ABSTRACT_CLASS_NAME;
23  import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractClassNameCheck.MSG_NO_ABSTRACT_CLASS_MODIFIER;
24  
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31  
32  public class AbstractClassNameCheckTest extends AbstractModuleTestSupport {
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/naming/abstractclassname";
36      }
37  
38      @Test
39      public void testIllegalAbstractClassName() throws Exception {
40          final DefaultConfiguration checkConfig =
41              createModuleConfig(AbstractClassNameCheck.class);
42          checkConfig.addAttribute("ignoreName", "false");
43          checkConfig.addAttribute("ignoreModifier", "true");
44  
45          final String pattern = "^Abstract.+$";
46  
47          final String[] expected = {
48              "3:1: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "InputAbstractClassName",
49                  pattern),
50              "6:1: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "NonAbstractClassName",
51                  pattern),
52              "10:5: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "NonAbstractInnerClass",
53                  pattern),
54          };
55  
56          verify(checkConfig, getPath("InputAbstractClassName.java"), expected);
57      }
58  
59      @Test
60      public void testCustomFormat() throws Exception {
61          final DefaultConfiguration checkConfig =
62              createModuleConfig(AbstractClassNameCheck.class);
63          checkConfig.addAttribute("ignoreName", "false");
64          checkConfig.addAttribute("ignoreModifier", "true");
65          checkConfig.addAttribute("format", "^NonAbstract.+$");
66  
67          final String[] expected = {
68              "3:1: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "InputAbstractClassName",
69                  "^NonAbstract.+$"),
70              "9:1: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "AbstractClassOther",
71                  "^NonAbstract.+$"),
72              "21:1: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "AbstractClassName2",
73                  "^NonAbstract.+$"),
74          };
75  
76          verify(checkConfig, getPath("InputAbstractClassName.java"), expected);
77      }
78  
79      @Test
80      public void testIllegalClassType() throws Exception {
81          final DefaultConfiguration checkConfig = createModuleConfig(AbstractClassNameCheck.class);
82          checkConfig.addAttribute("ignoreName", "true");
83          checkConfig.addAttribute("ignoreModifier", "false");
84  
85          final String[] expected = {
86              "18:1: " + getCheckMessage(MSG_NO_ABSTRACT_CLASS_MODIFIER, "AbstractClass"),
87              "22:5: " + getCheckMessage(MSG_NO_ABSTRACT_CLASS_MODIFIER, "AbstractInnerClass"),
88          };
89  
90          verify(checkConfig, getPath("InputAbstractClassName.java"), expected);
91      }
92  
93      @Test
94      public void testAllVariants() throws Exception {
95          final DefaultConfiguration checkConfig = createModuleConfig(AbstractClassNameCheck.class);
96          checkConfig.addAttribute("ignoreName", "false");
97          checkConfig.addAttribute("ignoreModifier", "false");
98  
99          final String pattern = "^Abstract.+$";
100 
101         final String[] expected = {
102             "3:1: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "InputAbstractClassName",
103                 pattern),
104             "6:1: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "NonAbstractClassName",
105                 pattern),
106             "10:5: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "NonAbstractInnerClass",
107                 pattern),
108             "18:1: " + getCheckMessage(MSG_NO_ABSTRACT_CLASS_MODIFIER, "AbstractClass"),
109             "22:5: " + getCheckMessage(MSG_NO_ABSTRACT_CLASS_MODIFIER, "AbstractInnerClass"),
110         };
111 
112         verify(checkConfig, getPath("InputAbstractClassName.java"), expected);
113     }
114 
115     @Test
116     public void testFalsePositive() throws Exception {
117         final DefaultConfiguration checkConfig = createModuleConfig(AbstractClassNameCheck.class);
118 
119         final String[] expected = {
120             "9:5: " + getCheckMessage(MSG_NO_ABSTRACT_CLASS_MODIFIER, "AbstractClass"),
121         };
122 
123         verify(checkConfig, getPath("InputAbstractClassNameFormerFalsePositive.java"), expected);
124     }
125 
126     @Test
127     public void testGetAcceptableTokens() {
128         final AbstractClassNameCheck classNameCheckObj = new AbstractClassNameCheck();
129         final int[] actual = classNameCheckObj.getAcceptableTokens();
130         final int[] expected = {
131             TokenTypes.CLASS_DEF,
132         };
133         Assert.assertArrayEquals("Invalid acceptable tokens", expected, actual);
134     }
135 
136     @Test
137     public void testGetRequiredTokens() {
138         final AbstractClassNameCheck classNameCheckObj = new AbstractClassNameCheck();
139         final int[] actual = classNameCheckObj.getRequiredTokens();
140         final int[] expected = {
141             TokenTypes.CLASS_DEF,
142         };
143         Assert.assertArrayEquals("Invalid required tokens", expected, actual);
144     }
145 }