1//
2// NodeAppenderTest.cpp
3//
4// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "NodeAppenderTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/DOM/NodeAppender.h"
15#include "Poco/DOM/Element.h"
16#include "Poco/DOM/Document.h"
17#include "Poco/DOM/DocumentFragment.h"
18#include "Poco/DOM/AutoPtr.h"
19
20
21using Poco::XML::NodeAppender;
22using Poco::XML::Element;
23using Poco::XML::Document;
24using Poco::XML::DocumentFragment;
25using Poco::XML::AutoPtr;
26using Poco::XML::XMLString;
27
28
29NodeAppenderTest::NodeAppenderTest(const std::string& name): CppUnit::TestCase(name)
30{
31}
32
33
34NodeAppenderTest::~NodeAppenderTest()
35{
36}
37
38
39void NodeAppenderTest::testAppendNode()
40{
41 AutoPtr<Document> pDoc = new Document;
42 AutoPtr<Element> pRoot = pDoc->createElement("root");
43 pDoc->appendChild(pRoot);
44
45 NodeAppender appender(pRoot);
46
47 AutoPtr<Element> pElem1 = pDoc->createElement("elem");
48 AutoPtr<Element> pElem2 = pDoc->createElement("elem");
49 AutoPtr<Element> pElem3 = pDoc->createElement("elem");
50
51 appender.appendChild(pElem1);
52 appender.appendChild(pElem2);
53 appender.appendChild(pElem3);
54
55 assertTrue (pRoot->firstChild() == pElem1);
56 assertTrue (pRoot->lastChild() == pElem3);
57
58 assertTrue (pElem1->nextSibling() == pElem2);
59 assertTrue (pElem2->nextSibling() == pElem3);
60 assertTrue (pElem3->nextSibling() == 0);
61
62 assertTrue (pElem1->previousSibling() == 0);
63 assertTrue (pElem2->previousSibling() == pElem1);
64 assertTrue (pElem3->previousSibling() == pElem2);
65
66 assertTrue (pElem1->parentNode() == pRoot);
67 assertTrue (pElem2->parentNode() == pRoot);
68 assertTrue (pElem3->parentNode() == pRoot);
69}
70
71
72void NodeAppenderTest::testAppendNodeList()
73{
74 AutoPtr<Document> pDoc = new Document;
75 AutoPtr<Element> pRoot = pDoc->createElement("root");
76 pDoc->appendChild(pRoot);
77
78 NodeAppender appender(pRoot);
79
80 AutoPtr<DocumentFragment> pFrag1 = pDoc->createDocumentFragment();
81 AutoPtr<DocumentFragment> pFrag2 = pDoc->createDocumentFragment();
82 AutoPtr<DocumentFragment> pFrag3 = pDoc->createDocumentFragment();
83
84 AutoPtr<Element> pElem1 = pDoc->createElement("elem");
85 AutoPtr<Element> pElem2 = pDoc->createElement("elem");
86 AutoPtr<Element> pElem3 = pDoc->createElement("elem");
87 AutoPtr<Element> pElem4 = pDoc->createElement("elem");
88
89 pFrag2->appendChild(pElem1);
90 pFrag2->appendChild(pElem2);
91 pFrag2->appendChild(pElem3);
92
93 pFrag3->appendChild(pElem4);
94
95 appender.appendChild(pFrag1);
96 assertTrue (pRoot->firstChild() == 0);
97
98 appender.appendChild(pFrag2);
99 assertTrue (pRoot->firstChild() == pElem1);
100 assertTrue (pRoot->lastChild() == pElem3);
101
102 assertTrue (pElem1->nextSibling() == pElem2);
103 assertTrue (pElem2->nextSibling() == pElem3);
104 assertTrue (pElem3->nextSibling() == 0);
105
106 assertTrue (pElem1->previousSibling() == 0);
107 assertTrue (pElem2->previousSibling() == pElem1);
108 assertTrue (pElem3->previousSibling() == pElem2);
109
110 assertTrue (pElem1->parentNode() == pRoot);
111 assertTrue (pElem2->parentNode() == pRoot);
112 assertTrue (pElem3->parentNode() == pRoot);
113
114 appender.appendChild(pFrag3);
115 assertTrue (pRoot->lastChild() == pElem4);
116 assertTrue (pElem4->parentNode() == pRoot);
117 assertTrue (pElem3->nextSibling() == pElem4);
118 assertTrue (pElem4->previousSibling() == pElem3);
119}
120
121
122void NodeAppenderTest::setUp()
123{
124}
125
126
127void NodeAppenderTest::tearDown()
128{
129}
130
131
132CppUnit::Test* NodeAppenderTest::suite()
133{
134 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NodeAppenderTest");
135
136 CppUnit_addTest(pSuite, NodeAppenderTest, testAppendNode);
137 CppUnit_addTest(pSuite, NodeAppenderTest, testAppendNodeList);
138
139 return pSuite;
140}
141