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.Collections;
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.AbbreviationAsWordInNameCheck;
30  
31  public class XpathRegressionAbbreviationAsWordInNameTest extends AbstractXpathTestSupport {
32  
33      private final String checkName = AbbreviationAsWordInNameCheck.class.getSimpleName();
34  
35      @Override
36      protected String getCheckName() {
37          return checkName;
38      }
39  
40      @Test
41      public void testAnnotation() throws Exception {
42          final File fileToProcess = new File(getPath(
43                  "InputXpathAbbreviationAsWordInNameAnnotation.java"));
44  
45          final DefaultConfiguration moduleConfig =
46                  createModuleConfig(AbbreviationAsWordInNameCheck.class);
47  
48          final String[] expectedViolation = {
49              "5:16: " + getCheckMessage(AbbreviationAsWordInNameCheck.class,
50                  AbbreviationAsWordInNameCheck.MSG_KEY, "ANNOTATION", 4),
51          };
52  
53          final List<String> expectedXpathQueries = Collections.singletonList(
54                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
55                      + "@text='InputXpathAbbreviationAsWordInNameAnnotation']]"
56                      + "/OBJBLOCK/ANNOTATION_DEF/IDENT[@text='ANNOTATION']"
57          );
58  
59          runVerifications(moduleConfig, fileToProcess, expectedViolation,
60                  expectedXpathQueries);
61      }
62  
63      @Test
64      public void testAnnotationField() throws Exception {
65          final File fileToProcess = new File(getPath(
66                  "InputXpathAbbreviationAsWordInNameAnnotationField.java"));
67  
68          final DefaultConfiguration moduleConfig =
69                  createModuleConfig(AbbreviationAsWordInNameCheck.class);
70  
71          final String[] expectedViolation = {
72              "5:12: " + getCheckMessage(AbbreviationAsWordInNameCheck.class,
73                  AbbreviationAsWordInNameCheck.MSG_KEY, "ANNOTATION_FIELD", 4),
74          };
75  
76          final List<String> expectedXpathQueries = Collections.singletonList(
77                  "/COMPILATION_UNIT/ANNOTATION_DEF[./IDENT["
78                      + "@text='InputXpathAbbreviationAsWordInNameAnnotationField']]"
79                      + "/OBJBLOCK/ANNOTATION_FIELD_DEF/IDENT[@text='ANNOTATION_FIELD']"
80          );
81  
82          runVerifications(moduleConfig, fileToProcess, expectedViolation,
83                  expectedXpathQueries);
84      }
85  
86      @Test
87      public void testClass() throws Exception {
88          final File fileToProcess = new File(getPath(
89                  "InputXpathAbbreviationAsWordInNameClass.java"));
90  
91          final DefaultConfiguration moduleConfig =
92                  createModuleConfig(AbbreviationAsWordInNameCheck.class);
93  
94          final String[] expectedViolation = {
95              "5:11: " + getCheckMessage(AbbreviationAsWordInNameCheck.class,
96                  AbbreviationAsWordInNameCheck.MSG_KEY, "CLASS", 4),
97          };
98  
99          final List<String> expectedXpathQueries = Collections.singletonList(
100                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
101                     + "@text='InputXpathAbbreviationAsWordInNameClass']]"
102                     + "/OBJBLOCK/CLASS_DEF/IDENT[@text='CLASS']"
103         );
104 
105         runVerifications(moduleConfig, fileToProcess, expectedViolation,
106                 expectedXpathQueries);
107     }
108 
109     @Test
110     public void testEnum() throws Exception {
111         final File fileToProcess = new File(getPath(
112                 "InputXpathAbbreviationAsWordInNameEnum.java"));
113 
114         final DefaultConfiguration moduleConfig =
115                 createModuleConfig(AbbreviationAsWordInNameCheck.class);
116 
117         final String[] expectedViolation = {
118             "5:10: " + getCheckMessage(AbbreviationAsWordInNameCheck.class,
119                 AbbreviationAsWordInNameCheck.MSG_KEY, "ENUMERATION", 4),
120         };
121 
122         final List<String> expectedXpathQueries = Collections.singletonList(
123                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
124                     + "@text='InputXpathAbbreviationAsWordInNameEnum']]"
125                     + "/OBJBLOCK/ENUM_DEF/IDENT[@text='ENUMERATION']"
126         );
127 
128         runVerifications(moduleConfig, fileToProcess, expectedViolation,
129                 expectedXpathQueries);
130     }
131 
132     @Test
133     public void testField() throws Exception {
134         final File fileToProcess = new File(getPath(
135                 "InputXpathAbbreviationAsWordInNameField.java"));
136 
137         final DefaultConfiguration moduleConfig =
138                 createModuleConfig(AbbreviationAsWordInNameCheck.class);
139 
140         final String[] expectedViolation = {
141             "5:9: " + getCheckMessage(AbbreviationAsWordInNameCheck.class,
142                 AbbreviationAsWordInNameCheck.MSG_KEY, "FIELD", 4),
143         };
144 
145         final List<String> expectedXpathQueries = Collections.singletonList(
146                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
147                     + "@text='InputXpathAbbreviationAsWordInNameField']]"
148                     + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='FIELD']"
149         );
150 
151         runVerifications(moduleConfig, fileToProcess, expectedViolation,
152                 expectedXpathQueries);
153     }
154 
155     @Test
156     public void testInterface() throws Exception {
157         final File fileToProcess = new File(getPath(
158                 "InputXpathAbbreviationAsWordInNameInterface.java"));
159 
160         final DefaultConfiguration moduleConfig =
161                 createModuleConfig(AbbreviationAsWordInNameCheck.class);
162 
163         final String[] expectedViolation = {
164             "5:15: " + getCheckMessage(AbbreviationAsWordInNameCheck.class,
165                 AbbreviationAsWordInNameCheck.MSG_KEY, "INTERFACE", 4),
166         };
167 
168         final List<String> expectedXpathQueries = Collections.singletonList(
169                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
170                     + "@text='InputXpathAbbreviationAsWordInNameInterface']]"
171                     + "/OBJBLOCK/INTERFACE_DEF/IDENT[@text='INTERFACE']"
172         );
173 
174         runVerifications(moduleConfig, fileToProcess, expectedViolation,
175                 expectedXpathQueries);
176     }
177 
178     @Test
179     public void testMethod() throws Exception {
180         final File fileToProcess = new File(getPath(
181                 "InputXpathAbbreviationAsWordInNameMethod.java"));
182 
183         final DefaultConfiguration moduleConfig =
184                 createModuleConfig(AbbreviationAsWordInNameCheck.class);
185 
186         final String[] expectedViolation = {
187             "5:10: " + getCheckMessage(AbbreviationAsWordInNameCheck.class,
188                 AbbreviationAsWordInNameCheck.MSG_KEY, "METHOD", 4),
189         };
190 
191         final List<String> expectedXpathQueries = Collections.singletonList(
192                 "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT["
193                     + "@text='InputXpathAbbreviationAsWordInNameMethod']]"
194                     + "/OBJBLOCK/METHOD_DEF/IDENT[@text='METHOD']"
195         );
196 
197         runVerifications(moduleConfig, fileToProcess, expectedViolation,
198                 expectedXpathQueries);
199     }
200 
201     @Test
202     public void testParameter() throws Exception {
203         final File fileToProcess = new File(getPath(
204                 "InputXpathAbbreviationAsWordInNameParameter.java"));
205 
206         final DefaultConfiguration moduleConfig =
207                 createModuleConfig(AbbreviationAsWordInNameCheck.class);
208 
209         final String[] expectedViolation = {
210             "5:21: " + getCheckMessage(AbbreviationAsWordInNameCheck.class,
211                 AbbreviationAsWordInNameCheck.MSG_KEY, "PARAMETER", 4),
212         };
213 
214         final List<String> expectedXpathQueries = Collections.singletonList(
215                 "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT["
216                     + "@text='InputXpathAbbreviationAsWordInNameParameter']]"
217                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
218                     + "/PARAMETERS/PARAMETER_DEF/IDENT[@text='PARAMETER']"
219         );
220 
221         runVerifications(moduleConfig, fileToProcess, expectedViolation,
222                 expectedXpathQueries);
223     }
224 
225     @Test
226     public void testVariable() throws Exception {
227         final File fileToProcess = new File(getPath(
228                 "InputXpathAbbreviationAsWordInNameVariable.java"));
229 
230         final DefaultConfiguration moduleConfig =
231                 createModuleConfig(AbbreviationAsWordInNameCheck.class);
232 
233         final String[] expectedViolation = {
234             "6:13: " + getCheckMessage(AbbreviationAsWordInNameCheck.class,
235                 AbbreviationAsWordInNameCheck.MSG_KEY, "VARIABLE", 4),
236         };
237 
238         final List<String> expectedXpathQueries = Collections.singletonList(
239                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
240                     + "@text='InputXpathAbbreviationAsWordInNameVariable']]"
241                     + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
242                     + "/SLIST/VARIABLE_DEF/IDENT[@text='VARIABLE']"
243         );
244 
245         runVerifications(moduleConfig, fileToProcess, expectedViolation,
246                 expectedXpathQueries);
247     }
248 
249 }