1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.filters;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23
24 import java.io.File;
25 import java.nio.charset.StandardCharsets;
26
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31 import com.puppycrawl.tools.checkstyle.JavaParser;
32 import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent;
33 import com.puppycrawl.tools.checkstyle.api.FileContents;
34 import com.puppycrawl.tools.checkstyle.api.FileText;
35 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
36 import com.puppycrawl.tools.checkstyle.api.Violation;
37 import net.sf.saxon.Configuration;
38 import net.sf.saxon.sxpath.XPathEvaluator;
39 import net.sf.saxon.sxpath.XPathExpression;
40 import nl.jqno.equalsverifier.EqualsVerifier;
41 import nl.jqno.equalsverifier.EqualsVerifierReport;
42
43 public class XpathFilterElementTest extends AbstractModuleTestSupport {
44
45 private File file;
46 private FileContents fileContents;
47
48 @BeforeEach
49 public void setUp() throws Exception {
50 file = new File(getPath("InputXpathFilterElementSuppressByXpath.java"));
51 fileContents = new FileContents(new FileText(file,
52 StandardCharsets.UTF_8.name()));
53 }
54
55 @Override
56 public String getPackageLocation() {
57 return "com/puppycrawl/tools/checkstyle/filters/xpathfilterelement";
58 }
59
60 @Test
61 public void testNonMatchingTokenType() throws Exception {
62 final String xpath = "//METHOD_DEF[./IDENT[@text='countTokens']]";
63 final XpathFilterElement filter = new XpathFilterElement(
64 "InputXpathFilterElementSuppressByXpath", "Test", null, null, xpath);
65 final TreeWalkerAuditEvent ev = getEvent(4, 4,
66 TokenTypes.CLASS_DEF);
67 assertWithMessage("Event should be accepted")
68 .that(filter.accept(ev))
69 .isTrue();
70 }
71
72 @Test
73 public void testNonMatchingColumnNumber() throws Exception {
74 final String xpath = "//CLASS_DEF[./IDENT[@text='InputXpathFilterElementSuppressByXpath']]";
75 final XpathFilterElement filter = new XpathFilterElement(
76 "InputXpathFilterElementSuppressByXpath", "Test", null, null, xpath);
77 final TreeWalkerAuditEvent ev = getEvent(3, 100,
78 TokenTypes.CLASS_DEF);
79 assertWithMessage("Event should be accepted")
80 .that(filter.accept(ev))
81 .isTrue();
82 }
83
84 @Test
85 public void testNullFileName() {
86 final XpathFilterElement filter = new XpathFilterElement(
87 "InputXpathFilterElementSuppressByXpath", "Test", null, null, null);
88 final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null,
89 null, null, null);
90 assertWithMessage("Event should be accepted")
91 .that(filter.accept(ev))
92 .isTrue();
93 }
94
95 @Test
96 public void testNonMatchingFileRegexp() throws Exception {
97 final XpathFilterElement filter =
98 new XpathFilterElement("NonMatchingRegexp", "Test", null, null, null);
99 final TreeWalkerAuditEvent ev = getEvent(3, 0,
100 TokenTypes.CLASS_DEF);
101 assertWithMessage("Event should be accepted")
102 .that(filter.accept(ev))
103 .isTrue();
104 }
105
106 @Test
107 public void testNullViolation() {
108 final XpathFilterElement filter = new XpathFilterElement(
109 "InputXpathFilterElementSuppressByXpath", "Test", null, null, null);
110 final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(null,
111 file.getName(), null, null);
112 assertWithMessage("Event should be accepted")
113 .that(filter.accept(ev))
114 .isTrue();
115 }
116
117 @Test
118 public void testDecideByMessage() throws Exception {
119 final Violation message = new Violation(1, 0, TokenTypes.CLASS_DEF, "", "",
120 null, null, null, getClass(), "Test");
121 final TreeWalkerAuditEvent ev = new TreeWalkerAuditEvent(fileContents, file.getName(),
122 message, JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS));
123 final XpathFilterElement filter1 = new XpathFilterElement(null, null, "Test", null, null);
124 final XpathFilterElement filter2 = new XpathFilterElement(null, null, "Bad", null, null);
125 assertWithMessage("Message match")
126 .that(filter1.accept(ev))
127 .isFalse();
128 assertWithMessage("Message not match")
129 .that(filter2.accept(ev))
130 .isTrue();
131 }
132
133
134
135
136
137
138
139
140 @Test
141 public void testEqualsAndHashCode() throws Exception {
142 final XPathEvaluator xpathEvaluator = new XPathEvaluator(Configuration.newConfiguration());
143 final EqualsVerifierReport ev = EqualsVerifier.forClass(XpathFilterElement.class)
144 .withPrefabValues(XPathExpression.class,
145 xpathEvaluator.createExpression("//METHOD_DEF"),
146 xpathEvaluator.createExpression("//VARIABLE_DEF"))
147 .usingGetClass()
148 .withIgnoredFields("xpathExpression", "isEmptyConfig")
149 .report();
150 assertWithMessage("Error: %s", ev.getMessage())
151 .that(ev.isSuccessful())
152 .isTrue();
153 }
154
155 private TreeWalkerAuditEvent getEvent(int line, int column, int tokenType)
156 throws Exception {
157 final Violation message =
158 new Violation(line, column, tokenType, "", "", null, null, null,
159 getClass(), null);
160 return new TreeWalkerAuditEvent(fileContents, file.getName(), message,
161 JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS));
162 }
163
164 }