/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: NodeTest.java 518852 2007-03-16 03:35:49Z vgritsenko $
*/
package org.apache.xindice.xml.dom;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.UserDataHandler;
/**
* Tests NodeImpl class
* @version $Revision: 518852 $, $Date: 2007-03-15 23:35:49 -0400 (Thu, 15 Mar 2007) $
*/
public class NodeTest extends DOMTestCase {
public String getXml() {
return
"<?xml version='1.0' encoding='UTF-8'?>" +
"<a xmlns:name='http://apache.org'>" +
"<b><a><common & entities></a><a><![CDATA[&&& CDATA Section! &&&]]></a></b>" +
"<name:c attr1='a' attr2='b'>" +
"<d attr3='c' attr4='d'/>" +
"</name:c>" +
"<c attr1='a' attr2='b'>" +
"<d attr3='c' attr4='d'/>" +
"</c>" +
"<name:c attr2='b' attr1='a'>" +
"<d attr3='c' attr4='d'/>" +
"</name:c>" +
"<name:c attr1='a' attr2='b'>" +
"<d attr3='c' attr5='d'/>" +
"</name:c>" +
"<d>Parent text<e>Child text</e></d>" +
"<!-- comment -->" +
"<x/>" +
"<cmp>" +
"<b/>" +
"<c attr1='a' attr2='b'>" +
"<d attr3='c' attr4='d'>" +
"<e attr='1'/>" +
"</d>" +
"</c>" +
"<f attr1='a' attr2='b'>" +
"<d attr3='c' attr4='d'>" +
"<e attr='2'/>" +
"</d>" +
"</f>" +
"<d>Parent text<e>Child text</e></d>" +
"</cmp>" +
"</a>";
}
public void testGetFirstChild() {
Node n = root.getFirstChild();
assertEquals(Node.ELEMENT_NODE, n.getNodeType());
assertEquals("b", n.getNodeName());
n = n.getFirstChild();
assertEquals(Node.ELEMENT_NODE, n.getNodeType());
assertEquals("a", n.getNodeName());
n = n.getFirstChild();
assertEquals(Node.TEXT_NODE, n.getNodeType());
n = n.getFirstChild();
assertNull(n);
n = ((Element) root).getElementsByTagName("x").item(0);
assertEquals("x", n.getNodeName());
n = n.getFirstChild();
assertNull(n);
}
public void testCommonEntities() {
Node n = root.getFirstChild().getFirstChild();
assertEquals("<common & entities>", ((NodeImpl) n).getTextContent());
}
public void testCDATA() {
Node n = root.getFirstChild().getFirstChild().getNextSibling().getFirstChild();
assertEquals("&&& CDATA Section! &&&", n.getNodeValue());
}
public void testComment() {
Node n = ((Element) root).getElementsByTagName("x").item(0).getPreviousSibling();
assertEquals(Node.COMMENT_NODE, n.getNodeType());
assertEquals(" comment ", n.getNodeValue());
}
public void testNodeEquality() throws Exception {
Node node1 = root.getFirstChild().getNextSibling();
assertEquals(true, ((NodeImpl) node1).isEqualNode(node1));
assertEquals(true, ((NodeImpl) node1).isSameNode(node1));
Node node2 = node1.getNextSibling();
// wrong node name
assertEquals(false, ((NodeImpl) node1).isEqualNode(node2));
Node node3 = node2.getNextSibling();
assertEquals(true, ((NodeImpl) node1).isEqualNode(node3));
Node node4 = node3.getNextSibling();
// wrong child attribute name
assertEquals(false, ((NodeImpl) node1).isEqualNode(node4));
}
public void testTextContent() throws Exception {
Node node = root.getFirstChild().getNextSibling().getAttributes().item(1);
assertEquals("b", ((NodeImpl) node).getTextContent());
Node node1 = root.getLastChild();
assertEquals("Parent textChild text", ((NodeImpl) node1).getTextContent());
((NodeImpl) node1).setTextContent("");
assertEquals("Parent textChild text", ((NodeImpl) node1).getTextContent());
((NodeImpl) node1).setTextContent("New text");
assertEquals("New text", ((NodeImpl) node1).getTextContent());
assertEquals(1, node1.getChildNodes().getLength());
}
public void testCloneNode() throws Exception {
Node node1 = root.getFirstChild().getNextSibling();
Node node2 = node1.cloneNode(true);
assertTrue("The cloned node must be equal to the original node",
((NodeImpl) node1).isEqualNode(node2));
assertFalse("The cloned node must not be the same as the original node",
((NodeImpl) node1).isSameNode(node2));
assertNull("The duplicate node must have no parent",
node2.getParentNode());
}
public void testCompareNodePosition() throws Exception {
Node root = doc.getElementsByTagName("cmp").item(0);
Node node1 = root.getFirstChild().getNextSibling();
assertEquals(NodeImpl.DOCUMENT_POSITION_CONTAINED_BY | NodeImpl.DOCUMENT_POSITION_FOLLOWING,
((NodeImpl) root).compareDocumentPosition(node1));
assertEquals(NodeImpl.DOCUMENT_POSITION_CONTAINS | NodeImpl.DOCUMENT_POSITION_PRECEDING,
((NodeImpl) node1).compareDocumentPosition(root));
node1 = root.getFirstChild().getNextSibling().getFirstChild();
Node node2 = root.getFirstChild().getNextSibling().getNextSibling().getFirstChild().getFirstChild();
assertEquals(NodeImpl.DOCUMENT_POSITION_FOLLOWING, ((NodeImpl) node1).compareDocumentPosition(node2));
node1 = root.getFirstChild().getNextSibling().getAttributes().item(0);
node2 = root.getFirstChild().getNextSibling().getFirstChild();
assertEquals(NodeImpl.DOCUMENT_POSITION_FOLLOWING, ((NodeImpl) node1).compareDocumentPosition(node2));
assertEquals(NodeImpl.DOCUMENT_POSITION_DISCONNECTED | NodeImpl.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC,
((NodeImpl) doc).compareDocumentPosition(DOMParser.toDocument(getXml())));
node1 = root.getFirstChild().getNextSibling();
node2 = root.getFirstChild().getNextSibling().getFirstChild();
assertEquals(NodeImpl.DOCUMENT_POSITION_FOLLOWING | NodeImpl.DOCUMENT_POSITION_CONTAINED_BY,
((NodeImpl) node1).compareDocumentPosition(node2));
}
public void testUserDataHandler() throws Exception {
Node node1 = root.getFirstChild().getNextSibling();
TestDataHandler handler = new TestDataHandler();
((NodeImpl) node1).setUserData("key", "data", handler);
node1.cloneNode(true);
assertEquals(getExpectedResult(UserDataHandler.NODE_CLONED, "key", "data", node1), handler.getResult());
((DocumentImpl) node1.getOwnerDocument()).renameNode(node1, "http://newURI", "test:bar");
assertEquals(getExpectedResult(UserDataHandler.NODE_RENAMED, "key", "data", node1), handler.getResult());
}
private String getExpectedResult(short op, String key, Object data, Node src) {
String result;
switch (op) {
case UserDataHandler.NODE_ADOPTED:
result = "NODE_ADOPTED";
break;
case UserDataHandler.NODE_CLONED:
result = "NODE_CLONED";
break;
case UserDataHandler.NODE_IMPORTED:
result = "NODE_IMPORTED";
break;
case UserDataHandler.NODE_RENAMED:
result = "NODE_RENAMED";
break;
default:
result = "Unknown";
}
return result + "|" + src.getNodeName() + "|" + key + "|" + data.toString();
}
private class TestDataHandler implements UserDataHandler {
String result;
public void handle(short op, String key, Object data, Node src, Node dst) {
result = getExpectedResult(op, key, data, src);
}
public String getResult() {
return result;
}
}
}