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