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.AvoidNestedBlocksCheck;
32  
33  public class XpathRegressionAvoidNestedBlocksTest extends AbstractXpathTestSupport {
34  
35      @Override
36      protected String getPackageLocation() {
37          return "org/checkstyle/suppressionxpathfilter/blocks/avoidnestedblocks";
38      }
39  
40      @Override
41      protected String getCheckName() {
42          return AvoidNestedBlocksCheck.class.getSimpleName();
43      }
44  
45      @Test
46      public void testEmpty() throws Exception {
47          final File fileToProcess = new File(
48                  getPath("InputXpathAvoidNestedBlocksEmpty.java"));
49  
50          final DefaultConfiguration moduleConfig =
51                  createModuleConfig(AvoidNestedBlocksCheck.class);
52  
53          final String[] expectedViolation = {
54              "6:9: " + getCheckMessage(AvoidNestedBlocksCheck.class,
55                      AvoidNestedBlocksCheck.MSG_KEY_BLOCK_NESTED),
56          };
57  
58          final List<String> expectedXpathQueries = Collections.singletonList(
59                  "/COMPILATION_UNIT/CLASS_DEF"
60                          + "[./IDENT[@text='InputXpathAvoidNestedBlocksEmpty']]"
61                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='empty']]/SLIST/SLIST"
62          );
63  
64          runVerifications(moduleConfig, fileToProcess, expectedViolation,
65                  expectedXpathQueries);
66      }
67  
68      @Test
69      public void testVariableAssignment() throws Exception {
70          final File fileToProcess = new File(
71                  getPath("InputXpathAvoidNestedBlocksVariable.java"));
72  
73          final DefaultConfiguration moduleConfig =
74                  createModuleConfig(AvoidNestedBlocksCheck.class);
75  
76          final String[] expectedViolation = {
77              "7:9: " + getCheckMessage(AvoidNestedBlocksCheck.class,
78                      AvoidNestedBlocksCheck.MSG_KEY_BLOCK_NESTED),
79          };
80  
81          final List<String> expectedXpathQueries = Collections.singletonList(
82                  "/COMPILATION_UNIT/CLASS_DEF"
83                          + "[./IDENT[@text='InputXpathAvoidNestedBlocksVariable']]"
84                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='varAssign']]/SLIST/SLIST"
85          );
86  
87          runVerifications(moduleConfig, fileToProcess, expectedViolation,
88                  expectedXpathQueries);
89      }
90  
91      @Test
92      public void testSwitchAllowInSwitchCaseFalse() throws Exception {
93          final File fileToProcess = new File(getPath(
94                  "InputXpathAvoidNestedBlocksNotAllowedInSwitchCase.java"));
95  
96          final DefaultConfiguration moduleConfig =
97                  createModuleConfig(AvoidNestedBlocksCheck.class);
98  
99          final String[] expectedViolation = {
100             "9:21: " + getCheckMessage(AvoidNestedBlocksCheck.class,
101                     AvoidNestedBlocksCheck.MSG_KEY_BLOCK_NESTED),
102         };
103 
104         final List<String> expectedXpathQueries = Arrays.asList(
105                 "/COMPILATION_UNIT/CLASS_DEF"
106                         + "[./IDENT[@text='InputXpathAvoidNestedBlocksNotAllowedInSwitchCase"
107                         + "']]/OBJBLOCK/METHOD_DEF"
108                         + "[./IDENT[@text='s']]/SLIST/LITERAL_SWITCH/CASE_GROUP/SLIST",
109                 "/COMPILATION_UNIT/CLASS_DEF"
110                         + "[./IDENT[@text='InputXpathAvoidNestedBlocksNotAllowedInSwitchCase"
111                         + "']]/OBJBLOCK/METHOD_DEF"
112                         + "[./IDENT[@text='s']]/SLIST/LITERAL_SWITCH/CASE_GROUP/SLIST/SLIST"
113         );
114 
115         runVerifications(moduleConfig, fileToProcess, expectedViolation,
116                 expectedXpathQueries);
117     }
118 
119     @Test
120     public void testSwitchAllowInSwitchCaseTrue() throws Exception {
121         final File fileToProcess = new File(
122                 getPath("InputXpathAvoidNestedBlocksAllowedInSwitchCase.java"));
123 
124         final DefaultConfiguration moduleConfig =
125                 createModuleConfig(AvoidNestedBlocksCheck.class);
126         moduleConfig.addProperty("allowInSwitchCase", "true");
127 
128         final String[] expectedViolation = {
129             "11:13: " + getCheckMessage(AvoidNestedBlocksCheck.class,
130                     AvoidNestedBlocksCheck.MSG_KEY_BLOCK_NESTED),
131         };
132 
133         final List<String> expectedXpathQueries = Collections.singletonList(
134                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
135                         + "[@text='InputXpathAvoidNestedBlocksAllowedInSwitchCase"
136                         + "']]/OBJBLOCK/METHOD_DEF[./IDENT[@text='s']]"
137                         + "/SLIST/LITERAL_SWITCH/CASE_GROUP/SLIST/SLIST"
138         );
139 
140         runVerifications(moduleConfig, fileToProcess, expectedViolation,
141                 expectedXpathQueries);
142     }
143 
144     @Test
145     public void testSwitchWithBreakOutside() throws Exception {
146         final File fileToProcess = new File(
147                 getPath("InputXpathAvoidNestedBlocksBreakOutside.java"));
148 
149         final DefaultConfiguration moduleConfig =
150                 createModuleConfig(AvoidNestedBlocksCheck.class);
151 
152         final String[] expectedViolation = {
153             "8:21: " + getCheckMessage(AvoidNestedBlocksCheck.class,
154                     AvoidNestedBlocksCheck.MSG_KEY_BLOCK_NESTED),
155         };
156 
157         final List<String> expectedXpathQueries = Arrays.asList(
158                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
159                         + "[@text='InputXpathAvoidNestedBlocksBreakOutside']]"
160                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='s']]"
161                         + "/SLIST/LITERAL_SWITCH/CASE_GROUP/SLIST",
162                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
163                         + "[@text='InputXpathAvoidNestedBlocksBreakOutside']]"
164                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='s']]"
165                         + "/SLIST/LITERAL_SWITCH/CASE_GROUP/SLIST/SLIST"
166         );
167 
168         runVerifications(moduleConfig, fileToProcess, expectedViolation,
169                 expectedXpathQueries);
170     }
171 
172 }