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.configuration;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.fail;
25  
26  import java.io.File;
27  import java.util.ArrayList;
28  import java.util.HashSet;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.NoSuchElementException;
32  import java.util.Set;
33  
34  import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
35  import org.apache.commons.lang.text.StrLookup;
36  import org.junit.Test;
37  
38  /**
39   * Test case for the {@link SubsetConfiguration} class.
40   *
41   * @author Emmanuel Bourg
42   * @version $Id: TestSubsetConfiguration.java 1225019 2011-12-27 21:18:54Z oheger $
43   */
44  public class TestSubsetConfiguration
45  {
46      static final String TEST_DIR = ConfigurationAssert.TEST_DIR_NAME;
47      static final String TEST_FILE = "testDigesterConfiguration2.xml";
48  
49      @Test
50      public void testGetProperty()
51      {
52          Configuration conf = new BaseConfiguration();
53          conf.setProperty("test.key1", "value1");
54          conf.setProperty("testing.key2", "value1");
55  
56          Configuration subset = new SubsetConfiguration(conf, "test", ".");
57          assertFalse("the subset is empty", subset.isEmpty());
58          assertTrue("'key1' not found in the subset", subset.containsKey("key1"));
59          assertFalse("'ng.key2' found in the subset", subset.containsKey("ng.key2"));
60      }
61  
62      @Test
63      public void testSetProperty()
64      {
65          Configuration conf = new BaseConfiguration();
66          Configuration subset = new SubsetConfiguration(conf, "test", ".");
67  
68          // set a property in the subset and check the parent
69          subset.setProperty("key1", "value1");
70          assertEquals("key1 in the subset configuration", "value1", subset.getProperty("key1"));
71          assertEquals("test.key1 in the parent configuration", "value1", conf.getProperty("test.key1"));
72  
73          // set a property in the parent and check in the subset
74          conf.setProperty("test.key2", "value2");
75          assertEquals("test.key2 in the parent configuration", "value2", conf.getProperty("test.key2"));
76          assertEquals("key2 in the subset configuration", "value2", subset.getProperty("key2"));
77      }
78  
79      @Test
80      public void testGetParentKey()
81      {
82          // subset with delimiter
83          SubsetConfiguration subset = new SubsetConfiguration(null, "prefix", ".");
84          assertEquals("parent key for \"key\"", "prefix.key", subset.getParentKey("key"));
85          assertEquals("parent key for \"\"", "prefix", subset.getParentKey(""));
86  
87          // subset without delimiter
88          subset = new SubsetConfiguration(null, "prefix", null);
89          assertEquals("parent key for \"key\"", "prefixkey", subset.getParentKey("key"));
90          assertEquals("parent key for \"\"", "prefix", subset.getParentKey(""));
91      }
92  
93      @Test
94      public void testGetChildKey()
95      {
96          // subset with delimiter
97          SubsetConfiguration subset = new SubsetConfiguration(null, "prefix", ".");
98          assertEquals("parent key for \"prefixkey\"", "key", subset.getChildKey("prefix.key"));
99          assertEquals("parent key for \"prefix\"", "", subset.getChildKey("prefix"));
100 
101         // subset without delimiter
102         subset = new SubsetConfiguration(null, "prefix", null);
103         assertEquals("parent key for \"prefixkey\"", "key", subset.getChildKey("prefixkey"));
104         assertEquals("parent key for \"prefix\"", "", subset.getChildKey("prefix"));
105     }
106 
107     @Test
108     public void testGetKeys()
109     {
110         Configuration conf = new BaseConfiguration();
111         conf.setProperty("test", "value0");
112         conf.setProperty("test.key1", "value1");
113         conf.setProperty("testing.key2", "value1");
114 
115         Configuration subset = new SubsetConfiguration(conf, "test", ".");
116 
117         Iterator<String> it = subset.getKeys();
118         assertEquals("1st key", "", it.next());
119         assertEquals("2nd key", "key1", it.next());
120         assertFalse("too many elements", it.hasNext());
121     }
122 
123     @Test
124     public void testGetKeysWithPrefix()
125     {
126         Configuration conf = new BaseConfiguration();
127         conf.setProperty("test.abc", "value0");
128         conf.setProperty("test.abc.key1", "value1");
129         conf.setProperty("test.abcdef.key2", "value1");
130 
131         Configuration subset = new SubsetConfiguration(conf, "test", ".");
132 
133         Iterator<String> it = subset.getKeys("abc");
134         assertEquals("1st key", "abc", it.next());
135         assertEquals("2nd key", "abc.key1", it.next());
136         assertFalse("too many elements", it.hasNext());
137     }
138 
139     @Test
140     public void testGetList()
141     {
142         Configuration conf = new BaseConfiguration();
143         conf.setProperty("test.abc", "value0,value1");
144         conf.addProperty("test.abc", "value3");
145 
146         Configuration subset = new SubsetConfiguration(conf, "test", ".");
147         List<Object> list = subset.getList("abc", new ArrayList<Object>());
148         assertEquals(3, list.size());
149     }
150 
151     @Test
152     public void testGetParent()
153     {
154         Configuration conf = new BaseConfiguration();
155         SubsetConfiguration subset = new SubsetConfiguration(conf, "prefix", ".");
156 
157         assertEquals("parent", conf, subset.getParent());
158     }
159 
160     @Test
161     public void testGetPrefix()
162     {
163         Configuration conf = new BaseConfiguration();
164         SubsetConfiguration subset = new SubsetConfiguration(conf, "prefix", ".");
165 
166         assertEquals("prefix", "prefix", subset.getPrefix());
167     }
168 
169     @Test
170     public void testSetPrefix()
171     {
172         Configuration conf = new BaseConfiguration();
173         SubsetConfiguration subset = new SubsetConfiguration(conf, null, ".");
174         subset.setPrefix("prefix");
175 
176         assertEquals("prefix", "prefix", subset.getPrefix());
177     }
178 
179     @Test
180     public void testThrowExceptionOnMissing()
181     {
182         BaseConfiguration config = new BaseConfiguration();
183         config.setThrowExceptionOnMissing(true);
184 
185         SubsetConfiguration subset = new SubsetConfiguration(config, "prefix");
186 
187         try
188         {
189             subset.getString("foo");
190             fail("NoSuchElementException expected");
191         }
192         catch (NoSuchElementException e)
193         {
194             // expected
195         }
196 
197         config.setThrowExceptionOnMissing(false);
198         assertNull(subset.getString("foo"));
199 
200 
201         subset.setThrowExceptionOnMissing(true);
202         try
203         {
204             config.getString("foo");
205             fail("NoSuchElementException expected");
206         }
207         catch (NoSuchElementException e)
208         {
209             // expected
210         }
211     }
212 
213     @SuppressWarnings("deprecation")
214     @Test
215     public void testNested() throws Exception
216     {
217         ConfigurationFactory factory = new ConfigurationFactory();
218         File src = new File(new File(TEST_DIR), TEST_FILE);
219         factory.setConfigurationURL(src.toURL());
220         Configuration config = factory.getConfiguration();
221         Configuration subConf = config.subset("tables.table(0)");
222         assertTrue(subConf.getKeys().hasNext());
223         Configuration subSubConf = subConf.subset("fields.field(1)");
224         Iterator<String> itKeys = subSubConf.getKeys();
225         Set<String> keys = new HashSet<String>();
226         keys.add("name");
227         keys.add("type");
228         while(itKeys.hasNext())
229         {
230             String k = itKeys.next();
231             assertTrue(keys.contains(k));
232             keys.remove(k);
233         }
234         assertTrue(keys.isEmpty());
235     }
236 
237     @Test
238     public void testClear()
239     {
240         Configuration config = new BaseConfiguration();
241         config.setProperty("test.key1", "value1");
242         config.setProperty("testing.key2", "value1");
243 
244         Configuration subset = config.subset("test");
245         subset.clear();
246 
247         assertTrue("the subset is not empty", subset.isEmpty());
248         assertFalse("the parent configuration is empty", config.isEmpty());
249     }
250 
251     @Test
252     public void testSetListDelimiter()
253     {
254         BaseConfiguration config = new BaseConfiguration();
255         Configuration subset = config.subset("prefix");
256         config.setListDelimiter('/');
257         subset.addProperty("list", "a/b/c");
258         assertEquals("Wrong size of list", 3, config.getList("prefix.list")
259                 .size());
260 
261         ((AbstractConfiguration) subset).setListDelimiter(';');
262         subset.addProperty("list2", "a;b;c");
263         assertEquals("Wrong size of list2", 3, config.getList("prefix.list2")
264                 .size());
265     }
266 
267     @Test
268     public void testGetListDelimiter()
269     {
270         BaseConfiguration config = new BaseConfiguration();
271         AbstractConfiguration subset = (AbstractConfiguration) config
272                 .subset("prefix");
273         config.setListDelimiter('/');
274         assertEquals("Wrong list delimiter in subset", '/', subset
275                 .getListDelimiter());
276         subset.setListDelimiter(';');
277         assertEquals("Wrong list delimiter in parent", ';', config
278                 .getListDelimiter());
279     }
280 
281     @Test
282     public void testSetDelimiterParsingDisabled()
283     {
284         BaseConfiguration config = new BaseConfiguration();
285         Configuration subset = config.subset("prefix");
286         config.setDelimiterParsingDisabled(true);
287         subset.addProperty("list", "a,b,c");
288         assertEquals("Wrong value of property", "a,b,c", config
289                 .getString("prefix.list"));
290 
291         ((AbstractConfiguration) subset).setDelimiterParsingDisabled(false);
292         subset.addProperty("list2", "a,b,c");
293         assertEquals("Wrong size of list2", 3, config.getList("prefix.list2")
294                 .size());
295     }
296 
297     @Test
298     public void testIsDelimiterParsingDisabled()
299     {
300         BaseConfiguration config = new BaseConfiguration();
301         AbstractConfiguration subset = (AbstractConfiguration) config
302                 .subset("prefix");
303         config.setDelimiterParsingDisabled(true);
304         assertTrue("Wrong value of list parsing flag in subset", subset
305                 .isDelimiterParsingDisabled());
306         subset.setDelimiterParsingDisabled(false);
307         assertFalse("Wrong value of list parsing flag in parent", config
308                 .isDelimiterParsingDisabled());
309     }
310 
311     /**
312      * Tests manipulating the interpolator.
313      */
314     @Test
315     public void testInterpolator()
316     {
317         BaseConfiguration config = new BaseConfiguration();
318         AbstractConfiguration subset = (AbstractConfiguration) config
319                 .subset("prefix");
320         InterpolationTestHelper.testGetInterpolator(subset);
321     }
322 
323     @Test
324     public void testLocalLookupsInInterpolatorAreInherited() {
325         BaseConfiguration config = new BaseConfiguration();
326         ConfigurationInterpolator interpolator = config.getInterpolator();
327         interpolator.registerLookup("brackets", new StrLookup(){
328 
329             @Override
330             public String lookup(String key) {
331                 return "(" + key +")";
332             }
333 
334         });
335         config.setProperty("prefix.var", "${brackets:x}");
336         AbstractConfiguration subset = (AbstractConfiguration) config
337                 .subset("prefix");
338         assertEquals("Local lookup was not inherited", "(x)", subset
339                 .getString("var", ""));
340     }
341 
342     @Test
343     public void testInterpolationForKeysOfTheParent() {
344         BaseConfiguration config = new BaseConfiguration();
345         config.setProperty("test", "junit");
346         config.setProperty("prefix.key", "${test}");
347         AbstractConfiguration subset = (AbstractConfiguration) config
348                 .subset("prefix");
349         assertEquals("Interpolation does not resolve parent keys", "junit",
350                 subset.getString("key", ""));
351     }
352 }