1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.configuration.internal

File CompositeConfigurationSourceTest.java

 

Code metrics

0
61
9
1
171
117
9
0.15
6.78
9
1

Classes

Class Line # Actions
CompositeConfigurationSourceTest 43 61 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.configuration.internal;
21   
22    import java.util.ArrayList;
23    import java.util.Arrays;
24    import java.util.List;
25    import java.util.Properties;
26   
27    import org.junit.Assert;
28   
29    import org.apache.commons.configuration.BaseConfiguration;
30    import org.apache.commons.configuration.Configuration;
31    import org.junit.Before;
32    import org.junit.Test;
33    import org.xwiki.component.util.ReflectionUtils;
34    import org.xwiki.properties.ConverterManager;
35    import org.xwiki.test.jmock.AbstractComponentTestCase;
36   
37    /**
38    * Unit tests for {@link CompositeConfigurationSource}.
39    *
40    * @version $Id: 8a7e39aa0a815abb072ddf3a482e695ac537c16c $
41    * @since 2.0M1
42    */
 
43    public class CompositeConfigurationSourceTest extends AbstractComponentTestCase
44    {
45    private CompositeConfigurationSource composite;
46   
47    private Configuration config1;
48   
49    private Configuration config2;
50   
 
51  8 toggle @Override
52    @Before
53    public void setUp() throws Exception
54    {
55  8 super.setUp();
56  8 this.composite = new CompositeConfigurationSource();
57  8 ConverterManager converterManager = getComponentManager().getInstance(ConverterManager.class);
58   
59  8 CommonsConfigurationSource source1 = new CommonsConfigurationSource();
60  8 ReflectionUtils.setFieldValue(source1, "converterManager", converterManager);
61  8 this.config1 = new BaseConfiguration();
62  8 source1.setConfiguration(this.config1);
63  8 this.composite.addConfigurationSource(source1);
64   
65  8 CommonsConfigurationSource source2 = new CommonsConfigurationSource();
66  8 ReflectionUtils.setFieldValue(source2, "converterManager", converterManager);
67  8 this.config2 = new BaseConfiguration();
68  8 source2.setConfiguration(this.config2);
69  8 this.composite.addConfigurationSource(source2);
70    }
71   
 
72  1 toggle @Test
73    public void testContainsKey()
74    {
75  1 config1.setProperty("key1", "value1");
76  1 config1.setProperty("key3", "value3");
77  1 config2.setProperty("key2", "value2");
78  1 config2.setProperty("key3", "value3");
79   
80  1 Assert.assertTrue(composite.containsKey("key1"));
81  1 Assert.assertTrue(composite.containsKey("key2"));
82  1 Assert.assertTrue(composite.containsKey("key3"));
83  1 Assert.assertFalse(composite.containsKey("unknown"));
84    }
85   
 
86  1 toggle @Test
87    public void testGetProperty()
88    {
89  1 config1.setProperty("key1", "value1");
90  1 config1.setProperty("key3", "value3");
91  1 config2.setProperty("key2", "value2");
92  1 config2.setProperty("key3", "value3");
93   
94  1 Assert.assertEquals("value1", composite.getProperty("key1"));
95  1 Assert.assertEquals("value2", composite.getProperty("key2"));
96  1 Assert.assertEquals("value3", composite.getProperty("key3"));
97  1 Assert.assertNull(composite.getProperty("unknown"));
98    }
99   
 
100  1 toggle @Test
101    public void testGetPropertyWithClass()
102    {
103  1 config1.setProperty("key1", "value1");
104  1 config1.setProperty("key3", "value3");
105  1 config2.setProperty("key2", "value2");
106  1 config2.setProperty("key3", "value3");
107   
108  1 Assert.assertEquals("value1", composite.getProperty("key1", String.class));
109  1 Assert.assertEquals("value2", composite.getProperty("key2", String.class));
110  1 Assert.assertEquals("value3", composite.getProperty("key3", String.class));
111  1 Assert.assertNull(composite.getProperty("unknown", String.class));
112    }
113   
 
114  1 toggle @Test
115    public void testGetPropertyWithDefaultValue()
116    {
117  1 config1.setProperty("key1", "value1");
118  1 config1.setProperty("key3", "value3");
119  1 config2.setProperty("key2", "value2");
120  1 config2.setProperty("key3", "value3");
121   
122  1 Assert.assertEquals("value1", composite.getProperty("key1", "default"));
123  1 Assert.assertEquals("value2", composite.getProperty("key2", "default"));
124  1 Assert.assertEquals("value3", composite.getProperty("key3", "default"));
125  1 Assert.assertEquals("default", composite.getProperty("unknown", "default"));
126    }
127   
 
128  1 toggle @Test
129    public void testGetKeys()
130    {
131  1 config1.setProperty("key1", "value1");
132  1 config2.setProperty("key2", "value2");
133   
134  1 List<String> expected = Arrays.asList("key1", "key2");
135  1 Assert.assertEquals(expected, composite.getKeys());
136    }
137   
 
138  1 toggle @Test
139    public void testIsEmpty()
140    {
141  1 Assert.assertTrue(composite.isEmpty());
142   
143  1 config2.setProperty("key", "value");
144  1 Assert.assertFalse(composite.isEmpty());
145    }
146   
 
147  1 toggle @Test
148    public void testGetPropertiesAndListsWhenEmpty()
149    {
150  1 Assert.assertTrue(composite.getProperty("unknown", Properties.class).isEmpty());
151  1 Assert.assertTrue(composite.getProperty("unknown", List.class).isEmpty());
152    }
153   
 
154  1 toggle @Test
155    public void testTypeConversionsWhenDefaultValuesAreNotUsed()
156    {
157  1 config1.setProperty("key1", "true");
158  1 config1.setProperty("key2", "item1,item2");
159  1 config1.setProperty("key3", "prop1=value1,prop2=value2");
160   
161    // Default value is not used since the property exists and is converted to boolean automatically
162  1 Assert.assertTrue(composite.getProperty("key1", false));
163   
164    // Default value is not used since the property exists and is converted to List automatically
165  1 Assert.assertEquals(Arrays.asList("item1", "item2"), composite.getProperty("key2", new ArrayList<String>()));
166   
167    // Default value is not used since the property exists and is converted to Properties automatically
168  1 Properties props = composite.getProperty("key3", new Properties());
169  1 Assert.assertEquals("value1", props.getProperty("prop1"));
170    }
171    }