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 static com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck.MSG_KEY_NEED_BRACES;
23  
24  import java.io.File;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
29  import org.junit.jupiter.api.Test;
30  
31  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32  import com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck;
33  
34  public class XpathRegressionNeedBracesTest extends AbstractXpathTestSupport {
35  
36      private final String checkName = NeedBracesCheck.class.getSimpleName();
37  
38      @Override
39      protected String getPackageLocation() {
40          return "org/checkstyle/suppressionxpathfilter/blocks/needbraces";
41      }
42  
43      @Override
44      protected String getCheckName() {
45          return checkName;
46      }
47  
48      @Test
49      public void testDo() throws Exception {
50          final File fileToProcess = new File(getPath(
51                  "InputXpathNeedBracesDo.java"));
52  
53          final DefaultConfiguration moduleConfig =
54                  createModuleConfig(NeedBracesCheck.class);
55  
56          final String[] expectedViolation = {
57              "13:9: " + getCheckMessage(NeedBracesCheck.class, MSG_KEY_NEED_BRACES, "do"),
58          };
59  
60          final List<String> expectedXpathQueries = Collections.singletonList(
61                  "/COMPILATION_UNIT/CLASS_DEF"
62                          + "[./IDENT[@text='InputXpathNeedBracesDo']]"
63                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_DO"
64          );
65  
66          runVerifications(moduleConfig, fileToProcess, expectedViolation,
67                  expectedXpathQueries);
68      }
69  
70      @Test
71      public void testSingleLine() throws Exception {
72          final File fileToProcess = new File(getPath(
73                  "InputXpathNeedBracesSingleLine.java"));
74  
75          final DefaultConfiguration moduleConfig =
76                  createModuleConfig(NeedBracesCheck.class);
77          moduleConfig.addProperty("allowSingleLineStatement", "true");
78  
79          final String[] expectedViolation = {
80              "16:9: " + getCheckMessage(NeedBracesCheck.class, MSG_KEY_NEED_BRACES, "if"),
81          };
82  
83          final List<String> expectedXpathQueries = Collections.singletonList(
84              "/COMPILATION_UNIT/CLASS_DEF"
85                  + "[./IDENT[@text='InputXpathNeedBracesSingleLine']]"
86                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_IF"
87          );
88  
89          runVerifications(moduleConfig, fileToProcess, expectedViolation,
90                  expectedXpathQueries);
91      }
92  
93      @Test
94      public void testSingleLineLambda() throws Exception {
95          final File fileToProcess = new File(getPath(
96                  "InputXpathNeedBracesSingleLineLambda.java"));
97  
98          final DefaultConfiguration moduleConfig =
99                  createModuleConfig(NeedBracesCheck.class);
100         moduleConfig.addProperty("tokens", "LAMBDA");
101         moduleConfig.addProperty("allowSingleLineStatement", "true");
102 
103         final String[] expectedViolation = {
104             "4:29: " + getCheckMessage(NeedBracesCheck.class, MSG_KEY_NEED_BRACES, "->"),
105         };
106 
107         final List<String> expectedXpathQueries = Collections.singletonList(
108             "/COMPILATION_UNIT/CLASS_DEF"
109                 + "[./IDENT[@text='InputXpathNeedBracesSingleLineLambda']]"
110                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='r3']]/ASSIGN/LAMBDA"
111         );
112 
113         runVerifications(moduleConfig, fileToProcess, expectedViolation,
114                 expectedXpathQueries);
115     }
116 
117     @Test
118     public void testEmptyLoopBody() throws Exception {
119         final File fileToProcess = new File(getPath(
120                 "InputXpathNeedBracesEmptyLoopBody.java"));
121 
122         final DefaultConfiguration moduleConfig =
123                 createModuleConfig(NeedBracesCheck.class);
124 
125         final String[] expectedViolation = {
126             "9:9: " + getCheckMessage(NeedBracesCheck.class, MSG_KEY_NEED_BRACES, "while"),
127         };
128 
129         final List<String> expectedXpathQueries = Collections.singletonList(
130             "/COMPILATION_UNIT/CLASS_DEF"
131                 + "[./IDENT[@text='InputXpathNeedBracesEmptyLoopBody']]"
132                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_WHILE"
133         );
134 
135         runVerifications(moduleConfig, fileToProcess, expectedViolation,
136                 expectedXpathQueries);
137     }
138 }