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 com.puppycrawl.tools.checkstyle.checks.coding;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.EqualsHashCodeCheck.MSG_KEY_EQUALS;
24  import static com.puppycrawl.tools.checkstyle.checks.coding.EqualsHashCodeCheck.MSG_KEY_HASHCODE;
25  
26  import java.io.File;
27  import java.util.Arrays;
28  import java.util.Collections;
29  import java.util.List;
30  
31  import org.junit.jupiter.api.Test;
32  
33  import com.google.common.collect.ImmutableMap;
34  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
36  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
37  
38  public class EqualsHashCodeCheckTest
39      extends AbstractModuleTestSupport {
40  
41      @Override
42      protected String getPackageLocation() {
43          return "com/puppycrawl/tools/checkstyle/checks/coding/equalshashcode";
44      }
45  
46      @Test
47      public void testSemantic() throws Exception {
48          final String[] expected = {
49              "96:13: " + getCheckMessage(MSG_KEY_HASHCODE),
50          };
51          verifyWithInlineConfigParser(
52                  getPath("InputEqualsHashCodeSemantic.java"), expected);
53      }
54  
55      @Test
56      public void testNoEquals() throws Exception {
57          final String[] expected = {
58              "10:5: " + getCheckMessage(MSG_KEY_EQUALS),
59          };
60          verifyWithInlineConfigParser(
61                  getPath("InputEqualsHashCodeNoEquals.java"), expected);
62      }
63  
64      @Test
65      public void testBooleanMethods() throws Exception {
66          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
67          verifyWithInlineConfigParser(
68                  getPath("InputEqualsHashCode.java"), expected);
69      }
70  
71      @Test
72      public void testMultipleInputs() throws Exception {
73          final DefaultConfiguration checkConfig =
74              createModuleConfig(EqualsHashCodeCheck.class);
75  
76          final List<String> expectedFirstInputErrors = Collections.singletonList(
77              "10:5: " + getCheckMessage(MSG_KEY_EQUALS)
78          );
79          final List<String> expectedSecondInputErrors = Collections.singletonList(
80              "96:13: " + getCheckMessage(MSG_KEY_HASHCODE)
81          );
82          final List<String> expectedThirdInputErrors =
83              Arrays.asList(CommonUtil.EMPTY_STRING_ARRAY);
84  
85          final String firstInput = getPath("InputEqualsHashCodeNoEquals.java");
86          final String secondInput = getPath("InputEqualsHashCodeSemantic.java");
87          final String thirdInput = getPath("InputEqualsHashCode.java");
88  
89          final File[] inputs = {
90              new File(firstInput),
91              new File(secondInput),
92              new File(thirdInput),
93          };
94  
95          verify(createChecker(checkConfig), inputs, ImmutableMap.of(
96              firstInput, expectedFirstInputErrors,
97              secondInput, expectedSecondInputErrors,
98              thirdInput, expectedThirdInputErrors
99          ));
100     }
101 
102     @Test
103     public void testEqualsParameter() throws Exception {
104         final String[] expected = {
105             "16:9: " + getCheckMessage(MSG_KEY_EQUALS),
106             "24:9: " + getCheckMessage(MSG_KEY_HASHCODE),
107             "54:9: " + getCheckMessage(MSG_KEY_HASHCODE),
108             "59:9: " + getCheckMessage(MSG_KEY_EQUALS),
109             "71:9: " + getCheckMessage(MSG_KEY_EQUALS),
110             "74:9: " + getCheckMessage(MSG_KEY_HASHCODE),
111             "81:9: " + getCheckMessage(MSG_KEY_EQUALS),
112             "88:9: " + getCheckMessage(MSG_KEY_HASHCODE),
113             "103:9: " + getCheckMessage(MSG_KEY_EQUALS),
114         };
115         verifyWithInlineConfigParser(
116                 getPath("InputEqualsHashCodeEqualsParameter.java"), expected);
117     }
118 
119     @Test
120     public void testTokensNotNull() {
121         final EqualsHashCodeCheck check = new EqualsHashCodeCheck();
122         assertWithMessage("Acceptable tokens should not be null")
123             .that(check.getAcceptableTokens())
124             .isNotNull();
125         assertWithMessage("Default tokens should not be null")
126             .that(check.getDefaultTokens())
127             .isNotNull();
128         assertWithMessage("Required tokens should not be null")
129             .that(check.getRequiredTokens())
130             .isNotNull();
131     }
132 
133 }