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