forked from mybatis/mybatis-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XPathParserTest.java
242 lines (210 loc) · 8.09 KB
/
XPathParserTest.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* Copyright 2009-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.parsing;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.ibatis.builder.BuilderException;
import org.apache.ibatis.io.Resources;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
class XPathParserTest {
private String resource = "resources/nodelet_test.xml";
// InputStream Source
@Test
void constructorWithInputStreamValidationVariablesEntityResolver() throws Exception {
try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
XPathParser parser = new XPathParser(inputStream, false, null, null);
testEvalMethod(parser);
}
}
@Test
void constructorWithInputStreamValidationVariables() throws IOException {
try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
XPathParser parser = new XPathParser(inputStream, false, null);
testEvalMethod(parser);
}
}
@Test
void constructorWithInputStreamValidation() throws IOException {
try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
XPathParser parser = new XPathParser(inputStream, false);
testEvalMethod(parser);
}
}
@Test
void constructorWithInputStream() throws IOException {
try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
XPathParser parser = new XPathParser(inputStream);
testEvalMethod(parser);
}
}
// Reader Source
@Test
void constructorWithReaderValidationVariablesEntityResolver() throws Exception {
try (Reader reader = Resources.getResourceAsReader(resource)) {
XPathParser parser = new XPathParser(reader, false, null, null);
testEvalMethod(parser);
}
}
@Test
void constructorWithReaderValidationVariables() throws IOException {
try (Reader reader = Resources.getResourceAsReader(resource)) {
XPathParser parser = new XPathParser(reader, false, null);
testEvalMethod(parser);
}
}
@Test
void constructorWithReaderValidation() throws IOException {
try (Reader reader = Resources.getResourceAsReader(resource)) {
XPathParser parser = new XPathParser(reader, false);
testEvalMethod(parser);
}
}
@Test
void constructorWithReader() throws IOException {
try (Reader reader = Resources.getResourceAsReader(resource)) {
XPathParser parser = new XPathParser(reader);
testEvalMethod(parser);
}
}
// Xml String Source
@Test
void constructorWithStringValidationVariablesEntityResolver() throws Exception {
XPathParser parser = new XPathParser(getXmlString(resource), false, null, null);
testEvalMethod(parser);
}
@Test
void constructorWithStringValidationVariables() throws IOException {
XPathParser parser = new XPathParser(getXmlString(resource), false, null);
testEvalMethod(parser);
}
@Test
void constructorWithStringValidation() throws IOException {
XPathParser parser = new XPathParser(getXmlString(resource), false);
testEvalMethod(parser);
}
@Test
void constructorWithString() throws IOException {
XPathParser parser = new XPathParser(getXmlString(resource));
testEvalMethod(parser);
}
// Document Source
@Test
void constructorWithDocumentValidationVariablesEntityResolver() {
XPathParser parser = new XPathParser(getDocument(resource), false, null, null);
testEvalMethod(parser);
}
@Test
void constructorWithDocumentValidationVariables() {
XPathParser parser = new XPathParser(getDocument(resource), false, null);
testEvalMethod(parser);
}
@Test
void constructorWithDocumentValidation() {
XPathParser parser = new XPathParser(getDocument(resource), false);
testEvalMethod(parser);
}
@Test
void constructorWithDocument() {
XPathParser parser = new XPathParser(getDocument(resource));
testEvalMethod(parser);
}
private Document getDocument(String resource) {
try {
InputSource inputSource = new InputSource(Resources.getResourceAsReader(resource));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(false);
factory.setCoalescing(false);
factory.setExpandEntityReferences(true);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(inputSource);// already closed resource in builder.parse method
} catch (Exception e) {
throw new BuilderException("Error creating document instance. Cause: " + e, e);
}
}
private String getXmlString(String resource) throws IOException {
try (BufferedReader bufferedReader = new BufferedReader(Resources.getResourceAsReader(resource))) {
StringBuilder sb = new StringBuilder();
String temp;
while ((temp = bufferedReader.readLine()) != null) {
sb.append(temp);
}
return sb.toString();
}
}
private void testEvalMethod(XPathParser parser) {
assertEquals((Long) 1970L, parser.evalLong("/employee/birth_date/year"));
assertEquals((short) 6, (short) parser.evalShort("/employee/birth_date/month"));
assertEquals((Integer) 15, parser.evalInteger("/employee/birth_date/day"));
assertEquals((Float) 5.8f, parser.evalFloat("/employee/height"));
assertEquals((Double) 5.8d, parser.evalDouble("/employee/height"));
assertEquals("${id_var}", parser.evalString("/employee/@id"));
assertEquals(Boolean.TRUE, parser.evalBoolean("/employee/active"));
assertEquals("<id>${id_var}</id>", parser.evalNode("/employee/@id").toString().trim());
assertEquals(7, parser.evalNodes("/employee/*").size());
XNode node = parser.evalNode("/employee/height");
assertEquals("employee/height", node.getPath());
assertEquals("employee[${id_var}]_height", node.getValueBasedIdentifier());
}
@Test
void formatXNodeToString() {
XPathParser parser = new XPathParser("<users><user><id>100</id><name>Tom</name><age>30</age><cars><car>BMW</car><car>Audi</car><car>Benz</car></cars></user></users>");
String usersNodeToString = parser.evalNode("/users").toString();
String userNodeToString = parser.evalNode("/users/user").toString();
String carsNodeToString = parser.evalNode("/users/user/cars").toString();
String usersNodeToStringExpect =
"<users>\n" +
" <user>\n" +
" <id>100</id>\n" +
" <name>Tom</name>\n" +
" <age>30</age>\n" +
" <cars>\n" +
" <car>BMW</car>\n" +
" <car>Audi</car>\n" +
" <car>Benz</car>\n" +
" </cars>\n" +
" </user>\n" +
"</users>\n";
String userNodeToStringExpect =
"<user>\n" +
" <id>100</id>\n" +
" <name>Tom</name>\n" +
" <age>30</age>\n" +
" <cars>\n" +
" <car>BMW</car>\n" +
" <car>Audi</car>\n" +
" <car>Benz</car>\n" +
" </cars>\n" +
"</user>\n";
String carsNodeToStringExpect =
"<cars>\n" +
" <car>BMW</car>\n" +
" <car>Audi</car>\n" +
" <car>Benz</car>\n" +
"</cars>\n";
assertEquals(usersNodeToStringExpect, usersNodeToString);
assertEquals(userNodeToStringExpect, userNodeToString);
assertEquals(carsNodeToStringExpect, carsNodeToString);
}
}