1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }