View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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 org.checkstyle.suppressionxpathfilter.naming;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.naming.AbstractClassNameCheck;
31  
32  public class XpathRegressionAbstractClassNameTest extends AbstractXpathTestSupport {
33  
34      private final String checkName = AbstractClassNameCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Override
42      protected String getPackageLocation() {
43          return "org/checkstyle/suppressionxpathfilter/naming/abstractclassname";
44      }
45  
46      @Test
47      public void testClassNameTop() throws Exception {
48          final File fileToProcess =
49                  new File(getPath("InputXpathAbstractClassNameTop.java"));
50  
51          final DefaultConfiguration moduleConfig =
52                  createModuleConfig(AbstractClassNameCheck.class);
53  
54          final String[] expectedViolation = {
55              "3:1: " + getCheckMessage(AbstractClassNameCheck.class,
56                  AbstractClassNameCheck.MSG_ILLEGAL_ABSTRACT_CLASS_NAME,
57                      "InputXpathAbstractClassNameTop", "^Abstract.+$"),
58          };
59  
60          final List<String> expectedXpathQueries = Arrays.asList(
61                  "/COMPILATION_UNIT/CLASS_DEF"
62                          + "[./IDENT[@text='InputXpathAbstractClassNameTop']]",
63                  "/COMPILATION_UNIT/CLASS_DEF"
64                          + "[./IDENT[@text='InputXpathAbstractClassNameTop']]"
65                          + "/MODIFIERS",
66                  "/COMPILATION_UNIT/CLASS_DEF"
67                          + "[./IDENT[@text='InputXpathAbstractClassNameTop']]"
68                          + "/MODIFIERS/LITERAL_PUBLIC"
69          );
70  
71          runVerifications(moduleConfig, fileToProcess, expectedViolation,
72                  expectedXpathQueries);
73      }
74  
75      @Test
76      public void testClassNameInner() throws Exception {
77          final File fileToProcess =
78                  new File(getPath("InputXpathAbstractClassNameInner.java"));
79  
80          final DefaultConfiguration moduleConfig =
81                  createModuleConfig(AbstractClassNameCheck.class);
82  
83          final String[] expectedViolation = {
84              "4:5: " + getCheckMessage(AbstractClassNameCheck.class,
85                  AbstractClassNameCheck.MSG_ILLEGAL_ABSTRACT_CLASS_NAME,
86                      "MyClass", "^Abstract.+$"),
87          };
88  
89          final List<String> expectedXpathQueries = Arrays.asList(
90                  "/COMPILATION_UNIT/CLASS_DEF"
91                          + "[./IDENT[@text='InputXpathAbstractClassNameInner']]"
92                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='MyClass']]",
93                  "/COMPILATION_UNIT/CLASS_DEF"
94                          + "[./IDENT[@text='InputXpathAbstractClassNameInner']]"
95                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='MyClass']]/MODIFIERS",
96                  "/COMPILATION_UNIT/CLASS_DEF"
97                          + "[./IDENT[@text='InputXpathAbstractClassNameInner']]"
98                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='MyClass']]/MODIFIERS/ABSTRACT"
99          );
100 
101         runVerifications(moduleConfig, fileToProcess, expectedViolation,
102                 expectedXpathQueries);
103     }
104 
105     @Test
106     public void testClassNameNoModifier() throws Exception {
107         final File fileToProcess =
108                 new File(getPath("InputXpathAbstractClassNameNoModifier.java"));
109 
110         final DefaultConfiguration moduleConfig =
111                 createModuleConfig(AbstractClassNameCheck.class);
112 
113         final String[] expectedViolation = {
114             "4:5: " + getCheckMessage(AbstractClassNameCheck.class,
115                 AbstractClassNameCheck.MSG_NO_ABSTRACT_CLASS_MODIFIER,
116                     "AbstractMyClass"),
117         };
118 
119         final List<String> expectedXpathQueries = Arrays.asList(
120                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
121                         + "InputXpathAbstractClassNameNoModifier']]"
122                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='AbstractMyClass']]",
123                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
124                         + "InputXpathAbstractClassNameNoModifier']]"
125                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='AbstractMyClass']]/MODIFIERS",
126                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
127                         + "InputXpathAbstractClassNameNoModifier']]"
128                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='AbstractMyClass']]/LITERAL_CLASS"
129         );
130 
131         runVerifications(moduleConfig, fileToProcess, expectedViolation,
132                 expectedXpathQueries);
133     }
134 
135 }