1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration2.interpol;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNull;
21 import static org.junit.jupiter.api.Assertions.assertSame;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24 import static org.mockito.Mockito.mock;
25
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Objects;
31 import java.util.function.Function;
32
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35
36
37
38
39 public class TestInterpolatorSpecification {
40
41
42 private static final String PREFIX1 = "p1";
43
44
45 private static final String PREFIX2 = "p2";
46
47
48
49
50
51
52
53
54 private static void checkDefaultLookups(final InterpolatorSpecification spec, final Lookup defLook1, final Lookup defLook2) {
55 assertEquals(2, spec.getDefaultLookups().size());
56 assertTrue(spec.getDefaultLookups().containsAll(Arrays.asList(defLook1, defLook2)));
57 }
58
59
60
61
62
63
64
65
66 private static void checkPrefixLookups(final InterpolatorSpecification spec, final Lookup prefLook1, final Lookup prefLook2) {
67 assertEquals(2, spec.getPrefixLookups().size());
68 assertSame(prefLook1, spec.getPrefixLookups().get(PREFIX1));
69 assertSame(prefLook2, spec.getPrefixLookups().get(PREFIX2));
70 }
71
72
73
74
75
76
77 private static Lookup createLookup() {
78 return mock(Lookup.class);
79 }
80
81
82 private InterpolatorSpecification.Builder builder;
83
84 @BeforeEach
85 public void setUp() throws Exception {
86 builder = new InterpolatorSpecification.Builder();
87 }
88
89
90
91
92 @Test
93 void testBuilderReuse() {
94 builder
95 .withDefaultLookup(createLookup())
96 .withInterpolator(mock(ConfigurationInterpolator.class))
97 .withPrefixLookup("test", createLookup())
98 .withParentInterpolator(mock(ConfigurationInterpolator.class))
99 .withStringConverter(obj -> "test")
100 .create();
101 final Lookup prefLook1 = createLookup();
102 final Lookup prefLook2 = createLookup();
103 final Lookup defLook1 = createLookup();
104 final Lookup defLook2 = createLookup();
105 final ConfigurationInterpolator parent = mock(ConfigurationInterpolator.class);
106 final Function<Object, String> stringConverter = Objects::toString;
107 final InterpolatorSpecification spec = builder
108 .withPrefixLookup(PREFIX1, prefLook1)
109 .withPrefixLookup(PREFIX2, prefLook2)
110 .withDefaultLookups(Arrays.asList(defLook1, defLook2))
111 .withParentInterpolator(parent)
112 .withStringConverter(stringConverter)
113 .create();
114 assertNull(spec.getInterpolator());
115 assertSame(parent, spec.getParentInterpolator());
116 assertSame(stringConverter, spec.getStringConverter());
117 checkPrefixLookups(spec, prefLook1, prefLook2);
118 checkDefaultLookups(spec, defLook1, defLook2);
119 }
120
121
122
123
124 @Test
125 void testCreateInstance() {
126 final Lookup prefLook1 = createLookup();
127 final Lookup prefLook2 = createLookup();
128 final Lookup defLook1 = createLookup();
129 final Lookup defLook2 = createLookup();
130 final ConfigurationInterpolator interpolator = mock(ConfigurationInterpolator.class);
131 final ConfigurationInterpolator parent = mock(ConfigurationInterpolator.class);
132 final Function<Object, String> stringConverter = Objects::toString;
133 final InterpolatorSpecification spec = builder
134 .withPrefixLookup(PREFIX1, prefLook1)
135 .withDefaultLookup(defLook1)
136 .withPrefixLookup(PREFIX2, prefLook2)
137 .withParentInterpolator(parent)
138 .withDefaultLookup(defLook2)
139 .withInterpolator(interpolator)
140 .withStringConverter(stringConverter)
141 .create();
142 assertSame(interpolator, spec.getInterpolator());
143 assertSame(parent, spec.getParentInterpolator());
144 assertSame(stringConverter, spec.getStringConverter());
145 checkPrefixLookups(spec, prefLook1, prefLook2);
146 checkDefaultLookups(spec, defLook1, defLook2);
147 }
148
149
150
151
152 @Test
153 void testCreateInstanceCollections() {
154 final Lookup prefLook1 = createLookup();
155 final Lookup prefLook2 = createLookup();
156 final Lookup defLook1 = createLookup();
157 final Lookup defLook2 = createLookup();
158 final Map<String, Lookup> prefixLookups = new HashMap<>();
159 prefixLookups.put(PREFIX1, prefLook1);
160 prefixLookups.put(PREFIX2, prefLook2);
161 final InterpolatorSpecification spec = builder
162 .withPrefixLookups(prefixLookups)
163 .withDefaultLookups(Arrays.asList(defLook1, defLook2))
164 .create();
165 checkPrefixLookups(spec, prefLook1, prefLook2);
166 checkDefaultLookups(spec, defLook1, defLook2);
167 }
168
169
170
171
172 @Test
173 void testGetDefaultLookupsModify() {
174 final InterpolatorSpecification spec = builder.withDefaultLookup(createLookup()).create();
175 final Collection<Lookup> lookups = spec.getDefaultLookups();
176 final Lookup lookup = createLookup();
177 assertThrows(UnsupportedOperationException.class, () -> lookups.add(lookup));
178 }
179
180
181
182
183 @Test
184 void testGetPrefixLookupsModify() {
185 final InterpolatorSpecification spec = builder.withPrefixLookup(PREFIX1, createLookup()).create();
186 final Lookup lookup = createLookup();
187 assertThrows(UnsupportedOperationException.class, () -> spec.getPrefixLookups().put(PREFIX1, lookup));
188 }
189
190
191
192
193 @Test
194 void testWithDefaultLookupNull() {
195 assertThrows(IllegalArgumentException.class, () -> builder.withDefaultLookup(null));
196 }
197
198
199
200
201 @Test
202 void testWithDefaultLookupsNull() {
203 final InterpolatorSpecification spec = builder.withDefaultLookups(null).create();
204 assertTrue(spec.getDefaultLookups().isEmpty());
205 }
206
207
208
209
210 @Test
211 void testWithPrefixLookupNoLookup() {
212 assertThrows(IllegalArgumentException.class, () -> builder.withPrefixLookup(PREFIX1, null));
213 }
214
215
216
217
218 @Test
219 void testWithPrefixLookupNoPrefix() {
220 final Lookup lookup = createLookup();
221 assertThrows(IllegalArgumentException.class, () -> builder.withPrefixLookup(null, lookup));
222 }
223
224
225
226
227 @Test
228 void testWithPrefixLookupsNull() {
229 final InterpolatorSpecification spec = builder.withPrefixLookups(null).create();
230 assertTrue(spec.getPrefixLookups().isEmpty());
231 }
232 }