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;
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.modifier.RedundantModifierCheck;
30  
31  public class XpathRegressionRedundantModifierTest extends AbstractXpathTestSupport {
32  
33      private final String checkName = RedundantModifierCheck.class.getSimpleName();
34  
35      @Override
36      protected String getCheckName() {
37          return checkName;
38      }
39  
40      @Test
41      public void test1() throws Exception {
42          final File fileToProcess =
43                  new File(getPath("InputXpathRedundantModifierClass.java"));
44  
45          final DefaultConfiguration moduleConfig =
46                  createModuleConfig(RedundantModifierCheck.class);
47  
48          final String[] expected = {
49              "7:10: " + getCheckMessage(RedundantModifierCheck.class,
50                      RedundantModifierCheck.MSG_KEY,
51                      "final"),
52          };
53  
54          final List<String> expectedXpathQueries = Arrays.asList(
55                  "/COMPILATION_UNIT/CLASS_DEF"
56                     + "[./IDENT[@text='InputXpathRedundantModifierClass']]"
57                     + "/OBJBLOCK/CLASS_DEF"
58                     + "[./IDENT[@text='Example1']]"
59                     + "/OBJBLOCK/METHOD_DEF"
60                     + "[./IDENT[@text='test']]"
61                     + "/SLIST/LITERAL_TRY/RESOURCE_SPECIFICATION/RESOURCES",
62  
63                  "/COMPILATION_UNIT/CLASS_DEF"
64                     + "[./IDENT[@text='InputXpathRedundantModifierClass']]"
65                     + "/OBJBLOCK/CLASS_DEF"
66                     + "[./IDENT[@text='Example1']]"
67                     + "/OBJBLOCK/METHOD_DEF"
68                     + "[./IDENT[@text='test']]"
69                     + "/SLIST/LITERAL_TRY/RESOURCE_SPECIFICATION/RESOURCES"
70                     + "/RESOURCE"
71                     + "[./IDENT[@text='a']]",
72  
73                  "/COMPILATION_UNIT/CLASS_DEF"
74                     + "[./IDENT[@text='InputXpathRedundantModifierClass']]"
75                     + "/OBJBLOCK/CLASS_DEF"
76                     + "[./IDENT[@text='Example1']]"
77                     + "/OBJBLOCK/METHOD_DEF"
78                     + "[./IDENT[@text='test']]"
79                     + "/SLIST/LITERAL_TRY/RESOURCE_SPECIFICATION/RESOURCES"
80                     + "/RESOURCE"
81                     + "[./IDENT[@text='a']]/MODIFIERS",
82  
83                  "/COMPILATION_UNIT/CLASS_DEF"
84                     + "[./IDENT[@text='InputXpathRedundantModifierClass']]"
85                     + "/OBJBLOCK/CLASS_DEF"
86                     + "[./IDENT[@text='Example1']]"
87                     + "/OBJBLOCK/METHOD_DEF"
88                     + "[./IDENT[@text='test']]"
89                     + "/SLIST/LITERAL_TRY/RESOURCE_SPECIFICATION/RESOURCES"
90                     + "/RESOURCE"
91                     + "[./IDENT[@text='a']]/MODIFIERS/FINAL"
92          );
93  
94          runVerifications(moduleConfig, fileToProcess, expected, expectedXpathQueries);
95  
96      }
97  
98      @Test
99      public void test2() throws Exception {
100         final File fileToProcess =
101                 new File(getPath("InputXpathRedundantModifierInterface.java"));
102 
103         final DefaultConfiguration moduleConfig =
104                 createModuleConfig(RedundantModifierCheck.class);
105         moduleConfig.addProperty("tokens", "METHOD_DEF");
106 
107         final String[] expected = {
108             "5:5: " + getCheckMessage(RedundantModifierCheck.class,
109                     RedundantModifierCheck.MSG_KEY,
110                     "public"),
111         };
112 
113         final List<String> expectedXpathQueries = Arrays.asList(
114                 "/COMPILATION_UNIT/INTERFACE_DEF"
115                    + "[./IDENT[@text='InputXpathRedundantModifierInterface']]"
116                    + "/OBJBLOCK"
117                    + "/METHOD_DEF"
118                    + "[./IDENT[@text='m']]",
119 
120                 "/COMPILATION_UNIT/INTERFACE_DEF"
121                    + "[./IDENT[@text='InputXpathRedundantModifierInterface']]"
122                    + "/OBJBLOCK"
123                    + "/METHOD_DEF"
124                    + "[./IDENT[@text='m']]"
125                    + "/MODIFIERS",
126 
127                 "/COMPILATION_UNIT/INTERFACE_DEF"
128                    + "[./IDENT[@text='InputXpathRedundantModifierInterface']]"
129                    + "/OBJBLOCK"
130                    + "/METHOD_DEF"
131                    + "[./IDENT[@text='m']]"
132                    + "/MODIFIERS"
133                    + "/LITERAL_PUBLIC"
134         );
135 
136         runVerifications(moduleConfig, fileToProcess, expected, expectedXpathQueries);
137     }
138 
139     @Test
140     public void test3() throws Exception {
141         final File fileToProcess =
142                 new File(getPath("InputXpathRedundantModifierWithEnum.java"));
143 
144         final DefaultConfiguration moduleConfig =
145                 createModuleConfig(RedundantModifierCheck.class);
146         moduleConfig.addProperty("jdkVersion", "11");
147 
148         final String[] expected = {
149             "5:9: " + getCheckMessage(RedundantModifierCheck.class,
150                     RedundantModifierCheck.MSG_KEY,
151                     "static"),
152         };
153 
154         final List<String> expectedXpathQueries = Arrays.asList(
155                 "/COMPILATION_UNIT/CLASS_DEF"
156                    + "[./IDENT[@text='InputXpathRedundantModifierWithEnum']]"
157                    + "/OBJBLOCK/INTERFACE_DEF"
158                    + "[./IDENT[@text='I']]"
159                    + "/OBJBLOCK/ENUM_DEF"
160                    + "[./IDENT[@text='E']]",
161 
162                 "/COMPILATION_UNIT/CLASS_DEF"
163                    + "[./IDENT[@text='InputXpathRedundantModifierWithEnum']]"
164                    + "/OBJBLOCK/INTERFACE_DEF"
165                    + "[./IDENT[@text='I']]"
166                    + "/OBJBLOCK/ENUM_DEF"
167                    + "[./IDENT[@text='E']]"
168                    + "/MODIFIERS",
169 
170                 "/COMPILATION_UNIT/CLASS_DEF"
171                    + "[./IDENT[@text='InputXpathRedundantModifierWithEnum']]"
172                    + "/OBJBLOCK/INTERFACE_DEF"
173                    + "[./IDENT[@text='I']]"
174                    + "/OBJBLOCK/ENUM_DEF"
175                    + "[./IDENT[@text='E']]"
176                    + "/MODIFIERS"
177                    + "/LITERAL_STATIC"
178         );
179 
180         runVerifications(moduleConfig, fileToProcess, expected, expectedXpathQueries);
181 
182     }
183 
184 }