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