1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.fail;
21
22 import java.awt.event.KeyEvent;
23 import java.util.List;
24
25 import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
26 import org.apache.commons.lang.text.StrLookup;
27
28
29
30
31
32
33
34
35
36
37
38 public class InterpolationTestHelper
39 {
40
41
42
43
44
45 public static void testInterpolation(Configuration config)
46 {
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";
52
53 assertEquals("Checking interpolated variable", dbProp, config
54 .getString("db"));
55 assertEquals("lookup fails, leave variable as is", config
56 .getString("dbFailedInterpolate"), unInterpolatedValue);
57
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]);
62
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"));
67 }
68
69
70
71
72
73
74
75 public static void testMultipleInterpolation(Configuration config)
76 {
77 config.setProperty("test.base-level", "/base-level");
78 config
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");
85
86 String expectedValue = "/base-level/first-level/second-level/third-level";
87
88 assertEquals(config.getString("test.third-level"),
89 expectedValue);
90 }
91
92
93
94
95
96
97
98 public static void testInterpolationLoop(Configuration config)
99 {
100 config.setProperty("test.a", "${test.b}");
101 config.setProperty("test.b", "${test.a}");
102
103 try
104 {
105 config.getString("test.a");
106 fail("IllegalStateException should have been thrown for looped property references");
107 }
108 catch (IllegalStateException e)
109 {
110
111 }
112
113 }
114
115
116
117
118
119
120 public static void testInterpolationSubset(Configuration config)
121 {
122 config.addProperty("test.a", new Integer(42));
123 config.addProperty("test.b", "${test.a}");
124 assertEquals("Wrong interpolated value", 42, config
125 .getInt("test.b"));
126 Configuration subset = config.subset("test");
127 assertEquals("Wrong string property", "42", subset
128 .getString("b"));
129 assertEquals("Wrong int property", 42, subset.getInt("b"));
130 }
131
132
133
134
135
136
137 public static void testInterpolationUnknownProperty(Configuration config)
138 {
139 config.addProperty("test.interpol", "${unknown.property}");
140 assertEquals("Wrong interpolated unknown property",
141 "${unknown.property}", config.getString("test.interpol"));
142 }
143
144
145
146
147
148
149 public static void testInterpolationSystemProperties(Configuration config)
150 {
151 String[] sysProperties =
152 { "java.version", "java.vendor", "os.name", "java.class.path" };
153 for (int i = 0; i < sysProperties.length; i++)
154 {
155 config.addProperty("prop" + i, "${sys:" + sysProperties[i] + "}");
156 }
157
158 for (int i = 0; i < sysProperties.length; i++)
159 {
160 assertEquals("Wrong value for system property "
161 + sysProperties[i], System.getProperty(sysProperties[i]),
162 config.getString("prop" + i));
163 }
164 }
165
166
167
168
169
170
171 public static void testInterpolationConstants(Configuration config)
172 {
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"));
179 }
180
181
182
183
184
185
186
187 public static void testInterpolationEscaped(Configuration config)
188 {
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"));
193 }
194
195
196
197
198
199
200 public static void testGetInterpolator(AbstractConfiguration config)
201 {
202 config.addProperty("var", "${echo:testVar}");
203 ConfigurationInterpolator interpol = config.getInterpolator();
204 interpol.registerLookup("echo", new StrLookup()
205 {
206 @Override
207 public String lookup(String varName)
208 {
209 return "Value of variable " + varName;
210 }
211 });
212 assertEquals("Wrong value of echo variable",
213 "Value of variable testVar", config.getString("var"));
214 }
215
216
217
218
219
220
221
222
223 public static Configuration testInterpolatedConfiguration(
224 AbstractConfiguration config)
225 {
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}");
236
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
251 .get(1));
252 assertEquals("Unresolvable variable not found",
253 "${unknown.property}", c.getProperty("inttest.interpol"));
254
255 return c;
256 }
257 }