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 private final String testJson = ConfigurationAssert.getTestFile("test.json").getAbsolutePath();
46
47 private JSONConfiguration jsonConfiguration;
48
49 @BeforeEach
50 public void setUp() throws Exception {
51 jsonConfiguration = new JSONConfiguration();
52 jsonConfiguration.read(new FileReader(testJson));
53 }
54
55 @Test
56 public void testCopyConstructor() {
57 final BaseHierarchicalConfiguration c = new BaseHierarchicalConfiguration();
58 c.addProperty("foo", "bar");
59
60 jsonConfiguration = new JSONConfiguration(c);
61 assertEquals("bar", jsonConfiguration.getString("foo"));
62 }
63
64
65
66
67 public void testGetList_nested_with_list() {
68 assertEquals(Arrays.asList("col1", "col2"), jsonConfiguration.getList(String.class, "key4.key5"));
69 }
70
71 @Test
72 public void testGetProperty_dictionary() {
73 assertEquals("Martin D'vloper", jsonConfiguration.getProperty("martin.name"));
74 assertEquals("Developer", jsonConfiguration.getProperty("martin.job"));
75 assertEquals("Elite", jsonConfiguration.getProperty("martin.skill"));
76 }
77
78 @Test
79 public void testGetProperty_dictionaryInList() {
80 assertEquals("UK", jsonConfiguration.getString("capitals(1).country"));
81 assertEquals("Washington", jsonConfiguration.getString("capitals(0).capital"));
82 }
83
84 @Test
85 public void testGetProperty_integer() {
86 final Object property = jsonConfiguration.getProperty("int1");
87 assertInstanceOf(Integer.class, property);
88 assertEquals(37, property);
89 }
90
91 @Test
92 public void testGetProperty_nested() {
93 assertEquals("value23", jsonConfiguration.getProperty("key2.key3"));
94 }
95
96 @Test
97 public void testGetProperty_nested_with_list() {
98 assertEquals(Arrays.asList("col1", "col2"), jsonConfiguration.getProperty("key4.key5"));
99 }
100
101 @Test
102 public void testGetProperty_simple() {
103 assertEquals("value1", jsonConfiguration.getProperty("key1"));
104 }
105
106 @Test
107 public void testGetProperty_subset() {
108 final Configuration subset = jsonConfiguration.subset("key4");
109 assertEquals(Arrays.asList("col1", "col2"), subset.getProperty("key5"));
110 }
111
112 @Test
113 public void testGetProperty_very_nested_properties() {
114 final Object property = jsonConfiguration.getProperty("very.nested.properties");
115 assertEquals(Arrays.asList("nested1", "nested2", "nested3"), property);
116 }
117
118
119
120
121 @Disabled
122 @Test
123 public void testListOfObjects() {
124 final Configuration subset = jsonConfiguration.subset("capitals");
125 assertNotNull(subset);
126 assertEquals(2, subset.size());
127
128 final List<Object> list = jsonConfiguration.getList("capitals");
129 assertNotNull(list);
130 assertEquals(2, list.size());
131
132
133
134
135
136 }
137
138 @Test
139 public void testSave() throws IOException, ConfigurationException {
140
141 final StringWriter sw = new StringWriter();
142 jsonConfiguration.write(sw);
143 final String output = sw.toString();
144
145
146 final ObjectMapper mapper = new ObjectMapper();
147 final MapType type = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
148 final Map<String, Object> parsed = mapper.readValue(output, type);
149 assertEquals(7, parsed.entrySet().size());
150 assertEquals("value1", parsed.get("key1"));
151
152 final Map<?, ?> key2 = (Map<? , ?>) parsed.get("key2");
153 assertEquals("value23", key2.get("key3"));
154
155 final List<?> key5 = (List<?>) ((Map<?, ?>) parsed.get("key4")).get("key5");
156 assertEquals(Arrays.asList("col1", "col2"), key5);
157
158 final List<?> capitals = (List<?>) parsed.get("capitals");
159 final Map<?, ?> capUk = (Map<?, ?>) capitals.get(1);
160 assertEquals("London", capUk.get("capital"));
161 }
162 }