1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.checkstyle.suppressionxpathfilter.whitespace;
21
22 import java.io.File;
23 import java.util.Collections;
24 import java.util.List;
25
26 import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27 import org.junit.jupiter.api.Test;
28
29 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30 import com.puppycrawl.tools.checkstyle.checks.whitespace.WhitespaceBeforeEmptyBodyCheck;
31
32 public class XpathRegressionWhitespaceBeforeEmptyBodyTest extends AbstractXpathTestSupport {
33
34 private final String checkName = WhitespaceBeforeEmptyBodyCheck.class.getSimpleName();
35
36 @Override
37 protected String getCheckName() {
38 return checkName;
39 }
40
41 @Override
42 public String getPackageLocation() {
43 return "org/checkstyle/suppressionxpathfilter/whitespace/whitespacebeforeemptybody";
44 }
45
46 @Test
47 public void testWhitespaceBeforeEmptyBodySwitch() throws Exception {
48 final File fileToProcess =
49 new File(getPath("InputXpathWhitespaceBeforeEmptyBodySwitch.java"));
50
51 final DefaultConfiguration moduleConfig =
52 createModuleConfig(WhitespaceBeforeEmptyBodyCheck.class);
53
54 final String[] expectedViolation = {
55 "6:19: " + getCheckMessage(WhitespaceBeforeEmptyBodyCheck.class,
56 WhitespaceBeforeEmptyBodyCheck.MSG_KEY, "{"),
57 };
58
59 final List<String> expectedXpathQueries = Collections.singletonList(
60 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
61 + "@text='InputXpathWhitespaceBeforeEmptyBodySwitch']]/OBJBLOCK"
62 + "/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_SWITCH/LCURLY"
63 );
64
65 runVerifications(moduleConfig, fileToProcess, expectedViolation,
66 expectedXpathQueries);
67 }
68
69 @Test
70 public void testWhitespaceBeforeEmptyBodyMethod() throws Exception {
71 final File fileToProcess =
72 new File(getPath("InputXpathWhitespaceBeforeEmptyBodyMethod.java"));
73
74 final DefaultConfiguration moduleConfig =
75 createModuleConfig(WhitespaceBeforeEmptyBodyCheck.class);
76
77 final String[] expectedViolation = {
78 "5:15: " + getCheckMessage(WhitespaceBeforeEmptyBodyCheck.class,
79 WhitespaceBeforeEmptyBodyCheck.MSG_KEY, "{"),
80 };
81
82 final List<String> expectedXpathQueries = Collections.singletonList(
83 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
84 + "@text='InputXpathWhitespaceBeforeEmptyBodyMethod']]/OBJBLOCK"
85 + "/METHOD_DEF[./IDENT[@text='foo']]/SLIST"
86 );
87
88 runVerifications(moduleConfig, fileToProcess, expectedViolation,
89 expectedXpathQueries);
90 }
91
92 @Test
93 public void testWhitespaceBeforeEmptyBodyLoop() throws Exception {
94 final File fileToProcess =
95 new File(getPath("InputXpathWhitespaceBeforeEmptyBodyLoop.java"));
96
97 final DefaultConfiguration moduleConfig =
98 createModuleConfig(WhitespaceBeforeEmptyBodyCheck.class);
99
100 final String[] expectedViolation = {
101 "6:36: " + getCheckMessage(WhitespaceBeforeEmptyBodyCheck.class,
102 WhitespaceBeforeEmptyBodyCheck.MSG_KEY, "{"),
103 };
104
105 final List<String> expectedXpathQueries = Collections.singletonList(
106 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
107 + "@text='InputXpathWhitespaceBeforeEmptyBodyLoop']]/OBJBLOCK"
108 + "/METHOD_DEF[./IDENT[@text='method']]/SLIST/LITERAL_FOR/SLIST"
109 );
110
111 runVerifications(moduleConfig, fileToProcess, expectedViolation,
112 expectedXpathQueries);
113 }
114
115 }