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.Collections;
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.AbstractParenPadCheck;
31  import com.puppycrawl.tools.checkstyle.checks.whitespace.PadOption;
32  import com.puppycrawl.tools.checkstyle.checks.whitespace.ParenPadCheck;
33  
34  public class XpathRegressionParenPadTest extends AbstractXpathTestSupport {
35  
36      private final String checkName = ParenPadCheck.class.getSimpleName();
37  
38      @Override
39      protected String getCheckName() {
40          return checkName;
41      }
42  
43      @Override
44      protected String getPackageLocation() {
45          return "org/checkstyle/suppressionxpathfilter/whitespace/parenpad";
46      }
47  
48      @Test
49      public void testLeftFollowed() throws Exception {
50          final File fileToProcess =
51                  new File(getPath("InputXpathParenPadLeftFollowed.java"));
52  
53          final DefaultConfiguration moduleConfig =
54                  createModuleConfig(ParenPadCheck.class);
55  
56          final String[] expectedViolation = {
57              "5:12: " + getCheckMessage(ParenPadCheck.class,
58                      AbstractParenPadCheck.MSG_WS_FOLLOWED, "("),
59          };
60  
61          final List<String> expectedXpathQueries = Collections.singletonList(
62              "/COMPILATION_UNIT/CLASS_DEF"
63                  + "[./IDENT[@text='InputXpathParenPadLeftFollowed']]"
64                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]/SLIST/LITERAL_IF/LPAREN"
65          );
66  
67          runVerifications(moduleConfig, fileToProcess, expectedViolation,
68                  expectedXpathQueries);
69      }
70  
71      @Test
72      public void testLeftNotFollowed() throws Exception {
73          final File fileToProcess =
74                  new File(getPath("InputXpathParenPadLeftNotFollowed.java"));
75  
76          final DefaultConfiguration moduleConfig =
77                  createModuleConfig(ParenPadCheck.class);
78          moduleConfig.addProperty("option", PadOption.SPACE.toString());
79  
80          final String[] expectedViolation = {
81              "5:12: " + getCheckMessage(ParenPadCheck.class,
82                      AbstractParenPadCheck.MSG_WS_NOT_FOLLOWED, "("),
83          };
84  
85          final List<String> expectedXpathQueries = Collections.singletonList(
86              "/COMPILATION_UNIT/CLASS_DEF"
87                  + "[./IDENT[@text='InputXpathParenPadLeftNotFollowed']]"
88                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]/SLIST/LITERAL_IF/LPAREN"
89          );
90  
91          runVerifications(moduleConfig, fileToProcess, expectedViolation,
92                  expectedXpathQueries);
93      }
94  
95      @Test
96      public void testRightPreceded() throws Exception {
97          final File fileToProcess =
98                  new File(getPath("InputXpathParenPadRightPreceded.java"));
99  
100         final DefaultConfiguration moduleConfig =
101                 createModuleConfig(ParenPadCheck.class);
102 
103         final String[] expectedViolation = {
104             "5:19: " + getCheckMessage(ParenPadCheck.class,
105                     AbstractParenPadCheck.MSG_WS_PRECEDED, ")"),
106         };
107 
108         final List<String> expectedXpathQueries = Collections.singletonList(
109             "/COMPILATION_UNIT/CLASS_DEF"
110                 + "[./IDENT[@text='InputXpathParenPadRightPreceded']]"
111                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]/SLIST/LITERAL_IF/RPAREN"
112         );
113 
114         runVerifications(moduleConfig, fileToProcess, expectedViolation,
115                 expectedXpathQueries);
116     }
117 
118     @Test
119     public void testRightNotPreceded() throws Exception {
120         final File fileToProcess =
121                 new File(getPath("InputXpathParenPadRightNotPreceded.java"));
122 
123         final DefaultConfiguration moduleConfig =
124                 createModuleConfig(ParenPadCheck.class);
125         moduleConfig.addProperty("option", PadOption.SPACE.toString());
126 
127         final String[] expectedViolation = {
128             "5:19: " + getCheckMessage(ParenPadCheck.class,
129                     AbstractParenPadCheck.MSG_WS_NOT_PRECEDED, ")"),
130         };
131 
132         final List<String> expectedXpathQueries = Collections.singletonList(
133             "/COMPILATION_UNIT/CLASS_DEF"
134                 + "[./IDENT[@text='InputXpathParenPadRightNotPreceded']]"
135                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]/SLIST/LITERAL_IF/RPAREN"
136         );
137 
138         runVerifications(moduleConfig, fileToProcess, expectedViolation,
139                 expectedXpathQueries);
140     }
141 
142 }