001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.configuration.tree.xpath;
018:
019: import java.util.Locale;
020:
021: import org.apache.commons.configuration.tree.ConfigurationNode;
022: import org.apache.commons.configuration.tree.DefaultConfigurationNode;
023: import org.apache.commons.jxpath.ri.QName;
024: import org.apache.commons.jxpath.ri.model.NodeIterator;
025: import org.apache.commons.jxpath.ri.model.NodePointer;
026:
027: /**
028: * Test class for ConfigurationNodePointer.
029: *
030: * @author Oliver Heger
031: * @version $Id: TestConfigurationNodePointer.java 502705 2007-02-02 19:55:37Z oheger $
032: */
033: public class TestConfigurationNodePointer extends AbstractXPathTest {
034: /** Stores the node pointer to be tested. */
035: NodePointer pointer;
036:
037: protected void setUp() throws Exception {
038: super .setUp();
039: pointer = new ConfigurationNodePointer(root, Locale
040: .getDefault());
041: }
042:
043: /**
044: * Tests comparing child node pointers for child nodes.
045: */
046: public void testCompareChildNodePointersChildren() {
047: NodePointer p1 = new ConfigurationNodePointer(pointer, root
048: .getChild(1));
049: NodePointer p2 = new ConfigurationNodePointer(pointer, root
050: .getChild(3));
051: assertEquals("Incorrect order", -1, pointer
052: .compareChildNodePointers(p1, p2));
053: assertEquals("Incorrect symmetric order", 1, pointer
054: .compareChildNodePointers(p2, p1));
055: }
056:
057: /**
058: * Tests comparing child node pointers for attribute nodes.
059: */
060: public void testCompareChildNodePointersAttributes() {
061: root
062: .addAttribute(new DefaultConfigurationNode("attr1",
063: "test1"));
064: root
065: .addAttribute(new DefaultConfigurationNode("attr2",
066: "test2"));
067: NodePointer p1 = new ConfigurationNodePointer(pointer, root
068: .getAttribute(0));
069: NodePointer p2 = new ConfigurationNodePointer(pointer, root
070: .getAttribute(1));
071: assertEquals("Incorrect order", -1, pointer
072: .compareChildNodePointers(p1, p2));
073: assertEquals("Incorrect symmetric order", 1, pointer
074: .compareChildNodePointers(p2, p1));
075: }
076:
077: /**
078: * tests comparing child node pointers for both child and attribute nodes.
079: */
080: public void testCompareChildNodePointersChildAndAttribute() {
081: root
082: .addAttribute(new DefaultConfigurationNode("attr1",
083: "test1"));
084: NodePointer p1 = new ConfigurationNodePointer(pointer, root
085: .getChild(2));
086: NodePointer p2 = new ConfigurationNodePointer(pointer, root
087: .getAttribute(0));
088: assertEquals("Incorrect order for attributes", 1, pointer
089: .compareChildNodePointers(p1, p2));
090: assertEquals("Incorrect symmetric order for attributes", -1,
091: pointer.compareChildNodePointers(p2, p1));
092: }
093:
094: /**
095: * Tests comparing child node pointers for child nodes that do not belong to
096: * the parent node.
097: */
098: public void testCompareChildNodePointersInvalidChildren() {
099: ConfigurationNode node = root.getChild(1);
100: NodePointer p1 = new ConfigurationNodePointer(pointer, node
101: .getChild(1));
102: NodePointer p2 = new ConfigurationNodePointer(pointer, node
103: .getChild(3));
104: assertEquals("Non child nodes could be sorted", 0, pointer
105: .compareChildNodePointers(p1, p2));
106: assertEquals("Non child nodes could be sorted symmetrically",
107: 0, pointer.compareChildNodePointers(p2, p1));
108: }
109:
110: /**
111: * Tests the attribute flag.
112: */
113: public void testIsAttribute() {
114: ConfigurationNode node = new DefaultConfigurationNode("test",
115: "testval");
116: NodePointer p = new ConfigurationNodePointer(pointer, node);
117: assertFalse("Node is an attribute", p.isAttribute());
118: node.setAttribute(true);
119: assertTrue("Node is no attribute", p.isAttribute());
120: }
121:
122: /**
123: * Tests if leaves in the tree are correctly detected.
124: */
125: public void testIsLeave() {
126: assertFalse("Root node is leaf", pointer.isLeaf());
127:
128: NodePointer p = pointer;
129: while (!p.isLeaf()) {
130: ConfigurationNode node = (ConfigurationNode) p.getNode();
131: assertTrue("Node has no children",
132: node.getChildrenCount() > 0);
133: p = new ConfigurationNodePointer(p, node.getChild(0));
134: }
135: assertTrue("Node has children", ((ConfigurationNode) p
136: .getNode()).getChildrenCount() == 0);
137: }
138:
139: /**
140: * Tests the iterators returned by the node pointer.
141: */
142: public void testIterators() {
143: checkIterators(pointer);
144: }
145:
146: /**
147: * Recursive helper method for testing the returned iterators.
148: *
149: * @param p the node pointer to test
150: */
151: private void checkIterators(NodePointer p) {
152: ConfigurationNode node = (ConfigurationNode) p.getNode();
153: NodeIterator it = p.childIterator(null, false, null);
154: assertEquals("Iterator count differs from children count", node
155: .getChildrenCount(), iteratorSize(it));
156:
157: for (int index = 1; it.setPosition(index); index++) {
158: NodePointer pchild = it.getNodePointer();
159: assertEquals("Wrong child", node.getChild(index - 1),
160: pchild.getNode());
161: checkIterators(pchild);
162: }
163:
164: it = p.attributeIterator(new QName(null, "*"));
165: assertEquals("Iterator count differs from attribute count",
166: node.getAttributeCount(), iteratorSize(it));
167: for (int index = 1; it.setPosition(index); index++) {
168: NodePointer pattr = it.getNodePointer();
169: assertTrue("Node pointer is no attribute", pattr
170: .isAttribute());
171: assertEquals("Wrong attribute", node
172: .getAttribute(index - 1), pattr.getNode());
173: checkIterators(pattr);
174: }
175: }
176: }
|