diff --git a/openide.util/test/unit/src/org/openide/xml/XMLUtilTest.java b/openide.util/test/unit/src/org/openide/xml/XMLUtilTest.java --- a/openide.util/test/unit/src/org/openide/xml/XMLUtilTest.java +++ b/openide.util/test/unit/src/org/openide/xml/XMLUtilTest.java @@ -49,6 +49,7 @@ import java.io.StringReader; import java.lang.ref.Reference; import java.lang.ref.WeakReference; +import java.util.List; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.stream.StreamSource; @@ -58,6 +59,7 @@ import org.openide.util.test.TestFileUtils; import org.w3c.dom.CDATASection; import org.w3c.dom.Document; +import org.w3c.dom.DocumentFragment; import org.w3c.dom.DocumentType; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -497,4 +499,142 @@ assertGC("can collect resolver", resolverRef); } + public void testAppendChildElement() throws Exception { + Document doc = XMLUtil.parse(new InputSource(new StringReader("")), false, true, null, null); + Element parent = doc.createElementNS("unittest", "parent"); + Element newElement1 = doc.createElementNS("unittest", "new_element1"); + Element newElement2 = doc.createElementNS("unittest", "new_element2"); + + XMLUtil.appendChildElement(parent, newElement1, new String[] { "new_element2", "new_element1" }); + NodeList children = parent.getChildNodes(); + assertEquals(1, children.getLength()); + + XMLUtil.appendChildElement(parent, newElement2, new String[] { "new_element2", "new_element1" }); + + children = parent.getChildNodes(); + assertEquals(2, children.getLength()); + Node firstChild = parent.getChildNodes().item(0); + Node secondChild = parent.getChildNodes().item(1); + assertEquals("new_element2", firstChild.getNodeName()); + assertEquals("new_element1", secondChild.getNodeName()); + } + + public void testFindSubElements() throws Exception { + Document doc = XMLUtil.parse(new InputSource(new StringReader(" ")), + false, true, null, null); + + Element parent = doc.getDocumentElement(); + assertEquals(5, parent.getChildNodes().getLength()); + List subElements = XMLUtil.findSubElements(parent); + assertEquals(3, subElements.size()); + Element firstChild = subElements.get(0); + Element secondChild = subElements.get(1); + Element thirdChild = subElements.get(2); + + assertEquals("child1", firstChild.getNodeName()); + assertEquals("child2", secondChild.getNodeName()); + assertEquals("child3", thirdChild.getNodeName()); + + Document failureDoc = XMLUtil.parse(new InputSource(new StringReader("Non whitespace")), + false, true, null, null); + Element failedParent = failureDoc.getDocumentElement(); + try { + XMLUtil.findSubElements(failedParent); + fail("expected IAE"); + } catch (IllegalArgumentException e) { } + } + + public void testFindElement() throws Exception { + String xmlDoc = " " + + " " + + " " + + " " + + " " + + " " + + ""; + + Document doc = XMLUtil.parse(new InputSource(new StringReader(xmlDoc)), false, true, null, null); + Element parent = doc.getDocumentElement(); + + Element noTable1 = XMLUtil.findElement(parent, "table", null); + assertNull(noTable1); + + Element noTable2 = XMLUtil.findElement(parent, "table", "h"); + assertNull(noTable2); + + Element noTable3 = XMLUtil.findElement(parent, "table", "f"); + assertNull(noTable3); + + Element table1 = XMLUtil.findElement(parent, "table", "http://www.w3.org/TR/html4/"); + assertNotNull(table1); + + Element table2 = XMLUtil.findElement(parent, "table", "http://www.w3schools.com/furniture"); + assertNotNull(table2); + + try { + XMLUtil.findElement(parent, "dup", null); + fail("Expected IAE"); + } catch (IllegalArgumentException e) { } + } + + public void testFindText() throws Exception { + Document doc = XMLUtil.parse(new InputSource(new StringReader("Text To Find")), + false, true, null, null); + + Element parent = doc.getDocumentElement(); + String foundText = XMLUtil.findText(parent); + + assertEquals("Text To Find", foundText); + + String notFoundText = XMLUtil.findText(parent.getFirstChild()); + + assertNull(notFoundText); + } + + public void testTranslateXML() throws Exception { + // don't add any whitespace to the first 3 nodes + String xmlDoc = "" + + " " + + " " + + " " + + "
ApplesBananas
" + + "
"; + + Document doc = XMLUtil.parse(new InputSource(new StringReader(xmlDoc)), false, true, null, null); + Element parent = doc.getDocumentElement(); + + Element translated = XMLUtil.translateXML((Element) parent.getFirstChild(), "http://www.w3.org/TR/html4/"); + assertEquals("http://www.w3.org/TR/html4/", translated.getNamespaceURI()); + assertEquals(1, translated.getAttributes().getLength()); + + assertEquals("http://www.w3.org/TR/html4/", translated.getFirstChild().getNamespaceURI()); + assertEquals(0, translated.getFirstChild().getAttributes().getLength()); + } + + public void testCopyDocument() throws Exception { + String srcXml = "" + + " " + + " " + + " " + + "
ApplesBananas
" + + "
"; + + Document srcDoc = XMLUtil.parse(new InputSource(new StringReader(srcXml)), false, true, null, null); + Element srcRoot = srcDoc.getDocumentElement(); + + Document dstDoc = XMLUtil.parse(new InputSource(new StringReader("")), false, true, null, null); + + Element dstRoot = dstDoc.getDocumentElement(); + XMLUtil.copyDocument(dstDoc, srcRoot, dstRoot, "http://www.w3.org/TR/html4/"); +// XMLUtil.write(dstDoc, System.out, "UTF-8"); + + assertEquals(1, dstDoc.getChildNodes().getLength()); + Element root = dstDoc.getDocumentElement(); + Element firstChild = (Element) root.getFirstChild(); + + assertEquals("table", firstChild.getNodeName()); + assertEquals("http://www.w3.org/TR/html4/", firstChild.getNamespaceURI()); + assertEquals(1, firstChild.getAttributes().getLength()); + } + }