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.Collections;
25  import java.util.List;
26  
27  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.checks.whitespace.SeparatorWrapCheck;
32  
33  public class XpathRegressionSeparatorWrapTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = SeparatorWrapCheck.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/separatorwrap";
45      }
46  
47      @Test
48      public void testClass() throws Exception {
49          final File fileToProcess =
50                  new File(getPath("InputXpathSeparatorWrapClass.java"));
51          final DefaultConfiguration moduleConfig =
52                  createModuleConfig(SeparatorWrapCheck.class);
53  
54          final String[] expectedViolation = {
55              "10:17: " + getCheckMessage(SeparatorWrapCheck.class,
56                          SeparatorWrapCheck.MSG_LINE_PREVIOUS, ","),
57          };
58  
59          final List<String> expectedXpathQueries = Collections.singletonList(
60                  "/COMPILATION_UNIT/CLASS_DEF"
61                          + "[./IDENT[@text='InputXpathSeparatorWrapClass']]"
62                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod']]"
63                          + "/PARAMETERS/COMMA"
64          );
65  
66          runVerifications(moduleConfig, fileToProcess, expectedViolation,
67                  expectedXpathQueries);
68      }
69  
70      @Test
71      public void testInterface() throws Exception {
72          final File fileToProcess =
73                  new File(getPath("InputXpathSeparatorWrapInterface.java"));
74  
75          final DefaultConfiguration moduleConfig =
76                  createModuleConfig(SeparatorWrapCheck.class);
77          moduleConfig.addProperty("tokens", "ELLIPSIS");
78          final String[] expectedViolation = {
79              "9:13: " + getCheckMessage(SeparatorWrapCheck.class,
80                          SeparatorWrapCheck.MSG_LINE_PREVIOUS, "..."),
81          };
82  
83          final List<String> expectedXpathQueries = Collections.singletonList(
84                  "/COMPILATION_UNIT/INTERFACE_DEF"
85                          + "[./IDENT[@text='InputXpathSeparatorWrapInterface']]"
86                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod2']]"
87                          + "/PARAMETERS/PARAMETER_DEF"
88                          + "[./IDENT[@text='parameters']]/ELLIPSIS"
89          );
90          runVerifications(moduleConfig, fileToProcess, expectedViolation,
91                  expectedXpathQueries);
92      }
93  
94      @Test
95      public void testMethod() throws Exception {
96          final File fileToProcess =
97                  new File(getPath("InputXpathSeparatorWrapMethod.java"));
98  
99          final DefaultConfiguration moduleConfig =
100                 createModuleConfig(SeparatorWrapCheck.class);
101         moduleConfig.addProperty("tokens", "DOT");
102 
103         final String[] expectedViolation = {
104             "9:13: " + getCheckMessage(SeparatorWrapCheck.class,
105                     SeparatorWrapCheck.MSG_LINE_PREVIOUS, "."),
106         };
107         final List<String> expectedXpathQueries = Arrays.asList(
108                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
109                         + "'InputXpathSeparatorWrapMethod']]"
110                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method1']]/SLIST"
111                         + "/VARIABLE_DEF[./IDENT[@text='stringLength']]"
112                         + "/ASSIGN/EXPR",
113                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
114                         + "'InputXpathSeparatorWrapMethod']]"
115                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method1']]/SLIST"
116                         + "/VARIABLE_DEF[./IDENT[@text='stringLength']]/ASSIGN"
117                         + "/EXPR/DOT[./IDENT[@text='stringArray']]"
118         );
119         runVerifications(moduleConfig, fileToProcess, expectedViolation,
120                 expectedXpathQueries);
121     }
122 }