1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     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 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          // Adds further attributes to the test node
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  }