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