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