1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration2;
18
19 import static org.hamcrest.CoreMatchers.containsString;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23
24 import java.io.StringReader;
25 import java.io.StringWriter;
26 import java.util.Arrays;
27 import java.util.List;
28
29 import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
30 import org.apache.commons.configuration2.convert.DisabledListDelimiterHandler;
31 import org.apache.commons.configuration2.ex.ConfigurationException;
32 import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;
33 import org.apache.commons.configuration2.io.FileHandler;
34 import org.apache.commons.lang3.StringUtils;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37
38
39
40
41 public class TestXMLListHandling {
42
43 private static final String SOURCE = "<config>" + "<values>a,b,c</values>" + "<split><value>1</value><value>2</value></split>"
44 + "<mixed><values>foo,blah</values><values>bar,baz</values></mixed>" + "</config>";
45
46
47 private static final String KEY_VALUES = "values";
48
49
50 private static final String KEY_SPLIT = "split.value";
51
52
53 private static final String ELEM_SPLIT = "value";
54
55
56
57
58
59
60
61
62 private static void checkCommaSeparated(final String xml, final String key, final String... values) {
63 final String strValues = StringUtils.join(values, ',');
64 final String element = element(key, strValues);
65 assertThat(xml, containsString(element));
66 }
67
68
69
70
71
72
73
74
75 private static void checkSplit(final String xml, final String key, final String... values) {
76 for (final String v : values) {
77 assertThat(xml, containsString(element(key, v)));
78 }
79 }
80
81
82
83
84
85
86
87
88 private static String element(final String key, final String value) {
89 return "<" + key + '>' + value + "</" + key + '>';
90 }
91
92
93
94
95
96
97
98 private static XMLConfiguration readFromString(final String xml) throws ConfigurationException {
99 final XMLConfiguration config = new XMLConfiguration();
100 config.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
101 final FileHandler handler = new FileHandler(config);
102 handler.load(new StringReader(xml));
103 return config;
104 }
105
106
107 private XMLConfiguration config;
108
109
110
111
112
113
114 private String saveToString() throws ConfigurationException {
115 final StringWriter writer = new StringWriter(4096);
116 final FileHandler handler = new FileHandler(config);
117 handler.save(writer);
118 return writer.toString();
119 }
120
121 @BeforeEach
122 public void setUp() throws Exception {
123 config = readFromString(SOURCE);
124 }
125
126
127
128
129 @Test
130 public void testAddListItem() throws ConfigurationException {
131 config.addProperty(KEY_VALUES, "d");
132 config.addProperty(KEY_SPLIT, "3");
133 final String xml = saveToString();
134
135 checkSplit(xml, ELEM_SPLIT, "1", "2", "3");
136 checkCommaSeparated(xml, KEY_VALUES, "a", "b", "c", "d");
137 }
138
139
140
141
142
143 @Test
144 public void testIncompatibleListDelimiterOnSaving() {
145 config.setListDelimiterHandler(DisabledListDelimiterHandler.INSTANCE);
146 assertThrows(ConfigurationRuntimeException.class, this::saveToString);
147 }
148
149
150
151
152 @Test
153 public void testMixedList() throws ConfigurationException {
154 final List<String> expected = Arrays.asList("foo", "blah", "bar", "baz");
155 assertEquals(expected, config.getList("mixed.values"));
156 final String xml = saveToString();
157
158 final XMLConfiguration c2 = readFromString(xml);
159 assertEquals(expected, c2.getList("mixed.values"));
160 }
161
162
163
164
165 @Test
166 public void testRemoveListItem() throws ConfigurationException {
167 config.clearProperty(KEY_VALUES + "(2)");
168 config.clearProperty(KEY_SPLIT + "(1)");
169 final String xml = saveToString();
170
171 checkSplit(xml, ELEM_SPLIT, "1");
172 checkCommaSeparated(xml, KEY_VALUES, "a", "b");
173 }
174
175
176
177
178 @Test
179 public void testSaveNoChanges() throws ConfigurationException {
180 final String xml = saveToString();
181
182 checkSplit(xml, ELEM_SPLIT, "1", "2");
183 checkCommaSeparated(xml, KEY_VALUES, "a", "b", "c");
184 }
185 }