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