|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| package nu.xom.tests; |
|
23 |
| |
|
24 |
| import java.io.File; |
|
25 |
| import java.io.IOException; |
|
26 |
| |
|
27 |
| import nu.xom.Attribute; |
|
28 |
| import nu.xom.Builder; |
|
29 |
| import nu.xom.Comment; |
|
30 |
| import nu.xom.DocType; |
|
31 |
| import nu.xom.Document; |
|
32 |
| import nu.xom.Element; |
|
33 |
| import nu.xom.Elements; |
|
34 |
| import nu.xom.IllegalAddException; |
|
35 |
| import nu.xom.IllegalNameException; |
|
36 |
| import nu.xom.MalformedURIException; |
|
37 |
| import nu.xom.MultipleParentException; |
|
38 |
| import nu.xom.NamespaceConflictException; |
|
39 |
| import nu.xom.NoSuchAttributeException; |
|
40 |
| import nu.xom.Node; |
|
41 |
| import nu.xom.Nodes; |
|
42 |
| import nu.xom.ParsingException; |
|
43 |
| import nu.xom.ProcessingInstruction; |
|
44 |
| import nu.xom.Text; |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| public class ElementTest extends XOMTestCase { |
|
57 |
| |
|
58 |
| private Element element; |
|
59 |
| private Element child1; |
|
60 |
| private Text child2; |
|
61 |
| private Comment child3; |
|
62 |
| private Element child4; |
|
63 |
| private Element child5; |
|
64 |
| private String[] legal = { |
|
65 |
| "http://www.is.edu/sakdsk#sjadh", |
|
66 |
| "http://www.is.edu/sakdsk?name=value&name=head", |
|
67 |
| "uri:isbn:0832473864", |
|
68 |
| "http://www.examples.com:80", |
|
69 |
| "http://www.examples.com:80/", |
|
70 |
| "http://www.is.edu/%20sakdsk#sjadh" |
|
71 |
| }; |
|
72 |
| |
|
73 |
| private String[] illegal = { |
|
74 |
| "http://www.is.edu/%sakdsk#sjadh", |
|
75 |
| "http://www.is.edu/k\u0245kakdsk#sjadh", |
|
76 |
| "!@#$%^&*()", |
|
77 |
| "fred", |
|
78 |
| "#fred", |
|
79 |
| "/fred" |
|
80 |
| }; |
|
81 |
| |
|
82 |
| |
|
83 |
68
| protected void setUp() {
|
|
84 |
| |
|
85 |
68
| child1 = new Element("test");
|
|
86 |
68
| child2 = new Text("test2");
|
|
87 |
68
| child3 = new Comment("test3");
|
|
88 |
68
| child4 = new Element("pre:test", "http://www.example.com");
|
|
89 |
68
| child5 = new Element("test", "http://www.example.com");
|
|
90 |
68
| element = new Element("name");
|
|
91 |
| |
|
92 |
68
| element.appendChild(child1);
|
|
93 |
68
| element.appendChild(child2);
|
|
94 |
68
| element.appendChild(child3);
|
|
95 |
68
| element.appendChild(child4);
|
|
96 |
68
| element.appendChild(child5);
|
|
97 |
68
| element.appendChild(" \r\n");
|
|
98 |
| |
|
99 |
| } |
|
100 |
| |
|
101 |
| |
|
102 |
68
| public ElementTest(String name) {
|
|
103 |
68
| super(name);
|
|
104 |
| } |
|
105 |
| |
|
106 |
| |
|
107 |
1
| public void testGetChildElementsNull() {
|
|
108 |
| |
|
109 |
1
| Elements elements = element.getChildElements(
|
|
110 |
| "", "http://www.example.com"); |
|
111 |
1
| assertEquals(2, elements.size());
|
|
112 |
1
| elements = element.getChildElements("", "");
|
|
113 |
1
| assertEquals(1, elements.size());
|
|
114 |
1
| elements = element.getChildElements(null,
|
|
115 |
| "http://www.example.com"); |
|
116 |
1
| assertEquals(2, elements.size());
|
|
117 |
1
| elements = element.getChildElements("", null);
|
|
118 |
1
| assertEquals(1, elements.size());
|
|
119 |
| |
|
120 |
| } |
|
121 |
| |
|
122 |
| private static class TestElement extends Element { |
|
123 |
| |
|
124 |
1
| public TestElement(String name) {
|
|
125 |
1
| super(name);
|
|
126 |
| } |
|
127 |
| |
|
128 |
1
| public String toString() {
|
|
129 |
1
| return "foo";
|
|
130 |
| } |
|
131 |
| |
|
132 |
| } |
|
133 |
| |
|
134 |
| |
|
135 |
1
| public void testOverrideToString() {
|
|
136 |
| |
|
137 |
1
| Element element = new TestElement("bar");
|
|
138 |
1
| assertEquals("foo", element.toString());
|
|
139 |
| |
|
140 |
| } |
|
141 |
| |
|
142 |
| |
|
143 |
1
| public void testGetFirstChildElement() {
|
|
144 |
| |
|
145 |
1
| Element first = element.getFirstChildElement("test");
|
|
146 |
1
| assertEquals(child1, first);
|
|
147 |
| |
|
148 |
1
| first = element.getFirstChildElement(
|
|
149 |
| "test", "http://www.example.com"); |
|
150 |
1
| assertEquals(child4, first);
|
|
151 |
| |
|
152 |
1
| assertNull(element.getFirstChildElement("nonesuch"));
|
|
153 |
1
| assertNull(element.getFirstChildElement("pre:test"));
|
|
154 |
1
| assertNull(
|
|
155 |
| element.getFirstChildElement( |
|
156 |
| "nonesuch", "http://www.example.com") |
|
157 |
| ); |
|
158 |
| |
|
159 |
| } |
|
160 |
| |
|
161 |
| |
|
162 |
1
| public void testElementNamedXMLNS() {
|
|
163 |
| |
|
164 |
1
| String name = "xmlns";
|
|
165 |
1
| Element e = new Element(name);
|
|
166 |
| |
|
167 |
1
| assertEquals(name, e.getLocalName());
|
|
168 |
1
| assertEquals(name, e.getQualifiedName());
|
|
169 |
1
| assertEquals("", e.getNamespacePrefix());
|
|
170 |
1
| assertEquals("", e.getNamespaceURI());
|
|
171 |
| |
|
172 |
| } |
|
173 |
| |
|
174 |
| |
|
175 |
1
| public void testElementWithPrefixXMLNS() {
|
|
176 |
| |
|
177 |
1
| try {
|
|
178 |
1
| new Element("xmlns:foo", "http://www.example.org/");
|
|
179 |
0
| fail("Allowed xmlns prefix on element");
|
|
180 |
| } |
|
181 |
| catch (NamespaceConflictException success) { |
|
182 |
1
| assertNotNull(success.getMessage());
|
|
183 |
| } |
|
184 |
| |
|
185 |
| } |
|
186 |
| |
|
187 |
| |
|
188 |
1
| public void testConstructor1() {
|
|
189 |
| |
|
190 |
1
| String name = "Jethro";
|
|
191 |
1
| Element e = new Element(name);
|
|
192 |
| |
|
193 |
1
| assertEquals(name, e.getLocalName());
|
|
194 |
1
| assertEquals(name, e.getQualifiedName());
|
|
195 |
1
| assertEquals("", e.getNamespacePrefix());
|
|
196 |
1
| assertEquals("", e.getNamespaceURI());
|
|
197 |
| } |
|
198 |
| |
|
199 |
| |
|
200 |
1
| public void testConstructor2() {
|
|
201 |
1
| String name = "sakjdhjhd";
|
|
202 |
1
| String uri = "http://www.something.com/";
|
|
203 |
1
| Element e = new Element(name, uri);
|
|
204 |
| |
|
205 |
1
| assertEquals(name, e.getLocalName());
|
|
206 |
1
| assertEquals(name, e.getQualifiedName());
|
|
207 |
1
| assertEquals("", e.getNamespacePrefix());
|
|
208 |
1
| assertEquals(uri, e.getNamespaceURI());
|
|
209 |
| |
|
210 |
| } |
|
211 |
| |
|
212 |
| |
|
213 |
1
| public void testConstructor3() {
|
|
214 |
| |
|
215 |
1
| String name = "red:sakjdhjhd";
|
|
216 |
1
| String uri = "http://www.something.com/";
|
|
217 |
1
| Element e = new Element(name, uri);
|
|
218 |
| |
|
219 |
1
| assertEquals("sakjdhjhd", e.getLocalName());
|
|
220 |
1
| assertEquals(name, e.getQualifiedName());
|
|
221 |
1
| assertEquals("red", e.getNamespacePrefix());
|
|
222 |
1
| assertEquals(uri, e.getNamespaceURI());
|
|
223 |
| |
|
224 |
| } |
|
225 |
| |
|
226 |
| |
|
227 |
1
| public void testCopyConstructorWithAdditionalNamespaces() {
|
|
228 |
| |
|
229 |
1
| Element original = new Element("red");
|
|
230 |
1
| original.addNamespaceDeclaration("pre", "http://www.example.org");
|
|
231 |
1
| Element copy = new Element(original);
|
|
232 |
1
| assertEquals("http://www.example.org", copy.getNamespaceURI("pre"));
|
|
233 |
| |
|
234 |
| } |
|
235 |
| |
|
236 |
| |
|
237 |
1
| public void testAllowEmptyNamespace() {
|
|
238 |
| |
|
239 |
1
| String name = "sakjdhjhd";
|
|
240 |
1
| String uri = "http://www.something.com/";
|
|
241 |
1
| Element e = new Element(name, uri);
|
|
242 |
| |
|
243 |
1
| e.setNamespaceURI("");
|
|
244 |
| |
|
245 |
1
| assertEquals("", e.getNamespaceURI());
|
|
246 |
| |
|
247 |
| } |
|
248 |
| |
|
249 |
| |
|
250 |
1
| public void testToString() {
|
|
251 |
| |
|
252 |
1
| String name = "sakjdhjhd";
|
|
253 |
1
| String uri = "http://www.something.com/";
|
|
254 |
1
| Element e = new Element(name, uri);
|
|
255 |
| |
|
256 |
1
| String s = e.toString();
|
|
257 |
1
| assertTrue(s.startsWith("[nu.xom.Element: "));
|
|
258 |
1
| assertTrue(s.endsWith("]"));
|
|
259 |
1
| assertTrue(s.indexOf(name) != -1);
|
|
260 |
| |
|
261 |
| } |
|
262 |
| |
|
263 |
| |
|
264 |
1
| public void testToXML() {
|
|
265 |
| |
|
266 |
1
| String name = "sakjdhjhd";
|
|
267 |
1
| String uri = "http://www.something.com/";
|
|
268 |
1
| Element e = new Element(name, uri);
|
|
269 |
| |
|
270 |
1
| String s = e.toXML();
|
|
271 |
1
| assertTrue(s.endsWith("/>"));
|
|
272 |
1
| assertTrue(s.startsWith("<" + name));
|
|
273 |
1
| assertTrue(s.indexOf(uri) != -1);
|
|
274 |
1
| assertTrue(s.indexOf("xmlns=") != -1);
|
|
275 |
| |
|
276 |
| } |
|
277 |
| |
|
278 |
| |
|
279 |
1
| public void testToXML2() throws ParsingException, IOException {
|
|
280 |
| |
|
281 |
1
| Builder builder = new Builder();
|
|
282 |
1
| File f = new File("data");
|
|
283 |
1
| f = new File(f, "soapresponse.xml");
|
|
284 |
1
| Document doc = builder.build(f);
|
|
285 |
1
| Element root = doc.getRootElement();
|
|
286 |
1
| String form = root.toXML();
|
|
287 |
1
| Document doc2
|
|
288 |
| = builder.build(form, f.toURL().toExternalForm()); |
|
289 |
1
| Element root2 = doc2.getRootElement();
|
|
290 |
1
| assertEquals(root, root2);
|
|
291 |
| |
|
292 |
| } |
|
293 |
| |
|
294 |
| |
|
295 |
1
| public void testToXMLWithXMLLangAttribute() {
|
|
296 |
1
| Element e = new Element("e");
|
|
297 |
1
| e.addAttribute(new Attribute("xml:lang",
|
|
298 |
| "http://www.w3.org/XML/1998/namespace", "en")); |
|
299 |
1
| assertEquals("<e xml:lang=\"en\" />", e.toXML());
|
|
300 |
| } |
|
301 |
| |
|
302 |
| |
|
303 |
1
| public void testAllowNullNamespace() {
|
|
304 |
1
| String name = "sakjdhjhd";
|
|
305 |
1
| String uri = "http://www.something.com/";
|
|
306 |
1
| Element e = new Element(name, uri);
|
|
307 |
| |
|
308 |
1
| e.setNamespaceURI(null);
|
|
309 |
| |
|
310 |
1
| assertEquals("", e.getNamespaceURI());
|
|
311 |
| |
|
312 |
| } |
|
313 |
| |
|
314 |
| |
|
315 |
1
| public void testCantInsertDoctype() {
|
|
316 |
1
| String name = "sakjdhjhd";
|
|
317 |
1
| String uri = "http://www.something.com/";
|
|
318 |
1
| Element e = new Element(name, uri);
|
|
319 |
| |
|
320 |
1
| try {
|
|
321 |
1
| e.appendChild(new DocType(name));
|
|
322 |
0
| fail("Appended a document type declaration to an element");
|
|
323 |
| } |
|
324 |
| catch (IllegalAddException success) { |
|
325 |
1
| assertNotNull(success.getMessage());
|
|
326 |
| } |
|
327 |
| |
|
328 |
| } |
|
329 |
| |
|
330 |
| |
|
331 |
1
| public void testXMLNamespace() {
|
|
332 |
| |
|
333 |
1
| String name = "red:sakjdhjhd";
|
|
334 |
1
| String uri = "http://www.red.com/";
|
|
335 |
1
| Element e = new Element(name, uri);
|
|
336 |
| |
|
337 |
1
| String xmlNamespace = "http://www.w3.org/XML/1998/namespace";
|
|
338 |
| |
|
339 |
1
| assertEquals(xmlNamespace, e.getNamespaceURI("xml"));
|
|
340 |
| |
|
341 |
1
| try {
|
|
342 |
1
| e.addNamespaceDeclaration("xml", "http://www.yahoo.com/");
|
|
343 |
0
| fail("remapped xml prefix!");
|
|
344 |
| } |
|
345 |
| catch (NamespaceConflictException success) { |
|
346 |
1
| assertNotNull(success.getMessage());
|
|
347 |
| } |
|
348 |
1
| assertEquals(e.getNamespaceURI("xml"), xmlNamespace);
|
|
349 |
| |
|
350 |
| |
|
351 |
1
| e.addNamespaceDeclaration("xml", xmlNamespace);
|
|
352 |
1
| assertEquals(xmlNamespace, e.getNamespaceURI("xml"));
|
|
353 |
| |
|
354 |
| |
|
355 |
| } |
|
356 |
| |
|
357 |
| |
|
358 |
1
| public void testUndeclareDefaultNamespace() {
|
|
359 |
| |
|
360 |
1
| String name = "red:sakjdhjhd";
|
|
361 |
1
| String uri = "http://www.red.com/";
|
|
362 |
1
| Element child = new Element(name, uri);
|
|
363 |
1
| Element parent = new Element(
|
|
364 |
| "parent", "http://www.example.com"); |
|
365 |
| |
|
366 |
1
| assertEquals("http://www.example.com",
|
|
367 |
| parent.getNamespaceURI("")); |
|
368 |
| |
|
369 |
1
| parent.appendChild(child);
|
|
370 |
1
| child.addNamespaceDeclaration("", "");
|
|
371 |
| |
|
372 |
1
| assertEquals("", child.getNamespaceURI(""));
|
|
373 |
1
| assertEquals(
|
|
374 |
| "http://www.example.com", |
|
375 |
| parent.getNamespaceURI("")); |
|
376 |
| |
|
377 |
1
| Element child2 = new Element("name", "http://www.default.com");
|
|
378 |
1
| parent.appendChild(child2);
|
|
379 |
| |
|
380 |
| |
|
381 |
1
| try {
|
|
382 |
1
| child2.addNamespaceDeclaration("", "");
|
|
383 |
0
| fail("Illegally undeclared default namespace");
|
|
384 |
| } |
|
385 |
| catch (NamespaceConflictException success) { |
|
386 |
1
| assertNotNull(success.getMessage());
|
|
387 |
| } |
|
388 |
| |
|
389 |
| } |
|
390 |
| |
|
391 |
| |
|
392 |
1
| public void testUnsetDefaultNamespaceWithAttribute() {
|
|
393 |
| |
|
394 |
1
| String name = "sakjdhjhd";
|
|
395 |
1
| String uri = "http://www.red.com/";
|
|
396 |
1
| Element element = new Element(name, uri);
|
|
397 |
1
| element.addAttribute(new Attribute("test", "test"));
|
|
398 |
1
| element.setNamespaceURI("");
|
|
399 |
1
| assertEquals("", element.getNamespaceURI(""));
|
|
400 |
| |
|
401 |
| } |
|
402 |
| |
|
403 |
| |
|
404 |
1
| public void testSetNamespaceWithAttribute() {
|
|
405 |
| |
|
406 |
1
| String name = "red:sakjdhjhd";
|
|
407 |
1
| String uri = "http://www.red.com/";
|
|
408 |
1
| Element element = new Element(name, uri);
|
|
409 |
1
| element.addAttribute(
|
|
410 |
| new Attribute("red:attribute", uri, "test")); |
|
411 |
1
| element.setNamespaceURI(uri);
|
|
412 |
1
| assertEquals(uri, element.getNamespaceURI());
|
|
413 |
1
| assertEquals(uri, element.getNamespaceURI("red"));
|
|
414 |
| |
|
415 |
| } |
|
416 |
| |
|
417 |
| |
|
418 |
1
| public void testAddNamespaceToElementWithAttribute() {
|
|
419 |
| |
|
420 |
1
| String name = "a";
|
|
421 |
1
| String uri = "http://www.w3.org/1999/xhtml";
|
|
422 |
1
| Element element = new Element(name);
|
|
423 |
1
| element.addAttribute(
|
|
424 |
| new Attribute("href", "http://www.elharo.com")); |
|
425 |
1
| element.setNamespaceURI(uri);
|
|
426 |
1
| assertEquals(uri, element.getNamespaceURI());
|
|
427 |
| |
|
428 |
| } |
|
429 |
| |
|
430 |
| |
|
431 |
1
| public void testSameNamespaceForElementAndAttribute() {
|
|
432 |
| |
|
433 |
1
| String name = "a";
|
|
434 |
1
| String uri = "http://www.w3.org/1999/xhtml";
|
|
435 |
1
| Element element = new Element(name);
|
|
436 |
1
| element.addAttribute(
|
|
437 |
| new Attribute("html:href", uri, "http://www.elharo.com")); |
|
438 |
1
| element.setNamespaceURI("http://www.example.com");
|
|
439 |
1
| element.setNamespacePrefix("pre");
|
|
440 |
1
| element.setNamespaceURI(uri);
|
|
441 |
1
| element.setNamespacePrefix("html");
|
|
442 |
1
| assertEquals(uri, element.getNamespaceURI());
|
|
443 |
1
| assertEquals("html", element.getNamespacePrefix());
|
|
444 |
| |
|
445 |
| } |
|
446 |
| |
|
447 |
| |
|
448 |
1
| public void testToXMLWithXMLAttributes() {
|
|
449 |
1
| Element e = new Element("test");
|
|
450 |
1
| e.addAttribute(
|
|
451 |
| new Attribute("xml:space", |
|
452 |
| "http://www.w3.org/XML/1998/namespace", |
|
453 |
| "preserve")); |
|
454 |
1
| e.addAttribute(
|
|
455 |
| new Attribute("zzz:zzz", "http://www.example.org", "preserve")); |
|
456 |
1
| String result = e.toXML();
|
|
457 |
1
| assertEquals("<test xmlns:zzz=\"http://www.example.org\" xml:space=\"preserve\" zzz:zzz=\"preserve\" />", result);
|
|
458 |
| } |
|
459 |
| |
|
460 |
| |
|
461 |
1
| public void testGetNamespacePrefixInt() {
|
|
462 |
1
| Element e = new Element("test");
|
|
463 |
1
| e.addAttribute(
|
|
464 |
| new Attribute("xml:space", |
|
465 |
| "http://www.w3.org/XML/1998/namespace", |
|
466 |
| "preserve")); |
|
467 |
1
| e.addAttribute(
|
|
468 |
| new Attribute("zzz:zzz", "http://www.example.org", "preserve")); |
|
469 |
1
| assertEquals(2, e.getNamespaceDeclarationCount());
|
|
470 |
1
| try {
|
|
471 |
1
| e.getNamespacePrefix(2);
|
|
472 |
0
| fail("Got prefix beyond bounds");
|
|
473 |
| } |
|
474 |
| catch (IndexOutOfBoundsException success) { |
|
475 |
1
| assertNotNull(success.getMessage());
|
|
476 |
| } |
|
477 |
| } |
|
478 |
| |
|
479 |
| |
|
480 |
| |
|
481 |
1
| public void testXMLPrefixOnElement() {
|
|
482 |
1
| Element e = new Element("xml:test", "http://www.w3.org/XML/1998/namespace");
|
|
483 |
1
| assertEquals(0, e.getNamespaceDeclarationCount());
|
|
484 |
1
| assertEquals("<xml:test />", e.toXML());
|
|
485 |
1
| try {
|
|
486 |
1
| e.getNamespacePrefix(0);
|
|
487 |
0
| fail("Got prefix beyond bounds");
|
|
488 |
| } |
|
489 |
| catch (IndexOutOfBoundsException success) { |
|
490 |
1
| assertNotNull(success.getMessage());
|
|
491 |
| } |
|
492 |
| } |
|
493 |
| |
|
494 |
| |
|
495 |
1
| public void testConflictingDefaultNamespace() {
|
|
496 |
| |
|
497 |
1
| Element e = new Element("foo", "http://www.foo.com/");
|
|
498 |
1
| try {
|
|
499 |
1
| e.addNamespaceDeclaration("", "http://www.bar.com/");
|
|
500 |
0
| fail("Added conflicting default namespace");
|
|
501 |
| } |
|
502 |
| catch (NamespaceConflictException success) { |
|
503 |
1
| String message = success.getMessage();
|
|
504 |
1
| assertTrue(message.indexOf("default namespace") > 0);
|
|
505 |
1
| assertTrue(message.indexOf("http://www.foo.com") > 0);
|
|
506 |
1
| assertTrue(message.indexOf("http://www.bar.com/") > 0);
|
|
507 |
| } |
|
508 |
| |
|
509 |
| } |
|
510 |
| |
|
511 |
| |
|
512 |
1
| public void testNamespaceMappings() {
|
|
513 |
| |
|
514 |
1
| String name = "red:sakjdhjhd";
|
|
515 |
1
| String uri = "http://www.red.com/";
|
|
516 |
1
| Element e = new Element(name, uri);
|
|
517 |
1
| e.addNamespaceDeclaration("blue", "http://www.blue.com/");
|
|
518 |
1
| e.addNamespaceDeclaration("green", "http://www.green.com/");
|
|
519 |
1
| Attribute a1 = new Attribute("test", "test");
|
|
520 |
1
| Attribute a2 = new Attribute(
|
|
521 |
| "pre1:green", "http://www.green.com/", "data"); |
|
522 |
1
| Attribute a3 = new Attribute(
|
|
523 |
| "yellow:sfsdadf", "http://www.yellow.com/", "data"); |
|
524 |
1
| e.addAttribute(a1);
|
|
525 |
1
| e.addAttribute(a2);
|
|
526 |
1
| e.addAttribute(a3);
|
|
527 |
| |
|
528 |
1
| assertEquals("http://www.red.com/", e.getNamespaceURI("red"));
|
|
529 |
1
| assertEquals(
|
|
530 |
| "http://www.green.com/", |
|
531 |
| e.getNamespaceURI("green")); |
|
532 |
1
| assertEquals(
|
|
533 |
| "http://www.blue.com/", |
|
534 |
| e.getNamespaceURI("blue")); |
|
535 |
1
| assertEquals(
|
|
536 |
| "http://www.green.com/", |
|
537 |
| e.getNamespaceURI("pre1")); |
|
538 |
1
| assertEquals(
|
|
539 |
| "http://www.yellow.com/", |
|
540 |
| e.getNamespaceURI("yellow")); |
|
541 |
| |
|
542 |
| |
|
543 |
1
| Element e2 = new Element(
|
|
544 |
| "mauve:child", "http://www.mauve.com"); |
|
545 |
1
| e.appendChild(e2);
|
|
546 |
1
| assertEquals(
|
|
547 |
| "http://www.red.com/", |
|
548 |
| e2.getNamespaceURI("red")); |
|
549 |
1
| assertEquals(
|
|
550 |
| "http://www.blue.com/", |
|
551 |
| e2.getNamespaceURI("blue")); |
|
552 |
1
| assertEquals(
|
|
553 |
| "http://www.green.com/", |
|
554 |
| e2.getNamespaceURI("green")); |
|
555 |
1
| assertEquals(
|
|
556 |
| "http://www.green.com/", |
|
557 |
| e2.getNamespaceURI("pre1")); |
|
558 |
1
| assertEquals(
|
|
559 |
| "http://www.yellow.com/", |
|
560 |
| e2.getNamespaceURI("yellow")); |
|
561 |
1
| assertNull(e2.getNamespaceURI("head"));
|
|
562 |
| |
|
563 |
| |
|
564 |
1
| try {
|
|
565 |
1
| e.addNamespaceDeclaration("pre1", "http://www.blue2.com");
|
|
566 |
0
| fail("Added conflicting namespace");
|
|
567 |
| } |
|
568 |
| catch (NamespaceConflictException success) { |
|
569 |
1
| String message = success.getMessage();
|
|
570 |
1
| assertTrue(message.indexOf("http://www.blue2.com") > 0);
|
|
571 |
1
| assertTrue(message.indexOf("pre1") > 0);
|
|
572 |
| } |
|
573 |
| |
|
574 |
1
| try {
|
|
575 |
1
| Attribute a4 = new Attribute(
|
|
576 |
| "pre1:mauve", "http://www.sadas.com/", "data"); |
|
577 |
1
| e.addAttribute(a4);
|
|
578 |
0
| fail("Added conflicting namespace");
|
|
579 |
| } |
|
580 |
| catch (NamespaceConflictException success) { |
|
581 |
1
| assertNotNull(success.getMessage());
|
|
582 |
| } |
|
583 |
| |
|
584 |
| |
|
585 |
| |
|
586 |
1
| try {
|
|
587 |
1
| Attribute a4 = new Attribute(
|
|
588 |
| "pre1:green", "http://www.example.com/", "data"); |
|
589 |
1
| e.addAttribute(a4);
|
|
590 |
| } |
|
591 |
| catch (NamespaceConflictException success) { |
|
592 |
1
| assertNotNull(success.getMessage());
|
|
593 |
| } |
|
594 |
| |
|
595 |
1
| e.removeNamespaceDeclaration("green");
|
|
596 |
1
| assertNull(e.getNamespaceURI("green"));
|
|
597 |
1
| e.addNamespaceDeclaration("green", "http://www.green2.com/");
|
|
598 |
1
| assertEquals(
|
|
599 |
| "http://www.green2.com/", |
|
600 |
| e.getNamespaceURI("green")); |
|
601 |
| |
|
602 |
| } |
|
603 |
| |
|
604 |
| |
|
605 |
1
| public void testAttributes() {
|
|
606 |
| |
|
607 |
1
| String name = "red:sakjdhjhd";
|
|
608 |
1
| String uri = "http://www.red.com/";
|
|
609 |
1
| Element e = new Element(name, uri);
|
|
610 |
| |
|
611 |
1
| Attribute a1 = new Attribute("name", "simple");
|
|
612 |
1
| Attribute a2 = new Attribute(
|
|
613 |
| "pre1:green", "http://www.green.com/", "data"); |
|
614 |
| |
|
615 |
1
| e.addAttribute(a1);
|
|
616 |
1
| e.addAttribute(a2);
|
|
617 |
| |
|
618 |
1
| assertEquals(
|
|
619 |
| a2, |
|
620 |
| e.getAttribute("green", "http://www.green.com/")); |
|
621 |
1
| assertEquals(a1, e.getAttribute("name"));
|
|
622 |
1
| assertEquals(a1, e.getAttribute("name", ""));
|
|
623 |
1
| assertEquals(e, a1.getParent());
|
|
624 |
1
| assertEquals("simple", e.getAttribute("name").getValue());
|
|
625 |
| |
|
626 |
1
| a1.detach();
|
|
627 |
1
| assertNull(a1.getParent());
|
|
628 |
1
| assertNull(e.getAttribute("name"));
|
|
629 |
| |
|
630 |
1
| Attribute removed = e.removeAttribute(a2);
|
|
631 |
1
| assertNull(a2.getParent());
|
|
632 |
1
| assertEquals(a2, removed);
|
|
633 |
1
| assertNull( e.getAttribute("green", "http://www.green.com/"));
|
|
634 |
| |
|
635 |
| } |
|
636 |
| |
|
637 |
| |
|
638 |
1
| public void testRemoveNullAttribute() {
|
|
639 |
| |
|
640 |
1
| String name = "red:sakjdhjhd";
|
|
641 |
1
| String uri = "http://www.red.com/";
|
|
642 |
1
| Element e = new Element(name, uri);
|
|
643 |
| |
|
644 |
1
| Attribute a1 = new Attribute("name", "simple");
|
|
645 |
1
| Attribute a2 = new Attribute(
|
|
646 |
| "pre1:green", "http://www.green.com/", "data"); |
|
647 |
| |
|
648 |
1
| e.addAttribute(a1);
|
|
649 |
1
| e.addAttribute(a2);
|
|
650 |
| |
|
651 |
1
| try {
|
|
652 |
1
| e.removeAttribute(null);
|
|
653 |
0
| fail("Removed Null Attribute");
|
|
654 |
| } |
|
655 |
| catch (NullPointerException success) { |
|
656 |
1
| assertNotNull(success.getMessage());
|
|
657 |
| } |
|
658 |
| |
|
659 |
| } |
|
660 |
| |
|
661 |
1
| public void testRemoveAttributeFromElementWithNoAttributes() {
|
|
662 |
| |
|
663 |
1
| String name = "red:sakjdhjhd";
|
|
664 |
1
| String uri = "http://www.red.com/";
|
|
665 |
1
| Element e = new Element(name, uri);
|
|
666 |
| |
|
667 |
1
| Attribute a1 = new Attribute("name", "simple");
|
|
668 |
| |
|
669 |
1
| try {
|
|
670 |
1
| e.removeAttribute(a1);
|
|
671 |
0
| fail("Removed Attribute that didn't belong");
|
|
672 |
| } |
|
673 |
| catch (NoSuchAttributeException success) { |
|
674 |
1
| assertTrue(success.getMessage().indexOf(a1.getQualifiedName()) > 0);
|
|
675 |
| } |
|
676 |
| |
|
677 |
| } |
|
678 |
| |
|
679 |
| |
|
680 |
1
| public void testRemoveAttributeFromElementWithDifferentAttributes() {
|
|
681 |
| |
|
682 |
1
| String name = "red:sakjdhjhd";
|
|
683 |
1
| String uri = "http://www.red.com/";
|
|
684 |
1
| Element e = new Element(name, uri);
|
|
685 |
1
| e.addAttribute(new Attribute("name", "value"));
|
|
686 |
1
| Attribute a1 = new Attribute("name", "simple");
|
|
687 |
| |
|
688 |
1
| try {
|
|
689 |
1
| e.removeAttribute(a1);
|
|
690 |
0
| fail("Removed Attribute that didn't belong");
|
|
691 |
| } |
|
692 |
| catch (NoSuchAttributeException success) { |
|
693 |
1
| assertTrue(success.getMessage().indexOf(a1.getQualifiedName()) > 0);
|
|
694 |
| } |
|
695 |
| |
|
696 |
| } |
|
697 |
| |
|
698 |
| |
|
699 |
1
| public void testGetValue() {
|
|
700 |
| |
|
701 |
1
| String name = "red:sakjdhjhd";
|
|
702 |
1
| String uri = "http://www.red.com/";
|
|
703 |
1
| Element e = new Element(name, uri);
|
|
704 |
| |
|
705 |
1
| assertEquals(e.getValue(), "");
|
|
706 |
| |
|
707 |
1
| e.appendChild(new Text("data"));
|
|
708 |
1
| assertEquals(e.getValue(), "data");
|
|
709 |
1
| e.appendChild(new Text(" moredata"));
|
|
710 |
1
| assertEquals(e.getValue(), "data moredata");
|
|
711 |
1
| e.appendChild(new Comment(" moredata"));
|
|
712 |
1
| assertEquals(e.getValue(), "data moredata");
|
|
713 |
1
| e.appendChild(
|
|
714 |
| new ProcessingInstruction("target", "moredata")); |
|
715 |
1
| assertEquals(e.getValue(), "data moredata");
|
|
716 |
| |
|
717 |
1
| Element e2 = new Element("child");
|
|
718 |
1
| e.appendChild(e2);
|
|
719 |
1
| assertEquals("data moredata", e.getValue());
|
|
720 |
1
| e2.appendChild(new Text("something"));
|
|
721 |
1
| assertEquals("data moredatasomething", e.getValue());
|
|
722 |
| |
|
723 |
| } |
|
724 |
| |
|
725 |
| |
|
726 |
1
| public void testSetLocalName() {
|
|
727 |
| |
|
728 |
1
| String name = "red:sakjdhjhd";
|
|
729 |
1
| String uri = "http://www.red.com/";
|
|
730 |
1
| Element e = new Element(name, uri);
|
|
731 |
| |
|
732 |
1
| assertEquals("sakjdhjhd", e.getLocalName());
|
|
733 |
| |
|
734 |
1
| e.setLocalName("dude");
|
|
735 |
1
| assertEquals("dude", e.getLocalName());
|
|
736 |
1
| e.setLocalName("digits__");
|
|
737 |
1
| assertEquals("digits__", e.getLocalName());
|
|
738 |
1
| e.setLocalName("digits1234");
|
|
739 |
1
| assertEquals("digits1234", e.getLocalName());
|
|
740 |
1
| e.setLocalName("digits-z");
|
|
741 |
1
| assertEquals("digits-z", e.getLocalName());
|
|
742 |
| |
|
743 |
1
| try {
|
|
744 |
1
| e.setLocalName("spaces ");
|
|
745 |
0
| fail("Local name allowed to contain spaces");
|
|
746 |
| } |
|
747 |
| catch (IllegalNameException success) { |
|
748 |
1
| assertNotNull(success.getMessage());
|
|
749 |
1
| assertEquals("spaces ", success.getData());
|
|
750 |
| } |
|
751 |
| |
|
752 |
1
| try {
|
|
753 |
1
| e.setLocalName("digits:test");
|
|
754 |
0
| fail("Local name allowed to contain colon");
|
|
755 |
| } |
|
756 |
| catch (IllegalNameException success) { |
|
757 |
1
| assertNotNull(success.getMessage());
|
|
758 |
| } |
|
759 |
| |
|
760 |
1
| try {
|
|
761 |
1
| e.setLocalName("digits!test");
|
|
762 |
0
| fail("Local name allowed to contain bang");
|
|
763 |
| } |
|
764 |
| catch (IllegalNameException success) { |
|
765 |
1
| assertNotNull(success.getMessage());
|
|
766 |
1
| assertEquals("digits!test", success.getData());
|
|
767 |
| } |
|
768 |
| |
|
769 |
1
| try {
|
|
770 |
1
| e.setLocalName("digits\u0000test");
|
|
771 |
0
| fail("Local name allowed to contain null");
|
|
772 |
| } |
|
773 |
| catch (IllegalNameException success) { |
|
774 |
1
| assertNotNull(success.getMessage());
|
|
775 |
1
| assertEquals("digits\u0000test", success.getData());
|
|
776 |
| } |
|
777 |
| |
|
778 |
| } |
|
779 |
| |
|
780 |
| |
|
781 |
1
| public void testSetNamespacePrefix() {
|
|
782 |
| |
|
783 |
1
| String name = "red:sakjdhjhd";
|
|
784 |
1
| String uri = "http://www.red.com/";
|
|
785 |
1
| String prefix = "red";
|
|
786 |
1
| Element e = new Element(name, uri);
|
|
787 |
| |
|
788 |
1
| assertEquals(prefix, e.getNamespacePrefix());
|
|
789 |
| |
|
790 |
1
| e.setNamespacePrefix("dude");
|
|
791 |
1
| assertEquals("dude", e.getNamespacePrefix());
|
|
792 |
1
| e.setNamespacePrefix("digits__");
|
|
793 |
1
| assertEquals("digits__", e.getNamespacePrefix());
|
|
794 |
1
| e.setNamespacePrefix("digits1234");
|
|
795 |
1
| assertEquals("digits1234", e.getNamespacePrefix());
|
|
796 |
1
| e.setNamespacePrefix("digits-z");
|
|
797 |
1
| assertEquals("digits-z", e.getNamespacePrefix());
|
|
798 |
1
| e.setNamespacePrefix("");
|
|
799 |
1
| assertEquals("", e.getNamespacePrefix());
|
|
800 |
1
| e.setNamespacePrefix(null);
|
|
801 |
1
| assertEquals("", e.getNamespacePrefix());
|
|
802 |
| |
|
803 |
1
| try {
|
|
804 |
1
| e.setNamespacePrefix("spaces ");
|
|
805 |
0
| fail("namespace prefix allowed to contain spaces");
|
|
806 |
| } |
|
807 |
| catch (IllegalNameException success) { |
|
808 |
1
| assertNotNull(success.getMessage());
|
|
809 |
1
| assertEquals("spaces ", success.getData());
|
|
810 |
| } |
|
811 |
| |
|
812 |
1
| try {
|
|
813 |
1
| e.setNamespacePrefix("digits:test");
|
|
814 |
0
| fail("namespace prefix allowed to contain colon");
|
|
815 |
| } |
|
816 |
| catch (IllegalNameException success) { |
|
817 |
1
| assertNotNull(success.getMessage());
|
|
818 |
| } |
|
819 |
| |
|
820 |
1
| try {
|
|
821 |
1
| e.setNamespacePrefix("digits!test");
|
|
822 |
0
| fail("namespace prefix allowed to contain bang");
|
|
823 |
| } |
|
824 |
| catch (IllegalNameException success) { |
|
825 |
1
| assertNotNull(success.getMessage());
|
|
826 |
1
| assertEquals("digits!test", success.getData());
|
|
827 |
| } |
|
828 |
| |
|
829 |
1
| try {
|
|
830 |
1
| e.setNamespacePrefix("digits\u0000test");
|
|
831 |
0
| fail("namespace prefix allowed to contain null");
|
|
832 |
| } |
|
833 |
| catch (IllegalNameException success) { |
|
834 |
1
| assertNotNull(success.getMessage());
|
|
835 |
1
| assertEquals("digits\u0000test", success.getData());
|
|
836 |
| } |
|
837 |
| |
|
838 |
| } |
|
839 |
| |
|
840 |
| |
|
841 |
1
| public void testSetNamespaceURI() {
|
|
842 |
| |
|
843 |
1
| String name = "red:sakjdhjhd";
|
|
844 |
1
| String uri = "http://www.red.com/";
|
|
845 |
1
| Element e = new Element(name, uri);
|
|
846 |
| |
|
847 |
1
| assertEquals(e.getNamespaceURI(), uri);
|
|
848 |
| |
|
849 |
1
| for (int i = 0; i < legal.length; i++) {
|
|
850 |
6
| e.setNamespaceURI(legal[i]);
|
|
851 |
6
| assertEquals(legal[i], e.getNamespaceURI());
|
|
852 |
| } |
|
853 |
| |
|
854 |
1
| for (int i = 0; i < illegal.length; i++) {
|
|
855 |
6
| try {
|
|
856 |
6
| e.setNamespaceURI(illegal[i]);
|
|
857 |
0
| fail("illegal namespace URI allowed");
|
|
858 |
| } |
|
859 |
| catch (MalformedURIException success) { |
|
860 |
6
| assertNotNull(success.getMessage());
|
|
861 |
| } |
|
862 |
| } |
|
863 |
| |
|
864 |
| } |
|
865 |
| |
|
866 |
1
| public void testRFC2396LegalRFC3986IllegalNamespaceURI() {
|
|
867 |
| |
|
868 |
1
| String name = "red:green";
|
|
869 |
1
| String uri = "dcp.tcp.pft://192.168.0.1:1002:3002?fec=1&crc=0'";
|
|
870 |
1
| try {
|
|
871 |
1
| new Element(name, uri);
|
|
872 |
0
| fail("Allowed RFC 2396 legal but RFC 3986 illegal URI");
|
|
873 |
| } |
|
874 |
| catch (MalformedURIException success) { |
|
875 |
1
| assertNotNull(success.getMessage());
|
|
876 |
| } |
|
877 |
| |
|
878 |
| } |
|
879 |
| |
|
880 |
1
| public void
|
|
881 |
| testSetNamespaceURIConflictsWithAdditionalNamespaceDeclaration() |
|
882 |
| { |
|
883 |
| |
|
884 |
1
| String name = "red:sakjdhjhd";
|
|
885 |
1
| String uri = "http://www.red.com/";
|
|
886 |
1
| Element e = new Element(name, uri);
|
|
887 |
1
| e.addNamespaceDeclaration("red", "http://www.red.com/");
|
|
888 |
| |
|
889 |
1
| try {
|
|
890 |
1
| e.setNamespaceURI("http://www.example.com");
|
|
891 |
0
| fail("illegal namespace conflict");
|
|
892 |
| } |
|
893 |
| catch (NamespaceConflictException success) { |
|
894 |
1
| assertNotNull(success.getMessage());
|
|
895 |
| } |
|
896 |
| |
|
897 |
| } |
|
898 |
| |
|
899 |
| |
|
900 |
1
| public void testSetNamespaceURIConflictsWithAttributeNamespace() {
|
|
901 |
| |
|
902 |
1
| String name = "red:sakjdhjhd";
|
|
903 |
1
| String uri = "http://www.red.com/";
|
|
904 |
1
| Element e = new Element(name, uri);
|
|
905 |
1
| e.addAttribute(
|
|
906 |
| new Attribute("red:test", "http://www.red.com/", "value")); |
|
907 |
| |
|
908 |
1
| try {
|
|
909 |
1
| e.setNamespaceURI("http://www.example.com");
|
|
910 |
0
| fail("illegal attribute namespace conflict");
|
|
911 |
| } |
|
912 |
| catch (NamespaceConflictException success) { |
|
913 |
1
| assertNotNull(success.getMessage());
|
|
914 |
| } |
|
915 |
| |
|
916 |
| } |
|
917 |
| |
|
918 |
| |
|
919 |
1
| public void testChangePrefix() {
|
|
920 |
| |
|
921 |
1
| String name = "red:sakjdhjhd";
|
|
922 |
1
| String uri = "http://www.red.com/";
|
|
923 |
1
| Element element = new Element(name, uri);
|
|
924 |
1
| element.addNamespaceDeclaration("blue", "http://www.foo.com/");
|
|
925 |
1
| element.addAttribute(new Attribute("green:money",
|
|
926 |
| "http://example.com/", "value")); |
|
927 |
1
| element.addNamespaceDeclaration("purple", uri);
|
|
928 |
1
| element.addAttribute(
|
|
929 |
| new Attribute("mauve:money", uri, "value")); |
|
930 |
| |
|
931 |
1
| try {
|
|
932 |
1
| element.setNamespacePrefix("blue");
|
|
933 |
0
| fail("Conflicting prefix allowed against additional " +
|
|
934 |
| "namespace declaration"); |
|
935 |
| } |
|
936 |
| catch (NamespaceConflictException success) { |
|
937 |
1
| assertNotNull(success.getMessage());
|
|
938 |
| } |
|
939 |
| |
|
940 |
1
| try {
|
|
941 |
1
| element.setNamespacePrefix("green");
|
|
942 |
0
| fail("Conflicting prefix allowed against attribute");
|
|
943 |
| } |
|
944 |
| catch (NamespaceConflictException success) { |
|
945 |
1
| assertNotNull(success.getMessage());
|
|
946 |
| } |
|
947 |
| |
|
948 |
1
| element.setNamespacePrefix("purple");
|
|
949 |
1
| assertEquals("purple", element.getNamespacePrefix());
|
|
950 |
1
| element.setNamespacePrefix("mauve");
|
|
951 |
1
| assertEquals("mauve", element.getNamespacePrefix());
|
|
952 |
| |
|
953 |
| } |
|
954 |
| |
|
955 |
| |
|
956 |
1
| public void testDeclareNamespacePrefix() {
|
|
957 |
| |
|
958 |
1
| String name = "red:sakjdhjhd";
|
|
959 |
1
| String uri = "http://www.red.com/";
|
|
960 |
1
| Element e = new Element(name, uri);
|
|
961 |
| |
|
962 |
1
| for (int i = 0; i < legal.length; i++) {
|
|
963 |
6
| e.removeNamespaceDeclaration("prefix");
|
|
964 |
6
| e.addNamespaceDeclaration("prefix", legal[i]);
|
|
965 |
6
| assertEquals(legal[i], e.getNamespaceURI("prefix"));
|
|
966 |
| } |
|
967 |
| |
|
968 |
1
| for (int i = 0; i < illegal.length; i++) {
|
|
969 |
6
| try {
|
|
970 |
6
| e.addNamespaceDeclaration("prefix", illegal[i]);
|
|
971 |
0
| fail("illegal namespace URI allowed " + illegal[i]);
|
|
972 |
| } |
|
973 |
| catch (MalformedURIException ex) { |
|
974 |
| |
|
975 |
6
| assertNotNull(ex.getMessage());
|
|
976 |
| } |
|
977 |
| } |
|
978 |
| |
|
979 |
| } |
|
980 |
| |
|
981 |
| |
|
982 |
1
| public void testInsertChildUsingString() {
|
|
983 |
| |
|
984 |
1
| String name = "red:sakjdhjhd";
|
|
985 |
1
| String uri = "http://www.red.com/";
|
|
986 |
1
| Element e = new Element(name, uri);
|
|
987 |
| |
|
988 |
1
| Element e2 = new Element(
|
|
989 |
| "mauve:child", "http://www.mauve.com"); |
|
990 |
1
| e.insertChild(e2, 0);
|
|
991 |
1
| Element e3 = new Element(
|
|
992 |
| "mauve:child", "http://www.mauve.com"); |
|
993 |
1
| e.insertChild(e3, 0);
|
|
994 |
| |
|
995 |
1
| e.insertChild("Hello", 0);
|
|
996 |
1
| assertEquals("Hello", e.getChild(0).getValue());
|
|
997 |
| |
|
998 |
| } |
|
999 |
| |
|
1000 |
| |
|
1001 |
1
| public void testInsertNull() {
|
|
1002 |
| |
|
1003 |
1
| String name = "red:sakjdhjhd";
|
|
1004 |
1
| String uri = "http://www.red.com/";
|
|
1005 |
1
| Element e = new Element(name, uri);
|
|
1006 |
1
| String data = null;
|
|
1007 |
1
| try {
|
|
1008 |
1
| e.insertChild(data, 0);
|
|
1009 |
0
| fail("Inserted null");
|
|
1010 |
| } |
|
1011 |
| catch (NullPointerException success) { |
|
1012 |
| |
|
1013 |
| } |
|
1014 |
| } |
|
1015 |
| |
|
1016 |
| |
|
1017 |
0
| public void appendNullChild() {
|
|
1018 |
| |
|
1019 |
0
| String name = "red:sakjdhjhd";
|
|
1020 |
0
| String uri = "http://www.red.com/";
|
|
1021 |
0
| Element e = new Element(name, uri);
|
|
1022 |
0
| String data = null;
|
|
1023 |
0
| try {
|
|
1024 |
0
| e.appendChild(data);
|
|
1025 |
0
| fail("Appended null");
|
|
1026 |
| } |
|
1027 |
| catch (NullPointerException success) { |
|
1028 |
| |
|
1029 |
| } |
|
1030 |
| } |
|
1031 |
| |
|
1032 |
| |
|
1033 |
1
| public void testInsertChild() {
|
|
1034 |
| |
|
1035 |
1
| String name = "red:sakjdhjhd";
|
|
1036 |
1
| String uri = "http://www.red.com/";
|
|
1037 |
1
| Element e = new Element(name, uri);
|
|
1038 |
| |
|
1039 |
1
| Element e2 = new Element("mv:child", "http://www.mauve.com");
|
|
1040 |
1
| e.insertChild(e2, 0);
|
|
1041 |
1
| Element e3 = new Element("mv:child", "http://www.mauve.com");
|
|
1042 |
1
| e.insertChild(e3, 0);
|
|
1043 |
1
| Element e4 = new Element("mv:child", "http://www.mauve.com");
|
|
1044 |
1
| e3.insertChild(e4, 0);
|
|
1045 |
| |
|
1046 |
1
| assertEquals(e3, e.getChild(0));
|
|
1047 |
| |
|
1048 |
1
| try {
|
|
1049 |
1
| Element root = new Element("root");
|
|
1050 |
1
| Document doc = new Document(root);
|
|
1051 |
1
| e.insertChild(doc, 0);
|
|
1052 |
0
| fail("Added document as child");
|
|
1053 |
| } |
|
1054 |
| catch (IllegalAddException ex) { |
|
1055 |
| |
|
1056 |
1
| assertNotNull(ex.getMessage());
|
|
1057 |
| } |
|
1058 |
1
| try {
|
|
1059 |
1
| e.insertChild(e2, 0);
|
|
1060 |
0
| fail("Added child twice");
|
|
1061 |
| } |
|
1062 |
| catch (MultipleParentException ex) { |
|
1063 |
| |
|
1064 |
1
| assertNotNull(ex.getMessage());
|
|
1065 |
| } |
|
1066 |
1
| try {
|
|
1067 |
1
| e.insertChild(e4, 0);
|
|
1068 |
0
| fail("Added child twice");
|
|
1069 |
| } |
|
1070 |
| catch (MultipleParentException ex) { |
|
1071 |
| |
|
1072 |
1
| assertNotNull(ex.getMessage());
|
|
1073 |
| } |
|
1074 |
1
| try {
|
|
1075 |
1
| e.insertChild((Node) null, 0);
|
|
1076 |
0
| fail("Added null child");
|
|
1077 |
| } |
|
1078 |
| catch (NullPointerException success) { |
|
1079 |
| |
|
1080 |
| } |
|
1081 |
1
| try {
|
|
1082 |
1
| e.insertChild(new Comment("test"), 20);
|
|
1083 |
0
| fail("Added comment after end");
|
|
1084 |
| } |
|
1085 |
| catch (IndexOutOfBoundsException success) { |
|
1086 |
| |
|
1087 |
1
| assertNotNull(success.getMessage());
|
|
1088 |
| } |
|
1089 |
1
| try {
|
|
1090 |
1
| e.insertChild(new Comment("test"), -20);
|
|
1091 |
0
| fail("Added comment before start");
|
|
1092 |
| } |
|
1093 |
| catch (IndexOutOfBoundsException success) { |
|
1094 |
| |
|
1095 |
| } |
|
1096 |
| |
|
1097 |
| } |
|
1098 |
| |
|
1099 |
| |
|
1100 |
1
| public void testInsertString() {
|
|
1101 |
| |
|
1102 |
1
| Element element = new Element("test");
|
|
1103 |
1
| element.insertChild("test", 0);
|
|
1104 |
| |
|
1105 |
1
| String s = null;
|
|
1106 |
1
| try {
|
|
1107 |
1
| element.insertChild(s, 0);
|
|
1108 |
0
| fail("Inserted string as null");
|
|
1109 |
| } |
|
1110 |
| catch (NullPointerException success) { |
|
1111 |
| |
|
1112 |
| } |
|
1113 |
| |
|
1114 |
1
| element.insertChild("" , 0);
|
|
1115 |
| |
|
1116 |
1
| assertEquals(2, element.getChildCount());
|
|
1117 |
| |
|
1118 |
| } |
|
1119 |
| |
|
1120 |
| |
|
1121 |
1
| public void testUnsetNamespaceWhenPrefixed() {
|
|
1122 |
| |
|
1123 |
1
| Element element
|
|
1124 |
| = new Element("prefix:name", "http://www.foo.com/"); |
|
1125 |
| |
|
1126 |
1
| try {
|
|
1127 |
1
| element.setNamespaceURI("");
|
|
1128 |
0
| fail("Unset namespace");
|
|
1129 |
| } |
|
1130 |
| catch (NamespaceConflictException success) { |
|
1131 |
1
| assertNotNull(success.getMessage());
|
|
1132 |
| } |
|
1133 |
1
| try {
|
|
1134 |
1
| element.setNamespaceURI(null);
|
|
1135 |
0
| fail("Unset namespace");
|
|
1136 |
| } |
|
1137 |
| catch (NamespaceConflictException success) { |
|
1138 |
1
| assertNotNull(success.getMessage());
|
|
1139 |
| } |
|
1140 |
| |
|
1141 |
| } |
|
1142 |
| |
|
1143 |
| |
|
1144 |
1
| public void testGetChildElements() {
|
|
1145 |
| |
|
1146 |
1
| Elements children = element.getChildElements();
|
|
1147 |
1
| assertEquals(3, children.size());
|
|
1148 |
1
| assertEquals(child1, children.get(0));
|
|
1149 |
1
| assertEquals(child4, children.get(1));
|
|
1150 |
1
| assertEquals(child5, children.get(2));
|
|
1151 |
| |
|
1152 |
1
| children = element.getChildElements("nonesuch");
|
|
1153 |
1
| assertEquals(0, children.size());
|
|
1154 |
| |
|
1155 |
1
| children = element.getChildElements("test");
|
|
1156 |
1
| assertEquals(1, children.size());
|
|
1157 |
1
| assertEquals(child1, children.get(0));
|
|
1158 |
| |
|
1159 |
1
| children = element.getChildElements(
|
|
1160 |
| "test", "http://www.example.com"); |
|
1161 |
1
| assertEquals(2, children.size());
|
|
1162 |
1
| assertEquals(child4, children.get(0));
|
|
1163 |
1
| assertEquals(child5, children.get(1));
|
|
1164 |
| |
|
1165 |
| } |
|
1166 |
| |
|
1167 |
| |
|
1168 |
1
| public void testAddAttribute() {
|
|
1169 |
| |
|
1170 |
1
| Element element = new Element("name");
|
|
1171 |
1
| Attribute a1 = new Attribute("name", "value");
|
|
1172 |
1
| Attribute a2 = new Attribute("xlink:type",
|
|
1173 |
| "http://www.w3.org/TR/1999/xlink", "simple"); |
|
1174 |
| |
|
1175 |
1
| element.addAttribute(a1);
|
|
1176 |
1
| element.addAttribute(a2);
|
|
1177 |
| |
|
1178 |
1
| assertEquals(2, element.getAttributeCount());
|
|
1179 |
| |
|
1180 |
| |
|
1181 |
1
| Element element2 = new Element("name");
|
|
1182 |
1
| try {
|
|
1183 |
1
| element2.addAttribute(a1);
|
|
1184 |
0
| fail("added attribute with existing parent");
|
|
1185 |
| } |
|
1186 |
| catch (MultipleParentException success) { |
|
1187 |
1
| assertNotNull(success.getMessage());
|
|
1188 |
| } |
|
1189 |
| |
|
1190 |
1
| a1.detach();
|
|
1191 |
1
| element2.addAttribute(a1);
|
|
1192 |
| |
|
1193 |
1
| a2.detach();
|
|
1194 |
1
| Element funky
|
|
1195 |
| = new Element("xlink:funky", "http://www.funky.org"); |
|
1196 |
1
| try {
|
|
1197 |
1
| funky.addAttribute(a2);
|
|
1198 |
0
| fail("added conflicting namespace");
|
|
1199 |
| } |
|
1200 |
| catch (NamespaceConflictException success) { |
|
1201 |
1
| assertNotNull(success.getMessage());
|
|
1202 |
| } |
|
1203 |
| |
|
1204 |
1
| a2.detach();
|
|
1205 |
1
| Element notasfunky = new Element(
|
|
1206 |
| "prefix:funky", "http://www.w3.org/TR/1999/xlink"); |
|
1207 |
1
| notasfunky.addAttribute(a2);
|
|
1208 |
| |
|
1209 |
| |
|
1210 |
1
| Attribute a3 = new Attribute(
|
|
1211 |
| "xlink:type", "http://www.w3.org/TR/1999/xlink", "simple"); |
|
1212 |
1
| Attribute a4 = new Attribute(
|
|
1213 |
| "xlink:href", "http://www.w3.org/1998/xlink", "simple"); |
|
1214 |
1
| Element test = new Element("test");
|
|
1215 |
1
| try {
|
|
1216 |
1
| test.addAttribute(a3);
|
|
1217 |
1
| test.addAttribute(a4);
|
|
1218 |
0
| fail("added conflicting attributes");
|
|
1219 |
| } |
|
1220 |
| catch (NamespaceConflictException success) { |
|
1221 |
1
| assertNotNull(success.getMessage());
|
|
1222 |
| } |
|
1223 |
| |
|
1224 |
1
| Attribute a5 = new Attribute(
|
|
1225 |
| "xlink:type", "http://www.w3.org/TR/1999/xlink", "simple"); |
|
1226 |
1
| Attribute a6 = new Attribute(
|
|
1227 |
| "xlink:type", "http://www.w3.org/1998/xlink", "simple"); |
|
1228 |
1
| Element test2 = new Element("test");
|
|
1229 |
1
| try {
|
|
1230 |
1
| test2.addAttribute(a5);
|
|
1231 |
1
| test2.addAttribute(a6);
|
|
1232 |
0
| fail("added conflicting attributes");
|
|
1233 |
| } |
|
1234 |
| catch (NamespaceConflictException success) { |
|
1235 |
1
| assertNotNull(success.getMessage());
|
|
1236 |
| } |
|
1237 |
| |
|
1238 |
| } |
|
1239 |
| |
|
1240 |
| |
|
1241 |
1
| public void testAddAttributesWithAdditionalNamespaces() {
|
|
1242 |
| |
|
1243 |
1
| Element element = new Element("name");
|
|
1244 |
1
| element.addNamespaceDeclaration(
|
|
1245 |
| "xlink", "http://www.w3.org/TR/1999/xlink"); |
|
1246 |
1
| element.addNamespaceDeclaration(
|
|
1247 |
| "pre", "http://www.example.com"); |
|
1248 |
| |
|
1249 |
| |
|
1250 |
1
| Attribute a1 = new Attribute("name", "value");
|
|
1251 |
1
| Attribute a2 = new Attribute("xlink:type",
|
|
1252 |
| "http://www.w3.org/TR/1999/xlink", "simple"); |
|
1253 |
| |
|
1254 |
1
| element.addAttribute(a1);
|
|
1255 |
1
| element.addAttribute(a2);
|
|
1256 |
| |
|
1257 |
1
| assertEquals(2, element.getAttributeCount());
|
|
1258 |
| |
|
1259 |
1
| try {
|
|
1260 |
1
| element.addAttribute(
|
|
1261 |
| new Attribute("pre:att", "ftp://example.com/", "value") |
|
1262 |
| ); |
|
1263 |
0
| fail("added attribute that conflicts with " +
|
|
1264 |
| "additional namespace declaration"); |
|
1265 |
| } |
|
1266 |
| catch (NamespaceConflictException success) { |
|
1267 |
1
| assertNotNull(success.getMessage());
|
|
1268 |
| } |
|
1269 |
| |
|
1270 |
1
| element.addAttribute(
|
|
1271 |
| new Attribute("ok:att", "ftp://example.com/", "value") |
|
1272 |
| ); |
|
1273 |
1
| assertEquals(3, element.getAttributeCount());
|
|
1274 |
| |
|
1275 |
1
| try {
|
|
1276 |
1
| element.addNamespaceDeclaration(
|
|
1277 |
| "ok", "http://www.example.net"); |
|
1278 |
0
| fail("added namespace declaration that " +
|
|
1279 |
| "conflicts with attribute"); |
|
1280 |
| } |
|
1281 |
| catch (NamespaceConflictException ex) { |
|
1282 |
1
| assertNotNull(ex.getMessage());
|
|
1283 |
| } |
|
1284 |
| |
|
1285 |
1
| assertEquals(
|
|
1286 |
| "ftp://example.com/", |
|
1287 |
| element.getNamespaceURI("ok")); |
|
1288 |
1
| assertEquals(
|
|
1289 |
| "http://www.w3.org/TR/1999/xlink", |
|
1290 |
| element.getNamespaceURI("xlink")); |
|
1291 |
1
| assertEquals(
|
|
1292 |
| "http://www.example.com", |
|
1293 |
| element.getNamespaceURI("pre")); |
|
1294 |
| |
|
1295 |
| } |
|
1296 |
| |
|
1297 |
| |
|
1298 |
1
| public void testTriple()
|
|
1299 |
| throws IOException, ParsingException { |
|
1300 |
1
| String data = "<b><c1 /><c2 /></b>";
|
|
1301 |
1
| Builder builder = new Builder();
|
|
1302 |
1
| Document doc = builder.build(data, "http://www.example.org/");
|
|
1303 |
1
| Node root = doc.getRootElement();
|
|
1304 |
1
| Node rootcopy = root.copy();
|
|
1305 |
1
| assertEquals(data, rootcopy.toXML());
|
|
1306 |
| } |
|
1307 |
| |
|
1308 |
| |
|
1309 |
1
| public void testCopyChildElementWithNoChildren() {
|
|
1310 |
| |
|
1311 |
1
| Element parent = new Element("parent");
|
|
1312 |
1
| Element child = new Element("child");
|
|
1313 |
1
| parent.appendChild(child);
|
|
1314 |
1
| Element copy = new Element(child);
|
|
1315 |
1
| assertEquals(child, copy);
|
|
1316 |
| |
|
1317 |
| } |
|
1318 |
| |
|
1319 |
| |
|
1320 |
1
| public void testSimpleCopy() {
|
|
1321 |
| |
|
1322 |
1
| Element parent = new Element("parent");
|
|
1323 |
1
| Element child = new Element("child");
|
|
1324 |
1
| parent.appendChild(child);
|
|
1325 |
1
| Element copy = new Element(parent);
|
|
1326 |
1
| assertEquals(parent, copy);
|
|
1327 |
| |
|
1328 |
| } |
|
1329 |
| |
|
1330 |
| |
|
1331 |
1
| public void testCopyElementWithAttributes() {
|
|
1332 |
| |
|
1333 |
1
| Element parent = new Element("parent");
|
|
1334 |
1
| Attribute a = new Attribute("name", "value");
|
|
1335 |
1
| parent.addAttribute(a);
|
|
1336 |
1
| Element copy = new Element(parent);
|
|
1337 |
1
| assertEquals(parent, copy);
|
|
1338 |
1
| Attribute copied = copy.getAttribute(0);
|
|
1339 |
1
| assertEquals(a, copied);
|
|
1340 |
1
| assertEquals(copy, copied.getParent());
|
|
1341 |
| |
|
1342 |
| } |
|
1343 |
| |
|
1344 |
| |
|
1345 |
1
| public void testCopyEmptyElement() {
|
|
1346 |
| |
|
1347 |
1
| Element parent = new Element("parent");
|
|
1348 |
1
| Element copy = new Element(parent);
|
|
1349 |
1
| assertEquals(parent, copy);
|
|
1350 |
| |
|
1351 |
| } |
|
1352 |
| |
|
1353 |
| |
|
1354 |
1
| public void testEmptyElementAsRootElementCopy() {
|
|
1355 |
| |
|
1356 |
1
| Element root = new Element("root");
|
|
1357 |
1
| Document doc = new Document(root);
|
|
1358 |
1
| Node copy = doc.copy();
|
|
1359 |
1
| assertEquals(doc, copy);
|
|
1360 |
| } |
|
1361 |
| |
|
1362 |
| |
|
1363 |
1
| public void testCopy() {
|
|
1364 |
| |
|
1365 |
1
| String name = "red:sakjdhjhd";
|
|
1366 |
1
| String uri = "http://www.red.com/";
|
|
1367 |
1
| String baseURI = "http://www.example.com/";
|
|
1368 |
1
| Element e = new Element(name, uri);
|
|
1369 |
| |
|
1370 |
1
| e.addNamespaceDeclaration("blue", "http://www.blue.com");
|
|
1371 |
1
| e.addNamespaceDeclaration("green", "http://www.green.com");
|
|
1372 |
1
| Attribute a1 = new Attribute("test", "test");
|
|
1373 |
1
| Attribute a2 = new Attribute("pre1:green",
|
|
1374 |
| "http://www.green.com/", "data"); |
|
1375 |
1
| Attribute a3 = new Attribute("yellow:sfsdadf",
|
|
1376 |
| "http://www.yellow.com/", "data"); |
|
1377 |
1
| e.addAttribute(a1);
|
|
1378 |
1
| e.addAttribute(a2);
|
|
1379 |
1
| e.addAttribute(a3);
|
|
1380 |
| |
|
1381 |
1
| Element e2 = new Element("mv:child", "http://www.mauve.com");
|
|
1382 |
1
| e.appendChild(e2);
|
|
1383 |
| |
|
1384 |
1
| Element e3 = new Element("mv:child", "http://www.mauve.com");
|
|
1385 |
1
| e.insertChild(e3, 0);
|
|
1386 |
1
| Element e4 = new Element("mv:child", "http://www.mauve.com");
|
|
1387 |
1
| e3.insertChild(e4, 0);
|
|
1388 |
1
| e.setBaseURI(baseURI);
|
|
1389 |
| |
|
1390 |
1
| Element copy = (Element) e.copy();
|
|
1391 |
| |
|
1392 |
1
| assertEquals(
|
|
1393 |
| e.getNamespaceURI("red"), |
|
1394 |
| copy.getNamespaceURI("red")); |
|
1395 |
1
| assertEquals(
|
|
1396 |
| e.getNamespaceURI("blue"), |
|
1397 |
| copy.getNamespaceURI("blue")); |
|
1398 |
1
| assertEquals(e.getValue(), copy.getValue());
|
|
1399 |
| |
|
1400 |
1
| Attribute ea = e.getAttribute("test");
|
|
1401 |
1
| Attribute ca = copy.getAttribute("test");
|
|
1402 |
1
| assertEquals(ea.getValue(), ca.getValue());
|
|
1403 |
1
| assertEquals(e.getBaseURI(), copy.getBaseURI());
|
|
1404 |
| |
|
1405 |
| } |
|
1406 |
| |
|
1407 |
| |
|
1408 |
1
| public void testDeepCopy() {
|
|
1409 |
| |
|
1410 |
1
| Element top = new Element("e");
|
|
1411 |
1
| Element parent = top;
|
|
1412 |
1
| for (int i = 0; i < 100; i++) {
|
|
1413 |
100
| Element child = new Element("e" + i);
|
|
1414 |
100
| parent.appendChild(child);
|
|
1415 |
100
| parent = child;
|
|
1416 |
| } |
|
1417 |
1
| Element copy = (Element) top.copy();
|
|
1418 |
1
| assertEquals(top, copy);
|
|
1419 |
| |
|
1420 |
| } |
|
1421 |
| |
|
1422 |
| |
|
1423 |
1
| public void testRemoveChildren() {
|
|
1424 |
| |
|
1425 |
1
| String name = "red:sakjdhjhd";
|
|
1426 |
1
| String uri = "http://www.red.com/";
|
|
1427 |
1
| Element parent = new Element(name, uri);
|
|
1428 |
| |
|
1429 |
1
| Attribute a1 = new Attribute("test", "test");
|
|
1430 |
1
| parent.addAttribute(a1);
|
|
1431 |
| |
|
1432 |
1
| Element child1 = new Element("mv:child", "http://www.mauve.com");
|
|
1433 |
1
| parent.appendChild(child1);
|
|
1434 |
1
| Element child2 = new Element("mv:child", "http://www.mauve.com");
|
|
1435 |
1
| parent.appendChild(child2);
|
|
1436 |
1
| Element grandchild = new Element("mv:child", "http://www.mauve.com");
|
|
1437 |
1
| child2.insertChild(grandchild, 0);
|
|
1438 |
| |
|
1439 |
1
| assertEquals(child2, grandchild.getParent());
|
|
1440 |
1
| assertEquals(parent, child1.getParent());
|
|
1441 |
1
| assertEquals(parent, child2.getParent());
|
|
1442 |
| |
|
1443 |
1
| Nodes result = parent.removeChildren();
|
|
1444 |
| |
|
1445 |
1
| assertEquals(0, parent.getChildCount());
|
|
1446 |
1
| assertNull(child1.getParent());
|
|
1447 |
1
| assertNull(child2.getParent());
|
|
1448 |
1
| assertEquals(child2, grandchild.getParent());
|
|
1449 |
1
| assertEquals(parent, a1.getParent());
|
|
1450 |
| |
|
1451 |
1
| assertEquals(2, result.size());
|
|
1452 |
1
| assertEquals(child1, result.get(0));
|
|
1453 |
1
| assertEquals(child2, result.get(1));
|
|
1454 |
1
| assertEquals(grandchild, child2.getChild(0));
|
|
1455 |
1
| assertEquals(child2, grandchild.getParent());
|
|
1456 |
| |
|
1457 |
| } |
|
1458 |
| |
|
1459 |
| |
|
1460 |
1
| public void testRemovedChildrenInheritBaseURI() {
|
|
1461 |
| |
|
1462 |
1
| String base = "http://www.example.com/";
|
|
1463 |
1
| Element parent = new Element("parent");
|
|
1464 |
1
| Element child = new Element("child");
|
|
1465 |
1
| parent.setBaseURI(base);
|
|
1466 |
1
| parent.appendChild(child);
|
|
1467 |
1
| parent.removeChildren();
|
|
1468 |
1
| assertEquals(base, child.getBaseURI());
|
|
1469 |
| |
|
1470 |
| } |
|
1471 |
| |
|
1472 |
| |
|
1473 |
1
| public void testRemoveNonElementChildren() {
|
|
1474 |
| |
|
1475 |
1
| String name = "red:sakjdhjhd";
|
|
1476 |
1
| String uri = "http://www.red.com/";
|
|
1477 |
1
| Element parent = new Element(name, uri);
|
|
1478 |
| |
|
1479 |
1
| Attribute a1 = new Attribute("test", "test");
|
|
1480 |
1
| parent.addAttribute(a1);
|
|
1481 |
| |
|
1482 |
1
| Node child1 = new Text("http://www.mauve.com");
|
|
1483 |
1
| parent.appendChild(child1);
|
|
1484 |
1
| Node child2 = new ProcessingInstruction(
|
|
1485 |
| "child", "http://www.mauve.com"); |
|
1486 |
1
| parent.appendChild(child2);
|
|
1487 |
1
| Node child3 = new Comment("http://www.mauve.com");
|
|
1488 |
1
| parent.appendChild(child3);
|
|
1489 |
| |
|
1490 |
1
| assertEquals(parent, child3.getParent());
|
|
1491 |
1
| assertEquals(parent, child1.getParent());
|
|
1492 |
1
| assertEquals(parent, child2.getParent());
|
|
1493 |
| |
|
1494 |
1
| Nodes result = parent.removeChildren();
|
|
1495 |
| |
|
1496 |
1
| assertEquals(0, parent.getChildCount());
|
|
1497 |
1
| assertNull(child1.getParent());
|
|
1498 |
1
| assertNull(child2.getParent());
|
|
1499 |
1
| assertNull(child3.getParent());
|
|
1500 |
1
| assertEquals(parent, a1.getParent());
|
|
1501 |
| |
|
1502 |
1
| assertEquals(3, result.size());
|
|
1503 |
1
| assertEquals(child1, result.get(0));
|
|
1504 |
1
| assertEquals(child2, result.get(1));
|
|
1505 |
1
| assertEquals(child3, result.get(2));
|
|
1506 |
| |
|
1507 |
| } |
|
1508 |
| |
|
1509 |
| |
|
1510 |
1
| public void testGetAttributeValue() {
|
|
1511 |
| |
|
1512 |
1
| String name = "sakjdhjhd";
|
|
1513 |
1
| Element e = new Element(name);
|
|
1514 |
| |
|
1515 |
1
| assertNull(e.getAttributeValue("test"));
|
|
1516 |
1
| assertNull(e.getAttributeValue("base",
|
|
1517 |
| "http://www.w3.org/XML/1998/namespace")); |
|
1518 |
| |
|
1519 |
1
| e.addAttribute(new Attribute("test", "value"));
|
|
1520 |
1
| e.addAttribute(new Attribute("xml:base",
|
|
1521 |
| "http://www.w3.org/XML/1998/namespace", |
|
1522 |
| "http://www.example.com/")); |
|
1523 |
| |
|
1524 |
1
| assertEquals("value", e.getAttributeValue("test"));
|
|
1525 |
1
| assertEquals(
|
|
1526 |
| "http://www.example.com/", |
|
1527 |
| e.getAttributeValue("base", |
|
1528 |
| "http://www.w3.org/XML/1998/namespace")); |
|
1529 |
1
| assertNull(e.getAttributeValue("xml:base"));
|
|
1530 |
1
| assertNull(e.getAttributeValue("base"));
|
|
1531 |
1
| assertNull(e.getAttributeValue("test",
|
|
1532 |
| "http://www.w3.org/XML/1998/namespace")); |
|
1533 |
| |
|
1534 |
| } |
|
1535 |
| |
|
1536 |
| |
|
1537 |
1
| public void testGetAttribute() {
|
|
1538 |
| |
|
1539 |
1
| String name = "sakjdhjhd";
|
|
1540 |
1
| Element e = new Element(name);
|
|
1541 |
| |
|
1542 |
1
| assertNull(e.getAttribute("test"));
|
|
1543 |
1
| assertNull(e.getAttribute("base",
|
|
1544 |
| "http://www.w3.org/XML/1998/namespace")); |
|
1545 |
| |
|
1546 |
1
| try {
|
|
1547 |
1
| e.getAttribute(0);
|
|
1548 |
| } |
|
1549 |
| catch (IndexOutOfBoundsException success) { |
|
1550 |
| |
|
1551 |
| } |
|
1552 |
| |
|
1553 |
1
| Attribute a1 = new Attribute("test", "value");
|
|
1554 |
1
| Attribute a2 = new Attribute("xml:base",
|
|
1555 |
| "http://www.w3.org/XML/1998/namespace", |
|
1556 |
| "http://www.example.com/"); |
|
1557 |
| |
|
1558 |
1
| e.addAttribute(a1);
|
|
1559 |
1
| e.addAttribute(a2);
|
|
1560 |
| |
|
1561 |
1
| assertEquals(a1, e.getAttribute("test"));
|
|
1562 |
1
| assertEquals(a2, e.getAttribute("base",
|
|
1563 |
| "http://www.w3.org/XML/1998/namespace")); |
|
1564 |
| |
|
1565 |
1
| try {
|
|
1566 |
1
| e.getAttribute(2);
|
|
1567 |
0
| fail("Got attribute beyond bounds");
|
|
1568 |
| } |
|
1569 |
| catch (IndexOutOfBoundsException success) { |
|
1570 |
1
| assertNotNull(success.getMessage());
|
|
1571 |
| } |
|
1572 |
| |
|
1573 |
1
| try {
|
|
1574 |
1
| e.getAttribute(-1);
|
|
1575 |
0
| fail("Got attribute with negative index");
|
|
1576 |
| } |
|
1577 |
| catch (IndexOutOfBoundsException success) { |
|
1578 |
1
| assertNotNull(success.getMessage());
|
|
1579 |
| } |
|
1580 |
| |
|
1581 |
| } |
|
1582 |
| |
|
1583 |
| |
|
1584 |
1
| public void testGetNamespacePrefix() {
|
|
1585 |
1
| Element html = new Element("html");
|
|
1586 |
1
| assertEquals("", html.getNamespacePrefix());
|
|
1587 |
| } |
|
1588 |
| |
|
1589 |
| |
|
1590 |
1
| public void testXMLPrefixAllowed() {
|
|
1591 |
1
| Element test = new Element("xml:base",
|
|
1592 |
| "http://www.w3.org/XML/1998/namespace"); |
|
1593 |
1
| assertEquals("xml", test.getNamespacePrefix());
|
|
1594 |
1
| assertEquals("http://www.w3.org/XML/1998/namespace", test.getNamespaceURI());
|
|
1595 |
1
| assertEquals("xml:base", test.getQualifiedName());
|
|
1596 |
| } |
|
1597 |
| |
|
1598 |
| |
|
1599 |
1
| public void testXMLPrefixNotAllowedWithWrongURI() {
|
|
1600 |
1
| try {
|
|
1601 |
1
| new Element("xml:base", "http://www.example.org/");
|
|
1602 |
0
| fail("Allowed wrong namespace for xml prefix");
|
|
1603 |
| } |
|
1604 |
| catch (NamespaceConflictException success) { |
|
1605 |
1
| assertNotNull(success.getMessage());
|
|
1606 |
| } |
|
1607 |
| |
|
1608 |
| } |
|
1609 |
| |
|
1610 |
| |
|
1611 |
1
| public void testWrongPrefixNotAllowedWithXMLURI() {
|
|
1612 |
| |
|
1613 |
1
| try {
|
|
1614 |
1
| new Element("test:base", "http://www.w3.org/XML/1998/namespace");
|
|
1615 |
0
| fail("Allowed XML namespace to be associated with non-xml prefix");
|
|
1616 |
| } |
|
1617 |
| catch (NamespaceConflictException success) { |
|
1618 |
1
| assertNotNull(success.getMessage());
|
|
1619 |
| } |
|
1620 |
| |
|
1621 |
| |
|
1622 |
| } |
|
1623 |
| |
|
1624 |
| |
|
1625 |
1
| public void testEmptyName() {
|
|
1626 |
| |
|
1627 |
1
| try {
|
|
1628 |
1
| new Element("");
|
|
1629 |
0
| fail("Allowed empty string for element name");
|
|
1630 |
| } |
|
1631 |
| catch (IllegalNameException success) { |
|
1632 |
1
| assertNotNull(success.getMessage());
|
|
1633 |
1
| assertEquals("", success.getData());
|
|
1634 |
| } |
|
1635 |
| |
|
1636 |
| } |
|
1637 |
| |
|
1638 |
| |
|
1639 |
1
| public void testBadNameStartCharacter() {
|
|
1640 |
| |
|
1641 |
1
| try {
|
|
1642 |
1
| new Element("1Kelvin");
|
|
1643 |
0
| fail("Allowed element name to begin with digit");
|
|
1644 |
| } |
|
1645 |
| catch (IllegalNameException success) { |
|
1646 |
1
| assertNotNull(success.getMessage());
|
|
1647 |
1
| assertEquals("1Kelvin", success.getData());
|
|
1648 |
| } |
|
1649 |
| |
|
1650 |
| } |
|
1651 |
| |
|
1652 |
| |
|
1653 |
1
| public void testNullName() {
|
|
1654 |
| |
|
1655 |
1
| try {
|
|
1656 |
1
| new Element((String) null);
|
|
1657 |
0
| fail("Allowed null for element name");
|
|
1658 |
| } |
|
1659 |
| catch (NullPointerException success) { |
|
1660 |
| |
|
1661 |
| } |
|
1662 |
| |
|
1663 |
| } |
|
1664 |
| |
|
1665 |
| |
|
1666 |
| } |