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  package org.apache.commons.configuration2;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.HashSet;
25  import java.util.Iterator;
26  import java.util.NoSuchElementException;
27  import java.util.Set;
28  
29  import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
30  import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
31  import org.apache.commons.configuration2.ex.ConfigurationException;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * A test class which tests functionality related to immutable configurations.
36   */
37  public class TestImmutableConfiguration {
38      /** Constant for the name of a test properties file. */
39      private static final String TEST_FILE = "test.properties";
40  
41      /**
42       * Creates a test configuration object filled with properties.
43       *
44       * @return the test configuration
45       * @throws ConfigurationException if an error occurs
46       */
47      private static PropertiesConfiguration createTestConfig() throws ConfigurationException {
48          return new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
49              .configure(new FileBasedBuilderParametersImpl().setFile(ConfigurationAssert.getTestFile(TEST_FILE))).getConfiguration();
50      }
51  
52      /**
53       * Obtains all keys from the given iteration.
54       *
55       * @param it the iterator
56       * @return a set with all keys
57       */
58      private static Set<String> fetchKeys(final Iterator<String> it) {
59          final Set<String> keys = new HashSet<>();
60          while (it.hasNext()) {
61              keys.add(it.next());
62          }
63          return keys;
64      }
65  
66      /**
67       * Tests that exceptions thrown by the wrapped configuration are handled correctly.
68       */
69      @Test
70      void testExceptionHandling() {
71          final PropertiesConfiguration config = new PropertiesConfiguration();
72          final String property = "nonExistingProperty";
73          config.setThrowExceptionOnMissing(true);
74          final ImmutableConfiguration ic = ConfigurationUtils.unmodifiableConfiguration(config);
75          final NoSuchElementException e = assertThrows(NoSuchElementException.class, () -> ic.getString(property));
76          assertTrue(e.getMessage().contains(property));
77      }
78  
79      /**
80       * Tests whether an immutable subset can be queried.
81       */
82      @Test
83      void testImmutableSubset() throws ConfigurationException {
84          final ImmutableConfiguration conf = ConfigurationUtils.unmodifiableConfiguration(createTestConfig());
85          final ImmutableConfiguration subset = conf.immutableSubset("test");
86          assertFalse(subset.isEmpty());
87          assertEquals(1000000, subset.getLong("long"));
88      }
89  
90      /**
91       * Tests whether data can be accessed from an unmodifiable configuration.
92       */
93      @Test
94      void testUnmodifiableConfigurationAccess() throws ConfigurationException {
95          final Configuration confOrg = createTestConfig();
96          final ImmutableConfiguration conf = ConfigurationUtils.unmodifiableConfiguration(confOrg);
97          assertFalse(conf.isEmpty());
98          for (final Iterator<String> it = confOrg.getKeys(); it.hasNext();) {
99              final String key = it.next();
100             assertTrue(conf.containsKey(key), "Key not contained: " + key);
101             assertEquals(confOrg.getProperty(key), conf.getProperty(key), "Wrong value for " + key);
102         }
103     }
104 
105     /**
106      * Tests that a cast to a mutable configuration is not possible.
107      */
108     @Test
109     void testUnmodifiableConfigurationCast() throws ConfigurationException {
110         final ImmutableConfiguration conf = ConfigurationUtils.unmodifiableConfiguration(createTestConfig());
111         assertThrows(ClassCastException.class, () -> {
112             final Configuration mutableConf = (Configuration) conf;
113             mutableConf.clear();
114         });
115     }
116 
117     /**
118      * Tests an iteration over the keys of the immutable configuration.
119      */
120     @Test
121     void testUnmodifiableConfigurationIterate() throws ConfigurationException {
122         final Configuration confOrg = createTestConfig();
123         final ImmutableConfiguration conf = ConfigurationUtils.unmodifiableConfiguration(confOrg);
124         assertEquals(fetchKeys(confOrg.getKeys()), fetchKeys(conf.getKeys()));
125     }
126 
127     /**
128      * Tests that it is not possible to remove keys using the iterator.
129      */
130     @Test
131     void testUnmodifiableConfigurationIteratorRemove() throws ConfigurationException {
132         final ImmutableConfiguration conf = ConfigurationUtils.unmodifiableConfiguration(createTestConfig());
133         final Iterator<String> it = conf.getKeys();
134         it.next();
135         assertThrows(UnsupportedOperationException.class, it::remove);
136     }
137 
138     /**
139      * Tests whether an update of the original configuration is visible for the immutable view.
140      */
141     @Test
142     void testUnmodifiableConfigurationLiveUpdate() throws ConfigurationException {
143         final Configuration confOrg = createTestConfig();
144         final ImmutableConfiguration conf = ConfigurationUtils.unmodifiableConfiguration(confOrg);
145         final String key = "new.property";
146         final String value = "new value";
147         confOrg.addProperty(key, value);
148         assertEquals(value, conf.getString(key));
149     }
150 
151     /**
152      * Tries to create an immutable configuration from a null object.
153      */
154     @Test
155     void testUnmodifiableConfigurationNull() {
156         assertThrows(NullPointerException.class, () -> ConfigurationUtils.unmodifiableConfiguration(null));
157     }
158 
159     /**
160      * Tests different access methods for properties.
161      */
162     @Test
163     void testUnmodifiableConfigurationOtherTypes() throws ConfigurationException {
164         final ImmutableConfiguration conf = ConfigurationUtils.unmodifiableConfiguration(createTestConfig());
165         assertEquals((byte) 10, conf.getByte("test.byte"));
166         assertTrue(conf.getBoolean("test.boolean"));
167         assertEquals(10.25, conf.getDouble("test.double"), .05);
168         assertEquals(20.25f, conf.getFloat("test.float"), .05);
169         assertEquals(10, conf.getInt("test.integer"));
170         assertEquals(1000000L, conf.getLong("test.long"));
171         assertEquals((short) 1, conf.getShort("test.short"));
172     }
173 
174     /**
175      * Tests whether an unmodifiable hierarchical configuration can be created.
176      */
177     @Test
178     void testUnmodifiableHierarchicalConfiguration() {
179         final HierarchicalConfiguration<?> conf = new BaseHierarchicalConfiguration();
180         final String key = "test";
181         conf.addProperty(key, Boolean.TRUE);
182         final ImmutableHierarchicalConfiguration ihc = ConfigurationUtils.unmodifiableConfiguration(conf);
183         assertTrue(ihc.getBoolean(key));
184         assertEquals(0, ihc.getMaxIndex(key));
185     }
186 }