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    
018    package org.apache.commons.configuration;
019    
020    import static org.junit.Assert.assertEquals;
021    import static org.junit.Assert.assertTrue;
022    
023    import java.util.Iterator;
024    import java.util.Map;
025    
026    import org.junit.After;
027    import org.junit.Before;
028    import org.junit.Test;
029    
030    /**
031     * @author rgladwel
032     */
033    public class TestConfigurationSet {
034    
035        ConfigurationMap.ConfigurationSet set;
036    
037        String[] properties = {
038                "booleanProperty",
039                "doubleProperty",
040                "floatProperty",
041                "intProperty",
042                "longProperty",
043                "shortProperty",
044                "stringProperty"
045        };
046    
047        Object[] values = {
048                Boolean.TRUE,
049                new Double(Double.MAX_VALUE),
050                new Float(Float.MAX_VALUE),
051                new Integer(Integer.MAX_VALUE),
052                new Long(Long.MAX_VALUE),
053                new Short(Short.MAX_VALUE),
054                "This is a string"
055        };
056    
057        /**
058         * Set up instance variables required by this test case.
059         */
060        @Before
061        public void setUp() throws Exception
062        {
063            BaseConfiguration configuration = new BaseConfiguration();
064            for(int i = 0; i < properties.length ; i++)
065                configuration.setProperty(properties[i], values[i]);
066            set = new ConfigurationMap.ConfigurationSet(configuration);
067        }
068    
069        /**
070         * Tear down instance variables required by this test case.
071         */
072        @After
073        public void tearDown()
074        {
075            set = null;
076        }
077    
078        @Test
079        public void testSize() {
080            assertEquals("Entry set does not match properties size.", properties.length, set.size());
081        }
082    
083        /**
084         * Class under test for Iterator iterator()
085         */
086        @Test
087        public void testIterator() {
088            Iterator<Map.Entry<Object, Object>> iterator = set.iterator();
089            while(iterator.hasNext()) {
090                Map.Entry<Object, Object> entry = iterator.next();
091                boolean found = false;
092                for(int i = 0; i < properties.length; i++) {
093                    if(entry.getKey().equals(properties[i])) {
094                        assertEquals("Incorrect value for property " +
095                                properties[i],values[i],entry.getValue());
096                        found = true;
097                    }
098                }
099                assertTrue("Could not find property " + entry.getKey(),found);
100                iterator.remove();
101            }
102            assertTrue("Iterator failed to remove all properties.",set.isEmpty());
103        }
104    }