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