1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertSame;
23 import static org.junit.Assert.assertTrue;
24
25 import java.lang.annotation.ElementType;
26 import java.math.BigDecimal;
27 import java.util.Iterator;
28 import java.util.List;
29
30 import org.junit.Test;
31
32
33
34
35
36
37
38 public class TestPropertyConverter
39 {
40
41 private static final Class<ElementType> ENUM_CLASS = ElementType.class;
42
43 @Test
44 public void testSplit()
45 {
46 String s = "abc, xyz , 123";
47 List<String> list = PropertyConverter.split(s, ',');
48
49 assertEquals("size", 3, list.size());
50 assertEquals("1st token for '" + s + "'", "abc", list.get(0));
51 assertEquals("2nd token for '" + s + "'", "xyz", list.get(1));
52 assertEquals("3rd token for '" + s + "'", "123", list.get(2));
53 }
54
55 @Test
56 public void testSplitNoTrim()
57 {
58 String s = "abc, xyz , 123";
59 List<String> list = PropertyConverter.split(s, ',', false);
60
61 assertEquals("size", 3, list.size());
62 assertEquals("1st token for '" + s + "'", "abc", list.get(0));
63 assertEquals("2nd token for '" + s + "'", " xyz ", list.get(1));
64 assertEquals("3rd token for '" + s + "'", " 123", list.get(2));
65 }
66
67 @Test
68 public void testSplitWithEscapedSeparator()
69 {
70 String s = "abc\\,xyz, 123";
71 List<String> list = PropertyConverter.split(s, ',');
72
73 assertEquals("size", 2, list.size());
74 assertEquals("1st token for '" + s + "'", "abc,xyz", list.get(0));
75 assertEquals("2nd token for '" + s + "'", "123", list.get(1));
76 }
77
78 @Test
79 public void testSplitEmptyValues()
80 {
81 String s = ",,";
82 List<String> list = PropertyConverter.split(s, ',');
83
84 assertEquals("size", 3, list.size());
85 assertEquals("1st token for '" + s + "'", "", list.get(0));
86 assertEquals("2nd token for '" + s + "'", "", list.get(1));
87 assertEquals("3rd token for '" + s + "'", "", list.get(2));
88 }
89
90 @Test
91 public void testSplitWithEndingSlash()
92 {
93 String s = "abc, xyz\\";
94 List<String> list = PropertyConverter.split(s, ',');
95
96 assertEquals("size", 2, list.size());
97 assertEquals("1st token for '" + s + "'", "abc", list.get(0));
98 assertEquals("2nd token for '" + s + "'", "xyz\\", list.get(1));
99 }
100
101 @Test
102 public void testSplitNull()
103 {
104 List<String> list = PropertyConverter.split(null, ',');
105 assertNotNull(list);
106 assertTrue(list.isEmpty());
107 }
108
109
110
111
112 @Test
113 public void testSplitEscapeEscapeChar()
114 {
115 List<String> list = PropertyConverter.split("C:\\Temp\\\\,xyz", ',');
116 assertEquals("Wrong list size", 2, list.size());
117 assertEquals("Wrong element 1", "C:\\Temp\\", list.get(0));
118 assertEquals("Wrong element 2", "xyz", list.get(1));
119 }
120
121
122
123
124 @Test
125 public void testEscapeDelimiters()
126 {
127 assertEquals("Wrong escaped delimiters",
128 "C:\\\\Temp\\\\\\,D:\\\\Data\\\\", PropertyConverter
129 .escapeDelimiters("C:\\Temp\\,D:\\Data\\", ','));
130 }
131
132
133
134
135 @Test
136 public void testEscapeListDelimiter()
137 {
138 assertEquals("Wrong escaped list delimiter", "C:\\Temp\\\\,D:\\Data\\",
139 PropertyConverter.escapeListDelimiter("C:\\Temp\\,D:\\Data\\",
140 ','));
141 }
142
143 @Test
144 public void testToIterator()
145 {
146 int[] array = new int[]{1, 2, 3};
147
148 Iterator<?> it = PropertyConverter.toIterator(array, ',');
149
150 assertEquals("1st element", new Integer(1), it.next());
151 assertEquals("2nd element", new Integer(2), it.next());
152 assertEquals("3rd element", new Integer(3), it.next());
153 }
154
155
156
157
158 @Test
159 public void testInterpolateString()
160 {
161 PropertiesConfiguration config = new PropertiesConfiguration();
162 config.addProperty("animal", "quick brown fox");
163 config.addProperty("target", "lazy dog");
164 assertEquals("Wrong interpolation",
165 "The quick brown fox jumps over the lazy dog.",
166 PropertyConverter.interpolate("The ${animal} jumps over the ${target}.", config));
167 }
168
169
170
171
172 @Test
173 public void testInterpolateObject()
174 {
175 assertEquals("Object was not correctly interpolated", new Integer(42),
176 PropertyConverter.interpolate(new Integer(42), new PropertiesConfiguration()));
177 }
178
179
180
181
182
183 @Test
184 public void testInterpolateRecursive()
185 {
186 PropertiesConfiguration config = new PropertiesConfiguration();
187 config.addProperty("animal", "${animal_attr} fox");
188 config.addProperty("target", "${target_attr} dog");
189 config.addProperty("animal_attr", "quick brown");
190 config.addProperty("target_attr", "lazy");
191 assertEquals("Wrong complex interpolation",
192 "The quick brown fox jumps over the lazy dog.",
193 PropertyConverter.interpolate("The ${animal} jumps over the ${target}.", config));
194 }
195
196
197
198
199
200 @Test(expected = IllegalStateException.class)
201 public void testCyclicInterpolation()
202 {
203 PropertiesConfiguration config = new PropertiesConfiguration();
204 config.addProperty("animal", "${animal_attr} ${species}");
205 config.addProperty("animal_attr", "quick brown");
206 config.addProperty("species", "${animal}");
207 PropertyConverter.interpolate("This is a ${animal}", config);
208 }
209
210
211
212
213
214 @Test
215 public void testInterpolationUnknownVariable()
216 {
217 PropertiesConfiguration config = new PropertiesConfiguration();
218 config.addProperty("animal", "quick brown fox");
219 assertEquals("Wrong interpolation",
220 "The quick brown fox jumps over ${target}.",
221 PropertyConverter.interpolate("The ${animal} jumps over ${target}.", config));
222 }
223
224
225
226
227
228 @Test
229 public void testToNumberDirect()
230 {
231 Integer i = new Integer(42);
232 assertSame("Wrong integer", i, PropertyConverter.toNumber(i, Integer.class));
233 BigDecimal d = new BigDecimal("3.1415");
234 assertSame("Wrong BigDecimal", d, PropertyConverter.toNumber(d, Integer.class));
235 }
236
237
238
239
240
241 @Test
242 public void testToNumberFromString()
243 {
244 assertEquals("Incorrect Integer value", new Integer(42), PropertyConverter.toNumber("42", Integer.class));
245 assertEquals("Incorrect Short value", new Short((short) 10), PropertyConverter.toNumber(new StringBuffer("10"), Short.class));
246 }
247
248
249
250
251
252 @Test
253 public void testToNumberFromHexString()
254 {
255 Number n = PropertyConverter.toNumber("0x10", Integer.class);
256 assertEquals("Incorrect Integer value", 16, n.intValue());
257 }
258
259
260
261
262
263 @Test(expected = ConversionException.class)
264 public void testToNumberFromInvalidHexString()
265 {
266 PropertyConverter.toNumber("0xNotAHexValue", Integer.class);
267 }
268
269
270
271
272
273 @Test
274 public void testToNumberFromBinaryString()
275 {
276 Number n = PropertyConverter.toNumber("0b1111", Integer.class);
277 assertEquals("Incorrect Integer value", 15, n.intValue());
278 }
279
280
281
282
283
284 @Test(expected = ConversionException.class)
285 public void testToNumberFromInvalidBinaryString()
286 {
287 PropertyConverter.toNumber("0bNotABinValue", Integer.class);
288 }
289
290
291
292
293
294 @Test(expected = ConversionException.class)
295 public void testToNumberFromInvalidString()
296 {
297 PropertyConverter.toNumber("Not a number", Byte.class);
298 }
299
300
301
302
303
304 @Test(expected = ConversionException.class)
305 public void testToNumberWithInvalidClass()
306 {
307 PropertyConverter.toNumber("42", Object.class);
308 }
309
310 @Test
311 public void testToEnumFromEnum()
312 {
313 assertEquals(ElementType.METHOD, PropertyConverter.toEnum(ElementType.METHOD, ENUM_CLASS));
314 }
315
316 @Test
317 public void testToEnumFromString()
318 {
319 assertEquals(ElementType.METHOD, PropertyConverter.toEnum("METHOD", ENUM_CLASS));
320 }
321
322 @Test(expected = ConversionException.class)
323 public void testToEnumFromInvalidString()
324 {
325 PropertyConverter.toEnum("FOO", ENUM_CLASS);
326 }
327
328 @Test
329 public void testToEnumFromNumber()
330 {
331 assertEquals(ElementType.METHOD, PropertyConverter.toEnum(
332 Integer.valueOf(ElementType.METHOD.ordinal()),
333 ENUM_CLASS));
334 }
335
336 @Test(expected = ConversionException.class)
337 public void testToEnumFromInvalidNumber()
338 {
339 PropertyConverter.toEnum(Integer.valueOf(-1), ENUM_CLASS);
340 }
341
342
343
344
345 @Test
346 public void testToNoConversionNeeded()
347 {
348 String value = "testValue";
349 assertEquals("Wrong conversion result", value,
350 PropertyConverter.to(String.class, value, null));
351 }
352 }