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