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 static org.junit.Assert.assertEquals;
020    
021    import java.util.List;
022    import java.util.Locale;
023    
024    import org.apache.commons.configuration.tree.ConfigurationNode;
025    import org.apache.commons.configuration.tree.DefaultConfigurationNode;
026    import org.apache.commons.jxpath.ri.QName;
027    import org.apache.commons.jxpath.ri.model.NodePointer;
028    import org.junit.Before;
029    import org.junit.Test;
030    
031    /**
032     * Test class for ConfigurationIteratorAttributes.
033     *
034     * @author <a
035     * href="http://commons.apache.org/configuration/team-list.html">Commons
036     * Configuration team</a>
037     * @version $Id: TestConfigurationIteratorAttributes.java 1226104 2011-12-31 15:37:16Z oheger $
038     */
039    public class TestConfigurationIteratorAttributes extends AbstractXPathTest
040    {
041        /** Constant for the name of another test attribute.*/
042        private static final String TEST_ATTR = "test";
043    
044        /** Stores the node pointer of the test node.*/
045        NodePointer pointer;
046    
047        @Override
048        @Before
049        public void setUp() throws Exception
050        {
051            super.setUp();
052    
053            // Adds further attributes to the test node
054            ConfigurationNode testNode = root.getChild(1);
055            testNode.addAttribute(new DefaultConfigurationNode(TEST_ATTR, "yes"));
056            pointer = new ConfigurationNodePointer(testNode, Locale.getDefault());
057        }
058    
059        /**
060         * Tests to iterate over all attributes.
061         */
062        @Test
063        public void testIterateAllAttributes()
064        {
065            ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName(null, "*"));
066            assertEquals("Wrong number of attributes", 2, iteratorSize(it));
067            List<ConfigurationNode> attrs = iterationElements(it);
068            assertEquals("Wrong first attribute", ATTR_NAME, attrs.get(0).getName());
069            assertEquals("Wrong first attribute", TEST_ATTR, attrs.get(1).getName());
070        }
071    
072        /**
073         * Tests to iterate over attributes with a specific name.
074         */
075        @Test
076        public void testIterateSpecificAttribute()
077        {
078            ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName(null, TEST_ATTR));
079            assertEquals("Wrong number of attributes", 1, iteratorSize(it));
080            assertEquals("Wrong attribute", TEST_ATTR, iterationElements(it).get(0).getName());
081        }
082    
083        /**
084         * Tests to iterate over non existing attributes.
085         */
086        @Test
087        public void testIterateUnknownAttribute()
088        {
089            ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName(null, "unknown"));
090            assertEquals("Found attributes", 0, iteratorSize(it));
091        }
092    
093        /**
094         * Tests iteration when a namespace is specified. This is not supported, so
095         * the iteration should be empty.
096         */
097        @Test
098        public void testIterateNamespace()
099        {
100            ConfigurationNodeIteratorAttribute it = new ConfigurationNodeIteratorAttribute(pointer, new QName("test", "*"));
101            assertEquals("Found attributes", 0, iteratorSize(it));
102        }
103    }