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.VariableDeclarationUsageDistanceCheck;
30
31 public class XpathRegressionVariableDeclarationUsageDistanceTest extends AbstractXpathTestSupport {
32 private final String checkName = VariableDeclarationUsageDistanceCheck.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 "InputXpathVariableDeclarationUsageDistanceOne.java"));
43
44 final DefaultConfiguration moduleConfig =
45 createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
46 moduleConfig.addProperty("allowedDistance", "1");
47 moduleConfig.addProperty("ignoreVariablePattern", "");
48 moduleConfig.addProperty("validateBetweenScopes", "true");
49 moduleConfig.addProperty("ignoreFinal", "false");
50
51 final String[] expectedViolation = {
52 "7:9: " + getCheckMessage(VariableDeclarationUsageDistanceCheck.class,
53 VariableDeclarationUsageDistanceCheck.MSG_KEY, "temp", 2, 1),
54 };
55
56 final List<String> expectedXpathQueries = Arrays.asList(
57 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
58 + "'InputXpathVariableDeclarationUsageDistanceOne']]/"
59 + "OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
60 + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]",
61 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
62 + "'InputXpathVariableDeclarationUsageDistanceOne']]/"
63 + "OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
64 + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]/MODIFIERS",
65 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
66 + "'InputXpathVariableDeclarationUsageDistanceOne']]/"
67 + "OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
68 + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]/TYPE",
69 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
70 + "'InputXpathVariableDeclarationUsageDistanceOne']]/"
71 + "OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
72 + "/SLIST/VARIABLE_DEF[./IDENT[@text='temp']]/TYPE/LITERAL_INT"
73 );
74
75 runVerifications(moduleConfig, fileToProcess, expectedViolation,
76 expectedXpathQueries);
77 }
78
79 @Test
80 public void testTwo() throws Exception {
81 final File fileToProcess = new File(getPath(
82 "InputXpathVariableDeclarationUsageDistanceTwo.java"));
83
84 final DefaultConfiguration moduleConfig =
85 createModuleConfig(VariableDeclarationUsageDistanceCheck.class);
86
87 moduleConfig.addProperty("allowedDistance", "1");
88 moduleConfig.addProperty("ignoreVariablePattern", "");
89 moduleConfig.addProperty("validateBetweenScopes", "true");
90 moduleConfig.addProperty("ignoreFinal", "false");
91
92 final String[] expectedViolation = {
93 "6:9: " + getCheckMessage(VariableDeclarationUsageDistanceCheck.class,
94 VariableDeclarationUsageDistanceCheck.MSG_KEY, "count", 2, 1),
95 };
96
97 final List<String> expectedXpathQueries = Arrays.asList(
98 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
99 + "'InputXpathVariableDeclarationUsageDistanceTwo']]"
100 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod2']]"
101 + "/SLIST/VARIABLE_DEF[./IDENT[@text='count']]",
102 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
103 + "'InputXpathVariableDeclarationUsageDistanceTwo']]"
104 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod2']]"
105 + "/SLIST/VARIABLE_DEF[./IDENT[@text='count']]/MODIFIERS",
106 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
107 + "'InputXpathVariableDeclarationUsageDistanceTwo']]"
108 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod2']]"
109 + "/SLIST/VARIABLE_DEF[./IDENT[@text='count']]/TYPE",
110 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
111 + "'InputXpathVariableDeclarationUsageDistanceTwo']]"
112 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='testMethod2']]"
113 + "/SLIST/VARIABLE_DEF[./IDENT[@text='count']]/TYPE/LITERAL_INT"
114 );
115
116 runVerifications(moduleConfig, fileToProcess, expectedViolation,
117 expectedXpathQueries);
118 }
119
120 }