1 | // |
2 | // NodeIteratorTest.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 "NodeIteratorTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/DOM/NodeIterator.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::NodeIterator; |
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 TestNodeFilter: public NodeFilter |
35 | { |
36 | short acceptNode(Node* node) |
37 | { |
38 | if (node->innerText() == "text1" ) |
39 | return NodeFilter::FILTER_ACCEPT; |
40 | else |
41 | return NodeFilter::FILTER_REJECT; |
42 | } |
43 | }; |
44 | } |
45 | |
46 | |
47 | NodeIteratorTest::NodeIteratorTest(const std::string& name): CppUnit::TestCase(name) |
48 | { |
49 | } |
50 | |
51 | |
52 | NodeIteratorTest::~NodeIteratorTest() |
53 | { |
54 | } |
55 | |
56 | |
57 | void NodeIteratorTest::testShowAll() |
58 | { |
59 | AutoPtr<Document> pDoc = new Document; |
60 | AutoPtr<Element> pRoot = pDoc->createElement("root" ); |
61 | AutoPtr<Element> pElem1 = pDoc->createElement("elem" ); |
62 | AutoPtr<Element> pElem2 = pDoc->createElement("elem" ); |
63 | AutoPtr<Text> pText1 = pDoc->createTextNode("text1" ); |
64 | AutoPtr<Text> pText2 = pDoc->createTextNode("text2" ); |
65 | |
66 | pElem1->appendChild(pText1); |
67 | pElem2->appendChild(pText2); |
68 | pRoot->appendChild(pElem1); |
69 | pRoot->appendChild(pElem2); |
70 | pDoc->appendChild(pRoot); |
71 | |
72 | NodeIterator it(pRoot, NodeFilter::SHOW_ALL); |
73 | |
74 | assertTrue (it.nextNode() == pRoot); |
75 | assertTrue (it.nextNode() == pElem1); |
76 | assertTrue (it.nextNode() == pText1); |
77 | assertTrue (it.nextNode() == pElem2); |
78 | assertTrue (it.nextNode() == pText2); |
79 | assertTrue (it.nextNode() == 0); |
80 | |
81 | assertTrue (it.previousNode() == pText2); |
82 | assertTrue (it.previousNode() == pElem2); |
83 | assertTrue (it.previousNode() == pText1); |
84 | assertTrue (it.previousNode() == pElem1); |
85 | assertTrue (it.previousNode() == pRoot); |
86 | assertTrue (it.previousNode() == 0); |
87 | } |
88 | |
89 | |
90 | void NodeIteratorTest::testShowElements() |
91 | { |
92 | AutoPtr<Document> pDoc = new Document; |
93 | AutoPtr<Element> pRoot = pDoc->createElement("root" ); |
94 | AutoPtr<Element> pElem1 = pDoc->createElement("elem" ); |
95 | AutoPtr<Element> pElem2 = pDoc->createElement("elem" ); |
96 | AutoPtr<Text> pText1 = pDoc->createTextNode("text1" ); |
97 | AutoPtr<Text> pText2 = pDoc->createTextNode("text2" ); |
98 | |
99 | pElem1->appendChild(pText1); |
100 | pElem2->appendChild(pText2); |
101 | pRoot->appendChild(pElem1); |
102 | pRoot->appendChild(pElem2); |
103 | pDoc->appendChild(pRoot); |
104 | |
105 | NodeIterator it(pRoot, NodeFilter::SHOW_ELEMENT); |
106 | |
107 | assertTrue (it.nextNode() == pRoot); |
108 | assertTrue (it.nextNode() == pElem1); |
109 | assertTrue (it.nextNode() == pElem2); |
110 | assertTrue (it.nextNode() == 0); |
111 | |
112 | assertTrue (it.previousNode() == pElem2); |
113 | assertTrue (it.previousNode() == pElem1); |
114 | assertTrue (it.previousNode() == pRoot); |
115 | assertTrue (it.previousNode() == 0); |
116 | } |
117 | |
118 | |
119 | void NodeIteratorTest::testFilter() |
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 | TestNodeFilter filter; |
135 | NodeIterator it(pRoot, NodeFilter::SHOW_ELEMENT, &filter); |
136 | |
137 | assertTrue (it.nextNode() == pElem1); |
138 | assertTrue (it.nextNode() == 0); |
139 | |
140 | assertTrue (it.previousNode() == pElem1); |
141 | assertTrue (it.previousNode() == 0); |
142 | } |
143 | |
144 | |
145 | void NodeIteratorTest::testShowNothing() |
146 | { |
147 | AutoPtr<Document> pDoc = new Document; |
148 | AutoPtr<Element> pRoot = pDoc->createElement("root" ); |
149 | AutoPtr<Element> pElem1 = pDoc->createElement("elem" ); |
150 | AutoPtr<Element> pElem2 = pDoc->createElement("elem" ); |
151 | AutoPtr<Text> pText1 = pDoc->createTextNode("text1" ); |
152 | AutoPtr<Text> pText2 = pDoc->createTextNode("text2" ); |
153 | |
154 | pElem1->appendChild(pText1); |
155 | pElem2->appendChild(pText2); |
156 | pRoot->appendChild(pElem1); |
157 | pRoot->appendChild(pElem2); |
158 | pDoc->appendChild(pRoot); |
159 | |
160 | NodeIterator it(pRoot, 0); |
161 | |
162 | assertTrue (it.nextNode() == 0); |
163 | |
164 | assertTrue (it.previousNode() == 0); |
165 | } |
166 | |
167 | |
168 | void NodeIteratorTest::setUp() |
169 | { |
170 | } |
171 | |
172 | |
173 | void NodeIteratorTest::tearDown() |
174 | { |
175 | } |
176 | |
177 | |
178 | CppUnit::Test* NodeIteratorTest::suite() |
179 | { |
180 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NodeIteratorTest" ); |
181 | |
182 | CppUnit_addTest(pSuite, NodeIteratorTest, testShowAll); |
183 | CppUnit_addTest(pSuite, NodeIteratorTest, testShowElements); |
184 | CppUnit_addTest(pSuite, NodeIteratorTest, testFilter); |
185 | CppUnit_addTest(pSuite, NodeIteratorTest, testShowNothing); |
186 | |
187 | return pSuite; |
188 | } |
189 | |