View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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;
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 }