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