1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration2;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
22 import static org.junit.jupiter.api.Assertions.assertNotNull;
23
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.io.StringWriter;
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.commons.configuration2.ex.ConfigurationException;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Disabled;
34 import org.junit.jupiter.api.Test;
35
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.fasterxml.jackson.databind.type.MapType;
38
39
40
41
42
43 public class TestJSONConfiguration {
44
45
46 private final String testJson = ConfigurationAssert.getTestFile("test.json").getAbsolutePath();
47
48 private JSONConfiguration jsonConfiguration;
49
50 @BeforeEach
51 public void setUp() throws Exception {
52 jsonConfiguration = new JSONConfiguration();
53 jsonConfiguration.read(new FileReader(testJson));
54 }
55
56 @Test
57 public void testCopyConstructor() {
58 final BaseHierarchicalConfiguration c = new BaseHierarchicalConfiguration();
59 c.addProperty("foo", "bar");
60
61 jsonConfiguration = new JSONConfiguration(c);
62 assertEquals("bar", jsonConfiguration.getString("foo"));
63 }
64
65
66
67
68 public void testGetListNestedWithList() {
69 assertEquals(Arrays.asList("col1", "col2"), jsonConfiguration.getList(String.class, "key4.key5"));
70 }
71
72 @Test
73 public void testGetPropertyDictionary() {
74 assertEquals("Martin D'vloper", jsonConfiguration.getProperty("martin.name"));
75 assertEquals("Developer", jsonConfiguration.getProperty("martin.job"));
76 assertEquals("Elite", jsonConfiguration.getProperty("martin.skill"));
77 }
78
79 @Test
80 public void testGetPropertyDictionaryInList() {
81 assertEquals("UK", jsonConfiguration.getString("capitals(1).country"));
82 assertEquals("Washington", jsonConfiguration.getString("capitals(0).capital"));
83 }
84
85 @Test
86 public void testGetPropertyInteger() {
87 final Object property = jsonConfiguration.getProperty("int1");
88 assertInstanceOf(Integer.class, property);
89 assertEquals(37, property);
90 }
91
92 @Test
93 public void testGetPropertyNested() {
94 assertEquals("value23", jsonConfiguration.getProperty("key2.key3"));
95 }
96
97 @Test
98 public void testGetPropertyNestedWithList() {
99 assertEquals(Arrays.asList("col1", "col2"), jsonConfiguration.getProperty("key4.key5"));
100 }
101
102 @Test
103 public void testGetPropertySimple() {
104 assertEquals("value1", jsonConfiguration.getProperty("key1"));
105 }
106
107 @Test
108 public void testGetPropertySubset() {
109 final Configuration subset = jsonConfiguration.subset("key4");
110 assertEquals(Arrays.asList("col1", "col2"), subset.getProperty("key5"));
111 }
112
113 @Test
114 public void testGetPropertyVeryNestedProperties() {
115 final Object property = jsonConfiguration.getProperty("very.nested.properties");
116 assertEquals(Arrays.asList("nested1", "nested2", "nested3"), property);
117 }
118
119
120
121
122 @Disabled
123 @Test
124 public void testListOfObjects() {
125 final Configuration subset = jsonConfiguration.subset("capitals");
126 assertNotNull(subset);
127 assertEquals(2, subset.size());
128
129 final List<Object> list = jsonConfiguration.getList("capitals");
130 assertNotNull(list);
131 assertEquals(2, list.size());
132
133
134
135
136
137 }
138
139 @Test
140 public void testSave() throws IOException, ConfigurationException {
141
142 final StringWriter sw = new StringWriter();
143 jsonConfiguration.write(sw);
144 final String output = sw.toString();
145
146
147 final ObjectMapper mapper = new ObjectMapper();
148 final MapType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
149 final Map<String, Object> parsed = mapper.readValue(output, type);
150 assertEquals(7, parsed.entrySet().size());
151 assertEquals("value1", parsed.get("key1"));
152
153 final Map<?, ?> key2 = (Map<?, ?>) parsed.get("key2");
154 assertEquals("value23", key2.get("key3"));
155
156 final List<?> key5 = (List<?>) ((Map<?, ?>) parsed.get("key4")).get("key5");
157 assertEquals(Arrays.asList("col1", "col2"), key5);
158
159 final List<?> capitals = (List<?>) parsed.get("capitals");
160 final Map<?, ?> capUk = (Map<?, ?>) capitals.get(1);
161 assertEquals("London", capUk.get("capital"));
162 }
163 }