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.EqualsHashCodeCheck;
30
31 public class XpathRegressionEqualsHashCodeTest extends AbstractXpathTestSupport {
32 @Override
33 protected String getCheckName() {
34 return EqualsHashCodeCheck.class.getSimpleName();
35 }
36
37 @Test
38 public void testEqualsOnly() throws Exception {
39 final File fileToProcess = new File(
40 getPath("InputXpathEqualsHashCodeEqualsOnly.java"));
41
42 final DefaultConfiguration moduleConfig = createModuleConfig(EqualsHashCodeCheck.class);
43
44 final String[] expectedViolation = {
45 "4:5: " + getCheckMessage(EqualsHashCodeCheck.class,
46 EqualsHashCodeCheck.MSG_KEY_HASHCODE),
47 };
48
49 final List<String> expectedXpathQueries = Arrays.asList(
50 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
51 + "[@text='InputXpathEqualsHashCodeEqualsOnly']]"
52 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]",
53 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
54 + "[@text='InputXpathEqualsHashCodeEqualsOnly']]/OBJBLOCK/"
55 + "METHOD_DEF[./IDENT[@text='equals']]/MODIFIERS",
56 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
57 + "[@text='InputXpathEqualsHashCodeEqualsOnly']]/OBJBLOCK/"
58 + "METHOD_DEF[./IDENT[@text='equals']]/MODIFIERS/LITERAL_PUBLIC"
59 );
60
61 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
62 }
63
64 @Test
65 public void testHashCodeOnly() throws Exception {
66 final File fileToProcess = new File(
67 getPath("InputXpathEqualsHashCodeHashCodeOnly.java"));
68
69 final DefaultConfiguration moduleConfig = createModuleConfig(EqualsHashCodeCheck.class);
70
71 final String[] expectedViolation = {
72 "4:5: " + getCheckMessage(EqualsHashCodeCheck.class,
73 EqualsHashCodeCheck.MSG_KEY_EQUALS),
74 };
75
76 final List<String> expectedXpathQueries = Arrays.asList(
77 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
78 + "[@text='InputXpathEqualsHashCodeHashCodeOnly']]"
79 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='hashCode']]",
80 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
81 + "[@text='InputXpathEqualsHashCodeHashCodeOnly']]"
82 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='hashCode']]/MODIFIERS",
83 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
84 + "[@text='InputXpathEqualsHashCodeHashCodeOnly']]"
85 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='hashCode']]/MODIFIERS/LITERAL_PUBLIC"
86 );
87
88 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
89 }
90
91 @Test
92 public void testNestedCase() throws Exception {
93 final File fileToProcess = new File(
94 getPath("InputXpathEqualsHashCodeNestedCase.java"));
95
96 final DefaultConfiguration moduleConfig = createModuleConfig(EqualsHashCodeCheck.class);
97
98 final String[] expectedViolation = {
99 "5:9: " + getCheckMessage(EqualsHashCodeCheck.class,
100 EqualsHashCodeCheck.MSG_KEY_HASHCODE),
101 };
102
103 final List<String> expectedXpathQueries = Arrays.asList(
104 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
105 + "[@text='InputXpathEqualsHashCodeNestedCase']]"
106 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='innerClass']]"
107 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]",
108 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
109 + "[@text='InputXpathEqualsHashCodeNestedCase']]"
110 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='innerClass']]"
111 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]/MODIFIERS",
112 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
113 + "[@text='InputXpathEqualsHashCodeNestedCase']]"
114 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='innerClass']]"
115 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]"
116 + "/MODIFIERS/LITERAL_PUBLIC"
117 );
118
119 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
120 }
121 }