1//
2// DocumentTest.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 "DocumentTest.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/Text.h"
17#include "Poco/DOM/NodeList.h"
18#include "Poco/DOM/AutoPtr.h"
19#include "Poco/DOM/DOMException.h"
20
21
22using Poco::XML::Element;
23using Poco::XML::Document;
24using Poco::XML::Text;
25using Poco::XML::Node;
26using Poco::XML::NodeList;
27using Poco::XML::AutoPtr;
28using Poco::XML::XMLString;
29using Poco::XML::DOMException;
30
31
32DocumentTest::DocumentTest(const std::string& name): CppUnit::TestCase(name)
33{
34}
35
36
37DocumentTest::~DocumentTest()
38{
39}
40
41
42void DocumentTest::testDocumentElement()
43{
44 AutoPtr<Document> pDoc = new Document;
45 AutoPtr<Element> pRoot = pDoc->createElement("root");
46
47 assertTrue (pDoc->documentElement() == 0);
48 pDoc->appendChild(pRoot);
49 assertTrue (pDoc->documentElement() == pRoot);
50
51 AutoPtr<Text> pText = pDoc->createTextNode(" ");
52 pDoc->insertBefore(pText, pRoot);
53 assertTrue (pDoc->documentElement() == pRoot);
54
55}
56
57
58void DocumentTest::testImport()
59{
60 AutoPtr<Document> pDoc1 = new Document;
61 AutoPtr<Element> pRoot1 = pDoc1->createElement("root");
62
63 AutoPtr<Document> pDoc2 = new Document;
64
65 try
66 {
67 pDoc2->appendChild(pRoot1);
68 fail("wrong document - must throw exception");
69 }
70 catch (DOMException&)
71 {
72 }
73
74 AutoPtr<Element> pRoot2 = static_cast<Element*>(pDoc2->importNode(pRoot1, false));
75 assertTrue (pRoot2->ownerDocument() == pDoc2);
76 assertTrue (pRoot1->ownerDocument() == pDoc1);
77
78 pDoc2->appendChild(pRoot2);
79}
80
81
82void DocumentTest::testImportDeep()
83{
84 AutoPtr<Document> pDoc1 = new Document;
85 AutoPtr<Element> pRoot1 = pDoc1->createElement("root");
86 AutoPtr<Element> pElem1 = pDoc1->createElement("elem");
87 AutoPtr<Text> pText1 = pDoc1->createTextNode("text");
88
89 pElem1->appendChild(pText1);
90 pRoot1->appendChild(pElem1);
91
92 pRoot1->setAttribute("a1", "v1");
93 pRoot1->setAttribute("a2", "v2");
94
95 AutoPtr<Document> pDoc2 = new Document;
96
97 try
98 {
99 pDoc2->appendChild(pRoot1);
100 fail("wrong document - must throw exception");
101 }
102 catch (DOMException&)
103 {
104 }
105
106 AutoPtr<Element> pRoot2 = static_cast<Element*>(pDoc2->importNode(pRoot1, true));
107 assertTrue (pRoot2->ownerDocument() == pDoc2);
108 assertTrue (pRoot2->firstChild()->ownerDocument() == pDoc2);
109 assertTrue (pRoot2->firstChild()->firstChild()->ownerDocument() == pDoc2);
110 assertTrue (pRoot1->ownerDocument() == pDoc1);
111 assertTrue (pRoot1->firstChild()->ownerDocument() == pDoc1);
112 assertTrue (pRoot1->firstChild()->firstChild()->ownerDocument() == pDoc1);
113
114 pDoc2->appendChild(pRoot2);
115}
116
117
118void DocumentTest::testElementsByTagName()
119{
120 AutoPtr<Document> pDoc = new Document;
121 AutoPtr<Element> pRoot = pDoc->createElement("root");
122 pDoc->appendChild(pRoot);
123 AutoPtr<NodeList> pNL1 = pDoc->getElementsByTagName("*");
124 AutoPtr<NodeList> pNL2 = pDoc->getElementsByTagName("elem");
125
126 assertTrue (pNL1->length() == 1);
127 assertTrue (pNL1->item(0) == pRoot);
128 assertTrue (pNL2->length() == 0);
129
130 AutoPtr<Element> pElem1 = pDoc->createElement("elem");
131 pRoot->appendChild(pElem1);
132
133 assertTrue (pNL1->length() == 2);
134 assertTrue (pNL2->length() == 1);
135 assertTrue (pNL1->item(0) == pRoot);
136 assertTrue (pNL1->item(1) == pElem1);
137 assertTrue (pNL2->item(0) == pElem1);
138
139 AutoPtr<Element> pElem2 = pDoc->createElement("Elem");
140 pRoot->appendChild(pElem2);
141
142 assertTrue (pNL1->length() == 3);
143 assertTrue (pNL2->length() == 1);
144 assertTrue (pNL1->item(0) == pRoot);
145 assertTrue (pNL1->item(1) == pElem1);
146 assertTrue (pNL1->item(2) == pElem2);
147 assertTrue (pNL2->item(0) == pElem1);
148}
149
150
151void DocumentTest::testElementsByTagNameNS()
152{
153 AutoPtr<Document> pDoc = new Document;
154 AutoPtr<Element> pRoot = pDoc->createElementNS("urn:ns1", "root");
155 pDoc->appendChild(pRoot);
156 AutoPtr<NodeList> pNL1 = pDoc->getElementsByTagNameNS("*", "*");
157 AutoPtr<NodeList> pNL2 = pDoc->getElementsByTagNameNS("*", "elem");
158
159 assertTrue (pNL1->length() == 1);
160 assertTrue (pNL1->item(0) == pRoot);
161 assertTrue (pNL2->length() == 0);
162
163 AutoPtr<Element> pElem1 = pDoc->createElementNS("urn:ns1", "elem");
164 pRoot->appendChild(pElem1);
165
166 assertTrue (pNL1->length() == 2);
167 assertTrue (pNL2->length() == 1);
168 assertTrue (pNL1->item(0) == pRoot);
169 assertTrue (pNL1->item(1) == pElem1);
170 assertTrue (pNL2->item(0) == pElem1);
171
172 AutoPtr<Element> pElem2 = pDoc->createElementNS("urn:ns1", "Elem");
173 pRoot->appendChild(pElem2);
174
175 assertTrue (pNL1->length() == 3);
176 assertTrue (pNL2->length() == 1);
177 assertTrue (pNL1->item(0) == pRoot);
178 assertTrue (pNL1->item(1) == pElem1);
179 assertTrue (pNL1->item(2) == pElem2);
180 assertTrue (pNL2->item(0) == pElem1);
181}
182
183
184void DocumentTest::testElementById()
185{
186 AutoPtr<Document> pDoc = new Document;
187 AutoPtr<Element> pRoot = pDoc->createElement("root");
188 pRoot->setAttribute("id", "0");
189 AutoPtr<Element> pElem1 = pDoc->createElement("elem");
190 pElem1->setAttribute("id", "1");
191 AutoPtr<Text> pText1 = pDoc->createTextNode("text");
192 AutoPtr<Element> pElem2 = pDoc->createElement("elem");
193 pElem2->setAttribute("id", "2");
194 AutoPtr<Element> pElem3 = pDoc->createElement("elem");
195 pElem3->setAttribute("id", "3");
196
197 pElem1->appendChild(pText1);
198 pElem1->appendChild(pElem2);
199 pRoot->appendChild(pElem1);
200 pRoot->appendChild(pElem3);
201 pDoc->appendChild(pRoot);
202
203 Element* pFound = pDoc->getElementById("0", "id");
204 assertTrue (pFound == pRoot);
205
206 pFound = pDoc->getElementById("1", "id");
207 assertTrue (pFound == pElem1);
208
209 pFound = pDoc->getElementById("2", "id");
210 assertTrue (pFound == pElem2);
211
212 pFound = pDoc->getElementById("3", "id");
213 assertTrue (pFound == pElem3);
214
215 pFound = pDoc->getElementById("4", "id");
216 assertTrue (pFound == 0);
217
218 pFound = pDoc->getElementById("0", "ID");
219 assertTrue (pFound == 0);
220}
221
222
223void DocumentTest::testElementByIdNS()
224{
225 AutoPtr<Document> pDoc = new Document;
226 AutoPtr<Element> pRoot = pDoc->createElementNS("urn:ns1", "root");
227 pRoot->setAttributeNS("urn:ns1", "id", "0");
228 AutoPtr<Element> pElem1 = pDoc->createElementNS("urn:ns1", "elem");
229 pElem1->setAttributeNS("urn:ns1", "id", "1");
230 AutoPtr<Text> pText1 = pDoc->createTextNode("text");
231 AutoPtr<Element> pElem2 = pDoc->createElementNS("urn:ns1", "elem");
232 pElem2->setAttributeNS("urn:ns1", "id", "2");
233 AutoPtr<Element> pElem3 = pDoc->createElementNS("urn:ns1", "elem");
234 pElem3->setAttributeNS("urn:ns1", "id", "3");
235
236 pElem1->appendChild(pText1);
237 pElem1->appendChild(pElem2);
238 pRoot->appendChild(pElem1);
239 pRoot->appendChild(pElem3);
240 pDoc->appendChild(pRoot);
241
242 Element* pFound = pDoc->getElementByIdNS("0", "urn:ns1", "id");
243 assertTrue (pFound == pRoot);
244
245 pFound = pDoc->getElementByIdNS("1", "urn:ns1", "id");
246 assertTrue (pFound == pElem1);
247
248 pFound = pDoc->getElementByIdNS("2", "urn:ns1", "id");
249 assertTrue (pFound == pElem2);
250
251 pFound = pDoc->getElementByIdNS("3", "urn:ns1", "id");
252 assertTrue (pFound == pElem3);
253
254 pFound = pDoc->getElementByIdNS("4", "urn:ns1", "id");
255 assertTrue (pFound == 0);
256
257 pFound = pDoc->getElementByIdNS("0", "urn:ns1", "ID");
258 assertTrue (pFound == 0);
259
260 pFound = pDoc->getElementByIdNS("0", "urn:ns2", "id");
261 assertTrue (pFound == 0);
262}
263
264
265void DocumentTest::setUp()
266{
267}
268
269
270void DocumentTest::tearDown()
271{
272}
273
274
275CppUnit::Test* DocumentTest::suite()
276{
277 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("DocumentTest");
278
279 CppUnit_addTest(pSuite, DocumentTest, testDocumentElement);
280 CppUnit_addTest(pSuite, DocumentTest, testImport);
281 CppUnit_addTest(pSuite, DocumentTest, testImportDeep);
282 CppUnit_addTest(pSuite, DocumentTest, testElementsByTagName);
283 CppUnit_addTest(pSuite, DocumentTest, testElementsByTagNameNS);
284 CppUnit_addTest(pSuite, DocumentTest, testElementById);
285 CppUnit_addTest(pSuite, DocumentTest, testElementByIdNS);
286
287 return pSuite;
288}
289