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.blocks;
21  
22  import java.io.File;
23  import java.util.Collections;
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.blocks.RightCurlyCheck;
31  import com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyOption;
32  
33  public class XpathRegressionRightCurlyTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = RightCurlyCheck.class.getSimpleName();
36  
37      @Override
38      protected String getPackageLocation() {
39          return "org/checkstyle/suppressionxpathfilter/blocks/rightcurly";
40      }
41  
42      @Override
43      protected String getCheckName() {
44          return checkName;
45      }
46  
47      @Test
48      public void testOne() throws Exception {
49          final File fileToProcess =
50                  new File(getPath("InputXpathRightCurlyOne.java"));
51  
52          final DefaultConfiguration moduleConfig =
53                  createModuleConfig(RightCurlyCheck.class);
54  
55          final String[] expectedViolation = {
56              "8:9: " + getCheckMessage(RightCurlyCheck.class,
57                  RightCurlyCheck.MSG_KEY_LINE_SAME, "}", 9),
58          };
59  
60          final List<String> expectedXpathQueries = Collections.singletonList(
61              "/COMPILATION_UNIT/CLASS_DEF"
62                  + "[./IDENT[@text='InputXpathRightCurlyOne']]/OBJBLOCK"
63                  + "/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_IF/SLIST/RCURLY"
64          );
65  
66          runVerifications(moduleConfig, fileToProcess, expectedViolation,
67                  expectedXpathQueries);
68      }
69  
70      @Test
71      public void testTwo() throws Exception {
72          final File fileToProcess =
73                  new File(getPath("InputXpathRightCurlyTwo.java"));
74  
75          final DefaultConfiguration moduleConfig =
76                  createModuleConfig(RightCurlyCheck.class);
77          moduleConfig.addProperty("option", RightCurlyOption.ALONE.toString());
78  
79          final String[] expectedViolation = {
80              "9:15: " + getCheckMessage(RightCurlyCheck.class,
81                  RightCurlyCheck.MSG_KEY_LINE_ALONE, "}", 15),
82          };
83  
84          final List<String> expectedXpathQueries = Collections.singletonList(
85              "/COMPILATION_UNIT/CLASS_DEF"
86                  + "[./IDENT[@text='InputXpathRightCurlyTwo']]/OBJBLOCK"
87                  + "/METHOD_DEF[./IDENT[@text='fooMethod']]/SLIST/LITERAL_TRY/SLIST/RCURLY"
88          );
89  
90          runVerifications(moduleConfig, fileToProcess, expectedViolation,
91                  expectedXpathQueries);
92      }
93  
94      @Test
95      public void testThree() throws Exception {
96          final File fileToProcess =
97                  new File(getPath("InputXpathRightCurlyThree.java"));
98  
99          final DefaultConfiguration moduleConfig =
100                 createModuleConfig(RightCurlyCheck.class);
101         moduleConfig.addProperty("option", RightCurlyOption.ALONE.toString());
102 
103         final String[] expectedViolation = {
104             "5:72: " + getCheckMessage(RightCurlyCheck.class,
105                 RightCurlyCheck.MSG_KEY_LINE_ALONE, "}", 72),
106         };
107 
108         final List<String> expectedXpathQueries = Collections.singletonList(
109             "/COMPILATION_UNIT/CLASS_DEF"
110                 + "[./IDENT[@text='InputXpathRightCurlyThree']]/OBJBLOCK"
111                 + "/METHOD_DEF[./IDENT[@text='sample']]/SLIST/LITERAL_IF/SLIST/RCURLY"
112         );
113 
114         runVerifications(moduleConfig, fileToProcess, expectedViolation,
115                 expectedXpathQueries);
116     }
117 
118     @Test
119     public void testFour() throws Exception {
120         final File fileToProcess =
121                 new File(getPath("InputXpathRightCurlyFour.java"));
122 
123         final DefaultConfiguration moduleConfig =
124                 createModuleConfig(RightCurlyCheck.class);
125         moduleConfig.addProperty("option", RightCurlyOption.SAME.toString());
126 
127         final String[] expectedViolation = {
128             "7:27: " + getCheckMessage(RightCurlyCheck.class,
129                 RightCurlyCheck.MSG_KEY_LINE_BREAK_BEFORE, "}", 27),
130         };
131 
132         final List<String> expectedXpathQueries = Collections.singletonList(
133             "/COMPILATION_UNIT/CLASS_DEF"
134                 + "[./IDENT[@text='InputXpathRightCurlyFour']]/OBJBLOCK"
135                 + "/METHOD_DEF[./IDENT[@text='sample']]/SLIST/LITERAL_IF/SLIST/RCURLY"
136         );
137 
138         runVerifications(moduleConfig, fileToProcess, expectedViolation,
139                 expectedXpathQueries);
140     }
141 }