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