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;
18  
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  /***
27   * @author rgladwel
28   */
29  public class TestConfigurationSet extends TestCase {
30  
31      ConfigurationMap.ConfigurationSet set;
32  
33      String[] properties = {
34              "booleanProperty",
35              "doubleProperty",
36              "floatProperty",
37              "intProperty",
38              "longProperty",
39              "shortProperty",
40              "stringProperty"
41      };
42  
43      Object[] values = {
44              Boolean.TRUE,
45              new Double(Double.MAX_VALUE),
46              new Float(Float.MAX_VALUE),
47              new Integer(Integer.MAX_VALUE),
48              new Long(Long.MAX_VALUE),
49              new Short(Short.MAX_VALUE),
50              "This is a string"
51      };
52  
53      /***
54       * Construct a new instance of this test case.
55       * @param name Name of the test case
56       */
57      public TestConfigurationSet(String name)
58      {
59          super(name);
60      }
61  
62      /***
63       * Set up instance variables required by this test case.
64       */
65      public void setUp() throws Exception
66      {
67          BaseConfiguration configuration = new BaseConfiguration();
68          for(int i = 0; i < properties.length ; i++)
69              configuration.setProperty(properties[i], values[i]);
70          set = new ConfigurationMap.ConfigurationSet(configuration);
71      }
72  
73      /***
74       * Return the tests included in this test suite.
75       */
76      public static Test suite()
77      {
78          return (new TestSuite(TestConfigurationSet.class));
79      }
80  
81      /***
82       * Tear down instance variables required by this test case.
83       */
84      public void tearDown()
85      {
86          set = null;
87      }
88  
89      public void testSize() {
90          assertEquals("Entry set does not match properties size.", properties.length, set.size());
91      }
92  
93      /***
94       * Class under test for Iterator iterator()
95       */
96      public void testIterator() {
97          Iterator iterator = set.iterator();
98          while(iterator.hasNext()) {
99              Object object = iterator.next();
100             assertTrue("Entry set iterator did not return EntrySet object, returned "
101                     + object.getClass().getName(), object instanceof Map.Entry);
102             Map.Entry entry = (Map.Entry) object;
103             boolean found = false;
104             for(int i = 0; i < properties.length; i++) {
105                 if(entry.getKey().equals(properties[i])) {
106                     assertEquals("Incorrect value for property " +
107                             properties[i],values[i],entry.getValue());
108                     found = true;
109                 }
110             }
111             assertTrue("Could not find property " + entry.getKey(),found);
112             iterator.remove();
113         }
114         assertTrue("Iterator failed to remove all properties.",set.isEmpty());
115     }
116 
117 }