01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.commons.configuration.tree.xpath;
18:
19: import java.util.List;
20: import java.util.Locale;
21:
22: import org.apache.commons.configuration.tree.ConfigurationNode;
23: import org.apache.commons.configuration.tree.DefaultConfigurationNode;
24: import org.apache.commons.jxpath.ri.QName;
25: import org.apache.commons.jxpath.ri.model.NodePointer;
26:
27: /**
28: * Test class for ConfigurationIteratorAttributes.
29: *
30: * @author Oliver Heger
31: * @version $Id: TestConfigurationIteratorAttributes.java 502705 2007-02-02 19:55:37Z oheger $
32: */
33: public class TestConfigurationIteratorAttributes extends
34: AbstractXPathTest {
35: /** Constant for the name of another test attribute.*/
36: private static final String TEST_ATTR = "test";
37:
38: /** Stores the node pointer of the test node.*/
39: NodePointer pointer;
40:
41: protected void setUp() throws Exception {
42: super .setUp();
43:
44: // Adds further attributes to the test node
45: ConfigurationNode testNode = root.getChild(1);
46: testNode.addAttribute(new DefaultConfigurationNode(TEST_ATTR,
47: "yes"));
48: pointer = new ConfigurationNodePointer(testNode, Locale
49: .getDefault());
50: }
51:
52: /**
53: * Tests to iterate over all attributes.
54: */
55: public void testIterateAllAttributes() {
56: ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(
57: pointer, new QName(null, "*"));
58: assertEquals("Wrong number of attributes", 2, iteratorSize(it));
59: List attrs = iterationElements(it);
60: assertEquals("Wrong first attribute", ATTR_NAME,
61: ((ConfigurationNode) attrs.get(0)).getName());
62: assertEquals("Wrong first attribute", TEST_ATTR,
63: ((ConfigurationNode) attrs.get(1)).getName());
64: }
65:
66: /**
67: * Tests to iterate over attributes with a specific name.
68: */
69: public void testIterateSpecificAttribute() {
70: ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(
71: pointer, new QName(null, TEST_ATTR));
72: assertEquals("Wrong number of attributes", 1, iteratorSize(it));
73: assertEquals("Wrong attribute", TEST_ATTR,
74: ((ConfigurationNode) iterationElements(it).get(0))
75: .getName());
76: }
77:
78: /**
79: * Tests to iterate over non existing attributes.
80: */
81: public void testIterateUnknownAttribute() {
82: ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(
83: pointer, new QName(null, "unknown"));
84: assertEquals("Found attributes", 0, iteratorSize(it));
85: }
86:
87: /**
88: * Tests iteration when a namespace is specified. This is not supported, so
89: * the iteration should be empty.
90: */
91: public void testIterateNamespace() {
92: ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(
93: pointer, new QName("test", "*"));
94: assertEquals("Found attributes", 0, iteratorSize(it));
95: }
96: }
|