1 | // |
2 | // XMLWriterTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "XMLWriterTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/XML/XMLWriter.h" |
15 | #include "Poco/SAX/AttributesImpl.h" |
16 | #include "Poco/Exception.h" |
17 | #include <sstream> |
18 | |
19 | |
20 | using Poco::XML::XMLWriter; |
21 | using Poco::XML::AttributesImpl; |
22 | |
23 | |
24 | XMLWriterTest::XMLWriterTest(const std::string& name): CppUnit::TestCase(name) |
25 | { |
26 | } |
27 | |
28 | |
29 | XMLWriterTest::~XMLWriterTest() |
30 | { |
31 | } |
32 | |
33 | |
34 | void XMLWriterTest::testTrivial() |
35 | { |
36 | std::ostringstream str; |
37 | XMLWriter writer(str, 0); |
38 | writer.startDocument(); |
39 | writer.startElement("" , "" , "foo" ); |
40 | writer.endElement("" , "" , "foo" ); |
41 | writer.endDocument(); |
42 | std::string xml = str.str(); |
43 | assertTrue (xml == "<foo/>" ); |
44 | } |
45 | |
46 | |
47 | void XMLWriterTest::testTrivialCanonical() |
48 | { |
49 | std::ostringstream str; |
50 | XMLWriter writer(str, XMLWriter::CANONICAL_XML); |
51 | writer.startDocument(); |
52 | writer.startElement("" , "" , "foo" ); |
53 | writer.endElement("" , "" , "foo" ); |
54 | writer.endDocument(); |
55 | std::string xml = str.str(); |
56 | assertTrue (xml == "<foo></foo>" ); |
57 | } |
58 | |
59 | |
60 | void XMLWriterTest::testTrivialDecl() |
61 | { |
62 | std::ostringstream str; |
63 | XMLWriter writer(str, XMLWriter::WRITE_XML_DECLARATION); |
64 | writer.startDocument(); |
65 | writer.startElement("" , "" , "foo" ); |
66 | writer.endElement("" , "" , "foo" ); |
67 | writer.endDocument(); |
68 | std::string xml = str.str(); |
69 | assertTrue (xml == "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo/>" ); |
70 | } |
71 | |
72 | |
73 | void XMLWriterTest::testTrivialDeclPretty() |
74 | { |
75 | std::ostringstream str; |
76 | XMLWriter writer(str, XMLWriter::WRITE_XML_DECLARATION | XMLWriter::PRETTY_PRINT); |
77 | writer.setNewLine("\n" ); |
78 | writer.startDocument(); |
79 | writer.startElement("" , "" , "foo" ); |
80 | writer.endElement("" , "" , "foo" ); |
81 | writer.endDocument(); |
82 | std::string xml = str.str(); |
83 | assertTrue (xml == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<foo/>\n" ); |
84 | } |
85 | |
86 | |
87 | void XMLWriterTest::testTrivialFragment() |
88 | { |
89 | std::ostringstream str; |
90 | XMLWriter writer(str, 0); |
91 | writer.startFragment(); |
92 | writer.startElement("" , "" , "foo" ); |
93 | writer.endElement("" , "" , "foo" ); |
94 | writer.endFragment(); |
95 | std::string xml = str.str(); |
96 | assertTrue (xml == "<foo/>" ); |
97 | } |
98 | |
99 | |
100 | void XMLWriterTest::testTrivialFragmentPretty() |
101 | { |
102 | std::ostringstream str; |
103 | XMLWriter writer(str, XMLWriter::WRITE_XML_DECLARATION | XMLWriter::PRETTY_PRINT); |
104 | writer.setNewLine("\n" ); |
105 | writer.startFragment(); |
106 | writer.startElement("" , "" , "foo" ); |
107 | writer.endElement("" , "" , "foo" ); |
108 | writer.endFragment(); |
109 | std::string xml = str.str(); |
110 | assertTrue (xml == "<foo/>\n" ); |
111 | } |
112 | |
113 | |
114 | void XMLWriterTest::testDTDPretty() |
115 | { |
116 | std::ostringstream str; |
117 | XMLWriter writer(str, XMLWriter::WRITE_XML_DECLARATION | XMLWriter::PRETTY_PRINT); |
118 | writer.setNewLine("\n" ); |
119 | writer.startDocument(); |
120 | writer.startDTD("test" , "" , "http://www.appinf.com/DTDs/test" ); |
121 | writer.endDTD(); |
122 | writer.startElement("" , "" , "foo" ); |
123 | writer.endElement("" , "" , "foo" ); |
124 | writer.endDocument(); |
125 | std::string xml = str.str(); |
126 | assertTrue (xml == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
127 | "<!DOCTYPE test SYSTEM \"http://www.appinf.com/DTDs/test\">\n" |
128 | "<foo/>\n" ); |
129 | } |
130 | |
131 | |
132 | void XMLWriterTest::testDTD() |
133 | { |
134 | std::ostringstream str; |
135 | XMLWriter writer(str, XMLWriter::WRITE_XML_DECLARATION); |
136 | writer.setNewLine("\n" ); |
137 | writer.startDocument(); |
138 | writer.startDTD("test" , "" , "http://www.appinf.com/DTDs/test" ); |
139 | writer.endDTD(); |
140 | writer.startElement("" , "" , "foo" ); |
141 | writer.endElement("" , "" , "foo" ); |
142 | writer.endDocument(); |
143 | std::string xml = str.str(); |
144 | assertTrue (xml == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
145 | "<!DOCTYPE test SYSTEM \"http://www.appinf.com/DTDs/test\">" |
146 | "<foo/>" ); |
147 | } |
148 | |
149 | |
150 | void XMLWriterTest::testDTDPublic() |
151 | { |
152 | std::ostringstream str; |
153 | XMLWriter writer(str, XMLWriter::WRITE_XML_DECLARATION); |
154 | writer.setNewLine("\n" ); |
155 | writer.startDocument(); |
156 | writer.startDTD("test" , "test" , "http://www.appinf.com/DTDs/test" ); |
157 | writer.endDTD(); |
158 | writer.startElement("" , "" , "foo" ); |
159 | writer.endElement("" , "" , "foo" ); |
160 | writer.endDocument(); |
161 | std::string xml = str.str(); |
162 | assertTrue (xml == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" |
163 | "<!DOCTYPE test PUBLIC \"test\" \"http://www.appinf.com/DTDs/test\">" |
164 | "<foo/>" ); |
165 | } |
166 | |
167 | |
168 | void XMLWriterTest::testDTDNotation() |
169 | { |
170 | std::ostringstream str; |
171 | XMLWriter writer(str, XMLWriter::WRITE_XML_DECLARATION | XMLWriter::PRETTY_PRINT); |
172 | writer.setNewLine("\n" ); |
173 | writer.startDocument(); |
174 | writer.startDTD("test" , "" , "" ); |
175 | std::string systemId("quicktime" ); |
176 | writer.notationDecl("mov" , 0, &systemId); |
177 | std::string publicId("-//W3C//NOTATION XML 1.0//EN" ); |
178 | writer.notationDecl("xml" , &publicId, 0); |
179 | writer.endDTD(); |
180 | writer.startElement("" , "" , "foo" ); |
181 | writer.endElement("" , "" , "foo" ); |
182 | writer.endDocument(); |
183 | std::string xml = str.str(); |
184 | assertTrue (xml == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
185 | "<!DOCTYPE test [\n" |
186 | "\t<!NOTATION mov SYSTEM \"quicktime\">\n" |
187 | "\t<!NOTATION xml PUBLIC \"-//W3C//NOTATION XML 1.0//EN\">\n" |
188 | "]>\n" |
189 | "<foo/>\n" ); |
190 | } |
191 | |
192 | |
193 | void XMLWriterTest::testDTDEntity() |
194 | { |
195 | std::ostringstream str; |
196 | XMLWriter writer(str, XMLWriter::WRITE_XML_DECLARATION | XMLWriter::PRETTY_PRINT); |
197 | writer.setNewLine("\n" ); |
198 | writer.startDocument(); |
199 | writer.startDTD("test" , "" , "" ); |
200 | std::string systemId("quicktime" ); |
201 | writer.notationDecl("mov" , 0, &systemId); |
202 | std::string publicId("-//W3C//NOTATION XML 1.0//EN" ); |
203 | writer.unparsedEntityDecl("movie" , 0, "movie.mov" , "mov" ); |
204 | writer.endDTD(); |
205 | writer.startElement("" , "" , "foo" ); |
206 | writer.endElement("" , "" , "foo" ); |
207 | writer.endDocument(); |
208 | std::string xml = str.str(); |
209 | assertTrue (xml == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
210 | "<!DOCTYPE test [\n" |
211 | "\t<!NOTATION mov SYSTEM \"quicktime\">\n" |
212 | "\t<!ENTITY movie SYSTEM \"movie.mov\" NDATA mov>\n" |
213 | "]>\n" |
214 | "<foo/>\n" ); |
215 | } |
216 | |
217 | |
218 | void XMLWriterTest::testAttributes() |
219 | { |
220 | std::ostringstream str; |
221 | XMLWriter writer(str, 0); |
222 | writer.startDocument(); |
223 | AttributesImpl attrs; |
224 | attrs.addAttribute("" , "" , "a1" , "CDATA" , "v1" ); |
225 | attrs.addAttribute("" , "" , "a2" , "CDATA" , "v2" ); |
226 | writer.startElement("" , "" , "el" , attrs); |
227 | writer.endElement("" , "" , "el" ); |
228 | writer.endDocument(); |
229 | std::string xml = str.str(); |
230 | assertTrue (xml == "<el a1=\"v1\" a2=\"v2\"/>" ); |
231 | } |
232 | |
233 | |
234 | void XMLWriterTest::testAttributesPretty() |
235 | { |
236 | std::ostringstream str; |
237 | XMLWriter writer(str, XMLWriter::PRETTY_PRINT | XMLWriter::PRETTY_PRINT_ATTRIBUTES); |
238 | writer.setNewLine(XMLWriter::NEWLINE_LF); |
239 | writer.startDocument(); |
240 | AttributesImpl attrs; |
241 | attrs.addAttribute("" , "" , "a1" , "CDATA" , "v1" ); |
242 | attrs.addAttribute("" , "" , "a2" , "CDATA" , "v2" ); |
243 | writer.startElement("" , "" , "el" , attrs); |
244 | writer.endElement("" , "" , "el" ); |
245 | writer.endDocument(); |
246 | std::string xml = str.str(); |
247 | assertTrue (xml == "<el\n\ta1=\"v1\"\n\ta2=\"v2\"/>\n" ); |
248 | } |
249 | |
250 | |
251 | void XMLWriterTest::testData() |
252 | { |
253 | std::ostringstream str; |
254 | XMLWriter writer(str, 0); |
255 | writer.startDocument(); |
256 | writer.dataElement("" , "" , "d" , "data" , "a1" , "v1" , "a2" , "v2" , "a3" , "v3" ); |
257 | writer.endDocument(); |
258 | std::string xml = str.str(); |
259 | assertTrue (xml == "<d a1=\"v1\" a2=\"v2\" a3=\"v3\">data</d>" ); |
260 | } |
261 | |
262 | |
263 | void XMLWriterTest::testEmptyData() |
264 | { |
265 | std::ostringstream str; |
266 | XMLWriter writer(str, 0); |
267 | writer.startDocument(); |
268 | writer.dataElement("" , "" , "d" , "" , "a1" , "v1" , "a2" , "v2" , "a3" , "v3" ); |
269 | writer.endDocument(); |
270 | std::string xml = str.str(); |
271 | assertTrue (xml == "<d a1=\"v1\" a2=\"v2\" a3=\"v3\"/>" ); |
272 | } |
273 | |
274 | |
275 | void XMLWriterTest::testDataPretty() |
276 | { |
277 | std::ostringstream str; |
278 | XMLWriter writer(str, XMLWriter::PRETTY_PRINT); |
279 | writer.setNewLine("\n" ); |
280 | writer.startDocument(); |
281 | writer.startElement("" , "" , "r" ); |
282 | writer.dataElement("" , "" , "d" , "data" , "a1" , "v1" , "a2" , "v2" , "a3" , "v3" ); |
283 | writer.endElement("" , "" , "r" ); |
284 | writer.endDocument(); |
285 | std::string xml = str.str(); |
286 | assertTrue (xml == "<r>\n\t<d a1=\"v1\" a2=\"v2\" a3=\"v3\">data</d>\n</r>\n" ); |
287 | } |
288 | |
289 | |
290 | void XMLWriterTest::testEmptyDataPretty() |
291 | { |
292 | std::ostringstream str; |
293 | XMLWriter writer(str, XMLWriter::PRETTY_PRINT); |
294 | writer.setNewLine("\n" ); |
295 | writer.startDocument(); |
296 | writer.startElement("" , "" , "r" ); |
297 | writer.dataElement("" , "" , "d" , "" , "a1" , "v1" , "a2" , "v2" , "a3" , "v3" ); |
298 | writer.endElement("" , "" , "r" ); |
299 | writer.endDocument(); |
300 | std::string xml = str.str(); |
301 | assertTrue (xml == "<r>\n\t<d a1=\"v1\" a2=\"v2\" a3=\"v3\"/>\n</r>\n" ); |
302 | } |
303 | |
304 | |
305 | void XMLWriterTest::() |
306 | { |
307 | std::ostringstream str; |
308 | XMLWriter writer(str, XMLWriter::PRETTY_PRINT); |
309 | writer.setNewLine("\n" ); |
310 | writer.startDocument(); |
311 | writer.comment("a comment" , 0, 9); |
312 | writer.startElement("" , "" , "r" ); |
313 | writer.comment("<another comment>" , 0, 17); |
314 | writer.dataElement("" , "" , "d" , "data" ); |
315 | writer.endElement("" , "" , "r" ); |
316 | writer.endDocument(); |
317 | std::string xml = str.str(); |
318 | assertTrue (xml == "<!--a comment-->\n<r>\n\t<!--<another comment>-->\n\t<d>data</d>\n</r>\n" ); |
319 | } |
320 | |
321 | |
322 | void XMLWriterTest::testPI() |
323 | { |
324 | std::ostringstream str; |
325 | XMLWriter writer(str, XMLWriter::PRETTY_PRINT); |
326 | writer.setNewLine("\n" ); |
327 | writer.startDocument(); |
328 | writer.processingInstruction("target" , "a processing instruction" ); |
329 | writer.startElement("" , "" , "r" ); |
330 | writer.processingInstruction("target" , "another processing instruction" ); |
331 | writer.dataElement("" , "" , "d" , "data" ); |
332 | writer.endElement("" , "" , "r" ); |
333 | writer.endDocument(); |
334 | std::string xml = str.str(); |
335 | assertTrue (xml == "<?target a processing instruction?>\n<r>\n\t<?target another processing instruction?>\n\t<d>data</d>\n</r>\n" ); |
336 | } |
337 | |
338 | |
339 | void XMLWriterTest::testCharacters() |
340 | { |
341 | std::ostringstream str; |
342 | XMLWriter writer(str, 0); |
343 | writer.startDocument(); |
344 | writer.startElement("" , "" , "r" ); |
345 | writer.characters("some \"chars\" that <must> be & escaped" ); |
346 | writer.endElement("" , "" , "r" ); |
347 | writer.endDocument(); |
348 | std::string xml = str.str(); |
349 | assertTrue (xml == "<r>some "chars" that <must> be & escaped</r>" ); |
350 | } |
351 | |
352 | |
353 | void XMLWriterTest::testEmptyCharacters() |
354 | { |
355 | std::ostringstream str; |
356 | XMLWriter writer(str, 0); |
357 | writer.startDocument(); |
358 | writer.startElement("" , "" , "r" ); |
359 | writer.characters("" ); |
360 | writer.endElement("" , "" , "r" ); |
361 | writer.endDocument(); |
362 | std::string xml = str.str(); |
363 | assertTrue (xml == "<r/>" ); |
364 | } |
365 | |
366 | |
367 | void XMLWriterTest::testCDATA() |
368 | { |
369 | std::ostringstream str; |
370 | XMLWriter writer(str, 0); |
371 | writer.startDocument(); |
372 | writer.startElement("" , "" , "r" ); |
373 | writer.startCDATA(); |
374 | writer.characters("some \"chars\" that <must> be & escaped" ); |
375 | writer.endCDATA(); |
376 | writer.endElement("" , "" , "r" ); |
377 | writer.endDocument(); |
378 | std::string xml = str.str(); |
379 | assertTrue (xml == "<r><![CDATA[some \"chars\" that <must> be & escaped]]></r>" ); |
380 | } |
381 | |
382 | |
383 | void XMLWriterTest::testRawCharacters() |
384 | { |
385 | std::ostringstream str; |
386 | XMLWriter writer(str, 0); |
387 | writer.startDocument(); |
388 | writer.startElement("" , "" , "r" ); |
389 | writer.startCDATA(); |
390 | writer.rawCharacters("some \"chars\" that <must> be & escaped" ); |
391 | writer.endCDATA(); |
392 | writer.endElement("" , "" , "r" ); |
393 | writer.endDocument(); |
394 | std::string xml = str.str(); |
395 | assertTrue (xml == "<r><![CDATA[some \"chars\" that <must> be & escaped]]></r>" ); |
396 | } |
397 | |
398 | |
399 | void XMLWriterTest::testAttributeCharacters() |
400 | { |
401 | std::ostringstream str; |
402 | XMLWriter writer(str, 0); |
403 | writer.startDocument(); |
404 | AttributesImpl attrs; |
405 | attrs.addAttribute("" , "" , "a1" , "CDATA" , "a b c\n\td" ); |
406 | attrs.addAttribute("" , "" , "a2" , "CDATA" , "a b c\r\nd" ); |
407 | writer.startElement("" , "" , "el" , attrs); |
408 | writer.endElement("" , "" , "el" ); |
409 | writer.endDocument(); |
410 | std::string xml = str.str(); |
411 | assertTrue (xml == "<el a1=\"a b c
	d\" a2=\"a b c
d\"/>" ); |
412 | } |
413 | |
414 | |
415 | void XMLWriterTest::testDefaultNamespace() |
416 | { |
417 | std::ostringstream str; |
418 | XMLWriter writer(str, 0); |
419 | writer.startDocument(); |
420 | writer.startPrefixMapping("" , "urn:ns" ); |
421 | writer.startElement("" , "" , "r" ); |
422 | writer.characters("data" ); |
423 | writer.endElement("" , "" , "r" ); |
424 | writer.endPrefixMapping("" ); |
425 | writer.endDocument(); |
426 | std::string xml = str.str(); |
427 | assertTrue (xml == "<r xmlns=\"urn:ns\">data</r>" ); |
428 | } |
429 | |
430 | |
431 | void XMLWriterTest::testQNamespaces() |
432 | { |
433 | std::ostringstream str; |
434 | XMLWriter writer(str, 0); |
435 | writer.startDocument(); |
436 | writer.startElement("urn:ns" , "r" , "p:r" ); |
437 | writer.characters("data" ); |
438 | writer.endElement("urn:ns" , "r" , "p:r" ); |
439 | writer.endDocument(); |
440 | std::string xml = str.str(); |
441 | assertTrue (xml == "<p:r xmlns:p=\"urn:ns\">data</p:r>" ); |
442 | } |
443 | |
444 | |
445 | void XMLWriterTest::testQNamespacesNested() |
446 | { |
447 | std::ostringstream str; |
448 | XMLWriter writer(str, 0); |
449 | writer.startDocument(); |
450 | writer.startElement("urn:ns" , "r" , "p:r" ); |
451 | writer.startElement("urn:ns" , "e" , "p:e" ); |
452 | writer.endElement("urn:ns" , "e" , "p:e" ); |
453 | writer.endElement("urn:ns" , "r" , "p:r" ); |
454 | writer.endDocument(); |
455 | std::string xml = str.str(); |
456 | assertTrue (xml == "<p:r xmlns:p=\"urn:ns\"><p:e/></p:r>" ); |
457 | } |
458 | |
459 | |
460 | void XMLWriterTest::testNamespaces() |
461 | { |
462 | std::ostringstream str; |
463 | XMLWriter writer(str, 0); |
464 | writer.startDocument(); |
465 | writer.startElement("urn:ns" , "r" , "" ); |
466 | writer.characters("data" ); |
467 | writer.endElement("urn:ns" , "r" , "" ); |
468 | writer.endDocument(); |
469 | std::string xml = str.str(); |
470 | assertTrue (xml == "<ns1:r xmlns:ns1=\"urn:ns\">data</ns1:r>" ); |
471 | } |
472 | |
473 | |
474 | void XMLWriterTest::testNamespacesCanonical() |
475 | { |
476 | std::ostringstream str; |
477 | XMLWriter writer(str, XMLWriter::CANONICAL_XML); |
478 | writer.startDocument(); |
479 | writer.startElement("urn:ns" , "r" , "" ); |
480 | writer.characters("data" ); |
481 | writer.endElement("urn:ns" , "r" , "" ); |
482 | writer.endDocument(); |
483 | std::string xml = str.str(); |
484 | assertTrue (xml == "<r xmlns=\"urn:ns\">data</r>" ); |
485 | } |
486 | |
487 | |
488 | void XMLWriterTest::testAttributeNamespaces() |
489 | { |
490 | std::ostringstream str; |
491 | XMLWriter writer(str, 0); |
492 | Poco::XML::AttributesImpl attrs; |
493 | attrs.addAttribute("urn:other" , "myattr" , "" , "" , "attrValue" ); |
494 | attrs.addAttribute("urn:ns" , "myattr2" , "" , "" , "attrValue2" ); |
495 | writer.startDocument(); |
496 | writer.startElement("urn:ns" , "r" , "" , attrs); |
497 | writer.characters("data" ); |
498 | writer.endElement("urn:ns" , "r" , "" ); |
499 | writer.endDocument(); |
500 | std::string xml = str.str(); |
501 | assertTrue (xml == "<ns2:r ns1:myattr=\"attrValue\" ns2:myattr2=\"attrValue2\" xmlns:ns1=\"urn:other\" xmlns:ns2=\"urn:ns\">data</ns2:r>" ); |
502 | } |
503 | |
504 | |
505 | void XMLWriterTest::testAttributeNamespacesCanonical() |
506 | { |
507 | std::ostringstream str; |
508 | XMLWriter writer(str, XMLWriter::CANONICAL_XML); |
509 | Poco::XML::AttributesImpl attrs; |
510 | attrs.addAttribute("urn:other" , "myattr" , "" , "" , "attrValue" ); |
511 | attrs.addAttribute("urn:ns" , "myattr2" , "" , "" , "attrValue2" ); |
512 | writer.startDocument(); |
513 | writer.startElement("urn:ns" , "r" , "" , attrs); |
514 | writer.characters("data" ); |
515 | writer.endElement("urn:ns" , "r" , "" ); |
516 | writer.endDocument(); |
517 | std::string xml = str.str(); |
518 | assertTrue (xml == "<r xmlns=\"urn:ns\" xmlns:ns1=\"urn:other\" myattr2=\"attrValue2\" ns1:myattr=\"attrValue\">data</r>" ); |
519 | } |
520 | |
521 | |
522 | void XMLWriterTest::testNamespacesNested() |
523 | { |
524 | std::ostringstream str; |
525 | XMLWriter writer(str, 0); |
526 | writer.startDocument(); |
527 | writer.startElement("urn:ns1" , "r" , "" ); |
528 | writer.startElement("urn:ns1" , "e" , "" ); |
529 | writer.endElement("urn:ns1" , "e" , "" ); |
530 | writer.startElement("urn:ns2" , "f" , "" ); |
531 | writer.endElement("urn:ns2" , "f" , "" ); |
532 | writer.endElement("urn:ns1" , "r" , "" ); |
533 | writer.endDocument(); |
534 | std::string xml = str.str(); |
535 | assertTrue (xml == "<ns1:r xmlns:ns1=\"urn:ns1\"><ns1:e/><ns2:f xmlns:ns2=\"urn:ns2\"/></ns1:r>" ); |
536 | } |
537 | |
538 | |
539 | void XMLWriterTest::testNamespacesNestedCanonical() |
540 | { |
541 | std::ostringstream str; |
542 | XMLWriter writer(str, XMLWriter::CANONICAL_XML); |
543 | writer.startDocument(); |
544 | writer.startElement("urn:ns1" , "r" , "" ); |
545 | writer.startElement("urn:ns1" , "e" , "" ); |
546 | writer.endElement("urn:ns1" , "e" , "" ); |
547 | Poco::XML::AttributesImpl attrs; |
548 | attrs.addAttribute("urn:ns1" , "myattr" , "myattr" , "" , "attrValue" ); |
549 | writer.startElement("urn:ns2" , "f" , "" , attrs); |
550 | writer.endElement("urn:ns2" , "f" , "" ); |
551 | writer.endElement("urn:ns1" , "r" , "" ); |
552 | writer.endDocument(); |
553 | std::string xml = str.str(); |
554 | assertTrue (xml == "<r xmlns=\"urn:ns1\"><e></e><ns1:f xmlns:ns1=\"urn:ns2\" myattr=\"attrValue\"></ns1:f></r>" ); |
555 | } |
556 | |
557 | |
558 | void XMLWriterTest::testExplicitNamespaces() |
559 | { |
560 | std::ostringstream str; |
561 | XMLWriter writer(str, 0); |
562 | writer.startDocument(); |
563 | writer.startPrefixMapping("p1" , "urn:ns1" ); |
564 | writer.startPrefixMapping("p2" , "urn:ns2" ); |
565 | writer.startElement("urn:ns1" , "r" , "" ); |
566 | writer.startElement("urn:ns2" , "e" , "" ); |
567 | writer.endElement("urn:ns2" , "e" , "" ); |
568 | writer.startPrefixMapping("p3" , "urn:ns3" ); |
569 | writer.startElement("urn:ns2" , "e" , "" ); |
570 | writer.startElement("urn:ns3" , "f" , "" ); |
571 | writer.endElement("urn:ns3" , "f" , "" ); |
572 | writer.endElement("urn:ns2" , "e" , "" ); |
573 | writer.endElement("urn:ns1" , "r" , "" ); |
574 | writer.endDocument(); |
575 | std::string xml = str.str(); |
576 | assertTrue (xml == "<p1:r xmlns:p1=\"urn:ns1\" xmlns:p2=\"urn:ns2\"><p2:e/><p2:e xmlns:p3=\"urn:ns3\"><p3:f/></p2:e></p1:r>" ); |
577 | } |
578 | |
579 | |
580 | void XMLWriterTest::testWellformed() |
581 | { |
582 | std::ostringstream str; |
583 | XMLWriter writer(str, 0); |
584 | writer.startDocument(); |
585 | writer.startElement("" , "" , "foo" ); |
586 | try |
587 | { |
588 | writer.endElement("" , "" , "bar" ); |
589 | fail("not wellformed - must throw exception" ); |
590 | } |
591 | catch (Poco::Exception&) |
592 | { |
593 | } |
594 | } |
595 | |
596 | |
597 | void XMLWriterTest::testWellformedNested() |
598 | { |
599 | std::ostringstream str; |
600 | XMLWriter writer(str, 0); |
601 | writer.startDocument(); |
602 | writer.startElement("" , "" , "foo" ); |
603 | writer.startElement("" , "" , "bar" ); |
604 | try |
605 | { |
606 | writer.endElement("" , "" , "foo" ); |
607 | fail("not wellformed - must throw exception" ); |
608 | } |
609 | catch (Poco::Exception&) |
610 | { |
611 | } |
612 | } |
613 | |
614 | |
615 | void XMLWriterTest::testWellformedNamespace() |
616 | { |
617 | std::ostringstream str; |
618 | XMLWriter writer(str, 0); |
619 | writer.startDocument(); |
620 | writer.startElement("urn:ns1" , "foo" , "" ); |
621 | writer.startElement("urn:ns2" , "bar" , "" ); |
622 | try |
623 | { |
624 | writer.endElement("urn:ns1" , "bar" , "" ); |
625 | fail("not wellformed - must throw exception" ); |
626 | } |
627 | catch (Poco::Exception&) |
628 | { |
629 | } |
630 | } |
631 | |
632 | |
633 | void XMLWriterTest::testEmpty() |
634 | { |
635 | std::ostringstream str; |
636 | XMLWriter writer(str, 0); |
637 | writer.startDocument(); |
638 | writer.startElement("" , "" , "foo" ); |
639 | writer.startElement("" , "" , "bar" ); |
640 | writer.emptyElement("" , "" , "empty" ); |
641 | writer.endElement("" , "" , "bar" ); |
642 | writer.endElement("" , "" , "foo" ); |
643 | writer.endDocument(); |
644 | std::string xml = str.str(); |
645 | assertTrue (xml == "<foo><bar><empty/></bar></foo>" ); |
646 | } |
647 | |
648 | |
649 | void XMLWriterTest::setUp() |
650 | { |
651 | } |
652 | |
653 | |
654 | void XMLWriterTest::tearDown() |
655 | { |
656 | } |
657 | |
658 | |
659 | CppUnit::Test* XMLWriterTest::suite() |
660 | { |
661 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("XMLWriterTest" ); |
662 | |
663 | CppUnit_addTest(pSuite, XMLWriterTest, testTrivial); |
664 | CppUnit_addTest(pSuite, XMLWriterTest, testTrivialCanonical); |
665 | CppUnit_addTest(pSuite, XMLWriterTest, testTrivialDecl); |
666 | CppUnit_addTest(pSuite, XMLWriterTest, testTrivialDeclPretty); |
667 | CppUnit_addTest(pSuite, XMLWriterTest, testTrivialFragment); |
668 | CppUnit_addTest(pSuite, XMLWriterTest, testTrivialFragmentPretty); |
669 | CppUnit_addTest(pSuite, XMLWriterTest, testDTDPretty); |
670 | CppUnit_addTest(pSuite, XMLWriterTest, testDTD); |
671 | CppUnit_addTest(pSuite, XMLWriterTest, testDTDPublic); |
672 | CppUnit_addTest(pSuite, XMLWriterTest, testDTDNotation); |
673 | CppUnit_addTest(pSuite, XMLWriterTest, testDTDEntity); |
674 | CppUnit_addTest(pSuite, XMLWriterTest, testAttributes); |
675 | CppUnit_addTest(pSuite, XMLWriterTest, testAttributesPretty); |
676 | CppUnit_addTest(pSuite, XMLWriterTest, testData); |
677 | CppUnit_addTest(pSuite, XMLWriterTest, testEmptyData); |
678 | CppUnit_addTest(pSuite, XMLWriterTest, testDataPretty); |
679 | CppUnit_addTest(pSuite, XMLWriterTest, testEmptyDataPretty); |
680 | CppUnit_addTest(pSuite, XMLWriterTest, testComment); |
681 | CppUnit_addTest(pSuite, XMLWriterTest, testPI); |
682 | CppUnit_addTest(pSuite, XMLWriterTest, testCharacters); |
683 | CppUnit_addTest(pSuite, XMLWriterTest, testEmptyCharacters); |
684 | CppUnit_addTest(pSuite, XMLWriterTest, testCDATA); |
685 | CppUnit_addTest(pSuite, XMLWriterTest, testRawCharacters); |
686 | CppUnit_addTest(pSuite, XMLWriterTest, testAttributeCharacters); |
687 | CppUnit_addTest(pSuite, XMLWriterTest, testDefaultNamespace); |
688 | CppUnit_addTest(pSuite, XMLWriterTest, testQNamespaces); |
689 | CppUnit_addTest(pSuite, XMLWriterTest, testQNamespacesNested); |
690 | CppUnit_addTest(pSuite, XMLWriterTest, testNamespaces); |
691 | CppUnit_addTest(pSuite, XMLWriterTest, testNamespacesCanonical); |
692 | CppUnit_addTest(pSuite, XMLWriterTest, testAttributeNamespaces); |
693 | CppUnit_addTest(pSuite, XMLWriterTest, testAttributeNamespacesCanonical); |
694 | CppUnit_addTest(pSuite, XMLWriterTest, testNamespacesNested); |
695 | CppUnit_addTest(pSuite, XMLWriterTest, testNamespacesNestedCanonical); |
696 | CppUnit_addTest(pSuite, XMLWriterTest, testExplicitNamespaces); |
697 | CppUnit_addTest(pSuite, XMLWriterTest, testWellformed); |
698 | CppUnit_addTest(pSuite, XMLWriterTest, testWellformedNested); |
699 | CppUnit_addTest(pSuite, XMLWriterTest, testWellformedNamespace); |
700 | CppUnit_addTest(pSuite, XMLWriterTest, testEmpty); |
701 | |
702 | return pSuite; |
703 | } |
704 | |