View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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  
34      private final String checkName = FinalClassCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Override
42      public String getPackageLocation() {
43          return "org/checkstyle/suppressionxpathfilter/design/finalclass";
44      }
45  
46      @Test
47      public void testDefault() throws Exception {
48          final File fileToProcess = new File(getPath(
49                  "InputXpathFinalClassDefault.java"));
50  
51          final DefaultConfiguration moduleConfig =
52                  createModuleConfig(FinalClassCheck.class);
53  
54          final String[] expectedViolation = {
55              "3:1: " + getCheckMessage(FinalClassCheck.class,
56                      FinalClassCheck.MSG_KEY, "InputXpathFinalClassDefault"),
57          };
58  
59          final List<String> expectedXpathQueries = Arrays.asList(
60                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
61                          + "@text='InputXpathFinalClassDefault']]",
62                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
63                          + "@text='InputXpathFinalClassDefault']]/MODIFIERS",
64                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
65                          + "@text='InputXpathFinalClassDefault']]/MODIFIERS/LITERAL_PUBLIC"
66          );
67  
68          runVerifications(moduleConfig, fileToProcess, expectedViolation,
69                  expectedXpathQueries);
70      }
71  
72      @Test
73      public void testInnerClass() throws Exception {
74          final File fileToProcess = new File(getPath(
75                  "InputXpathFinalClassInnerClass.java"));
76  
77          final DefaultConfiguration moduleConfig =
78                  createModuleConfig(FinalClassCheck.class);
79  
80          final String[] expectedViolation = {
81              "4:5: " + getCheckMessage(FinalClassCheck.class,
82                      FinalClassCheck.MSG_KEY, "Test"),
83          };
84  
85          final List<String> expectedXpathQueries = Arrays.asList(
86                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
87                          + "@text='InputXpathFinalClassInnerClass']]"
88                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Test']]",
89                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
90                          + "@text='InputXpathFinalClassInnerClass']]"
91                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Test']]/MODIFIERS",
92                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
93                          + "@text='InputXpathFinalClassInnerClass']]"
94                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Test']]/LITERAL_CLASS"
95          );
96  
97          runVerifications(moduleConfig, fileToProcess, expectedViolation,
98                  expectedXpathQueries);
99      }
100 
101     @Test
102     public void testStaticNestedClass() throws Exception {
103         final File fileToProcess = new File(getPath(
104                 "InputXpathFinalClassStaticNestedClass.java"));
105 
106         final DefaultConfiguration moduleConfig =
107                 createModuleConfig(FinalClassCheck.class);
108 
109         final String[] expectedViolation = {
110             "5:5: " + getCheckMessage(FinalClassCheck.class,
111                     FinalClassCheck.MSG_KEY, "StaticNestedClass"),
112         };
113 
114         final List<String> expectedXpathQueries = Arrays.asList(
115                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
116                         + "@text='InputXpathFinalClassStaticNestedClass']]"
117                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='StaticNestedClass']]",
118                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
119                         + "@text='InputXpathFinalClassStaticNestedClass']]"
120                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='StaticNestedClass']]/MODIFIERS",
121                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
122                         + "@text='InputXpathFinalClassStaticNestedClass']]"
123                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='StaticNestedClass']]"
124                         + "/MODIFIERS/LITERAL_STATIC"
125         );
126 
127         runVerifications(moduleConfig, fileToProcess, expectedViolation,
128                 expectedXpathQueries);
129     }
130 
131 }