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.EmptyLineSeparatorCheck;
31  
32  public class XpathRegressionEmptyLineSeparatorTest extends AbstractXpathTestSupport {
33  
34      @Override
35      protected String getCheckName() {
36          return EmptyLineSeparatorCheck.class.getSimpleName();
37      }
38  
39      @Test
40      public void testOne() throws Exception {
41          final File fileToProcess = new File(
42                  getPath("InputXpathEmptyLineSeparatorOne.java")
43          );
44  
45          final DefaultConfiguration moduleConfig =
46                  createModuleConfig(EmptyLineSeparatorCheck.class);
47          moduleConfig.addProperty("tokens", "PACKAGE_DEF");
48  
49          final String[] expectedViolation = {
50              "4:1: " + getCheckMessage(EmptyLineSeparatorCheck.class,
51                      EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED, "package"),
52          };
53  
54          final List<String> expectedXpathQueries = Arrays.asList(
55                  "/COMPILATION_UNIT", "/COMPILATION_UNIT/PACKAGE_DEF"
56          );
57  
58          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
59      }
60  
61      @Test
62      public void testTwo() throws Exception {
63          final File fileToProcess = new File(
64                  getPath("InputXpathEmptyLineSeparatorTwo.java")
65          );
66  
67          final DefaultConfiguration moduleConfig =
68                  createModuleConfig(EmptyLineSeparatorCheck.class);
69          moduleConfig.addProperty("allowMultipleEmptyLines", "false");
70  
71          final String[] expectedViolation = {
72              "6:1: " + getCheckMessage(EmptyLineSeparatorCheck.class,
73                      EmptyLineSeparatorCheck.MSG_MULTIPLE_LINES, "package"),
74          };
75  
76          final List<String> expectedXpathQueries = Arrays.asList(
77                  "/COMPILATION_UNIT", "/COMPILATION_UNIT/PACKAGE_DEF"
78          );
79  
80          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
81      }
82  
83      @Test
84      public void testThree() throws Exception {
85          final File fileToProcess = new File(
86                  getPath("InputXpathEmptyLineSeparatorThree.java")
87          );
88  
89          final DefaultConfiguration moduleConfig =
90                  createModuleConfig(EmptyLineSeparatorCheck.class);
91          moduleConfig.addProperty("tokens", "METHOD_DEF");
92  
93          final String[] expectedViolation = {
94              "9:5: " + getCheckMessage(EmptyLineSeparatorCheck.class,
95                      EmptyLineSeparatorCheck.MSG_SHOULD_BE_SEPARATED, "METHOD_DEF"),
96          };
97  
98          final List<String> expectedXpathQueries = Arrays.asList(
99                  "/COMPILATION_UNIT/CLASS_DEF"
100                         + "[./IDENT[@text='InputXpathEmptyLineSeparatorThree']]"
101                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo1']]",
102 
103                 "/COMPILATION_UNIT/CLASS_DEF"
104                         + "[./IDENT[@text='InputXpathEmptyLineSeparatorThree']]"
105                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo1']]"
106                         + "/MODIFIERS",
107 
108                 "/COMPILATION_UNIT/CLASS_DEF"
109                         + "[./IDENT[@text='InputXpathEmptyLineSeparatorThree']]"
110                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo1']]"
111                         + "/MODIFIERS/LITERAL_PUBLIC"
112         );
113 
114         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
115     }
116 
117     @Test
118     public void testFour() throws Exception {
119         final File fileToProcess = new File(
120                 getPath("InputXpathEmptyLineSeparatorFour.java")
121         );
122 
123         final DefaultConfiguration moduleConfig =
124                 createModuleConfig(EmptyLineSeparatorCheck.class);
125         moduleConfig.addProperty("allowMultipleEmptyLines", "false");
126 
127         final String[] expectedViolation = {
128             "12:25: " + getCheckMessage(EmptyLineSeparatorCheck.class,
129                     EmptyLineSeparatorCheck.MSG_MULTIPLE_LINES_AFTER, "}"),
130         };
131 
132         final List<String> expectedXpathQueries = Collections.singletonList(
133                 "/COMPILATION_UNIT/CLASS_DEF"
134                         + "[./IDENT[@text='InputXpathEmptyLineSeparatorFour']]"
135                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo1']]/SLIST/RCURLY"
136         );
137 
138         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
139     }
140 
141     @Test
142     public void testFive() throws Exception {
143         final File fileToProcess = new File(
144                 getPath("InputXpathEmptyLineSeparatorFive.java")
145         );
146 
147         final DefaultConfiguration moduleConfig =
148                 createModuleConfig(EmptyLineSeparatorCheck.class);
149         moduleConfig.addProperty("allowMultipleEmptyLines", "false");
150         moduleConfig.addProperty("allowMultipleEmptyLinesInsideClassMembers", "false");
151 
152         final String[] expectedViolation = {
153             "14:15: " + getCheckMessage(EmptyLineSeparatorCheck.class,
154                     EmptyLineSeparatorCheck.MSG_MULTIPLE_LINES_INSIDE),
155         };
156 
157         final List<String> expectedXpathQueries = Collections.singletonList(
158                 "/COMPILATION_UNIT/CLASS_DEF"
159                         + "[./IDENT[@text='InputXpathEmptyLineSeparatorFive']]"
160                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo1']]/SLIST/LITERAL_TRY/SLIST"
161                         + "/SINGLE_LINE_COMMENT/COMMENT_CONTENT[@text=' warn\\n']"
162         );
163 
164         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
165     }
166 }