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.assertArrayEquals;
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.assertNotEquals;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.util.Iterator;
27  import java.util.List;
28  
29  import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
30  import org.apache.commons.configuration2.builder.combined.CombinedConfigurationBuilder;
31  import org.apache.commons.configuration2.ex.ConfigurationException;
32  import org.apache.commons.configuration2.io.FileHandler;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Compare the behavior of various methods between CompositeConfiguration and normal (Properties) Configuration
37   */
38  public class TestEqualBehavior {
39  
40      /**
41       * Checks whether two configurations have the same size, the same key sequence and contain the same key -> value
42       * mappings
43       */
44      private void checkEquality(final String msg, final Configuration c1, final Configuration c2) {
45          final Iterator<String> it1 = c1.getKeys();
46          final Iterator<String> it2 = c2.getKeys();
47  
48          while (it1.hasNext() && it2.hasNext()) {
49              final String key1 = it1.next();
50              final String key2 = it2.next();
51              assertEquals(key1, key2, msg + ", Keys: ");
52              assertEquals(c1.containsKey(key1), c2.containsKey(key2), msg + ", Contains: ");
53          }
54          assertEquals(it1.hasNext(), it2.hasNext(), msg + ", Iterator: ");
55      }
56  
57      /**
58       * Checks whether two configurations have the same key -> value mapping
59       */
60      private void checkSameKey(final String msg, final String key, final Configuration c1, final Configuration c2) {
61          final String[] s1 = c1.getStringArray(key);
62          final String[] s2 = c2.getStringArray(key);
63  
64          assertArrayEquals(s1, s2, msg + ", String Array: ");
65  
66          final List<Object> list1 = c1.getList(key);
67          final List<Object> list2 = c2.getList(key);
68  
69          assertEquals(list1, list2, msg + ", List: ");
70      }
71  
72      private Configuration setupCompositeConfiguration() throws ConfigurationException {
73          final CombinedConfigurationBuilder builder = new CombinedConfigurationBuilder();
74          builder.configure(new FileBasedBuilderParametersImpl().setFile(ConfigurationAssert.getTestFile("testEqualDigester.xml")));
75          return builder.getConfiguration();
76      }
77  
78      private Configuration setupSimpleConfiguration() throws Exception {
79          final String simpleConfigurationFile = ConfigurationAssert.getTestFile("testEqual.properties").getAbsolutePath();
80          final PropertiesConfiguration c = new PropertiesConfiguration();
81          final FileHandler handler = new FileHandler(c);
82          handler.setFileName(simpleConfigurationFile);
83          handler.load();
84          return c;
85      }
86  
87      /**
88       * If we add a to an existing key, does it work?
89       */
90      @Test
91      void testAddingSet() throws Exception {
92          final Configuration simple = setupSimpleConfiguration();
93          final Configuration composite = setupCompositeConfiguration();
94  
95          final String key = "existing.property";
96          final String value = "new value";
97  
98          assertTrue(simple.containsKey(key));
99          assertEquals(simple.containsKey(key), composite.containsKey(key));
100 
101         simple.addProperty(key, value);
102         composite.addProperty(key, value);
103 
104         assertTrue(simple.containsKey(key));
105         assertEquals(simple.containsKey(key), composite.containsKey(key));
106 
107         checkSameKey("testAddingSet", key, simple, composite);
108         checkEquality("testAddingSet", simple, composite);
109     }
110 
111     /**
112      * If we add a key, does it work?
113      */
114     @Test
115     void testAddingUnset() throws Exception {
116         final Configuration simple = setupSimpleConfiguration();
117         final Configuration composite = setupCompositeConfiguration();
118 
119         final String key = "nonexisting.property";
120         final String value = "new value";
121 
122         assertFalse(simple.containsKey(key));
123         assertEquals(simple.containsKey(key), composite.containsKey(key));
124 
125         simple.addProperty(key, value);
126         composite.addProperty(key, value);
127 
128         checkSameKey("testAddingUnset", key, simple, composite);
129         checkEquality("testAddingUnset", simple, composite);
130     }
131 
132     /**
133      * If we delete a key, does it vanish? Does it leave all the other keys unchanged? How about an unset key?
134      */
135     @Test
136     void testDeletingExisting() throws Exception {
137         final Configuration simple = setupSimpleConfiguration();
138         final Configuration composite = setupCompositeConfiguration();
139 
140         final String key = "clear.property";
141 
142         assertTrue(simple.containsKey(key));
143         assertEquals(simple.containsKey(key), composite.containsKey(key));
144 
145         simple.clearProperty(key);
146         composite.clearProperty(key);
147 
148         assertFalse(simple.containsKey(key));
149         assertEquals(simple.containsKey(key), composite.containsKey(key));
150 
151         checkEquality("testDeletingExisting", simple, composite);
152     }
153 
154     @Test
155     void testDeletingNonExisting() throws Exception {
156         final Configuration simple = setupSimpleConfiguration();
157         final Configuration composite = setupCompositeConfiguration();
158 
159         final String key = "nonexisting.clear.property";
160 
161         assertFalse(simple.containsKey(key));
162         assertEquals(simple.containsKey(key), composite.containsKey(key));
163 
164         simple.clearProperty(key);
165         composite.clearProperty(key);
166 
167         assertFalse(simple.containsKey(key));
168         assertEquals(simple.containsKey(key), composite.containsKey(key));
169 
170         checkEquality("testDeletingNonExisting", simple, composite);
171     }
172 
173     /**
174      * Are both configurations equal after loading?
175      */
176     @Test
177     void testLoading() throws Exception {
178         final Configuration simple = setupSimpleConfiguration();
179         final Configuration composite = setupCompositeConfiguration();
180 
181         checkEquality("testLoading", simple, composite);
182     }
183 
184     @Test
185     void testSettingExisting() throws Exception {
186         final Configuration simple = setupSimpleConfiguration();
187         final Configuration composite = setupCompositeConfiguration();
188 
189         final String key = "existing.property";
190         final String value = "new value";
191 
192         assertTrue(simple.containsKey(key));
193         assertNotEquals(value, simple.getString(key));
194         assertEquals(simple.containsKey(key), composite.containsKey(key));
195 
196         simple.setProperty(key, value);
197         composite.setProperty(key, value);
198 
199         assertTrue(simple.containsKey(key));
200         assertEquals(simple.getString(key), value);
201         assertEquals(simple.containsKey(key), composite.containsKey(key));
202 
203         checkSameKey("testSettingExisting", key, simple, composite);
204         checkEquality("testSettingExisting", simple, composite);
205     }
206 
207     /**
208      * If we set a key, does it work? How about an existing key? Can we change it?
209      */
210     @Test
211     void testSettingNonExisting() throws Exception {
212         final Configuration simple = setupSimpleConfiguration();
213         final Configuration composite = setupCompositeConfiguration();
214 
215         final String key = "nonexisting.property";
216         final String value = "new value";
217 
218         assertFalse(simple.containsKey(key));
219         assertEquals(simple.containsKey(key), composite.containsKey(key));
220 
221         simple.setProperty(key, value);
222         composite.setProperty(key, value);
223 
224         assertTrue(simple.containsKey(key));
225         assertEquals(simple.containsKey(key), composite.containsKey(key));
226 
227         checkSameKey("testSettingNonExisting", key, simple, composite);
228         checkEquality("testSettingNonExisting", simple, composite);
229     }
230 }