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.Arrays;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceAfterCheck;
31  
32  public class XpathRegressionNoWhitespaceAfterTest extends AbstractXpathTestSupport {
33  
34      private final String checkName = NoWhitespaceAfterCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Test
42      public void testNoWhitespaceAfter() throws Exception {
43          final File fileToProcess =
44                  new File(getPath("InputXpathNoWhitespaceAfter.java"));
45  
46          final DefaultConfiguration moduleConfig =
47                  createModuleConfig(NoWhitespaceAfterCheck.class);
48  
49          final String[] expectedViolation = {
50              "4:15: " + getCheckMessage(NoWhitespaceAfterCheck.class,
51                      NoWhitespaceAfterCheck.MSG_KEY, "-"),
52          };
53  
54          final List<String> expectedXpathQueries = Arrays.asList(
55              "/COMPILATION_UNIT/CLASS_DEF"
56                  + "[./IDENT[@text='InputXpathNoWhitespaceAfter']]/OBJBLOCK"
57                  + "/VARIABLE_DEF[./IDENT[@text='bad']]/ASSIGN/EXPR",
58              "/COMPILATION_UNIT/CLASS_DEF"
59                  + "[./IDENT[@text='InputXpathNoWhitespaceAfter']]/OBJBLOCK"
60                  + "/VARIABLE_DEF[./IDENT[@text='bad']]/ASSIGN/EXPR/UNARY_MINUS["
61                  + "./NUM_INT[@text='1']]"
62          );
63  
64          runVerifications(moduleConfig, fileToProcess, expectedViolation,
65                  expectedXpathQueries);
66      }
67  
68      @Test
69      public void testTokens() throws Exception {
70          final File fileToProcess =
71                  new File(getPath("InputXpathNoWhitespaceAfterTokens.java"));
72  
73          final DefaultConfiguration moduleConfig =
74                  createModuleConfig(NoWhitespaceAfterCheck.class);
75          moduleConfig.addProperty("tokens", "DOT");
76  
77          final String[] expectedViolation = {
78              "4:16: " + getCheckMessage(NoWhitespaceAfterCheck.class,
79                      NoWhitespaceAfterCheck.MSG_KEY, "."),
80          };
81  
82          final List<String> expectedXpathQueries = Collections.singletonList(
83              "/COMPILATION_UNIT/CLASS_DEF"
84                  + "[./IDENT[@text='InputXpathNoWhitespaceAfterTokens']]"
85                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
86                  + "/TYPE/DOT[./IDENT[@text='String']]"
87                  + "/DOT[./IDENT[@text='java']]"
88          );
89  
90          runVerifications(moduleConfig, fileToProcess, expectedViolation,
91                  expectedXpathQueries);
92      }
93  
94      @Test
95      public void testAllowLineBreaks() throws Exception {
96          final File fileToProcess =
97              new File(getPath("InputXpathNoWhitespaceAfterLineBreaks.java"));
98  
99          final DefaultConfiguration moduleConfig =
100             createModuleConfig(NoWhitespaceAfterCheck.class);
101         moduleConfig.addProperty("allowLineBreaks", "false");
102 
103         final String[] expectedViolation = {
104             "6:13: " + getCheckMessage(NoWhitespaceAfterCheck.class,
105                 NoWhitespaceAfterCheck.MSG_KEY, "."),
106         };
107 
108         final List<String> expectedXpathQueries = Arrays.asList(
109             "/COMPILATION_UNIT/CLASS_DEF"
110                 + "[./IDENT[@text='InputXpathNoWhitespaceAfterLineBreaks']]"
111                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
112                 + "/SLIST/VARIABLE_DEF[./IDENT[@text='s']]",
113             "/COMPILATION_UNIT/CLASS_DEF"
114                 + "[./IDENT[@text='InputXpathNoWhitespaceAfterLineBreaks']]"
115                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
116                 + "/SLIST/VARIABLE_DEF[./IDENT[@text='s']]"
117                 + "/MODIFIERS",
118             "/COMPILATION_UNIT/CLASS_DEF"
119                 + "[./IDENT[@text='InputXpathNoWhitespaceAfterLineBreaks']]"
120                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
121                 + "/SLIST/VARIABLE_DEF[./IDENT[@text='s']]/TYPE",
122             "/COMPILATION_UNIT/CLASS_DEF"
123                 + "[./IDENT[@text='InputXpathNoWhitespaceAfterLineBreaks']]"
124                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
125                 + "/SLIST/VARIABLE_DEF[./IDENT[@text='s']]"
126                 + "/TYPE/DOT[./IDENT[@text='String']]"
127         );
128 
129         runVerifications(moduleConfig, fileToProcess, expectedViolation,
130             expectedXpathQueries);
131     }
132 }