1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
39 |
|
|
40 |
|
@version |
41 |
|
@since |
42 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (70) |
Complexity: 9 |
Complexity Density: 0.15 |
|
43 |
|
public class CompositeConfigurationSourceTest extends AbstractComponentTestCase |
44 |
|
{ |
45 |
|
private CompositeConfigurationSource composite; |
46 |
|
|
47 |
|
private Configuration config1; |
48 |
|
|
49 |
|
private Configuration config2; |
50 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
51 |
8 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
72 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
86 |
1 |
@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% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
100 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
114 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
128 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
138 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
147 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
154 |
1 |
@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 |
|
|
162 |
1 |
Assert.assertTrue(composite.getProperty("key1", false)); |
163 |
|
|
164 |
|
|
165 |
1 |
Assert.assertEquals(Arrays.asList("item1", "item2"), composite.getProperty("key2", new ArrayList<String>())); |
166 |
|
|
167 |
|
|
168 |
1 |
Properties props = composite.getProperty("key3", new Properties()); |
169 |
1 |
Assert.assertEquals("value1", props.getProperty("prop1")); |
170 |
|
} |
171 |
|
} |