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

File CommonsConfigurationSourceTest.java

 

Code metrics

0
47
11
1
168
111
11
0.23
4.27
11
1

Classes

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