1 | // |
2 | // NamespaceSupportTest.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 "NamespaceSupportTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/SAX/NamespaceSupport.h" |
15 | |
16 | |
17 | using Poco::XML::NamespaceSupport; |
18 | |
19 | |
20 | NamespaceSupportTest::NamespaceSupportTest(const std::string& name): CppUnit::TestCase(name) |
21 | { |
22 | } |
23 | |
24 | |
25 | NamespaceSupportTest::~NamespaceSupportTest() |
26 | { |
27 | } |
28 | |
29 | |
30 | void NamespaceSupportTest::testNamespaceSupport() |
31 | { |
32 | NamespaceSupport ns; |
33 | NamespaceSupport::PrefixSet prefixes; |
34 | ns.getDeclaredPrefixes(prefixes); |
35 | assertTrue (prefixes.size() == 2); |
36 | assertTrue (prefixes.find("xml" ) != prefixes.end()); |
37 | assertTrue (prefixes.find("xmlns" ) != prefixes.end()); |
38 | |
39 | ns.getPrefixes(prefixes); |
40 | assertTrue (prefixes.size() == 2); |
41 | assertTrue (prefixes.find("xml" ) != prefixes.end()); |
42 | assertTrue (prefixes.find("xmlns" ) != prefixes.end()); |
43 | |
44 | ns.pushContext(); |
45 | ns.declarePrefix("ns1" , "urn:ns1" ); |
46 | ns.declarePrefix("ns2" , "urn:ns2" ); |
47 | |
48 | ns.getDeclaredPrefixes(prefixes); |
49 | assertTrue (prefixes.size() == 2); |
50 | assertTrue (prefixes.find("ns1" ) != prefixes.end()); |
51 | assertTrue (prefixes.find("ns2" ) != prefixes.end()); |
52 | |
53 | ns.pushContext(); |
54 | ns.declarePrefix("ns3" , "urn:ns3" ); |
55 | |
56 | ns.getDeclaredPrefixes(prefixes); |
57 | assertTrue (prefixes.size() == 1); |
58 | assertTrue (prefixes.find("ns3" ) != prefixes.end()); |
59 | |
60 | ns.getPrefixes(prefixes); |
61 | assertTrue (prefixes.size() == 5); |
62 | assertTrue (prefixes.find("xml" ) != prefixes.end()); |
63 | assertTrue (prefixes.find("xmlns" ) != prefixes.end()); |
64 | assertTrue (prefixes.find("ns1" ) != prefixes.end()); |
65 | assertTrue (prefixes.find("ns2" ) != prefixes.end()); |
66 | assertTrue (prefixes.find("ns3" ) != prefixes.end()); |
67 | |
68 | ns.popContext(); |
69 | ns.getDeclaredPrefixes(prefixes); |
70 | assertTrue (prefixes.size() == 2); |
71 | assertTrue (prefixes.find("ns1" ) != prefixes.end()); |
72 | assertTrue (prefixes.find("ns2" ) != prefixes.end()); |
73 | |
74 | assertTrue (ns.isMapped("urn:ns1" )); |
75 | assertTrue (ns.isMapped("urn:ns2" )); |
76 | assertTrue (ns.isMapped("http://www.w3.org/XML/1998/namespace" )); |
77 | assertTrue (!ns.isMapped("urn:ns3" )); |
78 | |
79 | ns.getPrefixes("urn:ns2" , prefixes); |
80 | assertTrue (prefixes.size() == 1); |
81 | assertTrue (prefixes.find("ns2" ) != prefixes.end()); |
82 | |
83 | ns.pushContext(); |
84 | ns.declarePrefix("" , "urn:ns3" ); |
85 | ns.declarePrefix("NS2" , "urn:ns2" ); |
86 | |
87 | ns.getPrefixes("urn:ns2" , prefixes); |
88 | assertTrue (prefixes.size() == 2); |
89 | assertTrue (prefixes.find("ns2" ) != prefixes.end()); |
90 | assertTrue (prefixes.find("NS2" ) != prefixes.end()); |
91 | |
92 | ns.getPrefixes(prefixes); |
93 | assertTrue (prefixes.size() == 5); |
94 | assertTrue (prefixes.find("xml" ) != prefixes.end()); |
95 | assertTrue (prefixes.find("xmlns" ) != prefixes.end()); |
96 | assertTrue (prefixes.find("ns1" ) != prefixes.end()); |
97 | assertTrue (prefixes.find("ns2" ) != prefixes.end()); |
98 | assertTrue (prefixes.find("NS2" ) != prefixes.end()); |
99 | |
100 | ns.getDeclaredPrefixes(prefixes); |
101 | assertTrue (prefixes.size() == 2); |
102 | assertTrue (prefixes.find("" ) != prefixes.end()); |
103 | assertTrue (prefixes.find("NS2" ) != prefixes.end()); |
104 | |
105 | assertTrue (ns.getPrefix("urn:ns3" ) == "" ); |
106 | assertTrue (ns.getPrefix("urn:ns2" ) == "NS2" ); |
107 | assertTrue (ns.getPrefix("urn:ns4" ) == "" ); |
108 | |
109 | assertTrue (ns.isMapped("urn:ns3" )); |
110 | assertTrue (ns.isMapped("urn:ns2" )); |
111 | assertTrue (!ns.isMapped("urn:ns4" )); |
112 | |
113 | assertTrue (ns.getURI("xml" ) == "http://www.w3.org/XML/1998/namespace" ); |
114 | assertTrue (ns.getURI("ns1" ) == "urn:ns1" ); |
115 | assertTrue (ns.getURI("" ) == "urn:ns3" ); |
116 | assertTrue (ns.getURI("NS2" ) == "urn:ns2" ); |
117 | |
118 | std::string localName; |
119 | std::string namespaceURI; |
120 | bool declared = ns.processName("elem" , namespaceURI, localName, false); |
121 | assertTrue (declared); |
122 | assertTrue (localName == "elem" ); |
123 | assertTrue (namespaceURI == "urn:ns3" ); |
124 | |
125 | declared = ns.processName("NS2:elem" , namespaceURI, localName, false); |
126 | assertTrue (declared); |
127 | assertTrue (localName == "elem" ); |
128 | assertTrue (namespaceURI == "urn:ns2" ); |
129 | |
130 | declared = ns.processName("ns3:elem" , namespaceURI, localName, false); |
131 | assertTrue (!declared); |
132 | assertTrue (localName == "elem" ); |
133 | assertTrue (namespaceURI == "" ); |
134 | |
135 | declared = ns.processName("ns2:attr" , namespaceURI, localName, true); |
136 | assertTrue (declared); |
137 | assertTrue (localName == "attr" ); |
138 | assertTrue (namespaceURI == "urn:ns2" ); |
139 | |
140 | declared = ns.processName("attr" , namespaceURI, localName, true); |
141 | assertTrue (declared); |
142 | assertTrue (localName == "attr" ); |
143 | assertTrue (namespaceURI == "" ); |
144 | |
145 | declared = ns.processName("ns3:attr" , namespaceURI, localName, true); |
146 | assertTrue (!declared); |
147 | assertTrue (localName == "attr" ); |
148 | assertTrue (namespaceURI == "" ); |
149 | |
150 | ns.popContext(); |
151 | assertTrue (ns.getURI("xml" ) == "http://www.w3.org/XML/1998/namespace" ); |
152 | assertTrue (ns.getURI("ns1" ) == "urn:ns1" ); |
153 | assertTrue (ns.getURI("" ) == "" ); |
154 | assertTrue (ns.getURI("NS2" ) == "" ); |
155 | |
156 | declared = ns.processName("elem" , namespaceURI, localName, false); |
157 | assertTrue (declared); |
158 | assertTrue (localName == "elem" ); |
159 | assertTrue (namespaceURI == "" ); |
160 | |
161 | declared = ns.processName("ns2:elem" , namespaceURI, localName, false); |
162 | assertTrue (declared); |
163 | assertTrue (localName == "elem" ); |
164 | assertTrue (namespaceURI == "urn:ns2" ); |
165 | |
166 | declared = ns.processName("ns3:elem" , namespaceURI, localName, false); |
167 | assertTrue (!declared); |
168 | assertTrue (localName == "elem" ); |
169 | assertTrue (namespaceURI == "" ); |
170 | } |
171 | |
172 | |
173 | void NamespaceSupportTest::setUp() |
174 | { |
175 | } |
176 | |
177 | |
178 | void NamespaceSupportTest::tearDown() |
179 | { |
180 | } |
181 | |
182 | |
183 | CppUnit::Test* NamespaceSupportTest::suite() |
184 | { |
185 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NamespaceSupportTest" ); |
186 | |
187 | CppUnit_addTest(pSuite, NamespaceSupportTest, testNamespaceSupport); |
188 | |
189 | return pSuite; |
190 | } |
191 | |