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

File CommonsConfigurationSourceTest.java

 

Code metrics

0
47
11
1
167
110
11
0.23
4.27
11
1

Classes

Class Line # Actions
CommonsConfigurationSourceTest 42 47 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 10 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.configuration.BaseConfiguration;
27    import org.apache.commons.configuration.Configuration;
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.configuration.ConversionException;
33    import org.xwiki.properties.ConverterManager;
34    import org.xwiki.test.jmock.AbstractComponentTestCase;
35   
36    /**
37    * Unit tests for {@link CommonsConfigurationSource}.
38    *
39    * @version $Id: 399fe8f3a5800c0848a859235fed8a271214c759 $
40    * @since 2.0M1
41    */
 
42    public class CommonsConfigurationSourceTest extends AbstractComponentTestCase
43    {
44    private Configuration configuration;
45   
46    private CommonsConfigurationSource source;
47   
 
48  10 toggle @Override
49    @Before
50    public void setUp() throws Exception
51    {
52  10 super.setUp();
53   
54  10 this.source = new CommonsConfigurationSource();
55  10 ConverterManager converterManager = getComponentManager().getInstance(ConverterManager.class);
56  10 ReflectionUtils.setFieldValue(source, "converterManager", converterManager);
57  10 this.configuration = new BaseConfiguration();
58  10 this.source.setConfiguration(this.configuration);
59    }
60   
 
61  1 toggle @Test
62    public void testDefaultValue()
63    {
64  1 configuration.setProperty("string", "value");
65   
66  1 Assert.assertEquals("default", source.getProperty("unknown", "default"));
67  1 Assert.assertEquals("value", source.getProperty("string", "default"));
68  1 Assert.assertEquals(null, source.getProperty("unknown", (String) null));
69    }
70   
 
71  1 toggle @Test
72    public void testStringProperty()
73    {
74  1 configuration.setProperty("string", "value");
75   
76  1 Assert.assertEquals("value", source.getProperty("string"));
77  1 Assert.assertEquals("value", source.getProperty("string", String.class));
78   
79  1 Assert.assertNull(source.getProperty("unknown"));
80  1 Assert.assertNull(source.getProperty("unknown", String.class));
81    }
82   
 
83  1 toggle @Test(expected = ConversionException.class)
84    public void testStringPropertyWhenConversionError()
85    {
86  1 configuration.setProperty("string", "value");
87   
88    // Try to retrieve a String property as a Boolean
89  1 source.getProperty("string", Boolean.class);
90    }
91   
 
92  1 toggle @Test(expected = ConversionException.class)
93    public void testBooleanPropertyWhenConversionError()
94    {
95  1 configuration.setProperty("boolean", true);
96   
97    // Try to retrieve a Boolean property as a String
98  1 source.getProperty("boolean", String.class);
99    }
100   
 
101  1 toggle @Test
102    public void testBooleanProperty()
103    {
104    // Test boolean value
105  1 configuration.setProperty("boolean", true);
106   
107  1 Assert.assertEquals(true, source.getProperty("boolean"));
108  1 Assert.assertEquals(true, source.getProperty("boolean", Boolean.class));
109  1 Assert.assertEquals(true, source.getProperty("unknown", true));
110  1 Assert.assertEquals(false, source.getProperty("unknown", false));
111    }
112   
 
113  1 toggle @Test
114    public void testUnknownBooleanProperty()
115    {
116  1 Assert.assertNull(source.getProperty("unknown", Boolean.class));
117    }
118   
 
119  1 toggle @Test
120    public void testListProperty()
121    {
122  1 configuration.setProperty("list", "value1");
123  1 configuration.addProperty("list", "value2");
124  1 List<String> expected = Arrays.asList("value1", "value2");
125   
126  1 Assert.assertEquals(expected, source.getProperty("list"));
127  1 Assert.assertEquals(expected, source.getProperty("list", List.class));
128   
129  1 Assert.assertTrue(source.getProperty("unknown", List.class).isEmpty());
130  1 Assert.assertEquals(Arrays.asList("toto"), source.getProperty("unknown", Arrays.asList("toto")));
131    }
132   
 
133  1 toggle @Test
134    public void testListPropertyWhenArrayList()
135    {
136  1 configuration.setProperty("list", "value");
137  1 List<String> expected = Arrays.asList("value");
138   
139  1 Assert.assertEquals(expected, source.getProperty("list", Arrays.asList("default")));
140    }
141   
 
142  1 toggle @Test
143    public void testPropertiesProperty()
144    {
145  1 configuration.setProperty("properties", "key1=value1");
146  1 configuration.addProperty("properties", "key2=value2");
147  1 List<String> expectedList = Arrays.asList("key1=value1", "key2=value2");
148  1 Properties expectedProperties = new Properties();
149  1 expectedProperties.put("key1", "value1");
150  1 expectedProperties.put("key2", "value2");
151   
152  1 Assert.assertEquals(expectedList, source.getProperty("properties"));
153  1 Assert.assertEquals(expectedProperties, source.getProperty("properties", Properties.class));
154   
155  1 Assert.assertTrue(source.getProperty("unknown", Properties.class).isEmpty());
156    }
157   
 
158  1 toggle @Test
159    public void testIsEmpty()
160    {
161  1 Assert.assertTrue(configuration.isEmpty());
162   
163  1 configuration.addProperty("properties", "key2=value2");
164   
165  1 Assert.assertFalse(configuration.isEmpty());
166    }
167    }