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