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.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.NoLineWrapCheck;
31  
32  public class XpathRegressionNoLineWrapTest extends AbstractXpathTestSupport {
33  
34      @Override
35      protected String getCheckName() {
36          return NoLineWrapCheck.class.getSimpleName();
37      }
38  
39      @Override
40      protected String getPackageLocation() {
41          return "org/checkstyle/suppressionxpathfilter/whitespace/nolinewrap";
42      }
43  
44      @Test
45      public void testDefault() throws Exception {
46          final File fileToProcess = new File(
47                  getPath("InputXpathNoLineWrapDefault.java")
48          );
49  
50          final DefaultConfiguration moduleConfig =
51                  createModuleConfig(NoLineWrapCheck.class);
52  
53          final String[] expectedViolation = {
54              "1:1: " + getCheckMessage(NoLineWrapCheck.class,
55                      NoLineWrapCheck.MSG_KEY, "package"),
56          };
57  
58          final List<String> expectedXpathQueries = Arrays.asList(
59                  "/COMPILATION_UNIT", "/COMPILATION_UNIT/PACKAGE_DEF"
60          );
61  
62          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
63      }
64  
65      @Test
66      public void testMethodDef() throws Exception {
67          final File fileToProcess = new File(
68                  getPath("InputXpathNoLineWrapTokensMethodDef.java")
69          );
70  
71          final DefaultConfiguration moduleConfig =
72                  createModuleConfig(NoLineWrapCheck.class);
73          moduleConfig.addProperty("tokens", "METHOD_DEF");
74  
75          final String[] expectedViolation = {
76              "4:5: " + getCheckMessage(NoLineWrapCheck.class,
77                      NoLineWrapCheck.MSG_KEY, "METHOD_DEF"),
78          };
79  
80          final List<String> expectedXpathQueries = Arrays.asList(
81                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
82                      + "[@text='InputXpathNoLineWrapTokensMethodDef']]"
83                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test2']]",
84  
85                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
86                      + "[@text='InputXpathNoLineWrapTokensMethodDef']]"
87                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test2']]"
88                      + "/MODIFIERS",
89  
90                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
91                      + "[@text='InputXpathNoLineWrapTokensMethodDef']]"
92                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test2']]"
93                      + "/MODIFIERS/LITERAL_PUBLIC"
94  
95                  );
96  
97          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
98      }
99  
100     @Test
101     public void testTokensCtorDef() throws Exception {
102         final File fileToProcess = new File(
103                 getPath("InputXpathNoLineWrapTokensCtorDef.java")
104         );
105 
106         final DefaultConfiguration moduleConfig =
107                 createModuleConfig(NoLineWrapCheck.class);
108         moduleConfig.addProperty("tokens", "CTOR_DEF");
109 
110         final String[] expectedViolation = {
111             "4:5: " + getCheckMessage(NoLineWrapCheck.class,
112                     NoLineWrapCheck.MSG_KEY, "CTOR_DEF"),
113         };
114 
115         final List<String> expectedXpathQueries = Arrays.asList(
116                 "/COMPILATION_UNIT/CLASS_DEF"
117                     + "[./IDENT[@text='InputXpathNoLineWrapTokensCtorDef']]"
118                     + "/OBJBLOCK/CTOR_DEF[./IDENT[@text='InputXpathNoLineWrapTokensCtorDef']]",
119 
120                 "/COMPILATION_UNIT/CLASS_DEF"
121                     + "[./IDENT[@text='InputXpathNoLineWrapTokensCtorDef']]"
122                     + "/OBJBLOCK/CTOR_DEF[./IDENT[@text='InputXpathNoLineWrapTokensCtorDef']]"
123                     + "/MODIFIERS",
124 
125                 "/COMPILATION_UNIT/CLASS_DEF"
126                     + "[./IDENT[@text='InputXpathNoLineWrapTokensCtorDef']]"
127                     + "/OBJBLOCK/CTOR_DEF/IDENT[@text='InputXpathNoLineWrapTokensCtorDef']"
128         );
129 
130         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
131     }
132 
133 }