public void testCreateAttribute() throws Exception { DOMTestUtil.execute( new DOMTestUtil.Test() { public void execute(DocumentBuilderFactory dbf) throws Exception { String attrName = "attrIdentifier"; String attrValue = "attrValue"; String attrNs = "http://ws.apache.org/axis2/ns"; String attrNsPrefix = "axis2"; Document doc = dbf.newDocumentBuilder().newDocument(); Attr attr = doc.createAttribute(attrName); assertEquals("Attr name mismatch", attrName, attr.getName()); assertNull("Namespace value should be null", attr.getNamespaceURI()); attr = doc.createAttributeNS(attrNs, attrNsPrefix + ":" + attrName); assertEquals("Attr name mismatch", attrName, attr.getLocalName()); assertNotNull("Namespace value should not be null", attr.getNamespaceURI()); assertEquals("NamsspaceURI mismatch", attrNs, attr.getNamespaceURI()); assertEquals("namespace prefix mismatch", attrNsPrefix, attr.getPrefix()); attr.setValue(attrValue); } }); }
public void testCreateText() throws Exception { DOMTestUtil.execute( new DOMTestUtil.Test() { public void execute(DocumentBuilderFactory dbf) throws Exception { String textValue = "temp text value"; Document doc = dbf.newDocumentBuilder().newDocument(); Text txt = doc.createTextNode(textValue); assertEquals("Text value mismatch", textValue, txt.getData()); } }); }
public void testDocumentSiblings() throws Exception { DOMTestUtil.execute( new DOMTestUtil.Test() { public void execute(DocumentBuilderFactory dbf) throws Exception { Document doc = dbf.newDocumentBuilder().newDocument(); Element elem = doc.createElement("test"); doc.appendChild(elem); Node node = doc.getNextSibling(); assertNull("Document's next sibling has to be null", node); Node node2 = doc.getPreviousSibling(); assertNull("Document's previous sibling has to be null", node2); Node node3 = doc.getParentNode(); assertNull("Document's parent has to be null", node3); } }); }
public void testCreateElement() throws Exception { DOMTestUtil.execute( new DOMTestUtil.Test() { public void execute(DocumentBuilderFactory dbf) throws Exception { String tagName = "LocalName"; String namespace = "http://ws.apache.org/axis2/ns"; Document doc = dbf.newDocumentBuilder().newDocument(); Element elem = doc.createElement(tagName); assertEquals("Local name misnatch", tagName, elem.getNodeName()); elem = doc.createElementNS(namespace, "axis2:" + tagName); assertEquals("Local name misnatch", tagName, elem.getLocalName()); assertEquals("Namespace misnatch", namespace, elem.getNamespaceURI()); } }); }
public void testAllowedChildren() throws Exception { DOMTestUtil.execute( new DOMTestUtil.Test() { public void execute(DocumentBuilderFactory dbf) throws Exception { Document doc = dbf.newDocumentBuilder().newDocument(); doc.appendChild(doc.createComment("some comment")); doc.appendChild(doc.createProcessingInstruction("pi", "data")); // Document Object Model (DOM) Level 3 Core Specification, section 1.1.1 // says that text nodes are not allowed as children of a document. try { doc.appendChild(doc.createTextNode(" ")); fail("Expected DOMException"); } catch (DOMException ex) { assertEquals(DOMException.HIERARCHY_REQUEST_ERR, ex.code); } doc.appendChild(doc.createElement("root1")); // Multiple document elements are not allowed try { doc.appendChild(doc.createElement("root2")); fail("Expected DOMException"); } catch (DOMException ex) { assertEquals(DOMException.HIERARCHY_REQUEST_ERR, ex.code); } // PIs and comments after the document element are allowed doc.appendChild(doc.createProcessingInstruction("pi", "data")); doc.appendChild(doc.createComment("some comment")); // Again, text nodes are not allowed try { doc.appendChild(doc.createTextNode(" ")); fail("Expected DOMException"); } catch (DOMException ex) { assertEquals(DOMException.HIERARCHY_REQUEST_ERR, ex.code); } } }); }