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