1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 439648 2006-09-02 20:42:10Z oheger $
32 */
33 public class TestConfigurationIteratorAttributes extends XPathTest
34 {
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 {
43 super.setUp();
44
45
46 ConfigurationNode testNode = root.getChild(1);
47 testNode.addAttribute(new DefaultConfigurationNode(TEST_ATTR, "yes"));
48 pointer = new ConfigurationNodePointer(testNode, Locale.getDefault());
49 }
50
51 /***
52 * Tests to iterate over all attributes.
53 */
54 public void testIterateAllAttributes()
55 {
56 ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName(null, "*"));
57 assertEquals("Wrong number of attributes", 2, iteratorSize(it));
58 List attrs = iterationElements(it);
59 assertEquals("Wrong first attribute", ATTR_NAME, ((ConfigurationNode) attrs.get(0)).getName());
60 assertEquals("Wrong first attribute", TEST_ATTR, ((ConfigurationNode) attrs.get(1)).getName());
61 }
62
63 /***
64 * Tests to iterate over attributes with a specific name.
65 */
66 public void testIterateSpecificAttribute()
67 {
68 ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName(null, TEST_ATTR));
69 assertEquals("Wrong number of attributes", 1, iteratorSize(it));
70 assertEquals("Wrong attribute", TEST_ATTR, ((ConfigurationNode) iterationElements(it).get(0)).getName());
71 }
72
73 /***
74 * Tests to iterate over non existing attributes.
75 */
76 public void testIterateUnknownAttribute()
77 {
78 ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName(null, "unknown"));
79 assertEquals("Found attributes", 0, iteratorSize(it));
80 }
81
82 /***
83 * Tests iteration when a namespace is specified. This is not supported, so
84 * the iteration should be empty.
85 */
86 public void testIterateNamespace()
87 {
88 ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName("test", "*"));
89 assertEquals("Found attributes", 0, iteratorSize(it));
90 }
91 }