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