Skip to content

Commit 2b7b716

Browse files
committed
add tests
1 parent 1a39a6a commit 2b7b716

File tree

4 files changed

+68
-15
lines changed

4 files changed

+68
-15
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/xml/dom/XMLAttribute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public boolean getSpecified() {
102102
public boolean isId() {
103103
return isId;
104104
}
105-
105+
106106
@Override
107107
public String getNamespaceURI() {
108108
if (parent == null) return null;

net.lecousin.core/src/main/java/net/lecousin/framework/xml/dom/XMLDocument.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,14 @@ public EntityReference createEntityReference(String name) throws DOMException {
311311

312312
@Override
313313
public NodeList getElementsByTagName(String tagname) {
314-
// TODO
315-
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Not supported");
314+
if (root == null) return new XMLNodeList(null);
315+
return root.getElementsByTagName(tagname);
316316
}
317317

318318
@Override
319319
public NodeList getElementsByTagNameNS(String namespaceURI, String localName) {
320-
// TODO
321-
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Not supported");
320+
if (root == null) return new XMLNodeList(null);
321+
return root.getElementsByTagNameNS(namespaceURI, localName);
322322
}
323323

324324
@Override

net.lecousin.core/src/main/java/net/lecousin/framework/xml/dom/XMLElement.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ public XMLAttribute setAttributeNode(Attr newAttr) throws DOMException {
264264
if (a.isEqualNode(na)) {
265265
it.set(na);
266266
a.parent = null;
267+
na.setParent(this);
267268
return na;
268269
}
269270
}
@@ -363,8 +364,10 @@ public XMLAttribute setAttributeNodeNS(Attr newAttr) throws DOMException {
363364
XMLAttribute na = (XMLAttribute)newAttr;
364365
for (ListIterator<XMLAttribute> it = attributes.listIterator(); it.hasNext(); ) {
365366
XMLAttribute a = it.next();
366-
if (a.isEqualNode(na)) {
367+
if (a.getName().equals(na.getName())) {
367368
it.set(na);
369+
a.parent = null;
370+
na.setParent(this);
368371
return na;
369372
}
370373
}
@@ -463,6 +466,7 @@ public void declareNamespace(String uri, String prefix) {
463466
@Override
464467
public NodeList getElementsByTagName(String name) {
465468
LinkedList<XMLNode> elements = new LinkedList<>();
469+
if ("*".equals(name)) name = null;
466470
getElementsByTagName(name, elements);
467471
return new XMLNodeList(elements);
468472
}
@@ -471,10 +475,9 @@ protected void getElementsByTagName(String name, List<XMLNode> result) {
471475
for (XMLNode child : children) {
472476
if (!(child instanceof XMLElement)) continue;
473477
XMLElement e = (XMLElement)child;
474-
if (e.getNodeName().equals(name))
478+
if (name == null || e.getNodeName().equals(name))
475479
result.add(e);
476-
else
477-
e.getElementsByTagName(name, result);
480+
e.getElementsByTagName(name, result);
478481
}
479482
}
480483

@@ -483,10 +486,10 @@ protected void getElementsByTagName(String namespaceURI, String localName, List<
483486
for (XMLNode child : children) {
484487
if (!(child instanceof XMLElement)) continue;
485488
XMLElement e = (XMLElement)child;
486-
if (e.getLocalName().equals(localName) && ObjectUtil.equalsOrNull(namespaceURI, e.getNamespaceURI()))
489+
if (("*".equals(localName) || e.getLocalName().equals(localName)) &&
490+
("*".equals(namespaceURI) || ObjectUtil.equalsOrNull(namespaceURI, e.getNamespaceURI())))
487491
result.add(e);
488-
else
489-
e.getElementsByTagName(namespaceURI, localName, result);
492+
e.getElementsByTagName(namespaceURI, localName, result);
490493
}
491494
}
492495

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/xml/TestDOMModifications.java

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.w3c.dom.Document;
2020
import org.w3c.dom.DocumentType;
2121
import org.w3c.dom.Element;
22+
import org.w3c.dom.NodeList;
2223
import org.w3c.dom.Text;
2324

2425
public class TestDOMModifications extends TestDOM {
@@ -84,11 +85,22 @@ public void test() throws Exception {
8485
Assert.assertEquals(docType1, doc1.getLastChild());
8586
Assert.assertEquals(docType2, doc2.getFirstChild());
8687
Assert.assertEquals(root2, doc2.getLastChild());
87-
doc2.getChildNodes();
88+
Assert.assertNull(root2.getChildNodes().item(0));
89+
Assert.assertNull(doc2.getChildNodes().item(-1));
8890
// add attribute
8991
root1.setAttribute("a1", "v1");
9092
root2.setAttribute("a1", "v1");
9193
checkDocument(doc1, doc2);
94+
Assert.assertNull(root2.getAttributeNode("a1").getChildNodes().item(0));
95+
Assert.assertEquals(1, root2.getAttributes().getLength());
96+
Assert.assertEquals("v1", root2.getAttributes().getNamedItem("a1").getNodeValue());
97+
Assert.assertNull(root2.getAttributes().getNamedItem(null));
98+
Assert.assertNull(root2.getAttributes().getNamedItem("a2"));
99+
Assert.assertEquals("v1", root2.getAttributes().item(0).getNodeValue());
100+
Assert.assertNull(root2.getAttributes().item(-1));
101+
Assert.assertNull(root2.getAttributes().item(10));
102+
Assert.assertNull(root2.getAttributeNode("a1").getAttributes().getNamedItem("test"));
103+
Assert.assertNull(root2.getAttributeNode("a1").getAttributes().item(0));
92104
// add attribute
93105
root1.setAttribute("a2", "v2");
94106
root2.setAttribute("a2", "v2");
@@ -130,6 +142,9 @@ public void test() throws Exception {
130142
root1.getAttributeNodeNS("http://test3", "b").setPrefix("tutu");
131143
root2.getAttributeNodeNS("http://test3", "b").setPrefix("tutu");
132144
checkDocument(doc1, doc2);
145+
root1.getAttributeNodeNS("http://test3", "b").setPrefix("test3");
146+
root2.getAttributeNodeNS("http://test3", "b").setPrefix("test3");
147+
checkDocument(doc1, doc2);
133148
// change value
134149
root1.getAttributeNodeNS("http://test3", "b").setValue("BB");
135150
root2.getAttributeNodeNS("http://test3", "b").setValue("BB");
@@ -144,8 +159,8 @@ public void test() throws Exception {
144159
a.getSchemaTypeInfo();
145160
a.setTextContent("");
146161
// change value
147-
// TODO root2.setAttributeNodeNS(root2.getAttributeNodeNS("http://test3", "b").cloneNode(true));
148-
// TODO checkDocument(doc1, doc2);
162+
root2.setAttributeNodeNS(root2.getAttributeNodeNS("http://test3", "b").cloneNode(true));
163+
checkDocument(doc1, doc2);
149164
// remove attribute
150165
root1.removeAttributeNode(root1.getAttributeNodeNS("http://test3", "b"));
151166
root2.removeAttributeNode(root2.getAttributeNodeNS("http://test3", "b"));
@@ -246,6 +261,41 @@ public void test() throws Exception {
246261
root2Clone = root2.cloneNode(false);
247262
checkElement(root1Clone, root2Clone);
248263
Assert.assertFalse(root2Clone.hasChildNodes());
264+
// create element
265+
XMLElement e = doc2.createElementNS("http://hello", "hello:world");
266+
Assert.assertEquals("http://hello", e.getNamespaceURI());
267+
Assert.assertEquals("hello", e.getPrefix());
268+
Assert.assertEquals("world", e.getLocalName());
269+
270+
e = doc2.createElementNS(null, "empty");
271+
Assert.assertEquals("empty", e.getNodeName());
272+
273+
a = doc2.createAttribute("myId");
274+
a.setValue("myElement");
275+
e.addAttribute(a);
276+
e.setIdAttribute("myId", true);
277+
root2.appendChild(e);
278+
279+
e = doc2.getElementById("myElement");
280+
Assert.assertNotNull(e);
281+
Assert.assertEquals("empty", e.getNodeName());
282+
e = doc2.getElementById("myElement2");
283+
Assert.assertNull(e);
284+
285+
NodeList nodes = doc2.getElementsByTagName("empty");
286+
Assert.assertNotNull(nodes);
287+
Assert.assertEquals(1, nodes.getLength());
288+
e = (XMLElement)nodes.item(0);
289+
290+
a = doc2.createAttribute("myId");
291+
a.setValue("myElement2");
292+
e.setAttributeNodeNS(a);
293+
e.setIdAttributeNode(a, true);
294+
e = doc2.getElementById("myElement2");
295+
Assert.assertNotNull(e);
296+
Assert.assertEquals("empty", e.getNodeName());
297+
e = doc2.getElementById("myElement");
298+
Assert.assertNull(e);
249299
}
250300

251301
}

0 commit comments

Comments
 (0)