|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| package nu.xom.tests; |
|
22 |
| |
|
23 |
| import nu.xom.Element; |
|
24 |
| import nu.xom.IllegalNameException; |
|
25 |
| import nu.xom.MalformedURIException; |
|
26 |
| import nu.xom.Namespace; |
|
27 |
| import nu.xom.NamespaceConflictException; |
|
28 |
| import nu.xom.NoSuchChildException; |
|
29 |
| import nu.xom.Nodes; |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| public class NamespaceNodeTest extends XOMTestCase { |
|
41 |
| |
|
42 |
| |
|
43 |
21
| public NamespaceNodeTest(String name) {
|
|
44 |
21
| super(name);
|
|
45 |
| } |
|
46 |
| |
|
47 |
| |
|
48 |
1
| public void testGetters() {
|
|
49 |
| |
|
50 |
1
| Element root = new Element("pre:root", "http://www.example.org/");
|
|
51 |
1
| Nodes result = root.query("namespace::pre");
|
|
52 |
1
| assertEquals(1, result.size());
|
|
53 |
1
| Namespace namespace = (Namespace) result.get(0);
|
|
54 |
1
| assertEquals("pre", namespace.getPrefix());
|
|
55 |
1
| assertEquals("http://www.example.org/", namespace.getValue());
|
|
56 |
1
| assertEquals(root, namespace.getParent());
|
|
57 |
| |
|
58 |
| } |
|
59 |
| |
|
60 |
| |
|
61 |
1
| public void testCopy() {
|
|
62 |
| |
|
63 |
1
| Element root = new Element("pre:root", "http://www.example.org/");
|
|
64 |
1
| Nodes result = root.query("namespace::pre");
|
|
65 |
1
| assertEquals(1, result.size());
|
|
66 |
1
| Namespace namespace = (Namespace) result.get(0);
|
|
67 |
| |
|
68 |
1
| Namespace copy = (Namespace) namespace.copy();
|
|
69 |
1
| assertEquals(namespace, copy);
|
|
70 |
1
| assertEquals("pre", copy.getPrefix());
|
|
71 |
1
| assertEquals("http://www.example.org/", copy.getValue());
|
|
72 |
1
| assertEquals(null, copy.getParent());
|
|
73 |
| |
|
74 |
| } |
|
75 |
| |
|
76 |
| |
|
77 |
1
| public void testToXML() {
|
|
78 |
| |
|
79 |
1
| Element root = new Element("pre:root", "http://www.example.org/");
|
|
80 |
1
| Nodes result = root.query("namespace::pre");
|
|
81 |
1
| Namespace namespace = (Namespace) result.get(0);
|
|
82 |
1
| assertEquals("xmlns:pre=\"http://www.example.org/\"", namespace.toXML());
|
|
83 |
| |
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
1
| public void testGetChildCount() {
|
|
88 |
| |
|
89 |
1
| Element root = new Element("pre:root", "http://www.example.org/");
|
|
90 |
1
| Nodes result = root.query("namespace::pre");
|
|
91 |
1
| Namespace namespace = (Namespace) result.get(0);
|
|
92 |
1
| assertEquals(0, namespace.getChildCount());
|
|
93 |
| |
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
1
| public void testGetChild() {
|
|
98 |
| |
|
99 |
1
| Element root = new Element("pre:root", "http://www.example.org/");
|
|
100 |
1
| Nodes result = root.query("namespace::pre");
|
|
101 |
1
| Namespace namespace = (Namespace) result.get(0);
|
|
102 |
1
| try {
|
|
103 |
1
| namespace.getChild(0);
|
|
104 |
0
| fail("Got namespace child");
|
|
105 |
| } |
|
106 |
| catch (IndexOutOfBoundsException success) { |
|
107 |
1
| assertEquals("Namespaces do not have children", success.getMessage());
|
|
108 |
| } |
|
109 |
| |
|
110 |
| } |
|
111 |
| |
|
112 |
| |
|
113 |
1
| public void testToXMLOnDefaultNamespace() {
|
|
114 |
| |
|
115 |
1
| Element root = new Element("root", "http://www.example.org/");
|
|
116 |
1
| Nodes result = root.query("namespace::*[name() != 'xml']");
|
|
117 |
1
| Namespace namespace = (Namespace) result.get(0);
|
|
118 |
1
| assertEquals("xmlns=\"http://www.example.org/\"", namespace.toXML());
|
|
119 |
| |
|
120 |
| } |
|
121 |
| |
|
122 |
| |
|
123 |
1
| public void testDetachNamespaceNode() {
|
|
124 |
| |
|
125 |
1
| Element root = new Element("pre:root", "http://www.example.org/");
|
|
126 |
1
| Nodes result = root.query("namespace::pre");
|
|
127 |
1
| Namespace namespace = (Namespace) result.get(0);
|
|
128 |
1
| namespace.detach();
|
|
129 |
1
| assertNull(namespace.getParent());
|
|
130 |
| |
|
131 |
| } |
|
132 |
| |
|
133 |
| |
|
134 |
1
| public void testRemoveNamespaceNode() {
|
|
135 |
| |
|
136 |
1
| Element root = new Element("pre:root", "http://www.example.org/");
|
|
137 |
1
| Nodes result = root.query("namespace::pre");
|
|
138 |
1
| Namespace namespace = (Namespace) result.get(0);
|
|
139 |
1
| try {
|
|
140 |
1
| root.removeChild(namespace);
|
|
141 |
0
| fail("Namespaces are not children");
|
|
142 |
| } |
|
143 |
| catch (NoSuchChildException success) { |
|
144 |
1
| assertNotNull(success.getMessage());
|
|
145 |
| } |
|
146 |
| |
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
1
| public void testGetParent() {
|
|
151 |
| |
|
152 |
1
| Element root = new Element("pre:root", "http://www.example.org/");
|
|
153 |
1
| Nodes result = root.query("namespace::pre");
|
|
154 |
1
| Namespace namespace = (Namespace) result.get(0);
|
|
155 |
1
| assertEquals(root, namespace.getParent());
|
|
156 |
| |
|
157 |
| } |
|
158 |
| |
|
159 |
| |
|
160 |
1
| public void testToString() {
|
|
161 |
| |
|
162 |
1
| Element root = new Element("pre:root", "http://www.example.org/");
|
|
163 |
1
| Nodes result = root.query("namespace::pre");
|
|
164 |
1
| Namespace namespace = (Namespace) result.get(0);
|
|
165 |
1
| assertEquals("[Namespace: xmlns:pre=\"http://www.example.org/\"]", namespace.toString());
|
|
166 |
| |
|
167 |
| } |
|
168 |
| |
|
169 |
| |
|
170 |
1
| public void testIllegalPrefix() {
|
|
171 |
| |
|
172 |
1
| try {
|
|
173 |
1
| new Namespace("white space", "http://www.example.org", null);
|
|
174 |
0
| fail("Allowed prefix containing white space");
|
|
175 |
| } |
|
176 |
| catch (IllegalNameException success) { |
|
177 |
1
| assertNotNull(success.getMessage());
|
|
178 |
| } |
|
179 |
| |
|
180 |
| } |
|
181 |
| |
|
182 |
| |
|
183 |
1
| public void testEmptyStringPrefix() {
|
|
184 |
1
| Namespace ns = new Namespace("", "http://www.example.org", null);
|
|
185 |
1
| assertEquals("", ns.getPrefix());
|
|
186 |
| } |
|
187 |
| |
|
188 |
| |
|
189 |
1
| public void testNullPrefix() {
|
|
190 |
1
| Namespace ns = new Namespace(null, "http://www.example.org", null);
|
|
191 |
1
| assertEquals("", ns.getPrefix());
|
|
192 |
| } |
|
193 |
| |
|
194 |
| |
|
195 |
1
| public void testIllegalURI() {
|
|
196 |
| |
|
197 |
1
| try {
|
|
198 |
1
| new Namespace("pre", "http:// www.example.org", null);
|
|
199 |
0
| fail("Allowed URI containing white space");
|
|
200 |
| } |
|
201 |
| catch (MalformedURIException success) { |
|
202 |
1
| assertNotNull(success.getMessage());
|
|
203 |
| } |
|
204 |
| |
|
205 |
| } |
|
206 |
| |
|
207 |
| |
|
208 |
1
| public void testNullURI() {
|
|
209 |
1
| Namespace ns = new Namespace("", null, null);
|
|
210 |
1
| assertEquals("", ns.getValue());
|
|
211 |
| } |
|
212 |
| |
|
213 |
| |
|
214 |
1
| public void testCantBindPrefixToEmptyURI() {
|
|
215 |
| |
|
216 |
1
| try {
|
|
217 |
1
| new Namespace("pre", "", null);
|
|
218 |
0
| fail("Bound prefix to no namespace");
|
|
219 |
| } |
|
220 |
| catch (NamespaceConflictException success) { |
|
221 |
1
| assertNotNull(success.getMessage());
|
|
222 |
| } |
|
223 |
| |
|
224 |
| } |
|
225 |
| |
|
226 |
| |
|
227 |
1
| public void testCantBindXMLNS() {
|
|
228 |
| |
|
229 |
1
| try {
|
|
230 |
1
| new Namespace("xmlns", "", null);
|
|
231 |
0
| fail("Bound xmlns prefix to no namespace");
|
|
232 |
| } |
|
233 |
| catch (IllegalNameException success) { |
|
234 |
1
| assertNotNull(success.getMessage());
|
|
235 |
| } |
|
236 |
| |
|
237 |
| } |
|
238 |
| |
|
239 |
| |
|
240 |
1
| public void testCantBindXMLNSToDOMURI() {
|
|
241 |
| |
|
242 |
1
| try {
|
|
243 |
1
| new Namespace("xmlns", "http://www.w3.org/2000/xmlns/", null);
|
|
244 |
0
| fail("Bound xmlns prefix to DOM namespace");
|
|
245 |
| } |
|
246 |
| catch (IllegalNameException success) { |
|
247 |
1
| assertNotNull(success.getMessage());
|
|
248 |
| } |
|
249 |
| |
|
250 |
| } |
|
251 |
| |
|
252 |
| |
|
253 |
1
| public void testCantBindXMLPrefixToWrongURI() {
|
|
254 |
| |
|
255 |
1
| try {
|
|
256 |
1
| new Namespace("xml", "http://www.w3.org/2000/xmlns/", null);
|
|
257 |
0
| fail("Bound xml prefix to DOM namespace");
|
|
258 |
| } |
|
259 |
| catch (NamespaceConflictException success) { |
|
260 |
1
| assertNotNull(success.getMessage());
|
|
261 |
| } |
|
262 |
| |
|
263 |
| } |
|
264 |
| |
|
265 |
| |
|
266 |
1
| public void testCanBindXMLPrefixToCorrectURI() {
|
|
267 |
| |
|
268 |
1
| Namespace ns = new Namespace("xml", Namespace.XML_NAMESPACE, null);
|
|
269 |
1
| assertEquals(Namespace.XML_NAMESPACE, ns.getValue());
|
|
270 |
| |
|
271 |
| } |
|
272 |
| |
|
273 |
| |
|
274 |
1
| public void testCanBindNonXMLPrefixToXMLURI() {
|
|
275 |
| |
|
276 |
1
| try {
|
|
277 |
1
| new Namespace("pre", Namespace.XML_NAMESPACE, null);
|
|
278 |
0
| fail("Bound non-xml prefix to XML namespace");
|
|
279 |
| } |
|
280 |
| catch (NamespaceConflictException success) { |
|
281 |
1
| assertNotNull(success.getMessage());
|
|
282 |
| } |
|
283 |
| |
|
284 |
| } |
|
285 |
| |
|
286 |
| |
|
287 |
| } |