|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| package nu.xom.tests; |
|
23 |
| |
|
24 |
| import nu.xom.Element; |
|
25 |
| import nu.xom.IllegalDataException; |
|
26 |
| import nu.xom.IllegalTargetException; |
|
27 |
| import nu.xom.ProcessingInstruction; |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| public class ProcessingInstructionTest extends XOMTestCase { |
|
39 |
| |
|
40 |
| |
|
41 |
19
| public ProcessingInstructionTest(String name) {
|
|
42 |
19
| super(name);
|
|
43 |
| } |
|
44 |
| |
|
45 |
| |
|
46 |
| private ProcessingInstruction pi; |
|
47 |
| |
|
48 |
| |
|
49 |
19
| protected void setUp() {
|
|
50 |
19
| pi = new ProcessingInstruction("test", "test");
|
|
51 |
| } |
|
52 |
| |
|
53 |
| |
|
54 |
1
| public void testToXML() {
|
|
55 |
1
| assertEquals("<?test test?>", pi.toXML());
|
|
56 |
| } |
|
57 |
| |
|
58 |
| |
|
59 |
1
| public void testToString() {
|
|
60 |
1
| assertEquals(
|
|
61 |
| "[nu.xom.ProcessingInstruction: target=\"test\"; data=\"test\"]", |
|
62 |
| pi.toString()); |
|
63 |
| } |
|
64 |
| |
|
65 |
| |
|
66 |
1
| public void testToStringWithLineFeed() {
|
|
67 |
| |
|
68 |
1
| ProcessingInstruction p
|
|
69 |
| = new ProcessingInstruction("test", "content\ncontent"); |
|
70 |
1
| assertEquals(
|
|
71 |
| "[nu.xom.ProcessingInstruction: target=\"test\"; data=\"content\\ncontent\"]", |
|
72 |
| p.toString() |
|
73 |
| ); |
|
74 |
| |
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
1
| public void testToStringWithLotsOfData() {
|
|
79 |
| |
|
80 |
1
| ProcessingInstruction p
|
|
81 |
| = new ProcessingInstruction("target", |
|
82 |
| "content content 012345678901234567890123456789012345678901234567890123456789"); |
|
83 |
1
| String s = p.toString();
|
|
84 |
1
| assertTrue(s.endsWith("...\"]"));
|
|
85 |
| |
|
86 |
| } |
|
87 |
| |
|
88 |
| |
|
89 |
1
| public void testConstructor() {
|
|
90 |
| |
|
91 |
1
| assertEquals("test", pi.getValue());
|
|
92 |
1
| assertEquals("test", pi.getTarget());
|
|
93 |
| |
|
94 |
1
| try {
|
|
95 |
1
| new ProcessingInstruction("test:test", "test");
|
|
96 |
0
| fail("Processing instruction targets cannot contain colons");
|
|
97 |
| } |
|
98 |
| catch (IllegalTargetException success) { |
|
99 |
1
| assertNotNull(success.getMessage());
|
|
100 |
1
| assertEquals("test:test", success.getData());
|
|
101 |
| } |
|
102 |
| |
|
103 |
1
| try {
|
|
104 |
1
| new ProcessingInstruction("", "test");
|
|
105 |
0
| fail("Processing instruction targets cannot be empty");
|
|
106 |
| } |
|
107 |
| catch (IllegalTargetException success) { |
|
108 |
1
| assertNotNull(success.getMessage());
|
|
109 |
1
| assertEquals("", success.getData());
|
|
110 |
| } |
|
111 |
| |
|
112 |
1
| try {
|
|
113 |
1
| new ProcessingInstruction(null, "test");
|
|
114 |
0
| fail("Processing instruction targets cannot be empty");
|
|
115 |
| } |
|
116 |
| catch (IllegalTargetException success) { |
|
117 |
1
| assertNotNull(success.getMessage());
|
|
118 |
1
| assertNull(success.getData());
|
|
119 |
| } |
|
120 |
| |
|
121 |
1
| try {
|
|
122 |
1
| new ProcessingInstruction("12345", "test");
|
|
123 |
0
| fail("Processing instruction targets must be NCNames");
|
|
124 |
| } |
|
125 |
| catch (IllegalTargetException success) { |
|
126 |
1
| assertEquals("12345", success.getData());
|
|
127 |
| } |
|
128 |
| |
|
129 |
| |
|
130 |
1
| pi = new ProcessingInstruction("test", "");
|
|
131 |
1
| assertEquals("", pi.getValue());
|
|
132 |
1
| assertEquals("<?test?>", pi.toXML());
|
|
133 |
| |
|
134 |
| } |
|
135 |
| |
|
136 |
| |
|
137 |
1
| public void testSetTarget() {
|
|
138 |
| |
|
139 |
1
| try {
|
|
140 |
1
| pi.setTarget("test:test");
|
|
141 |
0
| fail("Processing instruction targets cannot contain colons");
|
|
142 |
| } |
|
143 |
| catch (IllegalTargetException success) { |
|
144 |
1
| assertNotNull(success.getMessage());
|
|
145 |
1
| assertEquals("test:test", success.getData());
|
|
146 |
| } |
|
147 |
| |
|
148 |
1
| try {
|
|
149 |
1
| pi.setTarget("");
|
|
150 |
0
| fail("Processing instruction targets cannot be empty");
|
|
151 |
| } |
|
152 |
| catch (IllegalTargetException success) { |
|
153 |
1
| assertNotNull(success.getMessage());
|
|
154 |
1
| assertEquals("", success.getData());
|
|
155 |
| } |
|
156 |
| |
|
157 |
1
| try {
|
|
158 |
1
| pi.setTarget(null);
|
|
159 |
0
| fail("Processing instruction targets cannot be empty");
|
|
160 |
| } |
|
161 |
| catch (IllegalTargetException success) { |
|
162 |
1
| assertNotNull(success.getMessage());
|
|
163 |
1
| assertNull(success.getData());
|
|
164 |
| } |
|
165 |
| |
|
166 |
1
| try {
|
|
167 |
1
| pi.setTarget("12345");
|
|
168 |
0
| fail("Processing instruction targets must be NCNames");
|
|
169 |
| } |
|
170 |
| catch (IllegalTargetException success) { |
|
171 |
1
| assertEquals("12345", success.getData());
|
|
172 |
| } |
|
173 |
| |
|
174 |
1
| pi.setTarget("testing123");
|
|
175 |
1
| assertEquals("testing123", pi.getTarget());
|
|
176 |
| |
|
177 |
| } |
|
178 |
| |
|
179 |
| |
|
180 |
1
| public void testCopyConstructor() {
|
|
181 |
| |
|
182 |
1
| ProcessingInstruction instruction1 = new ProcessingInstruction("target", "data");
|
|
183 |
1
| ProcessingInstruction instruction2 = new ProcessingInstruction(instruction1);
|
|
184 |
| |
|
185 |
1
| assertEquals(instruction1.getTarget(), instruction2.getTarget());
|
|
186 |
1
| assertEquals(instruction1.getValue(), instruction2.getValue());
|
|
187 |
1
| assertEquals(instruction1.toXML(), instruction2.toXML());
|
|
188 |
| |
|
189 |
| } |
|
190 |
| |
|
191 |
| |
|
192 |
1
| public void testSetValue() {
|
|
193 |
| |
|
194 |
1
| try {
|
|
195 |
1
| pi.setValue("kjsahdj ?>");
|
|
196 |
0
| fail("Should raise an IllegalDataException");
|
|
197 |
| } |
|
198 |
| catch (IllegalDataException success) { |
|
199 |
1
| assertEquals("kjsahdj ?>", success.getData());
|
|
200 |
1
| assertNotNull(success.getMessage());
|
|
201 |
| } |
|
202 |
| |
|
203 |
1
| try {
|
|
204 |
1
| pi.setValue("?>");
|
|
205 |
0
| fail("Should raise an IllegalDataException");
|
|
206 |
| } |
|
207 |
| catch (IllegalDataException success) { |
|
208 |
1
| assertEquals("?>", success.getData());
|
|
209 |
1
| assertNotNull(success.getMessage());
|
|
210 |
| } |
|
211 |
| |
|
212 |
1
| try {
|
|
213 |
1
| pi.setValue("kjsahdj ?> skhskjlhd");
|
|
214 |
0
| fail("Should raise an IllegalDataException");
|
|
215 |
| } |
|
216 |
| catch (IllegalDataException success) { |
|
217 |
1
| assertEquals("kjsahdj ?> skhskjlhd", success.getData());
|
|
218 |
1
| assertNotNull(success.getMessage());
|
|
219 |
| } |
|
220 |
| |
|
221 |
1
| try {
|
|
222 |
1
| pi.setValue(null);
|
|
223 |
0
| fail("Allowed null data");
|
|
224 |
| } |
|
225 |
| catch (IllegalDataException success) { |
|
226 |
1
| assertNull(success.getData());
|
|
227 |
1
| assertNotNull(success.getMessage());
|
|
228 |
| } |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
1
| String[] testData = {"<html></html>",
|
|
233 |
| "name=value", |
|
234 |
| "name='value'", |
|
235 |
| "name=\"value\"", |
|
236 |
| "salkdhsalkjhdkjsadhkj sadhsajkdh", |
|
237 |
| "<?", "? >", "--" |
|
238 |
| }; |
|
239 |
1
| for (int i = 0; i < testData.length; i++) {
|
|
240 |
8
| pi.setValue(testData[i]);
|
|
241 |
8
| assertEquals(testData[i], pi.getValue());
|
|
242 |
| } |
|
243 |
| |
|
244 |
| } |
|
245 |
| |
|
246 |
| |
|
247 |
1
| public void testNames() {
|
|
248 |
1
| assertEquals("test", pi.getTarget());
|
|
249 |
| } |
|
250 |
| |
|
251 |
| |
|
252 |
1
| public void testEquals() {
|
|
253 |
| |
|
254 |
1
| ProcessingInstruction pi1
|
|
255 |
| = new ProcessingInstruction("test", "afaf"); |
|
256 |
1
| ProcessingInstruction pi2
|
|
257 |
| = new ProcessingInstruction("test", "afaf"); |
|
258 |
1
| ProcessingInstruction pi3
|
|
259 |
| = new ProcessingInstruction("tegggst", "afaf"); |
|
260 |
1
| ProcessingInstruction pi4
|
|
261 |
| = new ProcessingInstruction("test", "1234"); |
|
262 |
| |
|
263 |
1
| assertEquals(pi1, pi1);
|
|
264 |
1
| assertEquals(pi1.hashCode(), pi1.hashCode());
|
|
265 |
1
| assertTrue(!pi1.equals(pi2));
|
|
266 |
1
| assertTrue(!pi1.equals(pi3));
|
|
267 |
1
| assertTrue(!pi3.equals(pi4));
|
|
268 |
1
| assertTrue(!pi2.equals(pi4));
|
|
269 |
1
| assertTrue(!pi2.equals(pi3));
|
|
270 |
| |
|
271 |
| } |
|
272 |
| |
|
273 |
| |
|
274 |
1
| public void testCopy() {
|
|
275 |
| |
|
276 |
1
| Element test = new Element("test");
|
|
277 |
1
| test.appendChild(pi);
|
|
278 |
1
| ProcessingInstruction c2 = (ProcessingInstruction) pi.copy();
|
|
279 |
| |
|
280 |
1
| assertEquals(pi, c2);
|
|
281 |
1
| assertEquals(pi.getValue(), c2.getValue());
|
|
282 |
1
| assertTrue(!pi.equals(c2));
|
|
283 |
1
| assertNull(c2.getParent());
|
|
284 |
| |
|
285 |
| } |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
1
| public void testCorrectSurrogates() {
|
|
290 |
| |
|
291 |
1
| String goodString = "test: \uD8F5\uDF80 ";
|
|
292 |
1
| pi.setValue(goodString);
|
|
293 |
1
| assertEquals(goodString, pi.getValue());
|
|
294 |
| |
|
295 |
| } |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
1
| public void testSurrogates() {
|
|
300 |
| |
|
301 |
1
| try {
|
|
302 |
1
| pi.setValue("test \uD8F5\uD8F5 test");
|
|
303 |
0
| fail("Allowed two high halves");
|
|
304 |
| } |
|
305 |
| catch (IllegalDataException success) { |
|
306 |
1
| assertEquals("test \uD8F5\uD8F5 test", success.getData());
|
|
307 |
1
| assertNotNull(success.getMessage());
|
|
308 |
| } |
|
309 |
| |
|
310 |
1
| try {
|
|
311 |
1
| pi.setValue("test \uDF80\uDF80 test");
|
|
312 |
0
| fail("Allowed two low halves");
|
|
313 |
| } |
|
314 |
| catch (IllegalDataException success) { |
|
315 |
1
| assertEquals("test \uDF80\uDF80 test", success.getData());
|
|
316 |
1
| assertNotNull(success.getMessage());
|
|
317 |
| } |
|
318 |
| |
|
319 |
1
| try {
|
|
320 |
1
| pi.setValue("test \uD8F5 \uDF80 test");
|
|
321 |
0
| fail("Allowed two halves split by space");
|
|
322 |
| } |
|
323 |
| catch (IllegalDataException success) { |
|
324 |
1
| assertEquals("test \uD8F5 \uDF80 test", success.getData());
|
|
325 |
1
| assertNotNull(success.getMessage());
|
|
326 |
| } |
|
327 |
| |
|
328 |
1
| try {
|
|
329 |
1
| pi.setValue("test \uDF80\uD8F5 test");
|
|
330 |
0
| fail("Allowed reversed pair");
|
|
331 |
| } |
|
332 |
| catch (IllegalDataException success) { |
|
333 |
1
| assertEquals("test \uDF80\uD8F5 test", success.getData());
|
|
334 |
1
| assertNotNull(success.getMessage());
|
|
335 |
| } |
|
336 |
| |
|
337 |
| } |
|
338 |
| |
|
339 |
| |
|
340 |
1
| public void testLeafNode() {
|
|
341 |
| |
|
342 |
1
| assertEquals(0, pi.getChildCount());
|
|
343 |
1
| try {
|
|
344 |
1
| pi.getChild(0);
|
|
345 |
0
| fail("Didn't throw IndexOutofBoundsException");
|
|
346 |
| } |
|
347 |
| catch (IndexOutOfBoundsException success) { |
|
348 |
1
| assertNotNull(success.getMessage());
|
|
349 |
| } |
|
350 |
| |
|
351 |
1
| assertNull(pi.getParent());
|
|
352 |
| |
|
353 |
1
| Element element = new Element("test");
|
|
354 |
1
| element.appendChild(pi);
|
|
355 |
1
| assertEquals(element, pi.getParent());
|
|
356 |
1
| assertEquals(pi, element.getChild(0));
|
|
357 |
| |
|
358 |
1
| element.removeChild(pi);
|
|
359 |
1
| assertEquals(0, element.getChildCount());
|
|
360 |
| |
|
361 |
| } |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
1
| public void testCarriageReturnInProcessingInstructionData() {
|
|
368 |
| |
|
369 |
1
| try {
|
|
370 |
1
| new ProcessingInstruction("target", "data\rdata");
|
|
371 |
0
| fail("Allowed carriage return in processing instruction data");
|
|
372 |
| } |
|
373 |
| catch (IllegalDataException success) { |
|
374 |
1
| assertEquals("data\rdata", success.getData());
|
|
375 |
1
| assertNotNull(success.getMessage());
|
|
376 |
| } |
|
377 |
| |
|
378 |
| } |
|
379 |
| |
|
380 |
| |
|
381 |
1
| public void testAllowReservedCharactersInData() {
|
|
382 |
1
| ProcessingInstruction pi = new ProcessingInstruction("target", "<test>&&greater;");
|
|
383 |
1
| String xml = pi.toXML();
|
|
384 |
1
| assertEquals("<?target <test>&&greater;?>", xml);
|
|
385 |
| } |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
1
| public void testNoInitialWhiteSpace() {
|
|
390 |
| |
|
391 |
1
| try {
|
|
392 |
1
| new ProcessingInstruction("target", " initial spaces");
|
|
393 |
0
| fail("allowed processing instruction data with leading space");
|
|
394 |
| } |
|
395 |
| catch (IllegalDataException success) { |
|
396 |
1
| assertEquals(" initial spaces", success.getData());
|
|
397 |
1
| assertNotNull(success.getMessage());
|
|
398 |
| } |
|
399 |
| |
|
400 |
1
| try {
|
|
401 |
1
| new ProcessingInstruction("target", "\tinitial tab");
|
|
402 |
0
| fail("allowed processing instruction data with leading space");
|
|
403 |
| } |
|
404 |
| catch (IllegalDataException success) { |
|
405 |
1
| assertEquals("\tinitial tab", success.getData());
|
|
406 |
1
| assertNotNull(success.getMessage());
|
|
407 |
| } |
|
408 |
| |
|
409 |
1
| try {
|
|
410 |
1
| new ProcessingInstruction("target", "\ninitial linefeed");
|
|
411 |
0
| fail("allowed processing instruction data with leading space");
|
|
412 |
| } |
|
413 |
| catch (IllegalDataException success) { |
|
414 |
1
| assertEquals("\ninitial linefeed", success.getData());
|
|
415 |
1
| assertNotNull(success.getMessage());
|
|
416 |
| } |
|
417 |
| |
|
418 |
1
| try {
|
|
419 |
1
| new ProcessingInstruction("target", "\r initial carriage return");
|
|
420 |
0
| fail("allowed processing instruction data with leading space");
|
|
421 |
| } |
|
422 |
| catch (IllegalDataException success) { |
|
423 |
1
| assertEquals("\r initial carriage return", success.getData());
|
|
424 |
1
| assertNotNull(success.getMessage());
|
|
425 |
| } |
|
426 |
| |
|
427 |
| } |
|
428 |
| |
|
429 |
| |
|
430 |
1
| public void testNoXMLTargets() {
|
|
431 |
| |
|
432 |
1
| try {
|
|
433 |
1
| new ProcessingInstruction("xml", "data");
|
|
434 |
0
| fail("allowed processing instruction with target xml");
|
|
435 |
| } |
|
436 |
| catch (IllegalTargetException success) { |
|
437 |
1
| assertEquals("xml", success.getData());
|
|
438 |
1
| assertNotNull(success.getMessage());
|
|
439 |
| } |
|
440 |
| |
|
441 |
1
| try {
|
|
442 |
1
| new ProcessingInstruction("XML", "data");
|
|
443 |
0
| fail("allowed processing instruction with target XML");
|
|
444 |
| } |
|
445 |
| catch (IllegalTargetException success) { |
|
446 |
1
| assertEquals("XML", success.getData());
|
|
447 |
1
| assertNotNull(success.getMessage());
|
|
448 |
| } |
|
449 |
| |
|
450 |
1
| try {
|
|
451 |
1
| new ProcessingInstruction("Xml", "data");
|
|
452 |
0
| fail("allowed processing instruction with target Xml");
|
|
453 |
| } |
|
454 |
| catch (IllegalTargetException success) { |
|
455 |
1
| assertEquals("Xml", success.getData());
|
|
456 |
1
| assertNotNull(success.getMessage());
|
|
457 |
| } |
|
458 |
| |
|
459 |
| } |
|
460 |
| |
|
461 |
| |
|
462 |
1
| public void testColonsNotAllowedInTargets() {
|
|
463 |
| |
|
464 |
1
| try {
|
|
465 |
1
| new ProcessingInstruction("pre:target", "data");
|
|
466 |
0
| fail("allowed processing instruction with target that uses a prefixed name");
|
|
467 |
| } |
|
468 |
| catch (IllegalTargetException success) { |
|
469 |
1
| assertEquals("pre:target", success.getData());
|
|
470 |
1
| assertNotNull(success.getMessage());
|
|
471 |
| } |
|
472 |
| |
|
473 |
1
| try {
|
|
474 |
1
| new ProcessingInstruction("pre:", "data");
|
|
475 |
0
| fail("allowed processing instruction with trailing colon in target");
|
|
476 |
| } |
|
477 |
| catch (IllegalTargetException success) { |
|
478 |
1
| assertEquals("pre:", success.getData());
|
|
479 |
1
| assertNotNull(success.getMessage());
|
|
480 |
| } |
|
481 |
| |
|
482 |
1
| try {
|
|
483 |
1
| new ProcessingInstruction(":target", "data");
|
|
484 |
0
| fail("allowed processing instruction with initial colon in target");
|
|
485 |
| } |
|
486 |
| catch (IllegalTargetException success) { |
|
487 |
1
| assertEquals(":target", success.getData());
|
|
488 |
1
| assertNotNull(success.getMessage());
|
|
489 |
| } |
|
490 |
| |
|
491 |
| } |
|
492 |
| |
|
493 |
| |
|
494 |
| } |