View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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.EqualsHashCodeCheck;
31  
32  public class XpathRegressionEqualsHashCodeTest extends AbstractXpathTestSupport {
33      @Override
34      protected String getCheckName() {
35          return EqualsHashCodeCheck.class.getSimpleName();
36      }
37  
38      @Override
39      protected String getPackageLocation() {
40          return "org/checkstyle/suppressionxpathfilter/coding/equalshashcode";
41      }
42  
43      @Test
44      public void testEqualsOnly() throws Exception {
45          final File fileToProcess = new File(
46              getPath("InputXpathEqualsHashCodeEqualsOnly.java"));
47  
48          final DefaultConfiguration moduleConfig = createModuleConfig(EqualsHashCodeCheck.class);
49  
50          final String[] expectedViolation = {
51              "4:5: " + getCheckMessage(EqualsHashCodeCheck.class,
52                      EqualsHashCodeCheck.MSG_KEY_HASHCODE),
53          };
54  
55          final List<String> expectedXpathQueries = Arrays.asList(
56                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
57                          + "[@text='InputXpathEqualsHashCodeEqualsOnly']]"
58                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]",
59                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
60                          + "[@text='InputXpathEqualsHashCodeEqualsOnly']]/OBJBLOCK/"
61                          + "METHOD_DEF[./IDENT[@text='equals']]/MODIFIERS",
62                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
63                          + "[@text='InputXpathEqualsHashCodeEqualsOnly']]/OBJBLOCK/"
64                          + "METHOD_DEF[./IDENT[@text='equals']]/MODIFIERS/LITERAL_PUBLIC"
65          );
66  
67          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
68      }
69  
70      @Test
71      public void testHashCodeOnly() throws Exception {
72          final File fileToProcess = new File(
73              getPath("InputXpathEqualsHashCodeHashCodeOnly.java"));
74  
75          final DefaultConfiguration moduleConfig = createModuleConfig(EqualsHashCodeCheck.class);
76  
77          final String[] expectedViolation = {
78              "4:5: " + getCheckMessage(EqualsHashCodeCheck.class,
79                      EqualsHashCodeCheck.MSG_KEY_EQUALS),
80          };
81  
82          final List<String> expectedXpathQueries = Arrays.asList(
83                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
84                          + "[@text='InputXpathEqualsHashCodeHashCodeOnly']]"
85                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='hashCode']]",
86                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
87                          + "[@text='InputXpathEqualsHashCodeHashCodeOnly']]"
88                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='hashCode']]/MODIFIERS",
89                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
90                          + "[@text='InputXpathEqualsHashCodeHashCodeOnly']]"
91                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='hashCode']]/MODIFIERS/LITERAL_PUBLIC"
92          );
93  
94          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
95      }
96  
97      @Test
98      public void testNestedCase() throws Exception {
99          final File fileToProcess = new File(
100             getPath("InputXpathEqualsHashCodeNestedCase.java"));
101 
102         final DefaultConfiguration moduleConfig = createModuleConfig(EqualsHashCodeCheck.class);
103 
104         final String[] expectedViolation = {
105             "5:9: " + getCheckMessage(EqualsHashCodeCheck.class,
106                     EqualsHashCodeCheck.MSG_KEY_HASHCODE),
107         };
108 
109         final List<String> expectedXpathQueries = Arrays.asList(
110                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
111                         + "[@text='InputXpathEqualsHashCodeNestedCase']]"
112                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='innerClass']]"
113                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]",
114                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
115                         + "[@text='InputXpathEqualsHashCodeNestedCase']]"
116                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='innerClass']]"
117                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]/MODIFIERS",
118                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
119                         + "[@text='InputXpathEqualsHashCodeNestedCase']]"
120                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='innerClass']]"
121                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]"
122                         + "/MODIFIERS/LITERAL_PUBLIC"
123         );
124 
125         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
126     }
127 }