1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 "37:13: " + getCheckMessage(MSG_KEY_HASHCODE),
50 };
51 verifyWithInlineConfigParser(
52 getPath("InputEqualsHashCodeSemanticTwo.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 "37: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("InputEqualsHashCodeSemanticTwo.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 }