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.blocks.LeftCurlyCheck;
31  import com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyOption;
32  
33  public class XpathRegressionLeftCurlyTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = LeftCurlyCheck.class.getSimpleName();
36  
37      @Override
38      protected String getCheckName() {
39          return checkName;
40      }
41  
42      @Test
43      public void testOne() throws Exception {
44          final File fileToProcess =
45                  new File(getPath("InputXpathLeftCurlyOne.java"));
46  
47          final DefaultConfiguration moduleConfig =
48                  createModuleConfig(LeftCurlyCheck.class);
49  
50          final String[] expectedViolation = {
51              "4:1: " + getCheckMessage(LeftCurlyCheck.class,
52                  LeftCurlyCheck.MSG_KEY_LINE_PREVIOUS, "{", 1),
53          };
54  
55          final List<String> expectedXpathQueries = Arrays.asList(
56              "/COMPILATION_UNIT/CLASS_DEF"
57                      + "[./IDENT[@text='InputXpathLeftCurlyOne']]/OBJBLOCK",
58              "/COMPILATION_UNIT/CLASS_DEF"
59                      + "[./IDENT[@text='InputXpathLeftCurlyOne']]/OBJBLOCK/LCURLY"
60          );
61  
62          runVerifications(moduleConfig, fileToProcess, expectedViolation,
63                  expectedXpathQueries);
64      }
65  
66      @Test
67      public void testTwo() throws Exception {
68          final File fileToProcess =
69                  new File(getPath("InputXpathLeftCurlyTwo.java"));
70  
71          final DefaultConfiguration moduleConfig =
72                  createModuleConfig(LeftCurlyCheck.class);
73          moduleConfig.addProperty("option", LeftCurlyOption.NL.toString());
74  
75          final String[] expectedViolation = {
76              "3:37: " + getCheckMessage(LeftCurlyCheck.class,
77                  LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 37),
78          };
79  
80          final List<String> expectedXpathQueries = Arrays.asList(
81              "/COMPILATION_UNIT/CLASS_DEF"
82                      + "[./IDENT[@text='InputXpathLeftCurlyTwo']]/OBJBLOCK",
83              "/COMPILATION_UNIT/CLASS_DEF"
84                      + "[./IDENT[@text='InputXpathLeftCurlyTwo']]/OBJBLOCK/LCURLY"
85          );
86  
87          runVerifications(moduleConfig, fileToProcess, expectedViolation,
88                  expectedXpathQueries);
89      }
90  
91      @Test
92      public void testThree() throws Exception {
93          final File fileToProcess =
94                  new File(getPath("InputXpathLeftCurlyThree.java"));
95  
96          final DefaultConfiguration moduleConfig =
97                  createModuleConfig(LeftCurlyCheck.class);
98  
99          final String[] expectedViolation = {
100             "5:19: " + getCheckMessage(LeftCurlyCheck.class,
101                 LeftCurlyCheck.MSG_KEY_LINE_BREAK_AFTER, "{", 19),
102         };
103 
104         final List<String> expectedXpathQueries = Collections.singletonList(
105             "/COMPILATION_UNIT/CLASS_DEF"
106                 + "[./IDENT[@text='InputXpathLeftCurlyThree']]/OBJBLOCK"
107                 + "/METHOD_DEF[./IDENT[@text='sample']]/SLIST/LITERAL_IF/SLIST"
108         );
109 
110         runVerifications(moduleConfig, fileToProcess, expectedViolation,
111                 expectedXpathQueries);
112     }
113 }