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.whitespace;
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.whitespace.EmptyForInitializerPadCheck;
31  import com.puppycrawl.tools.checkstyle.checks.whitespace.PadOption;
32  
33  public class XpathRegressionEmptyForInitializerPadTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = EmptyForInitializerPadCheck.class.getSimpleName();
36  
37      @Override
38      protected String getCheckName() {
39          return checkName;
40      }
41  
42      @Override
43      protected String getPackageLocation() {
44          return "org/checkstyle/suppressionxpathfilter/whitespace/emptyforinitializerpad";
45      }
46  
47      @Test
48      public void testPreceded() throws Exception {
49          final File fileToProcess = new File(
50                  getPath("InputXpathEmptyForInitializerPadPreceded.java"));
51  
52          final DefaultConfiguration moduleConfig =
53                  createModuleConfig(EmptyForInitializerPadCheck.class);
54  
55          final String[] expectedViolation = {
56              "5:15: " + getCheckMessage(EmptyForInitializerPadCheck.class,
57                      EmptyForInitializerPadCheck.MSG_PRECEDED, ";"),
58          };
59  
60          final List<String> expectedXpathQueries = Arrays.asList(
61              "/COMPILATION_UNIT/CLASS_DEF"
62                      + "[./IDENT[@text='InputXpathEmptyForInitializerPadPreceded']]"
63                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]/SLIST/LITERAL_FOR/FOR_INIT",
64              "/COMPILATION_UNIT/CLASS_DEF"
65                      + "[./IDENT[@text='InputXpathEmptyForInitializerPadPreceded']]"
66                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]/SLIST/LITERAL_FOR/SEMI[1]"
67          );
68  
69          runVerifications(moduleConfig, fileToProcess, expectedViolation,
70                  expectedXpathQueries);
71      }
72  
73      @Test
74      public void testNotPreceded() throws Exception {
75          final File fileToProcess = new File(
76              getPath("InputXpathEmptyForInitializerPadNotPreceded.java"));
77  
78          final DefaultConfiguration moduleConfig =
79                  createModuleConfig(EmptyForInitializerPadCheck.class);
80          moduleConfig.addProperty("option", PadOption.SPACE.toString());
81  
82          final String[] expectedViolation = {
83              "5:14: " + getCheckMessage(EmptyForInitializerPadCheck.class,
84                      EmptyForInitializerPadCheck.MSG_NOT_PRECEDED, ";"),
85          };
86  
87          final List<String> expectedXpathQueries = Arrays.asList(
88              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
89                  + "@text='InputXpathEmptyForInitializerPadNotPreceded']]"
90                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]/SLIST/LITERAL_FOR/FOR_INIT",
91              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
92                  + "@text='InputXpathEmptyForInitializerPadNotPreceded']]"
93                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]/SLIST/LITERAL_FOR/SEMI[1]"
94          );
95  
96          runVerifications(moduleConfig, fileToProcess, expectedViolation,
97                  expectedXpathQueries);
98      }
99  
100 }