View Javadoc

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 static org.junit.Assert.assertEquals;
20  
21  import java.util.List;
22  import java.util.Locale;
23  
24  import org.apache.commons.configuration.tree.ConfigurationNode;
25  import org.apache.commons.configuration.tree.DefaultConfigurationNode;
26  import org.apache.commons.jxpath.ri.QName;
27  import org.apache.commons.jxpath.ri.model.NodePointer;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  /**
32   * Test class for ConfigurationIteratorAttributes.
33   *
34   * @author <a
35   * href="http://commons.apache.org/configuration/team-list.html">Commons
36   * Configuration team</a>
37   * @version $Id: TestConfigurationIteratorAttributes.java 1226104 2011-12-31 15:37:16Z oheger $
38   */
39  public class TestConfigurationIteratorAttributes extends AbstractXPathTest
40  {
41      /** Constant for the name of another test attribute.*/
42      private static final String TEST_ATTR = "test";
43  
44      /** Stores the node pointer of the test node.*/
45      NodePointer pointer;
46  
47      @Override
48      @Before
49      public void setUp() throws Exception
50      {
51          super.setUp();
52  
53          // Adds further attributes to the test node
54          ConfigurationNode testNode = root.getChild(1);
55          testNode.addAttribute(new DefaultConfigurationNode(TEST_ATTR, "yes"));
56          pointer = new ConfigurationNodePointer(testNode, Locale.getDefault());
57      }
58  
59      /**
60       * Tests to iterate over all attributes.
61       */
62      @Test
63      public void testIterateAllAttributes()
64      {
65          ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName(null, "*"));
66          assertEquals("Wrong number of attributes", 2, iteratorSize(it));
67          List<ConfigurationNode> attrs = iterationElements(it);
68          assertEquals("Wrong first attribute", ATTR_NAME, attrs.get(0).getName());
69          assertEquals("Wrong first attribute", TEST_ATTR, attrs.get(1).getName());
70      }
71  
72      /**
73       * Tests to iterate over attributes with a specific name.
74       */
75      @Test
76      public void testIterateSpecificAttribute()
77      {
78          ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName(null, TEST_ATTR));
79          assertEquals("Wrong number of attributes", 1, iteratorSize(it));
80          assertEquals("Wrong attribute", TEST_ATTR, iterationElements(it).get(0).getName());
81      }
82  
83      /**
84       * Tests to iterate over non existing attributes.
85       */
86      @Test
87      public void testIterateUnknownAttribute()
88      {
89          ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName(null, "unknown"));
90          assertEquals("Found attributes", 0, iteratorSize(it));
91      }
92  
93      /**
94       * Tests iteration when a namespace is specified. This is not supported, so
95       * the iteration should be empty.
96       */
97      @Test
98      public void testIterateNamespace()
99      {
100         ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName("test", "*"));
101         assertEquals("Found attributes", 0, iteratorSize(it));
102     }
103 }