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.Iterator;
020: import java.util.List;
021:
022: import org.apache.commons.configuration.tree.ConfigurationNode;
023: import org.apache.commons.configuration.tree.DefaultConfigurationNode;
024: import org.apache.commons.jxpath.JXPathContext;
025: import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
026:
027: /**
028: * Test class for ConfigurationNodePointerFactory. This class does not directly
029: * call the factory's methods, but rather checks if it can be installed in a
030: * <code>JXPathContext</code> and if XPath expressions can be evaluated.
031: *
032: * @author Oliver Heger
033: * @version $Id: TestConfigurationNodePointerFactory.java 502705 2007-02-02 19:55:37Z oheger $
034: */
035: public class TestConfigurationNodePointerFactory extends
036: AbstractXPathTest {
037: /** Stores the JXPathContext used for testing. */
038: JXPathContext context;
039:
040: protected void setUp() throws Exception {
041: super .setUp();
042: JXPathContextReferenceImpl
043: .addNodePointerFactory(new ConfigurationNodePointerFactory());
044: context = JXPathContext.newContext(root);
045: context.setLenient(true);
046: }
047:
048: /**
049: * Tests simple XPath expressions.
050: */
051: public void testSimpleXPath() {
052: List nodes = context.selectNodes(CHILD_NAME1);
053: assertEquals("Incorrect number of results", 2, nodes.size());
054: for (Iterator it = nodes.iterator(); it.hasNext();) {
055: ConfigurationNode node = (ConfigurationNode) it.next();
056: assertEquals("Incorrect node name", CHILD_NAME1, node
057: .getName());
058: assertEquals("Incorrect parent node", root, node
059: .getParentNode());
060: }
061:
062: nodes = context.selectNodes("/" + CHILD_NAME1);
063: assertEquals("Incorrect number of results", 2, nodes.size());
064:
065: nodes = context.selectNodes(CHILD_NAME2 + "/" + CHILD_NAME1
066: + "/" + CHILD_NAME2);
067: assertEquals("Incorrect number of results", 18, nodes.size());
068: }
069:
070: /**
071: * Tests using indices to specify elements.
072: */
073: public void testIndices() {
074: assertEquals("Incorrect value", "1.2.3", context.getValue("/"
075: + CHILD_NAME2 + "[1]/" + CHILD_NAME1 + "[1]/"
076: + CHILD_NAME2 + "[2]"));
077: assertEquals("Incorrect value of last node", String
078: .valueOf(CHILD_COUNT), context.getValue(CHILD_NAME2
079: + "[last()]"));
080:
081: List nodes = context.selectNodes("/" + CHILD_NAME1 + "[1]/*");
082: assertEquals("Wrong number of children", CHILD_COUNT, nodes
083: .size());
084: int index = 1;
085: for (Iterator it = nodes.iterator(); it.hasNext(); index++) {
086: ConfigurationNode node = (ConfigurationNode) it.next();
087: assertEquals("Wrong node value for child " + index, "2."
088: + index, node.getValue());
089: }
090: }
091:
092: /**
093: * Tests accessing attributes.
094: */
095: public void testAttributes() {
096: root.addAttribute(new DefaultConfigurationNode("testAttr",
097: "true"));
098: assertEquals("Did not find attribute of root node", "true",
099: context.getValue("@testAttr"));
100: assertEquals("Incorrect attribute value", "1", context
101: .getValue("/" + CHILD_NAME2 + "[1]/@" + ATTR_NAME));
102:
103: assertTrue("Found elements with name attribute", context
104: .selectNodes("//" + CHILD_NAME2 + "[@name]").isEmpty());
105: ConfigurationNode node = (ConfigurationNode) root.getChild(2)
106: .getChild(1).getChildren(CHILD_NAME2).get(1);
107: node.addAttribute(new DefaultConfigurationNode("name",
108: "testValue"));
109: List nodes = context
110: .selectNodes("//" + CHILD_NAME2 + "[@name]");
111: assertEquals("Name attribute not found", 1, nodes.size());
112: assertEquals("Wrong node returned", node, nodes.get(0));
113: }
114:
115: /**
116: * Tests accessing a node's text.
117: */
118: public void testText() {
119: List nodes = context.selectNodes("//" + CHILD_NAME2
120: + "[text()='1.1.1']");
121: assertEquals("Incorrect number of result nodes", 1, nodes
122: .size());
123: }
124:
125: /**
126: * Tests accessing the parent axis.
127: */
128: public void testParentAxis() {
129: List nodes = context.selectNodes("/" + CHILD_NAME2
130: + "/parent::*");
131: assertEquals("Wrong number of parent nodes", 1, nodes.size());
132: }
133:
134: /**
135: * Tests accessing the following sibling axis.
136: */
137: public void testFollowingSiblingAxis() {
138: List nodes = context.selectNodes("/" + CHILD_NAME1
139: + "[2]/following-sibling::*");
140: assertEquals("Wrong number of following siblings", 1, nodes
141: .size());
142: ConfigurationNode node = (ConfigurationNode) nodes.get(0);
143: assertEquals("Wrong node type", CHILD_NAME2, node.getName());
144: assertEquals("Wrong index", String.valueOf(CHILD_COUNT), node
145: .getValue());
146: }
147:
148: /**
149: * Tests accessing the preceding sibling axis.
150: */
151: public void testPrecedingSiblingAxis() {
152: List nodes = context.selectNodes("/" + CHILD_NAME1
153: + "[2]/preceding-sibling::*");
154: assertEquals("Wrong number of preceding siblings", 3, nodes
155: .size());
156: for (int index = 0, value = 3; index < nodes.size(); index++, value--) {
157: assertEquals("Wrong node index", String.valueOf(value),
158: ((ConfigurationNode) nodes.get(index)).getValue());
159: }
160: }
161: }
|