-
Notifications
You must be signed in to change notification settings - Fork 27
/
XemblerTest.java
290 lines (277 loc) · 9.64 KB
/
XemblerTest.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* Copyright (c) 2013-2019, xembly.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the xembly.org nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.xembly;
import com.jcabi.matchers.XhtmlMatchers;
import com.jcabi.xml.XMLDocument;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.lang3.StringUtils;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
/**
* Test case for {@link Xembler}.
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class XemblerTest {
/**
* Xembler can change DOM document.
* @throws Exception If some problem inside
*/
@Test
public void makesChangesToDomDocument() throws Exception {
final Document dom = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
new Xembler(
new Directives(
StringUtils.join(
"ADD 'root'; STRICT '1'; ADD 'order';",
"ATTR 'tag', 'hello, world!';",
"ADD 'price'; SET \"$29.99\"; STRICT '1'; UP; UP;",
"XPATH '//order[price='$29.99']/price';",
"SET ' $39.99 ';"
)
)
).apply(dom);
MatcherAssert.assertThat(
XhtmlMatchers.xhtml(dom),
XhtmlMatchers.hasXPaths(
"/root/order[@tag='hello, world!']",
"/root/order[price=' $39.99 ']"
)
);
}
/**
* Xembler can change DOM document from builder.
* @throws Exception If some problem inside
*/
@Test
public void makesChangesToDomDocumentFromBuilder() throws Exception {
final Document dom = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
final Directives builder = new Directives()
.add("top")
.add("employees")
.add("paper")
.up()
.xpath("*")
.remove()
.addIf("employee")
.attr("id", "<443>")
.add("name")
.strict(1)
.set("\rСаша\t\nПушкин\n")
.up()
.up()
.xpath("/top/employees/employee[@id='<443>']/name")
.set("\"Юра Лермонтов\"");
new Xembler(new Directives(builder.toString())).applyQuietly(dom);
MatcherAssert.assertThat(
XhtmlMatchers.xhtml(dom),
XhtmlMatchers.hasXPaths(
"/top/employees/employee[@id='<443>']",
"//employee[name='\"Юра Лермонтов\"']"
)
);
}
/**
* Xembler can print XML.
* @throws Exception If some problem inside
*/
@Test
public void printsXmlDocument() throws Exception {
MatcherAssert.assertThat(
new Xembler(
new Directives().add("hey-you").add("hoy").set("\u20ac")
).xml(),
XhtmlMatchers.hasXPath("/hey-you/hoy[.='\u20ac']")
);
}
/**
* Xembler can show XML declaration line.
* @throws Exception If some problem inside
*/
@Test
public void rendersXmlDeclaration() throws Exception {
MatcherAssert.assertThat(
new Xembler(new Directives("ADD 'f';")).xmlQuietly(),
Matchers.equalTo("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<f/>")
);
}
/**
* Xembler can escape broken text.
* @throws Exception If some problem inside
* @since 0.14
*/
@Test
public void escapesBrokenText() throws Exception {
MatcherAssert.assertThat(
Xembler.escape("привет hello \u0000"),
Matchers.equalTo("привет hello \\u0000")
);
}
/**
* Xembler can modify a cloned node.
* @throws Exception If some problem inside
*/
@Test
public void modifiesClonedNode() throws Exception {
final Node node = new XMLDocument("<t/>").node().cloneNode(true);
new Xembler(new Directives().xpath("/t").add("x")).apply(node);
MatcherAssert.assertThat(
new XMLDocument(node),
XhtmlMatchers.hasXPath("/t/x")
);
}
/**
* Xembler can remove root node and add a new one.
* @throws Exception If some problem inside
*/
@Test
public void replacesRootNode() throws Exception {
final Node node = new XMLDocument("<e/>").node();
new Xembler(new Directives().xpath("/e").remove().add("p")).apply(node);
MatcherAssert.assertThat(
new XMLDocument(node),
XhtmlMatchers.hasXPath("/p")
);
}
/**
* Xembler can remove attribute node.
* @throws Exception If some problem inside
*/
@Test
public void removesAttribute() throws Exception {
final Node node = new XMLDocument("<i8 a6='foo'/>").node();
new Xembler(
new Directives()
.xpath("/i8/@a6")
.strict(1)
.remove()
.attr("a7", "foo-9")
).apply(node);
MatcherAssert.assertThat(
new XMLDocument(node),
XhtmlMatchers.hasXPath("/i8[@a7='foo-9' and not(@a6)]")
);
}
/**
* Xembler can remove root node.
* @throws Exception If some problem inside
*/
@Test
public void removesRootNode() throws Exception {
final Node node = new XMLDocument("<old/>").node();
new Xembler(
new Directives().xpath("/old").remove().add("new")
).apply(node);
MatcherAssert.assertThat(
new XMLDocument(node),
XhtmlMatchers.hasXPath("/new")
);
}
/**
* Test that concurrent invocations on shared DOM.
* doesn't ruin executing thread
* @todo #34
* I assumed that Xembler should be a thread-safe and
* this test must pass, but this assumption might be wrong and
* this test case is not an issue.
* Modify <code>int capacity</code> to play with results:
* case 1: change capacity to 1 to get test work within 1 thread
* case 2: change capacity to any positive number greater than 1
* to get test broken
* @throws Exception If some problem inside
*/
@Test
@Ignore
public void concurrentInvocationWithNoExceptions() throws Exception {
final ExecutorService service = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors()
);
final Document dom = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
final Node root = dom.appendChild(dom.createElement("root"));
final Xembler xembler = new Xembler(
new Directives(
"ADDIF 'blow';REMOVE;ADDIF 'blow';"
)
);
final int capacity = 10000;
final Collection<Callable<Node>> tasks =
new ArrayList<Callable<Node>>(
capacity
);
for (int idx = 0; idx < capacity; ++idx) {
final Callable<Node> callable = XemblerTest.callable(
xembler, root
);
tasks.add(callable);
}
final List<Future<Node>> futures = service.invokeAll(tasks);
for (final Future<Node> future : futures) {
future.get();
}
MatcherAssert.assertThat(
XhtmlMatchers.xhtml(dom),
XhtmlMatchers.hasXPaths(
"/root[count(blow) = 1]"
)
);
service.shutdown();
}
/**
* Builds up callable object.
* @param xembler Xembler's instance
* @param document DOM object
* @return Callable object
*/
private static Callable<Node> callable(
final Xembler xembler, final Node document
) {
return new Callable<Node>() {
@Override
public Node call() throws Exception {
return xembler.apply(document);
}
};
}
}