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.design;
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.design.FinalClassCheck;
31  
32  public class XpathRegressionFinalClassTest extends AbstractXpathTestSupport {
33      private final String checkName = FinalClassCheck.class.getSimpleName();
34  
35      @Override
36      protected String getCheckName() {
37          return checkName;
38      }
39  
40      @Override
41      protected String getPackageLocation() {
42          return "org/checkstyle/suppressionxpathfilter/design/finalclass";
43      }
44  
45      @Test
46      public void testDefault() throws Exception {
47          final File fileToProcess = new File(getPath(
48                  "InputXpathFinalClassDefault.java"));
49  
50          final DefaultConfiguration moduleConfig =
51                  createModuleConfig(FinalClassCheck.class);
52  
53          final String[] expectedViolation = {
54              "3:1: " + getCheckMessage(FinalClassCheck.class,
55                      FinalClassCheck.MSG_KEY, "InputXpathFinalClassDefault"),
56          };
57  
58          final List<String> expectedXpathQueries = Arrays.asList(
59                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
60                          + "@text='InputXpathFinalClassDefault']]",
61                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
62                          + "@text='InputXpathFinalClassDefault']]/MODIFIERS",
63                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
64                          + "@text='InputXpathFinalClassDefault']]/MODIFIERS/LITERAL_PUBLIC"
65          );
66  
67          runVerifications(moduleConfig, fileToProcess, expectedViolation,
68                  expectedXpathQueries);
69      }
70  
71      @Test
72      public void testInnerClass() throws Exception {
73          final File fileToProcess = new File(getPath(
74                  "InputXpathFinalClassInnerClass.java"));
75  
76          final DefaultConfiguration moduleConfig =
77                  createModuleConfig(FinalClassCheck.class);
78  
79          final String[] expectedViolation = {
80              "4:5: " + getCheckMessage(FinalClassCheck.class,
81                      FinalClassCheck.MSG_KEY, "Test"),
82          };
83  
84          final List<String> expectedXpathQueries = Arrays.asList(
85                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
86                          + "@text='InputXpathFinalClassInnerClass']]"
87                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Test']]",
88                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
89                          + "@text='InputXpathFinalClassInnerClass']]"
90                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Test']]/MODIFIERS",
91                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
92                          + "@text='InputXpathFinalClassInnerClass']]"
93                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Test']]/LITERAL_CLASS"
94          );
95  
96          runVerifications(moduleConfig, fileToProcess, expectedViolation,
97                  expectedXpathQueries);
98      }
99  }
100