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 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.LeftCurlyCheck;
32 import com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyOption;
33
34 public class XpathRegressionLeftCurlyTest extends AbstractXpathTestSupport {
35
36 private final String checkName = LeftCurlyCheck.class.getSimpleName();
37
38 @Override
39 protected String getPackageLocation() {
40 return "org/checkstyle/suppressionxpathfilter/blocks/leftcurly";
41 }
42
43 @Override
44 protected String getCheckName() {
45 return checkName;
46 }
47
48 @Test
49 public void testOne() throws Exception {
50 final File fileToProcess =
51 new File(getPath("InputXpathLeftCurlyOne.java"));
52
53 final DefaultConfiguration moduleConfig =
54 createModuleConfig(LeftCurlyCheck.class);
55
56 final String[] expectedViolation = {
57 "4:1: " + getCheckMessage(LeftCurlyCheck.class,
58 LeftCurlyCheck.MSG_KEY_LINE_PREVIOUS, "{", 1),
59 };
60
61 final List<String> expectedXpathQueries = Arrays.asList(
62 "/COMPILATION_UNIT/CLASS_DEF"
63 + "[./IDENT[@text='InputXpathLeftCurlyOne']]/OBJBLOCK",
64 "/COMPILATION_UNIT/CLASS_DEF"
65 + "[./IDENT[@text='InputXpathLeftCurlyOne']]/OBJBLOCK/LCURLY"
66 );
67
68 runVerifications(moduleConfig, fileToProcess, expectedViolation,
69 expectedXpathQueries);
70 }
71
72 @Test
73 public void testTwo() throws Exception {
74 final File fileToProcess =
75 new File(getPath("InputXpathLeftCurlyTwo.java"));
76
77 final DefaultConfiguration moduleConfig =
78 createModuleConfig(LeftCurlyCheck.class);
79 moduleConfig.addProperty("option", LeftCurlyOption.NL.toString());
80
81 final String[] expectedViolation = {
82 "3:37: " + getCheckMessage(LeftCurlyCheck.class,
83 LeftCurlyCheck.MSG_KEY_LINE_NEW, "{", 37),
84 };
85
86 final List<String> expectedXpathQueries = Arrays.asList(
87 "/COMPILATION_UNIT/CLASS_DEF"
88 + "[./IDENT[@text='InputXpathLeftCurlyTwo']]/OBJBLOCK",
89 "/COMPILATION_UNIT/CLASS_DEF"
90 + "[./IDENT[@text='InputXpathLeftCurlyTwo']]/OBJBLOCK/LCURLY"
91 );
92
93 runVerifications(moduleConfig, fileToProcess, expectedViolation,
94 expectedXpathQueries);
95 }
96
97 @Test
98 public void testThree() throws Exception {
99 final File fileToProcess =
100 new File(getPath("InputXpathLeftCurlyThree.java"));
101
102 final DefaultConfiguration moduleConfig =
103 createModuleConfig(LeftCurlyCheck.class);
104
105 final String[] expectedViolation = {
106 "5:19: " + getCheckMessage(LeftCurlyCheck.class,
107 LeftCurlyCheck.MSG_KEY_LINE_BREAK_AFTER, "{", 19),
108 };
109
110 final List<String> expectedXpathQueries = Collections.singletonList(
111 "/COMPILATION_UNIT/CLASS_DEF"
112 + "[./IDENT[@text='InputXpathLeftCurlyThree']]/OBJBLOCK"
113 + "/METHOD_DEF[./IDENT[@text='sample']]/SLIST/LITERAL_IF/SLIST"
114 );
115
116 runVerifications(moduleConfig, fileToProcess, expectedViolation,
117 expectedXpathQueries);
118 }
119 }