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.coding;
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.coding.AbstractSuperCheck;
31  import com.puppycrawl.tools.checkstyle.checks.coding.SuperFinalizeCheck;
32  
33  public class XpathRegressionSuperFinalizeTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = SuperFinalizeCheck.class.getSimpleName();
36  
37      @Override
38      protected String getCheckName() {
39          return checkName;
40      }
41  
42      @Override
43      protected String getPackageLocation() {
44          return "org/checkstyle/suppressionxpathfilter/coding/superfinalize";
45      }
46  
47      @Test
48      public void testDefault() throws Exception {
49          final File fileToProcess = new File(getPath("InputXpathSuperFinalizeNoFinalize.java"));
50          final DefaultConfiguration moduleConfig = createModuleConfig(SuperFinalizeCheck.class);
51  
52          final String[] expectedViolation = {
53              "4:17: " + getCheckMessage(SuperFinalizeCheck.class,
54                                          AbstractSuperCheck.MSG_KEY, "finalize"),
55          };
56  
57          final List<String> expectedXpathQueries = Collections.singletonList(
58              "/COMPILATION_UNIT/CLASS_DEF"
59                  + "[./IDENT[@text='InputXpathSuperFinalizeNoFinalize']]"
60                  + "/OBJBLOCK/METHOD_DEF/IDENT[@text='finalize']"
61          );
62          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
63      }
64  
65      @Test
66      public void testInnerClass() throws Exception {
67          final File fileToProcess =
68                              new File(getPath("InputXpathSuperFinalizeInnerClass.java"));
69          final DefaultConfiguration moduleConfig = createModuleConfig(SuperFinalizeCheck.class);
70  
71          final String[] expectedViolation = {
72              "5:17: " + getCheckMessage(SuperFinalizeCheck.class,
73                                          AbstractSuperCheck.MSG_KEY, "finalize"),
74          };
75  
76          final List<String> expectedXpathQueries = Collections.singletonList(
77              "/COMPILATION_UNIT/CLASS_DEF"
78                  + "[./IDENT[@text='InputXpathSuperFinalizeInnerClass']]"
79                  + "/OBJBLOCK/METHOD_DEF/IDENT[@text='finalize']"
80          );
81          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
82      }
83  
84      @Test
85      public void testAnonymousClass() throws Exception {
86          final File fileToProcess =
87                              new File(getPath("InputXpathSuperFinalizeAnonymousClass.java"));
88          final DefaultConfiguration moduleConfig = createModuleConfig(SuperFinalizeCheck.class);
89  
90          final String[] expectedViolation = {
91              "9:28: " + getCheckMessage(SuperFinalizeCheck.class,
92                                          AbstractSuperCheck.MSG_KEY, "finalize"),
93          };
94  
95          final List<String> expectedXpathQueries = Collections.singletonList(
96              "/COMPILATION_UNIT/CLASS_DEF"
97                  + "[./IDENT[@text='InputXpathSuperFinalizeAnonymousClass']]"
98                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='createAnonymousClass']]"
99                  + "/SLIST/VARIABLE_DEF[./IDENT[@text='anonymousClassObject']]"
100                 + "/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='Object']]"
101                 + "/OBJBLOCK/METHOD_DEF/IDENT[@text='finalize']"
102         );
103         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
104     }
105 }