1//
2// ElementTest.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 "ElementTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/DOM/Document.h"
15#include "Poco/DOM/Element.h"
16#include "Poco/DOM/Attr.h"
17#include "Poco/DOM/Text.h"
18#include "Poco/DOM/NamedNodeMap.h"
19#include "Poco/DOM/NodeList.h"
20#include "Poco/DOM/AutoPtr.h"
21
22
23using Poco::XML::Element;
24using Poco::XML::Document;
25using Poco::XML::Attr;
26using Poco::XML::Text;
27using Poco::XML::Node;
28using Poco::XML::NamedNodeMap;
29using Poco::XML::NodeList;
30using Poco::XML::AutoPtr;
31using Poco::XML::XMLString;
32
33
34ElementTest::ElementTest(const std::string& name): CppUnit::TestCase(name)
35{
36}
37
38
39ElementTest::~ElementTest()
40{
41}
42
43
44void ElementTest::testAttributes()
45{
46 AutoPtr<Document> pDoc = new Document;
47 AutoPtr<Element> pElem = pDoc->createElement("elem");
48
49 assertTrue (!pElem->hasAttributes());
50
51 pElem->setAttribute("a1", "v1");
52 assertTrue (pElem->hasAttributes());
53
54 assertTrue (pElem->hasAttribute("a1"));
55 assertTrue (pElem->getAttribute("a1") == "v1");
56
57 Attr* pAttr1 = pElem->getAttributeNode("a1");
58 assertTrue (pAttr1 != 0);
59 assertTrue (pAttr1->name() == "a1");
60 assertTrue (pAttr1->nodeName() == "a1");
61 assertTrue (pAttr1->value() == "v1");
62 assertTrue (pAttr1->nodeValue() == "v1");
63 assertTrue (pAttr1->ownerElement() == pElem);
64 assertTrue (pAttr1->ownerDocument() == pDoc);
65 assertTrue (pAttr1->innerText() == "v1");
66
67 assertTrue (pAttr1->previousSibling() == 0);
68 assertTrue (pAttr1->nextSibling() == 0);
69
70 pAttr1->setValue("V1");
71 assertTrue (pElem->getAttribute("a1") == "V1");
72
73 pElem->setAttribute("a2", "v2");
74 assertTrue (pElem->hasAttribute("a1"));
75 assertTrue (pElem->getAttribute("a1") == "V1");
76 assertTrue (pElem->hasAttribute("a2"));
77 assertTrue (pElem->getAttribute("a2") == "v2");
78
79 Attr* pAttr2 = pElem->getAttributeNode("a2");
80 assertTrue (pAttr2 != 0);
81 assertTrue (pAttr2->name() == "a2");
82 assertTrue (pAttr2->value() == "v2");
83 assertTrue (pAttr2->ownerElement() == pElem);
84
85 assertTrue (pAttr1->previousSibling() == 0);
86 assertTrue (pAttr1->nextSibling() == pAttr2);
87 assertTrue (pAttr2->previousSibling() == pAttr1);
88 assertTrue (pAttr2->nextSibling() == 0);
89
90 Attr* pAttr3 = pElem->getAttributeNode("a3");
91 assertTrue (pAttr3 == 0);
92
93 pAttr3 = pDoc->createAttribute("a3");
94 pAttr3->setValue("v3");
95 pElem->setAttributeNode(pAttr3);
96 pAttr3->release();
97
98 assertTrue (pElem->hasAttribute("a1"));
99 assertTrue (pElem->getAttribute("a1") == "V1");
100 assertTrue (pElem->hasAttribute("a2"));
101 assertTrue (pElem->getAttribute("a2") == "v2");
102 assertTrue (pElem->hasAttribute("a3"));
103 assertTrue (pElem->getAttribute("a3") == "v3");
104
105 assertTrue (pAttr1->previousSibling() == 0);
106 assertTrue (pAttr1->nextSibling() == pAttr2);
107 assertTrue (pAttr2->previousSibling() == pAttr1);
108 assertTrue (pAttr2->nextSibling() == pAttr3);
109 assertTrue (pAttr3->previousSibling() == pAttr2);
110 assertTrue (pAttr3->nextSibling() == 0);
111
112 pAttr2 = pDoc->createAttribute("a2");
113 pAttr2->setValue("V2");
114 pElem->setAttributeNode(pAttr2);
115 pAttr2->release();
116
117 assertTrue (pElem->hasAttribute("a1"));
118 assertTrue (pElem->getAttribute("a1") == "V1");
119 assertTrue (pElem->hasAttribute("a2"));
120 assertTrue (pElem->getAttribute("a2") == "V2");
121 assertTrue (pElem->hasAttribute("a3"));
122 assertTrue (pElem->getAttribute("a3") == "v3");
123
124 pAttr1 = pDoc->createAttribute("a1");
125 pAttr1->setValue("v1");
126 pElem->setAttributeNode(pAttr1);
127 pAttr1->release();
128
129 assertTrue (pElem->hasAttribute("a1"));
130 assertTrue (pElem->getAttribute("a1") == "v1");
131 assertTrue (pElem->hasAttribute("a2"));
132 assertTrue (pElem->getAttribute("a2") == "V2");
133 assertTrue (pElem->hasAttribute("a3"));
134 assertTrue (pElem->getAttribute("a3") == "v3");
135
136 pAttr3 = pDoc->createAttribute("a3");
137 pAttr3->setValue("V3");
138 pElem->setAttributeNode(pAttr3);
139 pAttr3->release();
140
141 assertTrue (pElem->hasAttribute("a1"));
142 assertTrue (pElem->getAttribute("a1") == "v1");
143 assertTrue (pElem->hasAttribute("a2"));
144 assertTrue (pElem->getAttribute("a2") == "V2");
145 assertTrue (pElem->hasAttribute("a3"));
146 assertTrue (pElem->getAttribute("a3") == "V3");
147
148 pElem->removeAttributeNode(pAttr3);
149 assertTrue (!pElem->hasAttribute("a3"));
150
151 pElem->removeAttribute("a1");
152 assertTrue (!pElem->hasAttribute("a1"));
153
154 pElem->removeAttribute("a2");
155 assertTrue (!pElem->hasAttribute("a2"));
156
157 assertTrue (!pElem->hasAttributes());
158}
159
160
161void ElementTest::testAttributesNS()
162{
163 AutoPtr<Document> pDoc = new Document;
164 AutoPtr<Element> pElem = pDoc->createElementNS("urn:ns1", "p:elem");
165
166 assertTrue (pElem->namespaceURI() == "urn:ns1");
167 assertTrue (pElem->prefix() == "p");
168 assertTrue (pElem->tagName() == "p:elem");
169 assertTrue (pElem->localName() == "elem");
170
171 assertTrue (!pElem->hasAttributes());
172
173 pElem->setAttributeNS("urn:ns1", "a1", "v1");
174 assertTrue (pElem->hasAttributes());
175
176 assertTrue (pElem->hasAttributeNS("urn:ns1", "a1"));
177 assertTrue (pElem->getAttributeNS("urn:ns1", "a1") == "v1");
178
179 Attr* pAttr1 = pElem->getAttributeNodeNS("urn:ns1", "a1");
180 assertTrue (pAttr1 != 0);
181 assertTrue (pAttr1->name() == "a1");
182 assertTrue (pAttr1->namespaceURI() == "urn:ns1");
183 assertTrue (pAttr1->prefix().empty());
184 assertTrue (pAttr1->localName() == "a1");
185 assertTrue (pAttr1->nodeName() == "a1");
186 assertTrue (pAttr1->value() == "v1");
187 assertTrue (pAttr1->nodeValue() == "v1");
188 assertTrue (pAttr1->ownerElement() == pElem);
189
190 pAttr1->setValue("V1");
191 assertTrue (pElem->getAttributeNS("urn:ns1", "a1") == "V1");
192
193 pElem->setAttributeNS("urn:ns1", "a2", "v2");
194 assertTrue (pElem->hasAttributeNS("urn:ns1", "a1"));
195 assertTrue (pElem->getAttributeNS("urn:ns1", "a1") == "V1");
196 assertTrue (pElem->hasAttributeNS("urn:ns1", "a2"));
197 assertTrue (pElem->getAttributeNS("urn:ns1", "a2") == "v2");
198
199 Attr* pAttr2 = pElem->getAttributeNodeNS("urn:ns1", "a2");
200 assertTrue (pAttr2 != 0);
201 assertTrue (pAttr2->name() == "a2");
202 assertTrue (pAttr2->namespaceURI() == "urn:ns1");
203 assertTrue (pAttr2->prefix().empty());
204 assertTrue (pAttr2->localName() == "a2");
205 assertTrue (pAttr2->value() == "v2");
206 assertTrue (pAttr2->ownerElement() == pElem);
207
208 Attr* pAttr3 = pElem->getAttributeNodeNS("urn:ns2", "p:a3");
209 assertTrue (pAttr3 == 0);
210
211 pAttr3 = pDoc->createAttributeNS("urn:ns2", "p:a3");
212 pAttr3->setValue("v3");
213 pElem->setAttributeNodeNS(pAttr3);
214 pAttr3->release();
215
216 assertTrue (pElem->hasAttributeNS("urn:ns1", "a1"));
217 assertTrue (pElem->getAttributeNS("urn:ns1", "a1") == "V1");
218 assertTrue (pElem->hasAttributeNS("urn:ns1", "a2"));
219 assertTrue (pElem->getAttributeNS("urn:ns1", "a2") == "v2");
220 assertTrue (pElem->hasAttributeNS("urn:ns2", "a3"));
221 assertTrue (pElem->getAttributeNS("urn:ns2", "a3") == "v3");
222
223 pAttr2 = pDoc->createAttributeNS("urn:ns1", "a2");
224 pAttr2->setValue("V2");
225 pElem->setAttributeNodeNS(pAttr2);
226 pAttr2->release();
227
228 assertTrue (pElem->hasAttributeNS("urn:ns1", "a1"));
229 assertTrue (pElem->getAttributeNS("urn:ns1", "a1") == "V1");
230 assertTrue (pElem->hasAttributeNS("urn:ns1", "a2"));
231 assertTrue (pElem->getAttributeNS("urn:ns1", "a2") == "V2");
232 assertTrue (pElem->hasAttributeNS("urn:ns2", "a3"));
233 assertTrue (pElem->getAttributeNS("urn:ns2", "a3") == "v3");
234
235 pAttr1 = pDoc->createAttributeNS("urn:ns1", "a1");
236 pAttr1->setValue("v1");
237 pElem->setAttributeNodeNS(pAttr1);
238 pAttr1->release();
239
240 assertTrue (pElem->hasAttributeNS("urn:ns1", "a1"));
241 assertTrue (pElem->getAttributeNS("urn:ns1", "a1") == "v1");
242 assertTrue (pElem->hasAttributeNS("urn:ns1", "a2"));
243 assertTrue (pElem->getAttributeNS("urn:ns1", "a2") == "V2");
244 assertTrue (pElem->hasAttributeNS("urn:ns2", "a3"));
245 assertTrue (pElem->getAttributeNS("urn:ns2", "a3") == "v3");
246
247 pAttr3 = pDoc->createAttributeNS("urn:ns2", "q:a3");
248 pAttr3->setValue("V3");
249 pElem->setAttributeNodeNS(pAttr3);
250 pAttr3->release();
251
252 assertTrue (pElem->hasAttributeNS("urn:ns1", "a1"));
253 assertTrue (pElem->getAttributeNS("urn:ns1", "a1") == "v1");
254 assertTrue (pElem->hasAttributeNS("urn:ns1", "a2"));
255 assertTrue (pElem->getAttributeNS("urn:ns1", "a2") == "V2");
256 assertTrue (pElem->hasAttributeNS("urn:ns2", "a3"));
257 assertTrue (pElem->getAttributeNS("urn:ns2", "a3") == "V3");
258
259 pElem->removeAttributeNode(pAttr3);
260 assertTrue (!pElem->hasAttributeNS("urn:ns2", "a3"));
261
262 pElem->removeAttributeNS("urn:ns1", "a1");
263 assertTrue (!pElem->hasAttributeNS("urn:ns1", "a1"));
264
265 pElem->removeAttributeNS("urn:ns1", "a2");
266 assertTrue (!pElem->hasAttributeNS("urn:ns1", "a2"));
267
268 assertTrue (!pElem->hasAttributes());
269}
270
271
272void ElementTest::testAttrMap()
273{
274 AutoPtr<Document> pDoc = new Document;
275 AutoPtr<Element> pElem = pDoc->createElement("elem");
276
277 AutoPtr<NamedNodeMap> pNNM = pElem->attributes();
278 assertTrue (pNNM->length() == 0);
279
280 pElem->setAttribute("a1", "v1");
281 assertTrue (pNNM->length() == 1);
282 assertTrue (pNNM->item(0)->nodeName() == "a1");
283 assertTrue (pNNM->getNamedItem("a1")->nodeName() == "a1");
284
285 pElem->setAttribute("a2", "v2");
286 assertTrue (pNNM->length() == 2);
287 assertTrue (pNNM->item(0)->nodeName() == "a1");
288 assertTrue (pNNM->getNamedItem("a1")->nodeName() == "a1");
289 assertTrue (pNNM->item(1)->nodeName() == "a2");
290 assertTrue (pNNM->getNamedItem("a2")->nodeName() == "a2");
291
292 Attr* pAttr = pDoc->createAttribute("a3");
293 pNNM->setNamedItem(pAttr);
294 pAttr->release();
295
296 assertTrue (pNNM->length() == 3);
297 assertTrue (pNNM->item(0)->nodeName() == "a1");
298 assertTrue (pNNM->getNamedItem("a1")->nodeName() == "a1");
299 assertTrue (pNNM->item(1)->nodeName() == "a2");
300 assertTrue (pNNM->getNamedItem("a2")->nodeName() == "a2");
301 assertTrue (pNNM->item(2)->nodeName() == "a3");
302 assertTrue (pNNM->getNamedItem("a3")->nodeName() == "a3");
303
304 pNNM->removeNamedItem("a2");
305 assertTrue (pNNM->length() == 2);
306 assertTrue (!pElem->hasAttribute("a2"));
307
308 pNNM->removeNamedItem("a3");
309 assertTrue (pNNM->length() == 1);
310 assertTrue (!pElem->hasAttribute("a3"));
311
312 pElem->removeAttribute("a1");
313 assertTrue (pNNM->length() == 0);
314}
315
316
317void ElementTest::testAttrMapNS()
318{
319 AutoPtr<Document> pDoc = new Document;
320 AutoPtr<Element> pElem = pDoc->createElementNS("urn:ns1", "elem");
321
322 AutoPtr<NamedNodeMap> pNNM = pElem->attributes();
323 assertTrue (pNNM->length() == 0);
324
325 pElem->setAttributeNS("urn:ns1", "a1", "v1");
326 assertTrue (pNNM->length() == 1);
327 assertTrue (pNNM->item(0)->nodeName() == "a1");
328 assertTrue (pNNM->getNamedItemNS("urn:ns1", "a1")->nodeName() == "a1");
329
330 pElem->setAttributeNS("urn:ns1", "a2", "v2");
331 assertTrue (pNNM->length() == 2);
332 assertTrue (pNNM->item(0)->nodeName() == "a1");
333 assertTrue (pNNM->getNamedItem("a1")->nodeName() == "a1");
334 assertTrue (pNNM->item(1)->nodeName() == "a2");
335 assertTrue (pNNM->getNamedItem("a2")->nodeName() == "a2");
336
337 Attr* pAttr = pDoc->createAttributeNS("urn:ns2", "a3");
338 pNNM->setNamedItem(pAttr);
339 pAttr->release();
340
341 assertTrue (pNNM->length() == 3);
342 assertTrue (pNNM->item(0)->nodeName() == "a1");
343 assertTrue (pNNM->getNamedItemNS("urn:ns1", "a1")->nodeName() == "a1");
344 assertTrue (pNNM->item(1)->nodeName() == "a2");
345 assertTrue (pNNM->getNamedItemNS("urn:ns1", "a2")->nodeName() == "a2");
346 assertTrue (pNNM->item(2)->nodeName() == "a3");
347 assertTrue (pNNM->getNamedItemNS("urn:ns2", "a3")->nodeName() == "a3");
348
349 pNNM->removeNamedItemNS("urn:ns1", "a2");
350 assertTrue (pNNM->length() == 2);
351 assertTrue (!pElem->hasAttributeNS("urn:ns1", "a2"));
352
353 pNNM->removeNamedItemNS("urn:ns2", "a3");
354 assertTrue (pNNM->length() == 1);
355 assertTrue (!pElem->hasAttributeNS("urn:ns2", "a3"));
356
357 pElem->removeAttributeNS("urn:ns1", "a1");
358 assertTrue (pNNM->length() == 0);
359}
360
361
362void ElementTest::testElementsByTagName()
363{
364 AutoPtr<Document> pDoc = new Document;
365 AutoPtr<Element> pRoot = pDoc->createElement("root");
366 AutoPtr<NodeList> pNL1 = pRoot->getElementsByTagName("*");
367 AutoPtr<NodeList> pNL2 = pRoot->getElementsByTagName("elem");
368
369 assertTrue (pNL1->length() == 0);
370 assertTrue (pNL2->length() == 0);
371
372 AutoPtr<Element> pElem1 = pDoc->createElement("elem");
373 pRoot->appendChild(pElem1);
374
375 assertTrue (pNL1->length() == 1);
376 assertTrue (pNL2->length() == 1);
377 assertTrue (pNL1->item(0) == pElem1);
378 assertTrue (pNL2->item(0) == pElem1);
379
380 AutoPtr<Element> pElem2 = pDoc->createElement("Elem");
381 pRoot->appendChild(pElem2);
382
383 assertTrue (pNL1->length() == 2);
384 assertTrue (pNL2->length() == 1);
385 assertTrue (pNL1->item(0) == pElem1);
386 assertTrue (pNL1->item(1) == pElem2);
387 assertTrue (pNL2->item(0) == pElem1);
388
389 AutoPtr<Element> pElem3 = pDoc->createElement("elem");
390 pRoot->appendChild(pElem3);
391
392 assertTrue (pNL1->length() == 3);
393 assertTrue (pNL2->length() == 2);
394 assertTrue (pNL1->item(0) == pElem1);
395 assertTrue (pNL1->item(1) == pElem2);
396 assertTrue (pNL1->item(2) == pElem3);
397 assertTrue (pNL2->item(0) == pElem1);
398 assertTrue (pNL2->item(1) == pElem3);
399
400 AutoPtr<Element> pElem11 = pDoc->createElement("elem");
401 pElem1->appendChild(pElem11);
402
403 assertTrue (pNL1->length() == 4);
404 assertTrue (pNL2->length() == 3);
405 assertTrue (pNL1->item(0) == pElem1);
406 assertTrue (pNL1->item(1) == pElem11);
407 assertTrue (pNL1->item(2) == pElem2);
408 assertTrue (pNL1->item(3) == pElem3);
409 assertTrue (pNL2->item(0) == pElem1);
410 assertTrue (pNL2->item(1) == pElem11);
411 assertTrue (pNL2->item(2) == pElem3);
412
413 AutoPtr<Element> pElem12 = pDoc->createElement("Elem");
414 pElem1->appendChild(pElem12);
415
416 assertTrue (pNL1->length() == 5);
417 assertTrue (pNL2->length() == 3);
418 assertTrue (pNL1->item(0) == pElem1);
419 assertTrue (pNL1->item(1) == pElem11);
420 assertTrue (pNL1->item(2) == pElem12);
421 assertTrue (pNL1->item(3) == pElem2);
422 assertTrue (pNL1->item(4) == pElem3);
423 assertTrue (pNL2->item(0) == pElem1);
424 assertTrue (pNL2->item(1) == pElem11);
425 assertTrue (pNL2->item(2) == pElem3);
426
427 AutoPtr<Element> pElem21 = pDoc->createElement("elem");
428 pElem2->appendChild(pElem21);
429
430 assertTrue (pNL1->length() == 6);
431 assertTrue (pNL2->length() == 4);
432 assertTrue (pNL1->item(0) == pElem1);
433 assertTrue (pNL1->item(1) == pElem11);
434 assertTrue (pNL1->item(2) == pElem12);
435 assertTrue (pNL1->item(3) == pElem2);
436 assertTrue (pNL1->item(4) == pElem21);
437 assertTrue (pNL1->item(5) == pElem3);
438 assertTrue (pNL2->item(0) == pElem1);
439 assertTrue (pNL2->item(1) == pElem11);
440 assertTrue (pNL2->item(2) == pElem21);
441 assertTrue (pNL2->item(3) == pElem3);
442}
443
444
445void ElementTest::testElementsByTagNameNS()
446{
447 AutoPtr<Document> pDoc = new Document;
448 AutoPtr<Element> pRoot = pDoc->createElementNS("urn:ns1", "root");
449 AutoPtr<NodeList> pNL1 = pRoot->getElementsByTagNameNS("*", "*");
450 AutoPtr<NodeList> pNL2 = pRoot->getElementsByTagNameNS("*", "elem");
451 AutoPtr<NodeList> pNL3 = pRoot->getElementsByTagNameNS("urn:ns1", "elem");
452
453 assertTrue (pNL1->length() == 0);
454 assertTrue (pNL2->length() == 0);
455
456 AutoPtr<Element> pElem1 = pDoc->createElementNS("urn:ns1", "elem");
457 pRoot->appendChild(pElem1);
458
459 assertTrue (pNL1->length() == 1);
460 assertTrue (pNL2->length() == 1);
461 assertTrue (pNL3->length() == 1);
462 assertTrue (pNL1->item(0) == pElem1);
463 assertTrue (pNL2->item(0) == pElem1);
464 assertTrue (pNL3->item(0) == pElem1);
465
466 AutoPtr<Element> pElem2 = pDoc->createElementNS("urn:ns1", "Elem");
467 pRoot->appendChild(pElem2);
468
469 assertTrue (pNL1->length() == 2);
470 assertTrue (pNL2->length() == 1);
471 assertTrue (pNL3->length() == 1);
472 assertTrue (pNL1->item(0) == pElem1);
473 assertTrue (pNL1->item(1) == pElem2);
474 assertTrue (pNL2->item(0) == pElem1);
475 assertTrue (pNL3->item(0) == pElem1);
476
477 AutoPtr<Element> pElem3 = pDoc->createElementNS("urn:ns2", "elem");
478 pRoot->appendChild(pElem3);
479
480 assertTrue (pNL1->length() == 3);
481 assertTrue (pNL2->length() == 2);
482 assertTrue (pNL3->length() == 1);
483 assertTrue (pNL1->item(0) == pElem1);
484 assertTrue (pNL1->item(1) == pElem2);
485 assertTrue (pNL1->item(2) == pElem3);
486 assertTrue (pNL2->item(0) == pElem1);
487 assertTrue (pNL2->item(1) == pElem3);
488 assertTrue (pNL3->item(0) == pElem1);
489
490 AutoPtr<Element> pElem11 = pDoc->createElementNS("urn:ns1", "elem");
491 pElem1->appendChild(pElem11);
492
493 assertTrue (pNL1->length() == 4);
494 assertTrue (pNL2->length() == 3);
495 assertTrue (pNL3->length() == 2);
496 assertTrue (pNL1->item(0) == pElem1);
497 assertTrue (pNL1->item(1) == pElem11);
498 assertTrue (pNL1->item(2) == pElem2);
499 assertTrue (pNL1->item(3) == pElem3);
500 assertTrue (pNL2->item(0) == pElem1);
501 assertTrue (pNL2->item(1) == pElem11);
502 assertTrue (pNL2->item(2) == pElem3);
503 assertTrue (pNL3->item(0) == pElem1);
504 assertTrue (pNL3->item(1) == pElem11);
505
506 AutoPtr<Element> pElem12 = pDoc->createElementNS("urn:ns1", "Elem");
507 pElem1->appendChild(pElem12);
508
509 assertTrue (pNL1->length() == 5);
510 assertTrue (pNL2->length() == 3);
511 assertTrue (pNL3->length() == 2);
512 assertTrue (pNL1->item(0) == pElem1);
513 assertTrue (pNL1->item(1) == pElem11);
514 assertTrue (pNL1->item(2) == pElem12);
515 assertTrue (pNL1->item(3) == pElem2);
516 assertTrue (pNL1->item(4) == pElem3);
517 assertTrue (pNL2->item(0) == pElem1);
518 assertTrue (pNL2->item(1) == pElem11);
519 assertTrue (pNL2->item(2) == pElem3);
520 assertTrue (pNL3->item(0) == pElem1);
521 assertTrue (pNL3->item(1) == pElem11);
522
523 AutoPtr<Element> pElem21 = pDoc->createElementNS("urn:ns1", "elem");
524 pElem2->appendChild(pElem21);
525
526 assertTrue (pNL1->length() == 6);
527 assertTrue (pNL2->length() == 4);
528 assertTrue (pNL3->length() == 3);
529 assertTrue (pNL1->item(0) == pElem1);
530 assertTrue (pNL1->item(1) == pElem11);
531 assertTrue (pNL1->item(2) == pElem12);
532 assertTrue (pNL1->item(3) == pElem2);
533 assertTrue (pNL1->item(4) == pElem21);
534 assertTrue (pNL1->item(5) == pElem3);
535 assertTrue (pNL2->item(0) == pElem1);
536 assertTrue (pNL2->item(1) == pElem11);
537 assertTrue (pNL2->item(2) == pElem21);
538 assertTrue (pNL2->item(3) == pElem3);
539 assertTrue (pNL3->item(0) == pElem1);
540 assertTrue (pNL3->item(1) == pElem11);
541 assertTrue (pNL3->item(2) == pElem21);
542}
543
544
545void ElementTest::testInnerText()
546{
547 AutoPtr<Document> pDoc = new Document;
548 AutoPtr<Element> pRoot = pDoc->createElement("root");
549 AutoPtr<Text> pText1 = pDoc->createTextNode("text1");
550 AutoPtr<Element> pElem1 = pDoc->createElement("elem1");
551 AutoPtr<Text> pText2 = pDoc->createTextNode("text2");
552 AutoPtr<Text> pText3 = pDoc->createTextNode("text3");
553
554 pElem1->appendChild(pText2);
555 pRoot->appendChild(pText1);
556 pRoot->appendChild(pElem1);
557 pRoot->appendChild(pText3);
558
559 XMLString innerText = pRoot->innerText();
560 assertTrue (innerText == "text1text2text3");
561}
562
563
564void ElementTest::testChildElement()
565{
566 AutoPtr<Document> pDoc = new Document;
567 AutoPtr<Element> pRoot = pDoc->createElement("root");
568 AutoPtr<Element> pElem1 = pDoc->createElement("elem1");
569 AutoPtr<Element> pElem2 = pDoc->createElement("elem2");
570 AutoPtr<Element> pElem3 = pDoc->createElement("elem3");
571 AutoPtr<Element> pElem4 = pDoc->createElement("elem3");
572
573 pRoot->appendChild(pElem1);
574 pRoot->appendChild(pElem2);
575 pRoot->appendChild(pElem3);
576 pRoot->appendChild(pElem4);
577
578 assertTrue (pRoot->getChildElement("elem1") == pElem1);
579 assertTrue (pRoot->getChildElement("elem2") == pElem2);
580 assertTrue (pRoot->getChildElement("elem3") == pElem3);
581 assertTrue (pRoot->getChildElement("elem4") == 0);
582
583 assertTrue (pElem1->getChildElement("elem11") == 0);
584}
585
586
587void ElementTest::testChildElementNS()
588{
589 AutoPtr<Document> pDoc = new Document;
590 AutoPtr<Element> pRoot = pDoc->createElementNS("urn:ns", "root");
591 AutoPtr<Element> pElem1 = pDoc->createElementNS("urn:ns", "elem1");
592 AutoPtr<Element> pElem2 = pDoc->createElementNS("urn:ns", "elem2");
593 AutoPtr<Element> pElem3 = pDoc->createElementNS("urn:ns", "elem3");
594 AutoPtr<Element> pElem4 = pDoc->createElementNS("urn:ns", "elem3");
595
596 pRoot->appendChild(pElem1);
597 pRoot->appendChild(pElem2);
598 pRoot->appendChild(pElem3);
599 pRoot->appendChild(pElem4);
600
601 assertTrue (pRoot->getChildElementNS("urn:ns", "elem1") == pElem1);
602 assertTrue (pRoot->getChildElementNS("urn:ns", "elem2") == pElem2);
603 assertTrue (pRoot->getChildElementNS("urn:ns", "elem3") == pElem3);
604 assertTrue (pRoot->getChildElementNS("urn:ns", "elem4") == 0);
605 assertTrue (pRoot->getChildElementNS("urn:NS", "elem1") == 0);
606
607 assertTrue (pElem1->getChildElementNS("urn:ns", "elem11") == 0);
608}
609
610
611void ElementTest::testNodeByPath()
612{
613 /*
614 <root>
615 <elem1>
616 <elemA/>
617 <elemA/>
618 </elem1>
619 <elem2>
620 <elemB attr1="value1"/>
621 <elemB attr1="value2"/>
622 <elemB attr1="value3"/>
623 <elemC attr1="value1">
624 <elemC1 attr1="value1"/>
625 <elemC2/>
626 </elemC>
627 <elemC attr1="value2"/>
628 </elem2>
629 <elem2>
630 <elemB attr1="value4"/>
631 </elem2>
632 </root>
633 */
634
635 AutoPtr<Document> pDoc = new Document;
636
637 AutoPtr<Element> pRoot = pDoc->createElement("root");
638 AutoPtr<Element> pElem1 = pDoc->createElement("elem1");
639 AutoPtr<Element> pElem11 = pDoc->createElement("elemA");
640 AutoPtr<Element> pElem12 = pDoc->createElement("elemA");
641 AutoPtr<Element> pElem2 = pDoc->createElement("elem2");
642 AutoPtr<Element> pElem21 = pDoc->createElement("elemB");
643 AutoPtr<Element> pElem22 = pDoc->createElement("elemB");
644 AutoPtr<Element> pElem23 = pDoc->createElement("elemB");
645 AutoPtr<Element> pElem24 = pDoc->createElement("elemC");
646 AutoPtr<Element> pElem25 = pDoc->createElement("elemC");
647 AutoPtr<Element> pElem3 = pDoc->createElement("elem2");
648 AutoPtr<Element> pElem31 = pDoc->createElement("elemB");
649
650 pElem21->setAttribute("attr1", "value1");
651 pElem22->setAttribute("attr1", "value2");
652 pElem23->setAttribute("attr1", "value3");
653
654 pElem24->setAttribute("attr1", "value1");
655 pElem25->setAttribute("attr1", "value2");
656
657 pElem31->setAttribute("attr1", "value4");
658
659 AutoPtr<Element> pElem241 = pDoc->createElement("elemC1");
660 AutoPtr<Element> pElem242 = pDoc->createElement("elemC2");
661 pElem241->setAttribute("attr1", "value1");
662 pElem24->appendChild(pElem241);
663 pElem24->appendChild(pElem242);
664
665 pElem1->appendChild(pElem11);
666 pElem1->appendChild(pElem12);
667 pElem2->appendChild(pElem21);
668 pElem2->appendChild(pElem22);
669 pElem2->appendChild(pElem23);
670 pElem2->appendChild(pElem24);
671 pElem2->appendChild(pElem25);
672
673 pElem3->appendChild(pElem31);
674
675 pRoot->appendChild(pElem1);
676 pRoot->appendChild(pElem2);
677 pRoot->appendChild(pElem3);
678
679 pDoc->appendChild(pRoot);
680
681 Node* pNode = pRoot->getNodeByPath("/");
682 assertTrue (pNode == pRoot);
683
684 pNode = pRoot->getNodeByPath("/elem1");
685 assertTrue (pNode == pElem1);
686
687 pNode = pDoc->getNodeByPath("/root/elem1");
688 assertTrue (pNode == pElem1);
689
690 pNode = pRoot->getNodeByPath("/elem2");
691 assertTrue (pNode == pElem2);
692
693 pNode = pRoot->getNodeByPath("/elem1/elemA");
694 assertTrue (pNode == pElem11);
695
696 pNode = pRoot->getNodeByPath("/elem1/elemA[0]");
697 assertTrue (pNode == pElem11);
698
699 pNode = pRoot->getNodeByPath("/elem1/elemA[1]");
700 assertTrue (pNode == pElem12);
701
702 pNode = pRoot->getNodeByPath("/elem1/elemA[2]");
703 assertTrue (pNode == 0);
704
705 pNode = pRoot->getNodeByPath("/elem2/elemB");
706 assertTrue (pNode == pElem21);
707
708 pNode = pRoot->getNodeByPath("/elem2/elemB[0]");
709 assertTrue (pNode == pElem21);
710
711 pNode = pRoot->getNodeByPath("/elem2/elemB[1]");
712 assertTrue (pNode == pElem22);
713
714 pNode = pRoot->getNodeByPath("/elem2/elemB[2]");
715 assertTrue (pNode == pElem23);
716
717 pNode = pRoot->getNodeByPath("/elem2/elemB[3]");
718 assertTrue (pNode == 0);
719
720 pNode = pRoot->getNodeByPath("/elem2/elemB[@attr1]");
721 assertTrue (pNode && pNode->nodeValue() == "value1");
722
723 pNode = pRoot->getNodeByPath("/elem2/elemB[@attr2]");
724 assertTrue (pNode == 0);
725
726 pNode = pRoot->getNodeByPath("/elem2/elemB[@attr1='value2']");
727 assertTrue (pNode == pElem22);
728
729 pNode = pRoot->getNodeByPath("/elem2/elemC[@attr1='value1']/elemC1");
730 assertTrue (pNode == pElem241);
731
732 pNode = pRoot->getNodeByPath("/elem2/elemC[@attr1='value1']/elemC1[@attr1]");
733 assertTrue (pNode && pNode->nodeValue() == "value1");
734
735 pNode = pDoc->getNodeByPath("//elemB[@attr1='value1']");
736 assertTrue (pNode == pElem21);
737
738 pNode = pDoc->getNodeByPath("//elemB[@attr1='value2']");
739 assertTrue (pNode == pElem22);
740
741 pNode = pDoc->getNodeByPath("//elemB[@attr1='value3']");
742 assertTrue (pNode == pElem23);
743
744 pNode = pDoc->getNodeByPath("//elemB[@attr1='value4']");
745 assertTrue (pNode == pElem31);
746
747 pNode = pDoc->getNodeByPath("//elemB[@attr1='value5']");
748 assertTrue (pNode == 0);
749
750 pNode = pDoc->getNodeByPath("//[@attr1='value1']");
751 assertTrue (pNode == pElem21);
752
753 pNode = pDoc->getNodeByPath("//[@attr1='value2']");
754 assertTrue (pNode == pElem22);
755
756 pNode = pRoot->getNodeByPath("/elem2/*[@attr1='value2']");
757 assertTrue (pNode == pElem22);
758}
759
760
761void ElementTest::testNodeByPathNS()
762{
763 /*
764 <ns1:root xmlns:ns1="urn:ns1">
765 <ns1:elem1>
766 <ns2:elemA xmlns:ns2="urn:ns2"/>
767 <ns3:elemA xmlns:ns3="urn:ns2"/>
768 </ns1:elem1>
769 <ns1:elem2>
770 <ns2:elemB ns2:attr1="value1" xmlns:ns2="urn:ns2"/>
771 <ns2:elemB ns2:attr1="value2" xmlns:ns2="urn:ns2"/>
772 <ns2:elemB ns2:attr1="value3" xmlns:ns2="urn:ns2"/>
773 <ns2:elemC ns2:attr1="value1" xmlns:ns2="urn:ns2">
774 <ns2:elemC1 ns2:attr1="value1"/>
775 <ns2:elemC2/>
776 </ns2:elemC>
777 <ns2:elemC ns2:attr1="value2" xmlns:ns2="urn:ns2"/>
778 </ns1:elem2>
779 <ns1:elem2>
780 <ns2:elemB ns2:attr1="value4" xmlns:ns2="urn:ns2"/>
781 </ns1:elem2>
782 </ns1:root>
783 */
784 AutoPtr<Document> pDoc = new Document;
785
786 AutoPtr<Element> pRoot = pDoc->createElementNS("urn:ns1", "ns1:root");
787 AutoPtr<Element> pElem1 = pDoc->createElementNS("urn:ns1", "ns1:elem1");
788 AutoPtr<Element> pElem11 = pDoc->createElementNS("urn:ns2", "ns2:elemA");
789 AutoPtr<Element> pElem12 = pDoc->createElementNS("urn:ns2", "ns2:elemA");
790 AutoPtr<Element> pElem2 = pDoc->createElementNS("urn:ns1", "ns1:elem2");
791 AutoPtr<Element> pElem21 = pDoc->createElementNS("urn:ns2", "ns2:elemB");
792 AutoPtr<Element> pElem22 = pDoc->createElementNS("urn:ns2", "ns2:elemB");
793 AutoPtr<Element> pElem23 = pDoc->createElementNS("urn:ns2", "ns2:elemB");
794 AutoPtr<Element> pElem24 = pDoc->createElementNS("urn:ns2", "ns2:elemC");
795 AutoPtr<Element> pElem25 = pDoc->createElementNS("urn:ns2", "ns2:elemC");
796 AutoPtr<Element> pElem3 = pDoc->createElementNS("urn:ns1", "ns1:elem2");
797 AutoPtr<Element> pElem31 = pDoc->createElementNS("urn:ns2", "ns2:elemB");
798
799 pElem21->setAttributeNS("urn:ns2", "ns2:attr1", "value1");
800 pElem22->setAttributeNS("urn:ns2", "ns2:attr1", "value2");
801 pElem23->setAttributeNS("urn:ns2", "ns2:attr1", "value3");
802 pElem31->setAttributeNS("urn:ns2", "ns2:attr1", "value4");
803
804 pElem24->setAttributeNS("urn:ns2", "ns2:attr1", "value1");
805 pElem25->setAttributeNS("urn:ns2", "ns2:attr1", "value2");
806
807 AutoPtr<Element> pElem241 = pDoc->createElementNS("urn:ns2", "elemC1");
808 AutoPtr<Element> pElem242 = pDoc->createElementNS("urn:ns2", "elemC2");
809 pElem241->setAttributeNS("urn:ns2", "ns2:attr1", "value1");
810 pElem24->appendChild(pElem241);
811 pElem24->appendChild(pElem242);
812
813 pElem1->appendChild(pElem11);
814 pElem1->appendChild(pElem12);
815 pElem2->appendChild(pElem21);
816 pElem2->appendChild(pElem22);
817 pElem2->appendChild(pElem23);
818 pElem2->appendChild(pElem24);
819 pElem2->appendChild(pElem25);
820 pElem3->appendChild(pElem31);
821
822 pRoot->appendChild(pElem1);
823 pRoot->appendChild(pElem2);
824 pRoot->appendChild(pElem3);
825
826 pDoc->appendChild(pRoot);
827
828 Element::NSMap nsMap;
829 nsMap.declarePrefix("ns1", "urn:ns1");
830 nsMap.declarePrefix("NS2", "urn:ns2");
831
832 Node* pNode = pRoot->getNodeByPathNS("/", nsMap);
833 assertTrue (pNode == pRoot);
834
835 pNode = pRoot->getNodeByPathNS("/ns1:elem1", nsMap);
836 assertTrue (pNode == pElem1);
837
838 pNode = pDoc->getNodeByPathNS("/ns1:root/ns1:elem1", nsMap);
839 assertTrue (pNode == pElem1);
840
841 pNode = pRoot->getNodeByPathNS("/ns1:elem2", nsMap);
842 assertTrue (pNode == pElem2);
843
844 pNode = pRoot->getNodeByPathNS("/ns1:elem1/NS2:elemA", nsMap);
845 assertTrue (pNode == pElem11);
846
847 pNode = pRoot->getNodeByPathNS("/ns1:elem1/NS2:elemA[0]", nsMap);
848 assertTrue (pNode == pElem11);
849
850 pNode = pRoot->getNodeByPathNS("/ns1:elem1/NS2:elemA[1]", nsMap);
851 assertTrue (pNode == pElem12);
852
853 pNode = pRoot->getNodeByPathNS("/ns1:elem1/NS2:elemA[2]", nsMap);
854 assertTrue (pNode == 0);
855
856 pNode = pRoot->getNodeByPathNS("/ns1:elem2/NS2:elemB", nsMap);
857 assertTrue (pNode == pElem21);
858
859 pNode = pRoot->getNodeByPathNS("/ns1:elem2/NS2:elemB[0]", nsMap);
860 assertTrue (pNode == pElem21);
861
862 pNode = pRoot->getNodeByPathNS("/ns1:elem2/NS2:elemB[1]", nsMap);
863 assertTrue (pNode == pElem22);
864
865 pNode = pRoot->getNodeByPathNS("/ns1:elem2/NS2:elemB[2]", nsMap);
866 assertTrue (pNode == pElem23);
867
868 pNode = pRoot->getNodeByPathNS("/ns1:elem2/NS2:elemB[3]", nsMap);
869 assertTrue (pNode == 0);
870
871 pNode = pRoot->getNodeByPathNS("/ns1:elem2/NS2:elemB[@NS2:attr1]", nsMap);
872 assertTrue (pNode && pNode->nodeValue() == "value1");
873
874 pNode = pRoot->getNodeByPathNS("/ns1:elem2/NS2:elemB[@NS2:attr2]", nsMap);
875 assertTrue (pNode == 0);
876
877 pNode = pRoot->getNodeByPathNS("/ns1:elem2/NS2:elemB[@NS2:attr1='value2']", nsMap);
878 assertTrue (pNode == pElem22);
879
880 pNode = pRoot->getNodeByPathNS("/ns1:elem2/NS2:elemC[@NS2:attr1='value1']/NS2:elemC1", nsMap);
881 assertTrue (pNode == pElem241);
882
883 pNode = pRoot->getNodeByPathNS("/ns1:elem2/NS2:elemC[@NS2:attr1='value1']/NS2:elemC1[@NS2:attr1]", nsMap);
884 assertTrue (pNode && pNode->nodeValue() == "value1");
885
886 pNode = pRoot->getNodeByPathNS("/NS2:elem1", nsMap);
887 assertTrue (pNode == 0);
888
889 pNode = pDoc->getNodeByPathNS("//NS2:elemB[@NS2:attr1='value1']", nsMap);
890 assertTrue (pNode == pElem21);
891
892 pNode = pDoc->getNodeByPathNS("//NS2:elemB[@NS2:attr1='value2']", nsMap);
893 assertTrue (pNode == pElem22);
894
895 pNode = pDoc->getNodeByPathNS("//NS2:elemB[@NS2:attr1='value3']", nsMap);
896 assertTrue (pNode == pElem23);
897
898 pNode = pDoc->getNodeByPathNS("//NS2:elemB[@NS2:attr1='value4']", nsMap);
899 assertTrue (pNode == pElem31);
900
901 pNode = pDoc->getNodeByPathNS("//NS2:elemB[@NS2:attr1='value5']", nsMap);
902 assertTrue (pNode == 0);
903
904 pNode = pDoc->getNodeByPathNS("//[@NS2:attr1='value1']", nsMap);
905 assertTrue (pNode == pElem21);
906
907 pNode = pDoc->getNodeByPathNS("//[@NS2:attr1='value2']", nsMap);
908 assertTrue (pNode == pElem22);
909
910 pNode = pRoot->getNodeByPathNS("/ns1:elem2/*[@NS2:attr1='value2']", nsMap);
911 assertTrue (pNode == pElem22);
912
913 pNode = pRoot->getNodeByPathNS("/ns1:elem2/NS2:*[@NS2:attr1='value2']", nsMap);
914 assertTrue (pNode == pElem22);
915
916 pNode = pRoot->getNodeByPathNS("/ns1:elem2/ns1:*[@NS2:attr1='value2']", nsMap);
917 assertTrue (pNode == 0);
918}
919
920
921void ElementTest::setUp()
922{
923}
924
925
926void ElementTest::tearDown()
927{
928}
929
930
931CppUnit::Test* ElementTest::suite()
932{
933 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ElementTest");
934
935 CppUnit_addTest(pSuite, ElementTest, testAttributes);
936 CppUnit_addTest(pSuite, ElementTest, testAttributesNS);
937 CppUnit_addTest(pSuite, ElementTest, testAttrMap);
938 CppUnit_addTest(pSuite, ElementTest, testAttrMapNS);
939 CppUnit_addTest(pSuite, ElementTest, testElementsByTagName);
940 CppUnit_addTest(pSuite, ElementTest, testElementsByTagNameNS);
941 CppUnit_addTest(pSuite, ElementTest, testInnerText);
942 CppUnit_addTest(pSuite, ElementTest, testChildElement);
943 CppUnit_addTest(pSuite, ElementTest, testChildElementNS);
944 CppUnit_addTest(pSuite, ElementTest, testNodeByPath);
945 CppUnit_addTest(pSuite, ElementTest, testNodeByPathNS);
946
947 return pSuite;
948}
949