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.MutableExceptionCheck;
31  
32  public class XpathRegressionMutableExceptionTest extends AbstractXpathTestSupport {
33  
34      private final String checkName = MutableExceptionCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Override
42      protected String getPackageLocation() {
43          return "org/checkstyle/suppressionxpathfilter/design/mutableexception";
44      }
45  
46      @Test
47      public void testDefault() throws Exception {
48          final File fileToProcess =
49              new File(getPath("InputXpathMutableExceptionDefault.java"));
50  
51          final DefaultConfiguration moduleConfig =
52              createModuleConfig(MutableExceptionCheck.class);
53  
54          final String[] expectedViolation = {
55              "5:9: " + getCheckMessage(MutableExceptionCheck.class,
56                  MutableExceptionCheck.MSG_KEY, "code"),
57          };
58  
59          final List<String> expectedXpathQueries = Arrays.asList(
60                  "/COMPILATION_UNIT/CLASS_DEF"
61                  + "[./IDENT[@text='InputXpathMutableExceptionDefault']]"
62                  + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='FooException']]"
63                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]",
64                  "/COMPILATION_UNIT/CLASS_DEF"
65                  + "[./IDENT[@text='InputXpathMutableExceptionDefault']]"
66                  + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='FooException']]"
67                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]/MODIFIERS",
68                  "/COMPILATION_UNIT/CLASS_DEF"
69                  + "[./IDENT[@text='InputXpathMutableExceptionDefault']]"
70                  + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='FooException']]"
71                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]/MODIFIERS/LITERAL_PRIVATE"
72          );
73  
74          runVerifications(moduleConfig, fileToProcess, expectedViolation,
75              expectedXpathQueries);
76      }
77  
78      @Test
79      public void testClassName() throws Exception {
80          final String classFormat = "^.*ExceptionClassName$";
81          final File fileToProcess =
82                  new File(getPath("InputXpathMutableExceptionClassName.java"));
83  
84          final DefaultConfiguration moduleConfig =
85                  createModuleConfig(MutableExceptionCheck.class);
86          moduleConfig.addProperty("format", classFormat);
87  
88          final String[] expectedViolation = {
89              "4:3: " + getCheckMessage(MutableExceptionCheck.class,
90                      MutableExceptionCheck.MSG_KEY, "code"),
91          };
92  
93          final List<String> expectedXpathQueries = Arrays.asList(
94                  "/COMPILATION_UNIT/CLASS_DEF"
95                  + "[./IDENT[@text='InputXpathMutableExceptionClassName']]"
96                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]",
97                  "/COMPILATION_UNIT/CLASS_DEF"
98                  + "[./IDENT[@text='InputXpathMutableExceptionClassName']]"
99                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]/MODIFIERS",
100                 "/COMPILATION_UNIT/CLASS_DEF"
101                 + "[./IDENT[@text='InputXpathMutableExceptionClassName']]"
102                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]/TYPE",
103                 "/COMPILATION_UNIT/CLASS_DEF"
104                 + "[./IDENT[@text='InputXpathMutableExceptionClassName']]"
105                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]/TYPE/LITERAL_INT"
106         );
107 
108         runVerifications(moduleConfig, fileToProcess, expectedViolation,
109                 expectedXpathQueries);
110     }
111 
112     @Test
113     public void testExtendedClassName() throws Exception {
114         final String extendedClassNameFormat = "^.*Throwable$";
115         final File fileToProcess =
116                 new File(getPath("InputXpathMutableExceptionExtendedClassName.java"));
117 
118         final DefaultConfiguration moduleConfig =
119                 createModuleConfig(MutableExceptionCheck.class);
120         moduleConfig.addProperty("extendedClassNameFormat", extendedClassNameFormat);
121 
122         final String[] expectedViolation = {
123             "6:9: " + getCheckMessage(MutableExceptionCheck.class,
124                     MutableExceptionCheck.MSG_KEY, "code"),
125         };
126 
127         final List<String> expectedXpathQueries = Arrays.asList(
128                 "/COMPILATION_UNIT/CLASS_DEF"
129                 + "[./IDENT[@text='InputXpathMutableExceptionExtendedClassName']]"
130                 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='FooException']]"
131                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]",
132                 "/COMPILATION_UNIT/CLASS_DEF"
133                 + "[./IDENT[@text='InputXpathMutableExceptionExtendedClassName']]"
134                 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='FooException']]"
135                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]/MODIFIERS",
136                 "/COMPILATION_UNIT/CLASS_DEF"
137                 + "[./IDENT[@text='InputXpathMutableExceptionExtendedClassName']]"
138                 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='FooException']]"
139                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]/MODIFIERS/LITERAL_PRIVATE");
140 
141         runVerifications(moduleConfig, fileToProcess, expectedViolation,
142                 expectedXpathQueries);
143     }
144 }