2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.configuration.interpol;
19
import static org.junit.Assert.assertEquals;
20
import static org.junit.Assert.assertFalse;
21
import static org.junit.Assert.assertNull;
22
import static org.junit.Assert.assertTrue;
24
import java.util.HashMap;
26
import java.util.Properties;
28
import org.apache.commons.lang.text.StrLookup;
29
import org.junit.After;
30
import org.junit.Before;
31
import org.junit.Test;
34
* Test class for ConfigurationInterpolator.
36
* @version $Id: TestConfigurationInterpolator.java 1225653 2011-12-29 21:06:26Z oheger $
38
public class TestConfigurationInterpolator
40
/** Constant for a test variable prefix. */
41
private static final String TEST_PREFIX = "prefix";
43
/** Constant for a test variable name. */
44
private static final String TEST_NAME = "varname";
46
/** Constant for the value of the test variable. */
47
private static final String TEST_VALUE = "TestVariableValue";
49
/** Stores the object to be tested. */
50
private ConfigurationInterpolator interpolator;
53
public void setUp() throws Exception
55
interpolator = new ConfigurationInterpolator();
59
* Cleans the test environment. Deregisters the test lookup object if
63
public void tearDown() throws Exception
65
ConfigurationInterpolator.deregisterGlobalLookup(TEST_PREFIX);
69
* Tests creating an instance. Does it contain some predefined lookups?
72
public void testInit()
74
assertNull("A default lookup is set", interpolator.getDefaultLookup());
75
assertFalse("No predefined lookups", interpolator.prefixSet().isEmpty());
79
* Tries to register a global lookup for a null prefix. This should cause an
82
@Test(expected = IllegalArgumentException.class)
83
public void testRegisterGlobalLookupNullPrefix()
85
ConfigurationInterpolator.registerGlobalLookup(null, StrLookup
90
* Tries to register a global null lookup. This should cause an exception.
92
@Test(expected = IllegalArgumentException.class)
93
public void testRegisterGlobalLookupNull()
95
ConfigurationInterpolator.registerGlobalLookup(TEST_PREFIX, null);
99
* Tests registering a global lookup object. This lookup object should then
100
* be available for instances created later on.
103
public void testRegisterGlobalLookup()
105
ConfigurationInterpolator.registerGlobalLookup(TEST_PREFIX, StrLookup
107
ConfigurationInterpolator int2 = new ConfigurationInterpolator();
108
assertTrue("No lookup registered for test prefix", int2.prefixSet()
109
.contains(TEST_PREFIX));
110
assertFalse("Existing instance was modified", interpolator.prefixSet()
111
.contains(TEST_PREFIX));
115
* Tests deregistering a global lookup object.
118
public void testDeregisterGlobalLookup()
120
ConfigurationInterpolator.registerGlobalLookup(TEST_PREFIX, StrLookup
122
assertTrue("Lookup could not be deregistered",
123
ConfigurationInterpolator.deregisterGlobalLookup(TEST_PREFIX));
124
ConfigurationInterpolator int2 = new ConfigurationInterpolator();
125
assertFalse("Deregistered lookup still available", int2.prefixSet()
126
.contains(TEST_PREFIX));
130
* Tests deregistering an unknown lookup.
133
public void testDeregisterGlobalLookupNonExisting()
135
assertFalse("Could deregister unknown global lookup",
136
ConfigurationInterpolator.deregisterGlobalLookup(TEST_PREFIX));
140
* Tests registering a lookup object at an instance.
143
public void testRegisterLookup()
145
int cnt = interpolator.prefixSet().size();
146
interpolator.registerLookup(TEST_PREFIX, StrLookup.noneLookup());
147
assertTrue("New lookup not registered", interpolator.prefixSet()
148
.contains(TEST_PREFIX));
149
assertEquals("Wrong number of registered lookups", cnt + 1,
150
interpolator.prefixSet().size());
151
ConfigurationInterpolator int2 = new ConfigurationInterpolator();
152
assertFalse("Local registration has global impact", int2.prefixSet()
153
.contains(TEST_PREFIX));
157
* Tests registering a null lookup object. This should cause an exception.
159
@Test(expected = IllegalArgumentException.class)
160
public void testRegisterLookupNull()
162
interpolator.registerLookup(TEST_PREFIX, null);
166
* Tests registering a lookup object for an undefined prefix. This should
167
* cause an exception.
169
@Test(expected = IllegalArgumentException.class)
170
public void testRegisterLookupNullPrefix()
172
interpolator.registerLookup(null, StrLookup.noneLookup());
176
* Tests deregistering a local lookup object.
179
public void testDeregisterLookup()
181
interpolator.registerLookup(TEST_PREFIX, StrLookup.noneLookup());
182
assertTrue("Derigstration not successfull", interpolator
183
.deregisterLookup(TEST_PREFIX));
184
assertFalse("Deregistered prefix still contained", interpolator
185
.prefixSet().contains(TEST_PREFIX));
189
* Tests deregistering an unknown lookup object.
192
public void testDeregisterLookupNonExisting()
194
assertFalse("Could deregister unknown lookup", interpolator
195
.deregisterLookup(TEST_PREFIX));
199
* Tests whether a variable can be resolved using the associated lookup
200
* object. The lookup is identified by the variable's prefix.
203
public void testLookupWithPrefix()
205
interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
206
assertEquals("Wrong variable value", TEST_VALUE, interpolator
207
.lookup(TEST_PREFIX + ':' + TEST_NAME));
211
* Tests the behavior of the lookup method for variables with an unknown
212
* prefix. These variables should not be resolved.
215
public void testLookupWithUnknownPrefix()
217
interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
218
assertNull("Variable could be resolved", interpolator
219
.lookup("UnknownPrefix:" + TEST_NAME));
220
assertNull("Variable with empty prefix could be resolved", interpolator
221
.lookup(":" + TEST_NAME));
225
* Tests looking up a variable without a prefix. This should trigger the
226
* default lookup object.
229
public void testLookupDefault()
231
interpolator.setDefaultLookup(setUpTestLookup());
232
assertEquals("Wrong variable value", TEST_VALUE, interpolator
237
* Tests looking up a variable without a prefix when no default lookup is
238
* specified. Result should be null in this case.
241
public void testLookupNoDefault()
243
assertNull("Variable could be resolved", interpolator.lookup(TEST_NAME));
247
* Tests the empty variable prefix. This is a special case, but legal.
250
public void testLookupEmptyPrefix()
252
interpolator.registerLookup("", setUpTestLookup());
253
assertEquals("Wrong variable value", TEST_VALUE, interpolator
254
.lookup(":" + TEST_NAME));
258
* Tests an empty variable name.
261
public void testLookupEmptyVarName()
263
Map<String, String> map = new HashMap<String, String>();
264
map.put("", TEST_VALUE);
265
interpolator.registerLookup(TEST_PREFIX, StrLookup.mapLookup(map));
266
assertEquals("Wrong variable value", TEST_VALUE, interpolator
267
.lookup(TEST_PREFIX + ":"));
271
* Tests an empty variable name without a prefix.
274
public void testLookupDefaultEmptyVarName()
276
Map<String, String> map = new HashMap<String, String>();
277
map.put("", TEST_VALUE);
278
interpolator.setDefaultLookup(StrLookup.mapLookup(map));
279
assertEquals("Wrong variable value", TEST_VALUE, interpolator
284
* Tests looking up a null variable. Result shoult be null, too.
287
public void testLookupNull()
289
assertNull("Could resolve null variable", interpolator.lookup(null));
293
* Creates a lookup object that can resolve the test variable.
295
* @return the test lookup object
297
private StrLookup setUpTestLookup()
299
Map<String, String> map = new HashMap<String, String>();
300
map.put(TEST_NAME, TEST_VALUE);
301
return StrLookup.mapLookup(map);
305
* Tests whether system properties can be correctly resolved.
308
public void testLookupSysProperties()
310
Properties sysProps = System.getProperties();
311
for (Object prop : sysProps.keySet())
313
String key = (String) prop;
314
assertEquals("Wrong value for system property " + key, sysProps
315
.getProperty(key), interpolator
316
.lookup(ConfigurationInterpolator.PREFIX_SYSPROPERTIES
322
* Tests whether constants can be correctly resolved.
325
public void testLookupConstants()
327
String varName = ConfigurationInterpolator.class.getName()
328
+ ".PREFIX_CONSTANTS";
329
assertEquals("Wrong constant value",
330
ConfigurationInterpolator.PREFIX_CONSTANTS, interpolator
331
.lookup(ConfigurationInterpolator.PREFIX_CONSTANTS
336
* Tests whether the default lookup is called for variables with a prefix
337
* when the lookup that was registered for this prefix is not able to
338
* resolve the variable.
341
public void testLookupDefaultAfterPrefixFails()
343
final String varName = TEST_PREFIX + ':' + TEST_NAME + "2";
344
interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
345
Map<String, Object> map = new HashMap<String, Object>();
346
map.put(varName, TEST_VALUE);
347
interpolator.setDefaultLookup(StrLookup.mapLookup(map));
348
assertEquals("Variable is not resolved by default lookup", TEST_VALUE,
349
interpolator.lookup(varName));