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.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractClassNameCheck.MSG_ILLEGAL_ABSTRACT_CLASS_NAME;
24  import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractClassNameCheck.MSG_NO_ABSTRACT_CLASS_MODIFIER;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  
31  public class AbstractClassNameCheckTest extends AbstractModuleTestSupport {
32  
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  
41          final String pattern = "^Abstract.+$";
42  
43          final String[] expected = {
44              "13:1: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "InputAbstractClassName",
45                  pattern),
46              "17:1: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "NonAbstractClassName",
47                  pattern),
48              "22:5: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "NonAbstractInnerClass",
49                  pattern),
50          };
51  
52          verifyWithInlineConfigParser(
53                  getPath("InputAbstractClassName.java"), expected);
54      }
55  
56      @Test
57      public void testCustomFormat() throws Exception {
58  
59          final String[] expected = {
60              "13:1: "
61                  + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "InputAbstractClassNameCustom",
62                  "^NonAbstract.+$"),
63              "20:1: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "AbstractClassOtherCustom",
64                  "^NonAbstract.+$"),
65              "32:1: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "AbstractClassName2Custom",
66                  "^NonAbstract.+$"),
67          };
68  
69          verifyWithInlineConfigParser(
70                  getPath("InputAbstractClassNameCustom.java"), expected);
71      }
72  
73      @Test
74      public void testIllegalClassType() throws Exception {
75  
76          final String[] expected = {
77              "18:1: " + getCheckMessage(MSG_NO_ABSTRACT_CLASS_MODIFIER, "AbstractClassType"),
78              "27:1: " + getCheckMessage(MSG_NO_ABSTRACT_CLASS_MODIFIER, "AbstractClassTypes"),
79          };
80  
81          verifyWithInlineConfigParser(
82                  getPath("InputAbstractClassNameType.java"), expected);
83      }
84  
85      @Test
86      public void testAllVariants() throws Exception {
87  
88          final String pattern = "^Abstract.+$";
89  
90          final String[] expected = {
91              "13:1: "
92                  + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "InputAbstractClassNameVariants",
93                  pattern),
94              "17:1: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "NonAbstractClassNameVa",
95                  pattern),
96              "22:5: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "NonAbstractInnerClassVa",
97                  pattern),
98              "30:1: " + getCheckMessage(MSG_NO_ABSTRACT_CLASS_MODIFIER, "AbstractClassVa"),
99              "35:5: " + getCheckMessage(MSG_NO_ABSTRACT_CLASS_MODIFIER, "AbstractInnerClassVa"),
100         };
101 
102         verifyWithInlineConfigParser(
103                 getPath("InputAbstractClassNameVariants.java"), expected);
104     }
105 
106     @Test
107     public void testFalsePositive() throws Exception {
108         final String pattern = "^Abstract.+$";
109         final String[] expected = {
110             "13:1: "
111                 + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME,
112                         "InputAbstractClassNameFormerFalsePositive",
113                 pattern),
114             "21:5: " + getCheckMessage(MSG_ILLEGAL_ABSTRACT_CLASS_NAME, "NonAbstractInnerClassFP",
115                 pattern),
116             "29:1: " + getCheckMessage(MSG_NO_ABSTRACT_CLASS_MODIFIER, "AbstractClassNameFP"),
117             "34:5: " + getCheckMessage(MSG_NO_ABSTRACT_CLASS_MODIFIER, "AbstractInnerClassFP"),
118         };
119 
120         verifyWithInlineConfigParser(
121                 getPath("InputAbstractClassNameFormerFalsePositive.java"), expected);
122     }
123 
124     @Test
125     public void testGetAcceptableTokens() {
126         final AbstractClassNameCheck classNameCheckObj = new AbstractClassNameCheck();
127         final int[] actual = classNameCheckObj.getAcceptableTokens();
128         final int[] expected = {
129             TokenTypes.CLASS_DEF,
130         };
131         assertWithMessage("Invalid acceptable tokens")
132             .that(actual)
133             .isEqualTo(expected);
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         assertWithMessage("Invalid required tokens")
144             .that(actual)
145             .isEqualTo(expected);
146     }
147 
148 }