View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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  
34      @Override
35      protected String getCheckName() {
36          return EqualsHashCodeCheck.class.getSimpleName();
37      }
38  
39      @Override
40      public String getPackageLocation() {
41          return "org/checkstyle/suppressionxpathfilter/coding/equalshashcode";
42      }
43  
44      @Test
45      public void testEqualsOnly() throws Exception {
46          final File fileToProcess = new File(
47              getPath("InputXpathEqualsHashCodeEqualsOnly.java"));
48  
49          final DefaultConfiguration moduleConfig = createModuleConfig(EqualsHashCodeCheck.class);
50  
51          final String[] expectedViolation = {
52              "4:5: " + getCheckMessage(EqualsHashCodeCheck.class,
53                      EqualsHashCodeCheck.MSG_KEY_HASHCODE),
54          };
55  
56          final List<String> expectedXpathQueries = Arrays.asList(
57                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
58                          + "[@text='InputXpathEqualsHashCodeEqualsOnly']]"
59                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]",
60                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
61                          + "[@text='InputXpathEqualsHashCodeEqualsOnly']]/OBJBLOCK/"
62                          + "METHOD_DEF[./IDENT[@text='equals']]/MODIFIERS",
63                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
64                          + "[@text='InputXpathEqualsHashCodeEqualsOnly']]/OBJBLOCK/"
65                          + "METHOD_DEF[./IDENT[@text='equals']]/MODIFIERS/LITERAL_PUBLIC"
66          );
67  
68          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
69      }
70  
71      @Test
72      public void testHashCodeOnly() throws Exception {
73          final File fileToProcess = new File(
74              getPath("InputXpathEqualsHashCodeHashCodeOnly.java"));
75  
76          final DefaultConfiguration moduleConfig = createModuleConfig(EqualsHashCodeCheck.class);
77  
78          final String[] expectedViolation = {
79              "4:5: " + getCheckMessage(EqualsHashCodeCheck.class,
80                      EqualsHashCodeCheck.MSG_KEY_EQUALS),
81          };
82  
83          final List<String> expectedXpathQueries = Arrays.asList(
84                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
85                          + "[@text='InputXpathEqualsHashCodeHashCodeOnly']]"
86                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='hashCode']]",
87                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
88                          + "[@text='InputXpathEqualsHashCodeHashCodeOnly']]"
89                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='hashCode']]/MODIFIERS",
90                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
91                          + "[@text='InputXpathEqualsHashCodeHashCodeOnly']]"
92                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='hashCode']]/MODIFIERS/LITERAL_PUBLIC"
93          );
94  
95          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
96      }
97  
98      @Test
99      public void testNestedCase() throws Exception {
100         final File fileToProcess = new File(
101             getPath("InputXpathEqualsHashCodeNestedCase.java"));
102 
103         final DefaultConfiguration moduleConfig = createModuleConfig(EqualsHashCodeCheck.class);
104 
105         final String[] expectedViolation = {
106             "5:9: " + getCheckMessage(EqualsHashCodeCheck.class,
107                     EqualsHashCodeCheck.MSG_KEY_HASHCODE),
108         };
109 
110         final List<String> expectedXpathQueries = Arrays.asList(
111                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
112                         + "[@text='InputXpathEqualsHashCodeNestedCase']]"
113                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='innerClass']]"
114                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]",
115                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
116                         + "[@text='InputXpathEqualsHashCodeNestedCase']]"
117                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='innerClass']]"
118                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]/MODIFIERS",
119                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
120                         + "[@text='InputXpathEqualsHashCodeNestedCase']]"
121                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='innerClass']]"
122                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='equals']]"
123                         + "/MODIFIERS/LITERAL_PUBLIC"
124         );
125 
126         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
127     }
128 
129 }