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.annotation;
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.annotation.MissingOverrideCheck;
31  
32  public class XpathRegressionMissingOverrideTest extends AbstractXpathTestSupport {
33  
34      private final String checkName = MissingOverrideCheck.class.getSimpleName();
35  
36      @Override
37      protected String getPackageLocation() {
38          return "org/checkstyle/suppressionxpathfilter/annotation/missingoverride";
39      }
40  
41      @Override
42      protected String getCheckName() {
43          return checkName;
44      }
45  
46      @Test
47      public void testClass() throws Exception {
48          final File fileToProcess = new File(getPath(
49                  "InputXpathMissingOverrideClass.java"));
50  
51          final DefaultConfiguration moduleConfig =
52                  createModuleConfig(MissingOverrideCheck.class);
53  
54          final String[] expectedViolation = {
55              "7:5: " + getCheckMessage(MissingOverrideCheck.class,
56                  MissingOverrideCheck.MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
57          };
58  
59          final List<String> expectedXpathQueries = Arrays.asList(
60                  "/COMPILATION_UNIT/CLASS_DEF"
61                          + "[./IDENT[@text='InputXpathMissingOverrideClass']]"
62                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]",
63                  "/COMPILATION_UNIT/CLASS_DEF"
64                          + "[./IDENT[@text='InputXpathMissingOverrideClass']]"
65                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]/MODIFIERS",
66                  "/COMPILATION_UNIT/CLASS_DEF"
67                          + "[./IDENT[@text='InputXpathMissingOverrideClass']]"
68                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]/MODIFIERS/LITERAL_PUBLIC"
69  
70          );
71  
72          runVerifications(moduleConfig, fileToProcess, expectedViolation,
73                  expectedXpathQueries);
74  
75      }
76  
77      @Test
78      public void testInterface() throws Exception {
79          final File fileToProcess = new File(getPath(
80                  "InputXpathMissingOverrideInterface.java"));
81  
82          final DefaultConfiguration moduleConfig =
83                  createModuleConfig(MissingOverrideCheck.class);
84  
85          final String[] expectedViolation = {
86              "7:5: " + getCheckMessage(MissingOverrideCheck.class,
87                  MissingOverrideCheck.MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
88          };
89  
90          final List<String> expectedXpathQueries = Arrays.asList(
91                  "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
92                          + "[@text='InputXpathMissingOverrideInterface']]"
93                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]",
94                  "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
95                          + "[@text='InputXpathMissingOverrideInterface']]"
96                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS",
97                  "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
98                          + "[@text='InputXpathMissingOverrideInterface']]"
99                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/TYPE",
100                 "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
101                         + "[@text='InputXpathMissingOverrideInterface']]"
102                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/TYPE/LITERAL_BOOLEAN"
103 
104         );
105 
106         runVerifications(moduleConfig, fileToProcess, expectedViolation,
107                 expectedXpathQueries);
108 
109     }
110 
111     @Test
112     public void testAnonymous() throws Exception {
113         final File fileToProcess = new File(getPath(
114                 "InputXpathMissingOverrideAnonymous.java"));
115 
116         final DefaultConfiguration moduleConfig =
117                 createModuleConfig(MissingOverrideCheck.class);
118 
119         final String[] expectedViolation = {
120             "8:9: " + getCheckMessage(MissingOverrideCheck.class,
121                 MissingOverrideCheck.MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
122         };
123 
124         final List<String> expectedXpathQueries = Arrays.asList(
125                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
126                         + "[@text='InputXpathMissingOverrideAnonymous']]"
127                         + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='r']]/ASSIGN/EXPR/"
128                         + "LITERAL_NEW[./IDENT[@text='Runnable']]"
129                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='run']]",
130                 "/COMPILATION_UNIT/CLASS_DEF"
131                         + "[./IDENT[@text='InputXpathMissingOverrideAnonymous']]"
132                         + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='r']]/ASSIGN/EXPR/"
133                         + "LITERAL_NEW[./IDENT[@text='Runnable']]"
134                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='run']]/MODIFIERS",
135                 "/COMPILATION_UNIT/CLASS_DEF"
136                         + "[./IDENT[@text='InputXpathMissingOverrideAnonymous']]"
137                         + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='r']]/ASSIGN/EXPR/"
138                         + "LITERAL_NEW[./IDENT[@text='Runnable']]"
139                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='run']]/MODIFIERS/LITERAL_PUBLIC"
140 
141         );
142 
143         runVerifications(moduleConfig, fileToProcess, expectedViolation,
144                 expectedXpathQueries);
145 
146     }
147 
148     @Test
149     public void testInheritDocInvalidPrivateMethod() throws Exception {
150         final File fileToProcess = new File(getPath(
151                 "InputXpathMissingOverrideInheritDocInvalidPrivateMethod.java"));
152 
153         final DefaultConfiguration moduleConfig =
154                 createModuleConfig(MissingOverrideCheck.class);
155 
156         final String[] expectedViolation = {
157             "7:5: " + getCheckMessage(MissingOverrideCheck.class,
158                 MissingOverrideCheck.MSG_KEY_TAG_NOT_VALID_ON, "{@inheritDoc}"),
159         };
160 
161         final List<String> expectedXpathQueries = Arrays.asList(
162                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
163                         + "[@text='InputXpathMissingOverrideInheritDocInvalidPrivateMethod']]"
164                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]",
165                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
166                         + "[@text='InputXpathMissingOverrideInheritDocInvalidPrivateMethod']]"
167                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS",
168                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
169                         + "[@text='InputXpathMissingOverrideInheritDocInvalidPrivateMethod']]"
170                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS/LITERAL_PRIVATE"
171 
172         );
173 
174         runVerifications(moduleConfig, fileToProcess, expectedViolation,
175                 expectedXpathQueries);
176 
177     }
178 
179     @Test
180     public void testInheritDocInvalidPublicMethod() throws Exception {
181         final File fileToProcess = new File(getPath(
182                 "InputXpathMissingOverrideInheritDocInvalidPublicMethod.java"));
183 
184         final DefaultConfiguration moduleConfig =
185                 createModuleConfig(MissingOverrideCheck.class);
186 
187         final String[] expectedViolation = {
188             "7:5: " + getCheckMessage(MissingOverrideCheck.class,
189                 MissingOverrideCheck.MSG_KEY_TAG_NOT_VALID_ON, "{@inheritDoc}"),
190         };
191 
192         final List<String> expectedXpathQueries = Arrays.asList(
193                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
194                         + "[@text='InputXpathMissingOverrideInheritDocInvalidPublicMethod']]"
195                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]",
196                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
197                         + "[@text='InputXpathMissingOverrideInheritDocInvalidPublicMethod']]"
198                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS",
199                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
200                         + "[@text='InputXpathMissingOverrideInheritDocInvalidPublicMethod']]"
201                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS/LITERAL_PUBLIC"
202 
203         );
204 
205         runVerifications(moduleConfig, fileToProcess, expectedViolation,
206                 expectedXpathQueries);
207 
208     }
209 
210     @Test
211     public void testJavaFiveCompatibilityOne() throws Exception {
212         final File fileToProcess = new File(getPath(
213                 "InputXpathMissingOverrideClass.java"));
214 
215         final DefaultConfiguration moduleConfig =
216                 createModuleConfig(MissingOverrideCheck.class);
217         moduleConfig.addProperty("javaFiveCompatibility", "true");
218 
219         final String[] expectedViolation = {
220             "7:5: " + getCheckMessage(MissingOverrideCheck.class,
221                     MissingOverrideCheck.MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
222         };
223 
224         final List<String> expectedXpathQueries = Arrays.asList(
225                 "/COMPILATION_UNIT/CLASS_DEF"
226                         + "[./IDENT[@text='InputXpathMissingOverrideClass']]"
227                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]",
228                 "/COMPILATION_UNIT/CLASS_DEF"
229                         + "[./IDENT[@text='InputXpathMissingOverrideClass']]"
230                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]/MODIFIERS",
231                 "/COMPILATION_UNIT/CLASS_DEF"
232                         + "[./IDENT[@text='InputXpathMissingOverrideClass']]"
233                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]/MODIFIERS/LITERAL_PUBLIC"
234 
235         );
236 
237         runVerifications(moduleConfig, fileToProcess, expectedViolation,
238                 expectedXpathQueries);
239 
240     }
241 
242     @Test
243     public void testJavaFiveCompatibilityTwo() throws Exception {
244         final File fileToProcess = new File(getPath(
245                 "InputXpathMissingOverrideInterface.java"));
246 
247         final DefaultConfiguration moduleConfig =
248                 createModuleConfig(MissingOverrideCheck.class);
249         moduleConfig.addProperty("javaFiveCompatibility", "true");
250 
251         final String[] expectedViolation = {
252             "7:5: " + getCheckMessage(MissingOverrideCheck.class,
253                     MissingOverrideCheck.MSG_KEY_ANNOTATION_MISSING_OVERRIDE),
254         };
255 
256         final List<String> expectedXpathQueries = Arrays.asList(
257                 "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
258                         + "[@text='InputXpathMissingOverrideInterface']]"
259                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]",
260                 "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
261                         + "[@text='InputXpathMissingOverrideInterface']]"
262                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS",
263                 "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
264                         + "[@text='InputXpathMissingOverrideInterface']]"
265                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/TYPE",
266                 "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
267                         + "[@text='InputXpathMissingOverrideInterface']]"
268                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/TYPE/LITERAL_BOOLEAN"
269 
270         );
271 
272         runVerifications(moduleConfig, fileToProcess, expectedViolation,
273                 expectedXpathQueries);
274 
275     }
276 
277 }