View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.UnnecessaryNullCheckWithInstanceOfCheck.MSG_UNNECESSARY_NULLCHECK;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  
29  public class UnnecessaryNullCheckWithInstanceOfCheckTest extends AbstractModuleTestSupport {
30  
31      @Override
32      protected String getPackageLocation() {
33          return "com/puppycrawl/tools/checkstyle/checks/coding/unnecessarynullcheckwithinstanceof";
34      }
35  
36      @Test
37      public void testUnnecessaryNullCheckWithInstanceof() throws Exception {
38  
39          final String[] expected = {
40              "12:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
41              "15:39: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
42              "24:14: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
43              "54:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
44              "57:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
45              "60:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
46              "69:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
47              "74:26: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
48              "85:19: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
49          };
50          verifyWithInlineConfigParser(getPath(
51              "InputUnnecessaryNullCheckWithInstanceOfOne.java"), expected);
52      }
53  
54      @Test
55      public void testUnnecessaryNullCheckWithInstanceofClass() throws Exception {
56  
57          final String[] expected = {
58              "14:21: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
59              "28:24: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
60          };
61          verifyWithInlineConfigParser(getPath(
62              "InputUnnecessaryNullCheckWithInstanceOfAnonymousClass.java"), expected);
63      }
64  
65      @Test
66      public void testUnnecessaryNullCheckWithInstanceofConditions() throws Exception {
67  
68          final String[] expected = {
69              "12:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
70              "17:31: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
71              "22:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
72              "27:54: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
73              "36:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
74              "36:55: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
75              "50:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
76          };
77          verifyWithInlineConfigParser(getPath(
78              "InputUnnecessaryNullCheckWithInstanceOfMultipleConditions.java"), expected);
79      }
80  
81      @Test
82      public void testUnnecessaryNullCheckWithInstanceofNested() throws Exception {
83  
84          final String[] expected = {
85              "11:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
86              "18:21: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
87              "26:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
88          };
89          verifyWithInlineConfigParser(getPath(
90              "InputUnnecessaryNullCheckWithInstanceOfNestedIf.java"), expected);
91      }
92  
93      @Test
94      public void testUnnecessaryNullCheckWithInstanceofLambda() throws Exception {
95  
96          final String[] expected = {
97              "17:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
98              "23:45: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
99              "26:33: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
100         };
101         verifyWithInlineConfigParser(getPath(
102             "InputUnnecessaryNullCheckWithInstanceOfLambda.java"), expected);
103     }
104 
105     @Test
106     public void testUnnecessaryNullCheckWithInstanceofSwitch() throws Exception {
107 
108         final String[] expected = {
109             "13:21: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
110             "35:21: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
111         };
112         verifyWithInlineConfigParser(getPath(
113             "InputUnnecessaryNullCheckWithInstanceOfSwitch.java"), expected);
114     }
115 
116     @Test
117     public void testUnnecessaryNullCheckWithInstanceofTernary() throws Exception {
118 
119         final String[] expected = {
120             "11:16: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
121             "18:25: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
122             "23:24: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
123             "26:16: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
124             "27:20: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
125             "33:16: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
126             "34:19: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
127             "43:24: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
128             "43:61: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
129             "49:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
130         };
131         verifyWithInlineConfigParser(getPath(
132             "InputUnnecessaryNullCheckWithInstanceOfTernary.java"), expected);
133     }
134 
135     @Test
136     public void testUnnecessaryNullCheckWithInstanceofTryCatch() throws Exception {
137 
138         final String[] expected = {
139             "11:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
140             "23:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
141             "33:17: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
142         };
143         verifyWithInlineConfigParser(getPath(
144             "InputUnnecessaryNullCheckWithInstanceOfTryCatch.java"), expected);
145     }
146 
147     @Test
148     public void testUnnecessaryNullCheckWithInstanceofTryVariable() throws Exception {
149 
150         final String[] expected = {
151             "12:27: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
152             "19:36: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
153         };
154         verifyWithInlineConfigParser(getPath(
155             "InputUnnecessaryNullCheckWithInstanceOfVariableAssignment.java"), expected);
156     }
157 
158     @Test
159     public void testUnnecessaryNullCheckWithInstanceofPattern() throws Exception {
160 
161         final String[] expected = {
162             "12:13: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
163         };
164         verifyWithInlineConfigParser(
165                 getPath(
166                 "InputUnnecessaryNullCheckWithInstanceOfTwo.java"),
167             expected);
168     }
169 
170     @Test
171     public void testUnnecessaryNullCheckWithInstanceofPair() throws Exception {
172 
173         final String[] expected = {
174             "15:56: " + getCheckMessage(MSG_UNNECESSARY_NULLCHECK),
175         };
176         verifyWithInlineConfigParser(
177                 getNonCompilablePath(
178                 "InputUnnecessaryNullCheckWithInstanceOfPair.java"),
179             expected);
180     }
181 
182     @Test
183     public void testTokensNotNull() {
184         final UnnecessaryNullCheckWithInstanceOfCheck check =
185             new UnnecessaryNullCheckWithInstanceOfCheck();
186         assertWithMessage("Acceptable tokens should not be null")
187             .that(check.getAcceptableTokens())
188             .isNotNull();
189         assertWithMessage("Default tokens should not be null")
190             .that(check.getDefaultTokens())
191             .isNotNull();
192         assertWithMessage("Required tokens should not be null")
193             .that(check.getRequiredTokens())
194             .isNotNull();
195     }
196 }