1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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