1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.configuration.internal

File CompositeConfigurationSourceTest.java

 

Code metrics

0
56
9
1
160
111
9
0.16
6.22
9
1

Classes

Class Line # Actions
CompositeConfigurationSourceTest 41 56 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.Arrays;
23    import java.util.List;
24    import java.util.Properties;
25   
26    import org.apache.commons.configuration2.Configuration;
27    import org.apache.commons.configuration2.PropertiesConfiguration;
28    import org.junit.Assert;
29    import org.junit.Before;
30    import org.junit.Test;
31    import org.xwiki.component.util.ReflectionUtils;
32    import org.xwiki.properties.ConverterManager;
33    import org.xwiki.test.jmock.AbstractComponentTestCase;
34   
35    /**
36    * Unit tests for {@link CompositeConfigurationSource}.
37    *
38    * @version $Id: def946e030c720f609e1284fc33cbbe8e0782011 $
39    * @since 2.0M1
40    */
 
41    public class CompositeConfigurationSourceTest extends AbstractComponentTestCase
42    {
43    private CompositeConfigurationSource composite;
44   
45    private Configuration config1;
46   
47    private Configuration config2;
48   
 
49  8 toggle @Override
50    @Before
51    public void setUp() throws Exception
52    {
53  8 super.setUp();
54  8 this.composite = new CompositeConfigurationSource();
55  8 ConverterManager converterManager = getComponentManager().getInstance(ConverterManager.class);
56   
57  8 CommonsConfigurationSource source1 = new CommonsConfigurationSource();
58  8 ReflectionUtils.setFieldValue(source1, "converterManager", converterManager);
59  8 this.config1 = new PropertiesConfiguration();
60  8 source1.setConfiguration(this.config1);
61  8 this.composite.addConfigurationSource(source1);
62   
63  8 CommonsConfigurationSource source2 = new CommonsConfigurationSource();
64  8 ReflectionUtils.setFieldValue(source2, "converterManager", converterManager);
65  8 this.config2 = new PropertiesConfiguration();
66  8 source2.setConfiguration(this.config2);
67  8 this.composite.addConfigurationSource(source2);
68    }
69   
 
70  1 toggle @Test
71    public void testContainsKey()
72    {
73  1 config1.setProperty("key1", "value1");
74  1 config1.setProperty("key3", "value3");
75  1 config2.setProperty("key2", "value2");
76  1 config2.setProperty("key3", "value3");
77   
78  1 Assert.assertTrue(composite.containsKey("key1"));
79  1 Assert.assertTrue(composite.containsKey("key2"));
80  1 Assert.assertTrue(composite.containsKey("key3"));
81  1 Assert.assertFalse(composite.containsKey("unknown"));
82    }
83   
 
84  1 toggle @Test
85    public void testGetProperty()
86    {
87  1 config1.setProperty("key1", "value1");
88  1 config1.setProperty("key3", "value3");
89  1 config2.setProperty("key2", "value2");
90  1 config2.setProperty("key3", "value3");
91   
92  1 Assert.assertEquals("value1", composite.getProperty("key1"));
93  1 Assert.assertEquals("value2", composite.getProperty("key2"));
94  1 Assert.assertEquals("value3", composite.getProperty("key3"));
95  1 Assert.assertNull(composite.getProperty("unknown"));
96    }
97   
 
98  1 toggle @Test
99    public void testGetPropertyWithClass()
100    {
101  1 config1.setProperty("key1", "value1");
102  1 config1.setProperty("key3", "value3");
103  1 config2.setProperty("key2", "value2");
104  1 config2.setProperty("key3", "value3");
105   
106  1 Assert.assertEquals("value1", composite.getProperty("key1", String.class));
107  1 Assert.assertEquals("value2", composite.getProperty("key2", String.class));
108  1 Assert.assertEquals("value3", composite.getProperty("key3", String.class));
109  1 Assert.assertNull(composite.getProperty("unknown", String.class));
110    }
111   
 
112  1 toggle @Test
113    public void testGetPropertyWithDefaultValue()
114    {
115  1 config1.setProperty("key1", "value1");
116  1 config1.setProperty("key3", "value3");
117  1 config2.setProperty("key2", "value2");
118  1 config2.setProperty("key3", "value3");
119   
120  1 Assert.assertEquals("value1", composite.getProperty("key1", "default"));
121  1 Assert.assertEquals("value2", composite.getProperty("key2", "default"));
122  1 Assert.assertEquals("value3", composite.getProperty("key3", "default"));
123  1 Assert.assertEquals("default", composite.getProperty("unknown", "default"));
124    }
125   
 
126  1 toggle @Test
127    public void testGetKeys()
128    {
129  1 config1.setProperty("key1", "value1");
130  1 config2.setProperty("key2", "value2");
131   
132  1 List<String> expected = Arrays.asList("key1", "key2");
133  1 Assert.assertEquals(expected, composite.getKeys());
134    }
135   
 
136  1 toggle @Test
137    public void testIsEmpty()
138    {
139  1 Assert.assertTrue(composite.isEmpty());
140   
141  1 config2.setProperty("key", "value");
142  1 Assert.assertFalse(composite.isEmpty());
143    }
144   
 
145  1 toggle @Test
146    public void testGetPropertiesAndListsWhenEmpty()
147    {
148  1 Assert.assertTrue(composite.getProperty("unknown", Properties.class).isEmpty());
149  1 Assert.assertTrue(composite.getProperty("unknown", List.class).isEmpty());
150    }
151   
 
152  1 toggle @Test
153    public void testTypeConversionsWhenDefaultValuesAreNotUsed()
154    {
155  1 config1.setProperty("key1", "true");
156   
157    // Default value is not used since the property exists and is converted to boolean automatically
158  1 Assert.assertTrue(composite.getProperty("key1", false));
159    }
160    }