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 java.io.File;
23 import java.util.Arrays;
24 import java.util.List;
25
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29 import com.puppycrawl.tools.checkstyle.checks.coding.UnusedLocalVariableCheck;
30
31 public class XpathRegressionUnusedLocalVariableTest extends AbstractXpathTestSupport {
32 private final String checkName = UnusedLocalVariableCheck.class.getSimpleName();
33
34 @Override
35 protected String getCheckName() {
36 return checkName;
37 }
38
39 @Test
40 public void testOne() throws Exception {
41 final File fileToProcess = new File(getPath(
42 "InputXpathUnusedLocalVariableOne.java"));
43
44 final DefaultConfiguration moduleConfig =
45 createModuleConfig(UnusedLocalVariableCheck.class);
46
47 final String[] expectedViolation = {
48 "6:9: " + getCheckMessage(UnusedLocalVariableCheck.class,
49 UnusedLocalVariableCheck.MSG_UNUSED_NAMED_LOCAL_VARIABLE, "a"),
50 };
51
52 final List<String> expectedXpathQueries = Arrays.asList(
53 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
54 + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
55 + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF[./IDENT[@text='a']]",
56 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
57 + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
58 + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
59 + "./IDENT[@text='a']]/MODIFIERS",
60 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
61 + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
62 + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
63 + "./IDENT[@text='a']]/TYPE",
64 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
65 + "@text='InputXpathUnusedLocalVariableOne']]/OBJBLOCK/"
66 + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
67 + "./IDENT[@text='a']]/TYPE/LITERAL_INT"
68 );
69
70 runVerifications(moduleConfig, fileToProcess, expectedViolation,
71 expectedXpathQueries);
72 }
73
74 @Test
75 public void testTwo() throws Exception {
76 final File fileToProcess = new File(getPath(
77 "InputXpathUnusedLocalVariableTwo.java"));
78
79 final DefaultConfiguration moduleConfig =
80 createModuleConfig(UnusedLocalVariableCheck.class);
81 moduleConfig.addProperty("allowUnnamedVariables", "false");
82
83 final String[] expectedViolation = {
84 "10:9: " + getCheckMessage(UnusedLocalVariableCheck.class,
85 UnusedLocalVariableCheck.MSG_UNUSED_LOCAL_VARIABLE, "b"),
86 };
87
88 final List<String> expectedXpathQueries = Arrays.asList(
89 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
90 + "@text='InputXpathUnusedLocalVariableTwo']]/OBJBLOCK/"
91 + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF[./IDENT[@text='b']]",
92 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
93 + "@text='InputXpathUnusedLocalVariableTwo']]/OBJBLOCK/"
94 + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
95 + "./IDENT[@text='b']]/MODIFIERS",
96 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
97 + "@text='InputXpathUnusedLocalVariableTwo']]/OBJBLOCK/"
98 + "METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
99 + "./IDENT[@text='b']]/TYPE", "/COMPILATION_UNIT/CLASS_DEF["
100 + "./IDENT[@text='InputXpathUnusedLocalVariableTwo']]/"
101 + "OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/SLIST/VARIABLE_DEF["
102 + "./IDENT[@text='b']]/TYPE/LITERAL_INT"
103 );
104
105 runVerifications(moduleConfig, fileToProcess, expectedViolation,
106 expectedXpathQueries);
107
108 }
109 }