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.EqualsAvoidNullCheck.MSG_EQUALS_AVOID_NULL;
24  import static com.puppycrawl.tools.checkstyle.checks.coding.EqualsAvoidNullCheck.MSG_EQUALS_IGNORE_CASE_AVOID_NULL;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  
30  public class EqualsAvoidNullCheckTest extends AbstractModuleTestSupport {
31  
32      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/coding/equalsavoidnull";
35      }
36  
37      @Test
38      public void testEqualsWithDefault() throws Exception {
39  
40          final String[] expected = {
41              "44:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
42              "46:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
43              "48:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
44              "50:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
45              "52:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
46              "54:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
47              "76:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
48              "78:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
49              "80:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
50              "82:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
51              "84:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
52              "86:27: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
53              "245:21: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
54              "251:24: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
55              "252:21: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
56              "254:24: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
57              "255:27: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
58              "256:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
59              "257:29: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
60              "258:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
61              "275:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
62              "276:35: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
63              "277:40: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
64              "279:40: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
65              "280:45: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
66              "281:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
67              "302:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
68              "307:29: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
69              "308:29: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
70              "315:25: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
71              "318:25: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
72              "319:34: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
73              "321:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
74              "322:45: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
75              "323:33: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
76              "327:43: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
77              "328:51: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
78              "329:43: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
79              "330:55: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
80              "343:28: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
81              "344:36: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
82              "345:42: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
83              "346:34: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
84              "347:37: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
85              "351:36: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
86              "352:44: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
87              "353:42: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
88              "354:45: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
89              "365:35: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
90              "376:30: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
91              "402:35: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
92              "423:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
93              "424:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
94              "425:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
95              "429:22: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
96          };
97          verifyWithInlineConfigParser(
98                  getPath("InputEqualsAvoidNull.java"), expected);
99      }
100 
101     @Test
102     public void testEqualsWithoutEqualsIgnoreCase() throws Exception {
103 
104         final String[] expected = {
105             "245:21: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
106             "251:24: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
107             "252:21: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
108             "254:24: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
109             "255:27: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
110             "256:33: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
111             "257:29: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
112             "258:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
113             "275:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
114             "276:35: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
115             "277:41: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
116             "279:40: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
117             "280:45: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
118             "281:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
119             "302:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
120             "307:29: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
121             "308:29: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
122             "315:25: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
123             "318:25: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
124             "319:34: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
125             "321:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
126             "322:45: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
127             "323:33: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
128             "326:66: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
129             "327:51: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
130             "328:53: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
131             "329:55: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
132             "342:28: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
133             "343:37: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
134             "344:42: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
135             "345:34: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
136             "346:37: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
137             "350:36: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
138             "351:45: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
139             "352:42: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
140             "353:45: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
141             "364:35: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
142             "375:30: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
143             "401:35: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
144             "422:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
145             "423:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
146             "424:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
147             "428:22: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
148         };
149         verifyWithInlineConfigParser(
150                 getPath("InputEqualsAvoidNullIgnoreCase.java"), expected);
151     }
152 
153     @Test
154     public void testEqualsOnTheSameLine() throws Exception {
155 
156         final String[] expected = {
157             "14:28: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
158             "21:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
159         };
160         verifyWithInlineConfigParser(
161                 getPath("InputEqualsAvoidNullOnTheSameLine.java"), expected);
162     }
163 
164     @Test
165     public void testEqualsNested() throws Exception {
166 
167         final String[] expected = {
168             "25:34: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
169             "26:34: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
170             "27:34: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
171             "33:34: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
172             "36:39: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
173             "39:39: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
174             "42:39: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
175             "45:39: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
176         };
177         verifyWithInlineConfigParser(
178                 getPath("InputEqualsAvoidNullNested.java"), expected);
179     }
180 
181     @Test
182     public void testEqualsSuperClass() throws Exception {
183 
184         final String[] expected = {
185             "23:35: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
186         };
187         verifyWithInlineConfigParser(
188                 getPath("InputEqualsAvoidNullSuperClass.java"), expected);
189     }
190 
191     @Test
192     public void testInputEqualsAvoidNullEnhancedInstanceof() throws Exception {
193 
194         final String[] expected = {
195             "15:45: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
196             "18:36: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
197             "26:50: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
198             "27:38: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
199             "28:38: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
200             "29:35: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
201         };
202         verifyWithInlineConfigParser(
203                 getNonCompilablePath("InputEqualsAvoidNullEnhancedInstanceof.java"),
204                 expected);
205     }
206 
207     @Test
208     public void testMisc() throws Exception {
209 
210         final String[] expected = {
211             "20:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
212         };
213         verifyWithInlineConfigParser(
214                 getPath("InputEqualsAvoidNullMisc.java"), expected);
215     }
216 
217     @Test
218     public void testRecordsAndCompactCtors() throws Exception {
219 
220         final String[] expected = {
221             "15:23: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
222             "22:34: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
223             "34:33: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
224             "41:33: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
225             "49:33: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
226         };
227         verifyWithInlineConfigParser(
228                 getNonCompilablePath("InputEqualsAvoidNullRecordsAndCompactCtors.java"),
229                 expected);
230     }
231 
232     @Test
233     public void testEqualsAvoidNullTextBlocks() throws Exception {
234 
235         final String[] expected = {
236             "13:24: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
237             "15:24: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
238             "22:19: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
239             "32:31: " + getCheckMessage(MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
240         };
241 
242         verifyWithInlineConfigParser(
243                 getNonCompilablePath("InputEqualsAvoidNullTextBlocks.java"),
244             expected);
245     }
246 
247     @Test
248     public void testTokensNotNull() {
249         final EqualsAvoidNullCheck check = new EqualsAvoidNullCheck();
250         assertWithMessage("Acceptable tokens should not be null")
251                 .that(check.getAcceptableTokens())
252                 .isNotNull();
253         assertWithMessage("Default tokens should not be null")
254                 .that(check.getDefaultTokens())
255                 .isNotNull();
256         assertWithMessage("Required tokens should not be null")
257                 .that(check.getRequiredTokens())
258                 .isNotNull();
259     }
260 
261     @Test
262     public void testEqualAvoidNull() throws Exception {
263         final String[] expected = {
264             "12:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
265             "13:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
266             "14:17: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
267             "17:22: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
268         };
269 
270         verifyWithInlineConfigParser(getPath("InputEqualsAvoidNull2.java"),
271                 expected);
272     }
273 
274     @Test
275     public void testEqualAvoidNullRecordPattern() throws Exception {
276         final String[] expected = {
277             "22:40: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
278             "29:34: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
279             "30:32: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
280             "31:33: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
281             "31:55: " + getCheckMessage(MSG_EQUALS_AVOID_NULL),
282         };
283 
284         verifyWithInlineConfigParser(
285                 getNonCompilablePath("InputEqualsAvoidNullRecordPattern.java"),
286                 expected);
287     }
288 }