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