View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.CovariantEqualsCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
29  
30  public class CovariantEqualsCheckTest
31      extends AbstractModuleTestSupport {
32  
33      @Override
34      public String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/coding/covariantequals";
36      }
37  
38      @Test
39      public void testDefaultOne()
40              throws Exception {
41          final String[] expected = {
42              "17:24: " + getCheckMessage(MSG_KEY),
43              "36:20: " + getCheckMessage(MSG_KEY),
44              "70:20: " + getCheckMessage(MSG_KEY),
45              "84:28: " + getCheckMessage(MSG_KEY),
46          };
47          verifyWithInlineConfigParser(
48                  getPath("InputCovariantEqualsOne.java"), expected);
49      }
50  
51      @Test
52      public void testDefaultTwo()
53              throws Exception {
54          final String[] expected = {
55              "41:20: " + getCheckMessage(MSG_KEY),
56              "45:9: " + getCheckMessage(MSG_KEY),
57          };
58          verifyWithInlineConfigParser(
59                  getPath("InputCovariantEqualsTwo.java"), expected);
60      }
61  
62      @Test
63      public void testCovariantEqualsRecords()
64              throws Exception {
65          final String[] expected = {
66              "13:24: " + getCheckMessage(MSG_KEY),
67              "29:28: " + getCheckMessage(MSG_KEY),
68          };
69          verifyWithInlineConfigParser(
70                  getPath("InputCovariantEqualsRecords.java"), expected);
71      }
72  
73      @Test
74      public void testTokensNotNull() {
75          final CovariantEqualsCheck check = new CovariantEqualsCheck();
76          assertWithMessage("Acceptable tokens should not be null")
77                  .that(check.getAcceptableTokens())
78                  .isNotNull();
79          assertWithMessage("Default tokens should not be null")
80                  .that(check.getDefaultTokens())
81                  .isNotNull();
82          assertWithMessage("Required tokens should not be null")
83                  .that(check.getRequiredTokens())
84                  .isNotNull();
85      }
86  
87      @Test
88      public void testCovariantEqualsCompactSourceFile()
89              throws Exception {
90          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
91          verifyWithInlineConfigParser(
92                  getNonCompilablePath(
93                      "compact/InputCovariantEqualsCompactSourceFile.java"),
94                  expected);
95      }
96  
97      @Test
98      public void testCovariantEqualsCompactSourceFileMultiple()
99              throws Exception {
100         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
101         verifyWithInlineConfigParser(
102                 getNonCompilablePath(
103                     "compact/InputCovariantEqualsCompactSourceFileMultiple.java"),
104                 expected);
105     }
106 
107     @Test
108     public void testCovariantEqualsCompactSourceFileNoViolation()
109             throws Exception {
110         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
111         verifyWithInlineConfigParser(
112                 getNonCompilablePath(
113                     "compact/InputCovariantEqualsCompactSourceFileNoViolation.java"),
114                 expected);
115     }
116 
117 }