1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.checkstyle.suppressionxpathfilter.coding;
21
22 import java.io.File;
23 import java.util.Arrays;
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.coding.MultipleVariableDeclarationsCheck;
31
32 public class XpathRegressionMultipleVariableDeclarationsTest extends AbstractXpathTestSupport {
33
34 private final String checkName = MultipleVariableDeclarationsCheck.class.getSimpleName();
35
36 @Override
37 protected String getCheckName() {
38 return checkName;
39 }
40
41 @Override
42 protected String getPackageLocation() {
43 return "org/checkstyle/suppressionxpathfilter/coding/multiplevariabledeclarations";
44 }
45
46 @Test
47 public void testCommaSeparator() throws Exception {
48 final File fileToProcess = new File(
49 getPath("InputXpathMultipleVariableDeclarationsCommaSeparator.java"));
50
51 final DefaultConfiguration moduleConfig =
52 createModuleConfig(MultipleVariableDeclarationsCheck.class);
53
54 final String[] expectedViolation = {
55 "4:5: " + getCheckMessage(MultipleVariableDeclarationsCheck.class,
56 MultipleVariableDeclarationsCheck.MSG_MULTIPLE_COMMA),
57 };
58
59 final List<String> expectedXpathQueries = Arrays.asList(
60 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
61 + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
62 + "/VARIABLE_DEF[./IDENT[@text='i']]",
63 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
64 + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
65 + "/VARIABLE_DEF[./IDENT[@text='i']]/MODIFIERS",
66 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
67 + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
68 + "/VARIABLE_DEF[./IDENT[@text='i']]/TYPE",
69 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
70 + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
71 + "/VARIABLE_DEF[./IDENT[@text='i']]/TYPE/LITERAL_INT",
72 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
73 + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
74 + "/VARIABLE_DEF[./IDENT[@text='j']]",
75 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
76 + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
77 + "/VARIABLE_DEF[./IDENT[@text='j']]/MODIFIERS",
78 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
79 + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
80 + "/VARIABLE_DEF[./IDENT[@text='j']]/TYPE",
81 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
82 + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
83 + "/VARIABLE_DEF[./IDENT[@text='j']]/TYPE/LITERAL_INT"
84 );
85
86 runVerifications(moduleConfig, fileToProcess, expectedViolation,
87 expectedXpathQueries);
88 }
89
90 @Test
91 public void testMultipleVariableDeclarations() throws Exception {
92 final File fileToProcess = new File(
93 getPath("InputXpathMultipleVariableDeclarations.java"));
94
95 final DefaultConfiguration moduleConfig =
96 createModuleConfig(MultipleVariableDeclarationsCheck.class);
97
98 final String[] expectedViolation = {
99 "4:5: " + getCheckMessage(MultipleVariableDeclarationsCheck.class,
100 MultipleVariableDeclarationsCheck.MSG_MULTIPLE),
101 };
102
103 final List<String> expectedXpathQueries = Arrays.asList(
104 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
105 + "@text='InputXpathMultipleVariableDeclarations']]/OBJBLOCK"
106 + "/VARIABLE_DEF[./IDENT[@text='i1']]",
107 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
108 + "@text='InputXpathMultipleVariableDeclarations']]/OBJBLOCK"
109 + "/VARIABLE_DEF[./IDENT[@text='i1']]/MODIFIERS",
110 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
111 + "@text='InputXpathMultipleVariableDeclarations']]/OBJBLOCK"
112 + "/VARIABLE_DEF[./IDENT[@text='i1']]/TYPE",
113 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
114 + "@text='InputXpathMultipleVariableDeclarations']]/OBJBLOCK"
115 + "/VARIABLE_DEF[./IDENT[@text='i1']]/TYPE/LITERAL_INT"
116 );
117
118 runVerifications(moduleConfig, fileToProcess, expectedViolation,
119 expectedXpathQueries);
120 }
121 }