1 | // |
2 | // TreeWalkerTest.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 "TreeWalkerTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/DOM/TreeWalker.h" |
15 | #include "Poco/DOM/NodeFilter.h" |
16 | #include "Poco/DOM/Document.h" |
17 | #include "Poco/DOM/Element.h" |
18 | #include "Poco/DOM/Text.h" |
19 | #include "Poco/DOM/AutoPtr.h" |
20 | |
21 | |
22 | using Poco::XML::TreeWalker; |
23 | using Poco::XML::NodeFilter; |
24 | using Poco::XML::Element; |
25 | using Poco::XML::Document; |
26 | using Poco::XML::Text; |
27 | using Poco::XML::Node; |
28 | using Poco::XML::AutoPtr; |
29 | using Poco::XML::XMLString; |
30 | |
31 | |
32 | namespace |
33 | { |
34 | class RejectNodeFilter: public NodeFilter |
35 | { |
36 | short acceptNode(Node* node) |
37 | { |
38 | if (node->nodeType() != Node::ELEMENT_NODE || node->innerText() == "text1" || node->nodeName() == "root" ) |
39 | return NodeFilter::FILTER_ACCEPT; |
40 | else |
41 | return NodeFilter::FILTER_REJECT; |
42 | } |
43 | }; |
44 | |
45 | class SkipNodeFilter: public NodeFilter |
46 | { |
47 | short acceptNode(Node* node) |
48 | { |
49 | if (node->nodeType() != Node::ELEMENT_NODE || node->innerText() == "text1" ) |
50 | return NodeFilter::FILTER_ACCEPT; |
51 | else |
52 | return NodeFilter::FILTER_SKIP; |
53 | } |
54 | }; |
55 | } |
56 | |
57 | |
58 | TreeWalkerTest::TreeWalkerTest(const std::string& name): CppUnit::TestCase(name) |
59 | { |
60 | } |
61 | |
62 | |
63 | TreeWalkerTest::~TreeWalkerTest() |
64 | { |
65 | } |
66 | |
67 | |
68 | void TreeWalkerTest::testShowAll() |
69 | { |
70 | AutoPtr<Document> pDoc = new Document; |
71 | AutoPtr<Element> pRoot = pDoc->createElement("root" ); |
72 | AutoPtr<Element> pElem1 = pDoc->createElement("elem" ); |
73 | AutoPtr<Element> pElem2 = pDoc->createElement("elem" ); |
74 | AutoPtr<Text> pText1 = pDoc->createTextNode("text1" ); |
75 | AutoPtr<Text> pText2 = pDoc->createTextNode("text2" ); |
76 | |
77 | pElem1->appendChild(pText1); |
78 | pElem2->appendChild(pText2); |
79 | pRoot->appendChild(pElem1); |
80 | pRoot->appendChild(pElem2); |
81 | pDoc->appendChild(pRoot); |
82 | |
83 | TreeWalker it(pRoot, NodeFilter::SHOW_ALL); |
84 | |
85 | assertTrue (it.currentNode() == pRoot); |
86 | assertTrue (it.nextNode() == pElem1); |
87 | assertTrue (it.nextNode() == pText1); |
88 | assertTrue (it.nextNode() == pElem2); |
89 | assertTrue (it.nextNode() == pText2); |
90 | assertTrue (it.nextNode() == 0); |
91 | |
92 | assertTrue (it.currentNode() == pText2); |
93 | assertTrue (it.previousNode() == pElem2); |
94 | assertTrue (it.previousNode() == pText1); |
95 | assertTrue (it.previousNode() == pElem1); |
96 | assertTrue (it.previousNode() == pRoot); |
97 | assertTrue (it.previousNode() == 0); |
98 | |
99 | assertTrue (it.currentNode() == pRoot); |
100 | assertTrue (it.parentNode() == 0); |
101 | assertTrue (it.currentNode() == pRoot); |
102 | assertTrue (it.firstChild() == pElem1); |
103 | assertTrue (it.parentNode() == pRoot); |
104 | assertTrue (it.lastChild() == pElem2); |
105 | assertTrue (it.previousSibling() == pElem1); |
106 | assertTrue (it.previousSibling() == 0); |
107 | assertTrue (it.currentNode() == pElem1); |
108 | assertTrue (it.nextSibling() == pElem2); |
109 | assertTrue (it.nextSibling() == 0); |
110 | assertTrue (it.currentNode() == pElem2); |
111 | assertTrue (it.firstChild() == pText2); |
112 | assertTrue (it.nextSibling() == 0); |
113 | assertTrue (it.previousSibling() == 0); |
114 | assertTrue (it.parentNode() == pElem2); |
115 | assertTrue (it.lastChild() == pText2); |
116 | } |
117 | |
118 | |
119 | void TreeWalkerTest::testShowElements() |
120 | { |
121 | AutoPtr<Document> pDoc = new Document; |
122 | AutoPtr<Element> pRoot = pDoc->createElement("root" ); |
123 | AutoPtr<Element> pElem1 = pDoc->createElement("elem" ); |
124 | AutoPtr<Element> pElem2 = pDoc->createElement("elem" ); |
125 | AutoPtr<Text> pText1 = pDoc->createTextNode("text1" ); |
126 | AutoPtr<Text> pText2 = pDoc->createTextNode("text2" ); |
127 | |
128 | pElem1->appendChild(pText1); |
129 | pElem2->appendChild(pText2); |
130 | pRoot->appendChild(pElem1); |
131 | pRoot->appendChild(pElem2); |
132 | pDoc->appendChild(pRoot); |
133 | |
134 | TreeWalker it(pRoot, NodeFilter::SHOW_ELEMENT); |
135 | |
136 | assertTrue (it.currentNode() == pRoot); |
137 | assertTrue (it.nextNode() == pElem1); |
138 | assertTrue (it.nextNode() == pElem2); |
139 | assertTrue (it.nextNode() == 0); |
140 | |
141 | assertTrue (it.currentNode() == pElem2); |
142 | assertTrue (it.previousNode() == pElem1); |
143 | assertTrue (it.previousNode() == pRoot); |
144 | assertTrue (it.previousNode() == 0); |
145 | |
146 | assertTrue (it.currentNode() == pRoot); |
147 | assertTrue (it.parentNode() == 0); |
148 | assertTrue (it.currentNode() == pRoot); |
149 | assertTrue (it.firstChild() == pElem1); |
150 | assertTrue (it.parentNode() == pRoot); |
151 | assertTrue (it.lastChild() == pElem2); |
152 | assertTrue (it.firstChild() == 0); |
153 | assertTrue (it.currentNode() == pElem2); |
154 | assertTrue (it.lastChild() == 0); |
155 | assertTrue (it.currentNode() == pElem2); |
156 | assertTrue (it.previousSibling() == pElem1); |
157 | assertTrue (it.firstChild() == 0); |
158 | assertTrue (it.lastChild() == 0); |
159 | assertTrue (it.parentNode() == pRoot); |
160 | } |
161 | |
162 | |
163 | void TreeWalkerTest::testFilter() |
164 | { |
165 | AutoPtr<Document> pDoc = new Document; |
166 | AutoPtr<Element> pRoot = pDoc->createElement("root" ); |
167 | AutoPtr<Element> pElem1 = pDoc->createElement("elem" ); |
168 | AutoPtr<Element> pElem2 = pDoc->createElement("elem" ); |
169 | AutoPtr<Text> pText1 = pDoc->createTextNode("text1" ); |
170 | AutoPtr<Text> pText2 = pDoc->createTextNode("text2" ); |
171 | |
172 | pElem1->appendChild(pText1); |
173 | pElem2->appendChild(pText2); |
174 | pRoot->appendChild(pElem1); |
175 | pRoot->appendChild(pElem2); |
176 | pDoc->appendChild(pRoot); |
177 | |
178 | SkipNodeFilter skipFilter; |
179 | TreeWalker it1(pRoot, NodeFilter::SHOW_ELEMENT, &skipFilter); |
180 | |
181 | assertTrue (it1.nextNode() == pElem1); |
182 | assertTrue (it1.nextNode() == 0); |
183 | |
184 | assertTrue (it1.currentNode() == pElem1); |
185 | assertTrue (it1.previousNode() == 0); |
186 | |
187 | assertTrue (it1.parentNode() == 0); |
188 | assertTrue (it1.firstChild() == 0); |
189 | assertTrue (it1.lastChild() == 0); |
190 | assertTrue (it1.nextSibling() == 0); |
191 | assertTrue (it1.previousSibling() == 0); |
192 | |
193 | TreeWalker it2(pRoot, NodeFilter::SHOW_ALL, &skipFilter); |
194 | |
195 | assertTrue (it2.nextNode() == pElem1); |
196 | assertTrue (it2.nextNode() == pText1); |
197 | assertTrue (it2.nextNode() == pText2); |
198 | assertTrue (it2.nextNode() == 0); |
199 | |
200 | assertTrue (it2.currentNode() == pText2); |
201 | assertTrue (it2.previousNode() == pText1); |
202 | assertTrue (it2.previousNode() == pElem1); |
203 | assertTrue (it2.previousNode() == 0); |
204 | |
205 | assertTrue (it2.currentNode() == pElem1); |
206 | assertTrue (it2.parentNode() == 0); |
207 | assertTrue (it2.nextSibling() == 0); |
208 | assertTrue (it2.previousSibling() == 0); |
209 | assertTrue (it2.firstChild() == pText1); |
210 | assertTrue (it2.nextSibling() == 0); |
211 | assertTrue (it2.previousSibling() == 0); |
212 | assertTrue (it2.parentNode() == pElem1); |
213 | |
214 | RejectNodeFilter rejectFilter; |
215 | TreeWalker it3(pRoot, NodeFilter::SHOW_ELEMENT, &rejectFilter); |
216 | |
217 | assertTrue (it3.nextNode() == pElem1); |
218 | assertTrue (it3.nextNode() == 0); |
219 | |
220 | assertTrue (it3.currentNode() == pElem1); |
221 | assertTrue (it3.previousNode() == pRoot); |
222 | assertTrue (it3.previousNode() == 0); |
223 | |
224 | assertTrue (it3.currentNode() == pRoot); |
225 | assertTrue (it3.parentNode() == 0); |
226 | assertTrue (it3.firstChild() == pElem1); |
227 | assertTrue (it3.nextSibling() == 0); |
228 | assertTrue (it3.previousSibling() == 0); |
229 | assertTrue (it3.parentNode() == pRoot); |
230 | assertTrue (it3.lastChild() == pElem1); |
231 | |
232 | TreeWalker it4(pRoot, NodeFilter::SHOW_ALL, &rejectFilter); |
233 | |
234 | assertTrue (it4.nextNode() == pElem1); |
235 | assertTrue (it4.nextNode() == pText1); |
236 | assertTrue (it4.nextNode() == 0); |
237 | |
238 | assertTrue (it4.currentNode() == pText1); |
239 | assertTrue (it4.previousNode() == pElem1); |
240 | assertTrue (it4.previousNode() == pRoot); |
241 | assertTrue (it4.previousNode() == 0); |
242 | |
243 | assertTrue (it4.currentNode() == pRoot); |
244 | assertTrue (it4.parentNode() == 0); |
245 | assertTrue (it4.firstChild() == pElem1); |
246 | assertTrue (it4.firstChild() == pText1); |
247 | assertTrue (it4.nextSibling() == 0); |
248 | assertTrue (it4.previousSibling() == 0); |
249 | assertTrue (it4.parentNode() == pElem1); |
250 | assertTrue (it4.lastChild() == pText1); |
251 | assertTrue (it4.parentNode() == pElem1); |
252 | assertTrue (it4.nextSibling() == 0); |
253 | assertTrue (it4.previousSibling() == 0); |
254 | assertTrue (it4.parentNode() == pRoot); |
255 | } |
256 | |
257 | |
258 | void TreeWalkerTest::testShowNothing() |
259 | { |
260 | AutoPtr<Document> pDoc = new Document; |
261 | AutoPtr<Element> pRoot = pDoc->createElement("root" ); |
262 | AutoPtr<Element> pElem1 = pDoc->createElement("elem" ); |
263 | AutoPtr<Element> pElem2 = pDoc->createElement("elem" ); |
264 | AutoPtr<Text> pText1 = pDoc->createTextNode("text1" ); |
265 | AutoPtr<Text> pText2 = pDoc->createTextNode("text2" ); |
266 | |
267 | pElem1->appendChild(pText1); |
268 | pElem2->appendChild(pText2); |
269 | pRoot->appendChild(pElem1); |
270 | pRoot->appendChild(pElem2); |
271 | pDoc->appendChild(pRoot); |
272 | |
273 | TreeWalker it(pRoot, 0); |
274 | |
275 | assertTrue (it.nextNode() == 0); |
276 | |
277 | assertTrue (it.previousNode() == 0); |
278 | |
279 | assertTrue (it.currentNode() == pRoot); |
280 | assertTrue (it.firstChild() == 0); |
281 | assertTrue (it.lastChild() == 0); |
282 | assertTrue (it.nextSibling() == 0); |
283 | assertTrue (it.previousSibling() == 0); |
284 | } |
285 | |
286 | |
287 | void TreeWalkerTest::setUp() |
288 | { |
289 | } |
290 | |
291 | |
292 | void TreeWalkerTest::tearDown() |
293 | { |
294 | } |
295 | |
296 | |
297 | CppUnit::Test* TreeWalkerTest::suite() |
298 | { |
299 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TreeWalkerTest" ); |
300 | |
301 | CppUnit_addTest(pSuite, TreeWalkerTest, testShowAll); |
302 | CppUnit_addTest(pSuite, TreeWalkerTest, testShowElements); |
303 | CppUnit_addTest(pSuite, TreeWalkerTest, testFilter); |
304 | CppUnit_addTest(pSuite, TreeWalkerTest, testShowNothing); |
305 | |
306 | return pSuite; |
307 | } |
308 | |