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