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