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