1 | // |
2 | // AttributesImplTest.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 "AttributesImplTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/SAX/AttributesImpl.h" |
15 | |
16 | |
17 | using Poco::XML::AttributesImpl; |
18 | |
19 | |
20 | AttributesImplTest::AttributesImplTest(const std::string& name): CppUnit::TestCase(name) |
21 | { |
22 | } |
23 | |
24 | |
25 | AttributesImplTest::~AttributesImplTest() |
26 | { |
27 | } |
28 | |
29 | |
30 | void AttributesImplTest::testNoNamespaces() |
31 | { |
32 | AttributesImpl attrs; |
33 | |
34 | assertTrue (attrs.getLength() == 0); |
35 | assertTrue (attrs.getIndex("foo" ) == -1); |
36 | assertTrue (attrs.getValue("foo" ).empty()); |
37 | |
38 | attrs.addAttribute("" , "" , "a1" , "CDATA" , "v1" ); |
39 | assertTrue (attrs.getLength() == 1); |
40 | assertTrue (attrs.getIndex("a1" ) == 0); |
41 | assertTrue (attrs.getQName(0) == "a1" ); |
42 | assertTrue (attrs.getType(0) == "CDATA" ); |
43 | assertTrue (attrs.getValue(0) == "v1" ); |
44 | assertTrue (attrs.isSpecified(0)); |
45 | |
46 | assertTrue (attrs.getType("a1" ) == "CDATA" ); |
47 | assertTrue (attrs.getValue("a1" ) == "v1" ); |
48 | |
49 | attrs.addAttribute("" , "" , "a2" , "CDATA" , "v2" ); |
50 | assertTrue (attrs.getLength() == 2); |
51 | assertTrue (attrs.getIndex("a2" ) == 1); |
52 | assertTrue (attrs.getQName(1) == "a2" ); |
53 | assertTrue (attrs.getType(1) == "CDATA" ); |
54 | assertTrue (attrs.getValue(1) == "v2" ); |
55 | assertTrue (attrs.isSpecified(1)); |
56 | |
57 | assertTrue (attrs.getType("a2" ) == "CDATA" ); |
58 | assertTrue (attrs.getValue("a2" ) == "v2" ); |
59 | |
60 | attrs.addAttribute("" , "" , "a3" , "CDATA" , "v3" ); |
61 | assertTrue (attrs.getLength() == 3); |
62 | assertTrue (attrs.getIndex("a3" ) == 2); |
63 | assertTrue (attrs.getValue("a3" ) == "v3" ); |
64 | |
65 | attrs.removeAttribute(0); |
66 | assertTrue (attrs.getLength() == 2); |
67 | assertTrue (attrs.getIndex("a1" ) == -1); |
68 | assertTrue (attrs.getIndex("a2" ) == 0); |
69 | assertTrue (attrs.getIndex("a3" ) == 1); |
70 | assertTrue (attrs.getQName(0) == "a2" ); |
71 | assertTrue (attrs.getQName(1) == "a3" ); |
72 | |
73 | attrs.removeAttribute("a3" ); |
74 | assertTrue (attrs.getLength() == 1); |
75 | assertTrue (attrs.getIndex("a1" ) == -1); |
76 | assertTrue (attrs.getIndex("a2" ) == 0); |
77 | assertTrue (attrs.getIndex("a3" ) == -1); |
78 | assertTrue (attrs.getQName(0) == "a2" ); |
79 | } |
80 | |
81 | |
82 | void AttributesImplTest::testNamespaces() |
83 | { |
84 | AttributesImpl attrs; |
85 | |
86 | assertTrue (attrs.getLength() == 0); |
87 | assertTrue (attrs.getIndex("urn:ns" , "foo" ) == -1); |
88 | assertTrue (attrs.getValue("urn:ns" , "foo" ).empty()); |
89 | |
90 | attrs.addAttribute("urn:ns" , "a1" , "p:a1" , "CDATA" , "v1" ); |
91 | assertTrue (attrs.getLength() == 1); |
92 | assertTrue (attrs.getIndex("urn:ns" , "a1" ) == 0); |
93 | assertTrue (attrs.getQName(0) == "p:a1" ); |
94 | assertTrue (attrs.getLocalName(0) == "a1" ); |
95 | assertTrue (attrs.getURI(0) == "urn:ns" ); |
96 | assertTrue (attrs.getType(0) == "CDATA" ); |
97 | assertTrue (attrs.getValue(0) == "v1" ); |
98 | assertTrue (attrs.isSpecified(0)); |
99 | |
100 | assertTrue (attrs.getType("urn:ns" , "a1" ) == "CDATA" ); |
101 | assertTrue (attrs.getValue("urn:ns" , "a1" ) == "v1" ); |
102 | |
103 | attrs.addAttribute("urn:ns" , "a2" , "p:a2" , "CDATA" , "v2" ); |
104 | assertTrue (attrs.getLength() == 2); |
105 | assertTrue (attrs.getIndex("urn:ns" , "a2" ) == 1); |
106 | assertTrue (attrs.getQName(1) == "p:a2" ); |
107 | assertTrue (attrs.getLocalName(1) == "a2" ); |
108 | assertTrue (attrs.getURI(1) == "urn:ns" ); |
109 | assertTrue (attrs.getType(1) == "CDATA" ); |
110 | assertTrue (attrs.getValue(1) == "v2" ); |
111 | assertTrue (attrs.isSpecified(1)); |
112 | |
113 | assertTrue (attrs.getType("urn:ns" , "a2" ) == "CDATA" ); |
114 | assertTrue (attrs.getValue("urn:ns" , "a2" ) == "v2" ); |
115 | |
116 | assertTrue (attrs.getIndex("urn:ns2" , "a2" ) == -1); |
117 | |
118 | attrs.addAttribute("urn:ns2" , "a3" , "q:a3" , "CDATA" , "v3" ); |
119 | assertTrue (attrs.getLength() == 3); |
120 | assertTrue (attrs.getIndex("urn:ns2" , "a3" ) == 2); |
121 | assertTrue (attrs.getValue("urn:ns2" , "a3" ) == "v3" ); |
122 | |
123 | attrs.removeAttribute(0); |
124 | assertTrue (attrs.getLength() == 2); |
125 | assertTrue (attrs.getIndex("urn:ns" , "a1" ) == -1); |
126 | assertTrue (attrs.getIndex("urn:ns" , "a2" ) == 0); |
127 | assertTrue (attrs.getIndex("urn:ns2" , "a3" ) == 1); |
128 | assertTrue (attrs.getQName(0) == "p:a2" ); |
129 | assertTrue (attrs.getLocalName(0) == "a2" ); |
130 | assertTrue (attrs.getURI(0) == "urn:ns" ); |
131 | assertTrue (attrs.getQName(1) == "q:a3" ); |
132 | assertTrue (attrs.getLocalName(1) == "a3" ); |
133 | assertTrue (attrs.getURI(1) == "urn:ns2" ); |
134 | |
135 | attrs.removeAttribute("urn:ns" , "a3" ); |
136 | assertTrue (attrs.getLength() == 2); |
137 | |
138 | attrs.removeAttribute("urn:ns2" , "a3" ); |
139 | assertTrue (attrs.getLength() == 1); |
140 | assertTrue (attrs.getIndex("urn:ns" , "a1" ) == -1); |
141 | assertTrue (attrs.getIndex("urn:ns" , "a2" ) == 0); |
142 | assertTrue (attrs.getIndex("urn:ns2" , "a3" ) == -1); |
143 | assertTrue (attrs.getQName(0) == "p:a2" ); |
144 | } |
145 | |
146 | |
147 | void AttributesImplTest::testAccessors() |
148 | { |
149 | AttributesImpl attrs; |
150 | attrs.addAttribute("urn:ns1" , "a1" , "p:a1" , "CDATA" , "v1" ); |
151 | attrs.addAttribute("urn:ns1" , "a2" , "p:a2" , "CDATA" , "v2" , false); |
152 | attrs.addAttribute("urn:ns2" , "a3" , "q:a3" , "CDATA" , "v3" , true); |
153 | |
154 | assertTrue (attrs.getQName(0) == "p:a1" ); |
155 | assertTrue (attrs.getQName(1) == "p:a2" ); |
156 | assertTrue (attrs.getQName(2) == "q:a3" ); |
157 | |
158 | assertTrue (attrs.getLocalName(0) == "a1" ); |
159 | assertTrue (attrs.getLocalName(1) == "a2" ); |
160 | assertTrue (attrs.getLocalName(2) == "a3" ); |
161 | |
162 | assertTrue (attrs.getURI(0) == "urn:ns1" ); |
163 | assertTrue (attrs.getURI(1) == "urn:ns1" ); |
164 | assertTrue (attrs.getURI(2) == "urn:ns2" ); |
165 | |
166 | assertTrue (attrs.getValue(0) == "v1" ); |
167 | assertTrue (attrs.getValue(1) == "v2" ); |
168 | assertTrue (attrs.getValue(2) == "v3" ); |
169 | |
170 | assertTrue (attrs.isSpecified(0)); |
171 | assertTrue (!attrs.isSpecified(1)); |
172 | assertTrue (attrs.isSpecified(2)); |
173 | |
174 | attrs.setType(0, "NMTOKEN" ); |
175 | assertTrue (attrs.getType(0) == "NMTOKEN" ); |
176 | assertTrue (attrs.getType("urn:ns1" , "a1" ) == "NMTOKEN" ); |
177 | |
178 | attrs.setValue(1, "v2 v2" ); |
179 | assertTrue (attrs.getValue(1) == "v2 v2" ); |
180 | assertTrue (attrs.getValue("urn:ns1" , "a2" ) == "v2 v2" ); |
181 | assertTrue (attrs.isSpecified(1)); |
182 | |
183 | attrs.setLocalName(2, "A3" ); |
184 | assertTrue (attrs.getLocalName(2) == "A3" ); |
185 | attrs.setQName(2, "p:A3" ); |
186 | assertTrue (attrs.getQName(2) == "p:A3" ); |
187 | attrs.setURI(2, "urn:ns1" ); |
188 | assertTrue (attrs.getURI(2) == "urn:ns1" ); |
189 | |
190 | assertTrue (attrs.getValue("urn:ns1" , "A3" ) == "v3" ); |
191 | } |
192 | |
193 | |
194 | void AttributesImplTest::testCopy() |
195 | { |
196 | AttributesImpl attrs; |
197 | attrs.addAttribute("urn:ns1" , "a1" , "p:a1" , "CDATA" , "v1" ); |
198 | attrs.addAttribute("urn:ns1" , "a2" , "p:a2" , "CDATA" , "v2" ); |
199 | attrs.addAttribute("urn:ns2" , "a3" , "q:a3" , "CDATA" , "v3" ); |
200 | |
201 | AttributesImpl attrs2; |
202 | attrs2.setAttributes(attrs); |
203 | |
204 | assertTrue (attrs2.getLength() == 3); |
205 | |
206 | assertTrue (attrs2.getQName(0) == "p:a1" ); |
207 | assertTrue (attrs2.getQName(1) == "p:a2" ); |
208 | assertTrue (attrs2.getQName(2) == "q:a3" ); |
209 | |
210 | assertTrue (attrs2.getLocalName(0) == "a1" ); |
211 | assertTrue (attrs2.getLocalName(1) == "a2" ); |
212 | assertTrue (attrs2.getLocalName(2) == "a3" ); |
213 | |
214 | assertTrue (attrs2.getURI(0) == "urn:ns1" ); |
215 | assertTrue (attrs2.getURI(1) == "urn:ns1" ); |
216 | assertTrue (attrs2.getURI(2) == "urn:ns2" ); |
217 | |
218 | assertTrue (attrs2.getValue(0) == "v1" ); |
219 | assertTrue (attrs2.getValue(1) == "v2" ); |
220 | assertTrue (attrs2.getValue(2) == "v3" ); |
221 | } |
222 | |
223 | |
224 | void AttributesImplTest::setUp() |
225 | { |
226 | } |
227 | |
228 | |
229 | void AttributesImplTest::tearDown() |
230 | { |
231 | } |
232 | |
233 | |
234 | CppUnit::Test* AttributesImplTest::suite() |
235 | { |
236 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("AttributesImplTest" ); |
237 | |
238 | CppUnit_addTest(pSuite, AttributesImplTest, testNoNamespaces); |
239 | CppUnit_addTest(pSuite, AttributesImplTest, testNamespaces); |
240 | CppUnit_addTest(pSuite, AttributesImplTest, testAccessors); |
241 | CppUnit_addTest(pSuite, AttributesImplTest, testCopy); |
242 | |
243 | return pSuite; |
244 | } |
245 | |