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.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.coding.NoFinalizerCheck;
31  
32  public class XpathRegressionNoFinalizerTest extends AbstractXpathTestSupport {
33  
34      @Override
35      protected String getCheckName() {
36          return NoFinalizerCheck.class.getSimpleName();
37      }
38  
39      @Override
40      protected String getPackageLocation() {
41          return "org/checkstyle/suppressionxpathfilter/coding/nofinalizer";
42      }
43  
44      @Test
45      public void testMain() throws Exception {
46          final File fileToProcess = new File(
47                  getPath("InputXpathNoFinalizerMain.java"));
48  
49          final DefaultConfiguration moduleConfig =
50                  createModuleConfig(NoFinalizerCheck.class);
51  
52          final String[] expectedViolation = {
53              "8:5: " + getCheckMessage(NoFinalizerCheck.class,
54                      NoFinalizerCheck.MSG_KEY),
55          };
56  
57          final List<String> expectedXpathQueries = Arrays.asList(
58                  "/COMPILATION_UNIT/CLASS_DEF"
59                  + "[./IDENT[@text='InputXpathNoFinalizerMain']]"
60                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='finalize']]",
61                  "/COMPILATION_UNIT/CLASS_DEF"
62                  + "[./IDENT[@text='InputXpathNoFinalizerMain']]"
63                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS",
64                  "/COMPILATION_UNIT/CLASS_DEF"
65                  + "[./IDENT[@text='InputXpathNoFinalizerMain']]"
66                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS/LITERAL_PROTECTED"
67          );
68  
69          runVerifications(moduleConfig, fileToProcess, expectedViolation,
70                  expectedXpathQueries);
71      }
72  
73      @Test
74      public void testInner() throws Exception {
75          final File fileToProcess = new File(
76                  getPath("InputXpathNoFinalizerInner.java"));
77  
78          final DefaultConfiguration moduleConfig =
79                  createModuleConfig(NoFinalizerCheck.class);
80  
81          final String[] expectedViolation = {
82              "10:9: " + getCheckMessage(NoFinalizerCheck.class,
83                      NoFinalizerCheck.MSG_KEY),
84          };
85  
86          final List<String> expectedXpathQueries = Arrays.asList(
87                  "/COMPILATION_UNIT/CLASS_DEF"
88                  + "[./IDENT[@text='InputXpathNoFinalizerInner']]"
89                  + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
90                  + "METHOD_DEF[./IDENT[@text='finalize']]",
91                  "/COMPILATION_UNIT/CLASS_DEF"
92                  + "[./IDENT[@text='InputXpathNoFinalizerInner']]"
93                  + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
94                  + "METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS",
95                  "/COMPILATION_UNIT/CLASS_DEF"
96                  + "[./IDENT[@text='InputXpathNoFinalizerInner']]"
97                  + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
98                  + "METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS/"
99                  + "ANNOTATION[./IDENT[@text='Override']]",
100                 "/COMPILATION_UNIT/CLASS_DEF"
101                 + "[./IDENT[@text='InputXpathNoFinalizerInner']]"
102                 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
103                 + "METHOD_DEF[./IDENT[@text='finalize']]/MODIFIERS/"
104                 + "ANNOTATION[./IDENT[@text='Override']]/AT"
105         );
106 
107         runVerifications(moduleConfig, fileToProcess, expectedViolation,
108                 expectedXpathQueries);
109     }
110 
111 }