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