View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.puppycrawl.tools.checkstyle.checks.coding.EqualsHashCodeCheck.MSG_KEY_EQUALS;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.EqualsHashCodeCheck.MSG_KEY_HASHCODE;
24  
25  import java.io.File;
26  import java.util.Arrays;
27  import java.util.Collections;
28  import java.util.List;
29  
30  import org.junit.Assert;
31  import org.junit.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.CommonUtils;
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 DefaultConfiguration checkConfig =
49              createModuleConfig(EqualsHashCodeCheck.class);
50          final String[] expected = {
51              "94:13: " + getCheckMessage(MSG_KEY_HASHCODE),
52          };
53          verify(checkConfig, getPath("InputEqualsHashCodeSemantic.java"), expected);
54      }
55  
56      @Test
57      public void testNoEquals() throws Exception {
58          final DefaultConfiguration checkConfig =
59              createModuleConfig(EqualsHashCodeCheck.class);
60          final String[] expected = {
61              "4:5: " + getCheckMessage(MSG_KEY_EQUALS),
62          };
63          verify(checkConfig, getPath("InputEqualsHashCodeNoEquals.java"), expected);
64      }
65  
66      @Test
67      public void testBooleanMethods() throws Exception {
68          final DefaultConfiguration checkConfig =
69              createModuleConfig(EqualsHashCodeCheck.class);
70          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
71          verify(checkConfig, getPath("InputEqualsHashCode.java"), expected);
72      }
73  
74      @Test
75      public void testMultipleInputs() throws Exception {
76          final DefaultConfiguration checkConfig =
77              createModuleConfig(EqualsHashCodeCheck.class);
78  
79          final List<String> expectedFirstInputErrors = Collections.singletonList(
80              "4:5: " + getCheckMessage(MSG_KEY_EQUALS)
81          );
82          final List<String> expectedSecondInputErrors = Collections.singletonList(
83              "94:13: " + getCheckMessage(MSG_KEY_HASHCODE)
84          );
85          final List<String> expectedThirdInputErrors =
86              Arrays.asList(CommonUtils.EMPTY_STRING_ARRAY);
87  
88          final String firstInput = getPath("InputEqualsHashCodeNoEquals.java");
89          final String secondInput = getPath("InputEqualsHashCodeSemantic.java");
90          final String thirdInput = getPath("InputEqualsHashCode.java");
91  
92          final File[] inputs = {
93              new File(firstInput),
94              new File(secondInput),
95              new File(thirdInput),
96          };
97  
98          verify(createChecker(checkConfig), inputs, ImmutableMap.of(
99              firstInput, expectedFirstInputErrors,
100             secondInput, expectedSecondInputErrors,
101             thirdInput, expectedThirdInputErrors
102         ));
103     }
104 
105     @Test
106     public void testEqualsParameter() throws Exception {
107         final DefaultConfiguration checkConfig =
108             createModuleConfig(EqualsHashCodeCheck.class);
109         final String[] expected = {
110             "10:9: " + getCheckMessage(MSG_KEY_EQUALS),
111             "18:9: " + getCheckMessage(MSG_KEY_HASHCODE),
112             "48:9: " + getCheckMessage(MSG_KEY_HASHCODE),
113             "53:9: " + getCheckMessage(MSG_KEY_EQUALS),
114             "65:9: " + getCheckMessage(MSG_KEY_EQUALS),
115             "68:9: " + getCheckMessage(MSG_KEY_HASHCODE),
116             "75:9: " + getCheckMessage(MSG_KEY_EQUALS),
117             "82:9: " + getCheckMessage(MSG_KEY_HASHCODE),
118         };
119         verify(checkConfig, getPath("InputEqualsHashCodeEqualsParameter.java"), expected);
120     }
121 
122     @Test
123     public void testTokensNotNull() {
124         final EqualsHashCodeCheck check = new EqualsHashCodeCheck();
125         Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
126         Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
127         Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
128     }
129 
130 }