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