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 org.checkstyle.suppressionxpathfilter.coding;
21  
22  import java.io.File;
23  import java.util.List;
24  
25  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryNullCheckWithInstanceOfCheck;
30  
31  public class XpathRegressionUnnecessaryNullCheckWithInstanceOfTest
32      extends AbstractXpathTestSupport {
33  
34      private final String checkName = UnnecessaryNullCheckWithInstanceOfCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Override
42      protected String getPackageLocation() {
43          return "org/checkstyle/suppressionxpathfilter/coding/unnecessarynullcheckwithinstanceof";
44      }
45  
46      @Test
47      public void test1() throws Exception {
48          final File fileToProcess =
49                  new File(getPath("InputXpathUnnecessaryNullCheckWithInstanceOf.java"));
50          final DefaultConfiguration moduleConfig =
51                  createModuleConfig(UnnecessaryNullCheckWithInstanceOfCheck.class);
52  
53          final String[] expected = {
54              "5:13: " + getCheckMessage(UnnecessaryNullCheckWithInstanceOfCheck.class,
55                      UnnecessaryNullCheckWithInstanceOfCheck.MSG_UNNECESSARY_NULLCHECK),
56          };
57          final List<String> expectedXpathQueries = List.of(
58              "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
59                  + "'InputXpathUnnecessaryNullCheckWithInstanceOf']]"
60                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='methodWithUnnecessaryNullCheck1']]"
61                  + "/SLIST/LITERAL_IF/EXPR/LAND/NOT_EQUAL/IDENT[@text='obj']"
62          );
63  
64          runVerifications(moduleConfig, fileToProcess, expected, expectedXpathQueries);
65      }
66  
67      @Test
68      public void test2() throws Exception {
69          final File fileToProcess =
70                  new File(getPath(
71                      "InputXpathUnnecessaryNullCheckWithInstanceOfAnonymous.java"));
72          final DefaultConfiguration moduleConfig =
73                  createModuleConfig(UnnecessaryNullCheckWithInstanceOfCheck.class);
74  
75          final String[] expected = {
76              "8:17: " + getCheckMessage(UnnecessaryNullCheckWithInstanceOfCheck.class,
77                      UnnecessaryNullCheckWithInstanceOfCheck.MSG_UNNECESSARY_NULLCHECK),
78          };
79          final List<String> expectedXpathQueries = List.of(
80              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
81                  + "[@text='InputXpathUnnecessaryNullCheckWithInstanceOfAnonymous']]"
82                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='runnable']]/ASSIGN/EXPR/LITERAL_NEW"
83                  + "[./IDENT[@text='Runnable']]/OBJBLOCK/METHOD_DEF[./IDENT[@text='run']]"
84                  + "/SLIST/LITERAL_IF/EXPR/LAND/NOT_EQUAL/IDENT[@text='obj']"
85          );
86  
87          runVerifications(moduleConfig, fileToProcess, expected, expectedXpathQueries);
88      }
89  
90      @Test
91      public void test3() throws Exception {
92          final File fileToProcess =
93                  new File(getPath(
94                      "InputXpathUnnecessaryNullCheckWithInstanceOfInterface.java"));
95          final DefaultConfiguration moduleConfig =
96                  createModuleConfig(UnnecessaryNullCheckWithInstanceOfCheck.class);
97  
98          final String[] expected = {
99              "6:16: " + getCheckMessage(UnnecessaryNullCheckWithInstanceOfCheck.class,
100                     UnnecessaryNullCheckWithInstanceOfCheck.MSG_UNNECESSARY_NULLCHECK),
101         };
102         final List<String> expectedXpathQueries = List.of(
103             "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
104                 + "[@text='InputXpathUnnecessaryNullCheckWithInstanceOfInterface']]"
105                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='validateString']]/SLIST/LITERAL_RETURN"
106                 + "/EXPR/LAND/NOT_EQUAL/IDENT[@text='obj']"
107         );
108 
109         runVerifications(moduleConfig, fileToProcess, expected, expectedXpathQueries);
110     }
111 }
112