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.Arrays;
24 import java.util.List;
25
26 import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27 import org.junit.jupiter.api.Test;
28
29 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30 import com.puppycrawl.tools.checkstyle.checks.coding.EqualsAvoidNullCheck;
31
32 public class XpathRegressionEqualsAvoidNullTest extends AbstractXpathTestSupport {
33
34 private static final Class<EqualsAvoidNullCheck> CLAZZ = EqualsAvoidNullCheck.class;
35
36 @Override
37 protected String getCheckName() {
38 return CLAZZ.getSimpleName();
39 }
40
41 @Override
42 protected String getPackageLocation() {
43 return "org/checkstyle/suppressionxpathfilter/coding/equalsavoidnull";
44 }
45
46 @Test
47 public void testEquals() throws Exception {
48 final File fileToProcess = new File(
49 getPath("InputXpathEqualsAvoidNull.java"));
50
51 final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
52
53 final String[] expectedViolation = {
54 "6:26: " + getCheckMessage(CLAZZ,
55 EqualsAvoidNullCheck.MSG_EQUALS_AVOID_NULL),
56 };
57
58 final List<String> expectedXpathQueries = Arrays.asList(
59 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
60 + "[@text='InputXpathEqualsAvoidNull']]"
61 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/EXPR",
62 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
63 + "[@text='InputXpathEqualsAvoidNull']]"
64 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/EXPR/METHOD_CALL");
65
66 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
67 }
68
69 @Test
70 public void testEqualsIgnoreCase() throws Exception {
71 final File fileToProcess = new File(
72 getPath("InputXpathEqualsAvoidNullIgnoreCase.java"));
73
74 final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
75
76 final String[] expectedViolation = {
77 "6:36: " + getCheckMessage(CLAZZ,
78 EqualsAvoidNullCheck.MSG_EQUALS_IGNORE_CASE_AVOID_NULL),
79 };
80
81 final List<String> expectedXpathQueries = Arrays.asList(
82 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
83 + "[@text='InputXpathEqualsAvoidNullIgnoreCase']]"
84 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/EXPR",
85 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
86 + "[@text='InputXpathEqualsAvoidNullIgnoreCase']]"
87 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/EXPR/METHOD_CALL");
88
89 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
90 }
91 }