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