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.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertIterableEquals;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27
28 import org.junit.jupiter.api.Test;
29
30
31
32
33 public class TestConfigurationLookup {
34
35
36 private static final String VAR = "testVariable";
37
38
39 private static final Object VALUE = "SomeTestValue";
40
41
42
43
44 @Test
45 void testInitNoConfig() {
46 assertThrows(IllegalArgumentException.class, () -> new ConfigurationLookup(null));
47 }
48
49
50
51
52 @Test
53 void testLookupComplex() {
54 final int count = 5;
55 final Configuration conf = new BaseConfiguration();
56 for (int i = 0; i < count; i++) {
57 conf.addProperty(VAR, String.valueOf(VALUE) + i);
58 }
59 final ConfigurationLookup lookup = new ConfigurationLookup(conf);
60 final Collection<?> col = (Collection<?>) lookup.lookup(VAR);
61
62 final List<String> expected = new ArrayList<>();
63 for (int i = 0; i < count; i++) {
64 expected.add(String.valueOf(VALUE) + i);
65 }
66 assertIterableEquals(expected, col);
67 }
68
69
70
71
72 @Test
73 void testLookupNotFound() {
74 final Configuration conf = new BaseConfiguration();
75 final ConfigurationLookup lookup = new ConfigurationLookup(conf);
76 assertNull(lookup.lookup(VAR));
77 }
78
79
80
81
82 @Test
83 void testLookupNotFoundEx() {
84 final BaseConfiguration conf = new BaseConfiguration();
85 conf.setThrowExceptionOnMissing(true);
86 final ConfigurationLookup lookup = new ConfigurationLookup(conf);
87 assertNull(lookup.lookup(VAR));
88 }
89
90
91
92
93 @Test
94 void testLookupSuccess() {
95 final Configuration conf = new BaseConfiguration();
96 conf.addProperty(VAR, VALUE);
97 final ConfigurationLookup lookup = new ConfigurationLookup(conf);
98 assertEquals(VALUE, lookup.lookup(VAR));
99 }
100 }