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;
19
import static org.junit.Assert.assertEquals;
20
import static org.junit.Assert.fail;
22
import java.awt.event.KeyEvent;
23
import java.util.List;
25
import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
26
import org.apache.commons.lang.text.StrLookup;
29
* A helper class that defines a bunch of tests related to variable
30
* interpolation. It can be used for running these tests on different
31
* configuration implementations.
34
* href="http://commons.apache.org/configuration/team-list.html">Commons
35
* Configuration team</a>
36
* @version $Id: InterpolationTestHelper.java 1222452 2011-12-22 21:06:17Z oheger $
38
public class InterpolationTestHelper
41
* Tests basic interpolation facilities of the specified configuration.
43
* @param config the configuration to test
45
public static void testInterpolation(Configuration config)
47
config.setProperty("applicationRoot", "/home/applicationRoot");
48
config.setProperty("db", "${applicationRoot}/db/hypersonic");
49
String unInterpolatedValue = "${applicationRoot2}/db/hypersonic";
50
config.setProperty("dbFailedInterpolate", unInterpolatedValue);
51
String dbProp = "/home/applicationRoot/db/hypersonic";
53
assertEquals("Checking interpolated variable", dbProp, config
55
assertEquals("lookup fails, leave variable as is", config
56
.getString("dbFailedInterpolate"), unInterpolatedValue);
58
config.setProperty("arrayInt", "${applicationRoot}/1");
59
String[] arrayInt = config.getStringArray("arrayInt");
60
assertEquals("check first entry was interpolated",
61
"/home/applicationRoot/1", arrayInt[0]);
63
config.addProperty("path", "/temp,C:\\Temp,/usr/local/tmp");
64
config.setProperty("path.current", "${path}");
65
assertEquals("Interpolation with multi-valued property",
66
"/temp", config.getString("path.current"));
70
* Tests an interpolation over multiple levels (i.e. the replacement of a
71
* variable is another variable and so on).
73
* @param config the configuration to test
75
public static void testMultipleInterpolation(Configuration config)
77
config.setProperty("test.base-level", "/base-level");
79
.setProperty("test.first-level",
80
"${test.base-level}/first-level");
81
config.setProperty("test.second-level",
82
"${test.first-level}/second-level");
83
config.setProperty("test.third-level",
84
"${test.second-level}/third-level");
86
String expectedValue = "/base-level/first-level/second-level/third-level";
88
assertEquals(config.getString("test.third-level"),
93
* Tests an invalid interpolation that results in an infinite loop. This
94
* loop should be detected and an exception should be thrown.
96
* @param config the configuration to test
98
public static void testInterpolationLoop(Configuration config)
100
config.setProperty("test.a", "${test.b}");
101
config.setProperty("test.b", "${test.a}");
105
config.getString("test.a");
106
fail("IllegalStateException should have been thrown for looped property references");
108
catch (IllegalStateException e)
116
* Tests interpolation when a subset configuration is involved.
118
* @param config the configuration to test
120
public static void testInterpolationSubset(Configuration config)
122
config.addProperty("test.a", new Integer(42));
123
config.addProperty("test.b", "${test.a}");
124
assertEquals("Wrong interpolated value", 42, config
126
Configuration subset = config.subset("test");
127
assertEquals("Wrong string property", "42", subset
129
assertEquals("Wrong int property", 42, subset.getInt("b"));
133
* Tests interpolation when the referred property is not found.
135
* @param config the configuration to test
137
public static void testInterpolationUnknownProperty(Configuration config)
139
config.addProperty("test.interpol", "${unknown.property}");
140
assertEquals("Wrong interpolated unknown property",
141
"${unknown.property}", config.getString("test.interpol"));
145
* Tests interpolation of system properties.
147
* @param config the configuration to test
149
public static void testInterpolationSystemProperties(Configuration config)
151
String[] sysProperties =
152
{ "java.version", "java.vendor", "os.name", "java.class.path" };
153
for (int i = 0; i < sysProperties.length; i++)
155
config.addProperty("prop" + i, "${sys:" + sysProperties[i] + "}");
158
for (int i = 0; i < sysProperties.length; i++)
160
assertEquals("Wrong value for system property "
161
+ sysProperties[i], System.getProperty(sysProperties[i]),
162
config.getString("prop" + i));
167
* Tests interpolation of constant values.
169
* @param config the configuration to test
171
public static void testInterpolationConstants(Configuration config)
173
config.addProperty("key.code",
174
"${const:java.awt.event.KeyEvent.VK_CANCEL}");
175
assertEquals("Wrong value of constant variable",
176
KeyEvent.VK_CANCEL, config.getInt("key.code"));
177
assertEquals("Wrong value when fetching constant from cache",
178
KeyEvent.VK_CANCEL, config.getInt("key.code"));
182
* Tests whether a variable can be escaped, so that it won't be
185
* @param config the configuration to test
187
public static void testInterpolationEscaped(Configuration config)
189
config.addProperty("var", "x");
190
config.addProperty("escVar", "Use the variable $${${var}}.");
191
assertEquals("Wrong escaped variable", "Use the variable ${x}.",
192
config.getString("escVar"));
196
* Tests accessing and manipulating the interpolator object.
198
* @param config the configuration to test
200
public static void testGetInterpolator(AbstractConfiguration config)
202
config.addProperty("var", "${echo:testVar}");
203
ConfigurationInterpolator interpol = config.getInterpolator();
204
interpol.registerLookup("echo", new StrLookup()
207
public String lookup(String varName)
209
return "Value of variable " + varName;
212
assertEquals("Wrong value of echo variable",
213
"Value of variable testVar", config.getString("var"));
217
* Tests obtaining a configuration with all variables replaced by their
220
* @param config the configuration to test
221
* @return the interpolated configuration
223
public static Configuration testInterpolatedConfiguration(
224
AbstractConfiguration config)
226
config.setProperty("applicationRoot", "/home/applicationRoot");
227
config.setProperty("db", "${applicationRoot}/db/hypersonic");
228
config.setProperty("inttest.interpol", "${unknown.property}");
229
config.setProperty("intkey.code",
230
"${const:java.awt.event.KeyEvent.VK_CANCEL}");
231
config.setProperty("inttest.sysprop", "${sys:java.version}");
232
config.setProperty("inttest.numvalue", "3\\,1415");
233
config.setProperty("inttest.value", "${inttest.numvalue}");
234
config.setProperty("inttest.list", "${db}");
235
config.addProperty("inttest.list", "${inttest.value}");
237
Configuration c = config.interpolatedConfiguration();
238
assertEquals("Property not replaced",
239
"/home/applicationRoot/db/hypersonic", c.getProperty("db"));
240
assertEquals("Const variable not replaced", KeyEvent.VK_CANCEL,
241
c.getInt("intkey.code"));
242
assertEquals("Sys property not replaced", System
243
.getProperty("java.version"), c.getProperty("inttest.sysprop"));
244
assertEquals("Delimiter value not replaced", "3,1415", c
245
.getProperty("inttest.value"));
246
List<?> lst = (List<?>) c.getProperty("inttest.list");
247
assertEquals("Wrong number of list elements", 2, lst.size());
248
assertEquals("List element 0 not replaced",
249
"/home/applicationRoot/db/hypersonic", lst.get(0));
250
assertEquals("List element 1 not replaced", "3,1415", lst
252
assertEquals("Unresolvable variable not found",
253
"${unknown.property}", c.getProperty("inttest.interpol"));