/
XpathFileGeneratorAstFilterTest.java
173 lines (144 loc) · 7.65 KB
/
XpathFileGeneratorAstFilterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2022 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle;
import static com.google.common.truth.Truth.assertWithMessage;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.junit.jupiter.api.Test;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.api.Violation;
import com.puppycrawl.tools.checkstyle.checks.blocks.LeftCurlyCheck;
import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
public class XpathFileGeneratorAstFilterTest {
@Test
public void testAcceptNoToken() {
final Violation violation = new Violation(0, 0, 0, null, null, null, null,
null, XpathFileGeneratorAstFilterTest.class, null);
final TreeWalkerAuditEvent event = new TreeWalkerAuditEvent(null, null, violation, null);
final XpathFileGeneratorAstFilter filter = new XpathFileGeneratorAstFilter();
filter.finishLocalSetup();
assertWithMessage("filter accepted")
.that(filter.accept(event))
.isTrue();
final AuditEvent auditEvent = new AuditEvent(this, "Test.java", violation);
assertWithMessage("filter has no queries")
.that(XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(auditEvent))
.isNull();
}
@Test
public void test() throws Exception {
final Violation violation = new Violation(3, 47, TokenTypes.LCURLY,
"messages.properties", null, null, SeverityLevel.ERROR, null, LeftCurlyCheck.class,
null);
final TreeWalkerAuditEvent event = createTreeWalkerAuditEvent(
"InputXpathFileGeneratorAstFilter.java", violation);
final XpathFileGeneratorAstFilter filter = new XpathFileGeneratorAstFilter();
filter.finishLocalSetup();
assertWithMessage("filter accepted")
.that(filter.accept(event))
.isTrue();
final AuditEvent auditEvent = new AuditEvent(this,
getPath("InputXpathFileGeneratorAstFilter.java"), violation);
assertWithMessage("expected xpath")
.that(XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(auditEvent))
.isEqualTo("/COMPILATION_UNIT/CLASS_DEF[./IDENT"
+ "[@text='InputXpathFileGeneratorAstFilter']]/OBJBLOCK/LCURLY");
}
@Test
public void testNoXpathQuery() throws Exception {
final Violation violation = new Violation(10, 10, TokenTypes.LCURLY,
"messages.properties", null, null, SeverityLevel.ERROR, null, LeftCurlyCheck.class,
null);
final TreeWalkerAuditEvent event = createTreeWalkerAuditEvent(
"InputXpathFileGeneratorAstFilter.java", violation);
final XpathFileGeneratorAstFilter filter = new XpathFileGeneratorAstFilter();
filter.finishLocalSetup();
assertWithMessage("filter accepted")
.that(filter.accept(event))
.isTrue();
final AuditEvent auditEvent = new AuditEvent(this,
getPath("InputXpathFileGeneratorAstFilter.java"), violation);
assertWithMessage("expected null")
.that(XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(auditEvent))
.isNull();
}
@Test
public void testTabWidth() throws Exception {
final Violation violation = new Violation(6, 7, TokenTypes.LITERAL_RETURN,
"messages.properties", null, null, SeverityLevel.ERROR, null,
XpathFileGeneratorAstFilterTest.class, null);
final TreeWalkerAuditEvent event = createTreeWalkerAuditEvent(
"InputXpathFileGeneratorAstFilter.java", violation);
final XpathFileGeneratorAstFilter filter = new XpathFileGeneratorAstFilter();
filter.finishLocalSetup();
filter.setTabWidth(6);
assertWithMessage("filter accepted")
.that(filter.accept(event))
.isTrue();
final AuditEvent auditEvent = new AuditEvent(this,
getPath("InputXpathFileGeneratorAstFilter.java"), violation);
assertWithMessage("expected xpath")
.that(XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(auditEvent))
.isEqualTo("/COMPILATION_UNIT"
+ "/CLASS_DEF[./IDENT[@text='InputXpathFileGeneratorAstFilter']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='tabMethod']]/SLIST/LITERAL_RETURN");
}
/**
* We cannot reproduce situation when {@code finishLocalSetup} is called
* twice. So, we have to use reflection to be sure that even in such
* situation state of the field will be cleared.
*
* @throws Exception when code tested throws exception
*/
@Test
@SuppressWarnings("unchecked")
public void testClearState() throws Exception {
final Violation violation = new Violation(3, 47, TokenTypes.LCURLY,
"messages.properties", null, null, SeverityLevel.ERROR, null, LeftCurlyCheck.class,
null);
final TreeWalkerAuditEvent event = createTreeWalkerAuditEvent(
"InputXpathFileGeneratorAstFilter.java", violation);
final XpathFileGeneratorAstFilter filter = new XpathFileGeneratorAstFilter();
filter.finishLocalSetup();
assertWithMessage("State is not cleared on finishLocalSetup")
.that(TestUtil.isStatefulFieldClearedDuringLocalSetup(filter, event,
"MESSAGE_QUERY_MAP",
variableStack -> ((Map<Violation, String>) variableStack).isEmpty()))
.isTrue();
}
private static TreeWalkerAuditEvent createTreeWalkerAuditEvent(String fileName,
Violation violation) throws Exception {
final File file = new File(getPath(fileName));
final FileText fileText = new FileText(file.getAbsoluteFile(), System.getProperty(
"file.encoding", StandardCharsets.UTF_8.name()));
final FileContents fileContents = new FileContents(fileText);
final DetailAST rootAst = JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS);
return new TreeWalkerAuditEvent(fileContents, fileName, violation, rootAst);
}
private static String getPath(String filename) {
return "src/test/resources/com/puppycrawl/tools/checkstyle/xpathfilegeneratorastfilter/"
+ filename;
}
}