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