001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.configuration.interpol;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Properties;
023:
024: import org.apache.commons.lang.text.StrLookup;
025:
026: import junit.framework.TestCase;
027:
028: /**
029: * Test class for ConfigurationInterpolator.
030: *
031: * @version $Id: TestConfigurationInterpolator.java 490375 2006-12-26 21:28:04Z oheger $
032: */
033: public class TestConfigurationInterpolator extends TestCase {
034: /** Constant for a test variable prefix. */
035: private static final String TEST_PREFIX = "prefix";
036:
037: /** Constant for a test variable name. */
038: private static final String TEST_NAME = "varname";
039:
040: /** Constant for the value of the test variable. */
041: private static final String TEST_VALUE = "TestVariableValue";
042:
043: /** Stores the object to be tested. */
044: private ConfigurationInterpolator interpolator;
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048: interpolator = new ConfigurationInterpolator();
049: }
050:
051: /**
052: * Cleans the test environment. Deregisters the test lookup object if
053: * necessary.
054: */
055: protected void tearDown() throws Exception {
056: ConfigurationInterpolator.deregisterGlobalLookup(TEST_PREFIX);
057: }
058:
059: /**
060: * Tests creating an instance. Does it contain some predefined lookups?
061: */
062: public void testInit() {
063: assertNull("A default lookup is set", interpolator
064: .getDefaultLookup());
065: assertFalse("No predefined lookups", interpolator.prefixSet()
066: .isEmpty());
067: }
068:
069: /**
070: * Tries to register a global lookup for a null prefix. This should cause an
071: * exception.
072: */
073: public void testRegisterGlobalLookupNullPrefix() {
074: try {
075: ConfigurationInterpolator.registerGlobalLookup(null,
076: StrLookup.noneLookup());
077: fail("Could register global lookup with null prefix!");
078: } catch (IllegalArgumentException iex) {
079: // ok
080: }
081: }
082:
083: /**
084: * Tries to register a global null lookup. This should cause an exception.
085: */
086: public void testRegisterGlobalLookupNull() {
087: try {
088: ConfigurationInterpolator.registerGlobalLookup(TEST_PREFIX,
089: null);
090: fail("Could register global null lookup!");
091: } catch (IllegalArgumentException iex) {
092: // ok
093: }
094: }
095:
096: /**
097: * Tests registering a global lookup object. This lookup object should then
098: * be available for instances created later on.
099: */
100: public void testRegisterGlobalLookup() {
101: ConfigurationInterpolator.registerGlobalLookup(TEST_PREFIX,
102: StrLookup.noneLookup());
103: ConfigurationInterpolator int2 = new ConfigurationInterpolator();
104: assertTrue("No lookup registered for test prefix", int2
105: .prefixSet().contains(TEST_PREFIX));
106: assertFalse("Existing instance was modified", interpolator
107: .prefixSet().contains(TEST_PREFIX));
108: }
109:
110: /**
111: * Tests deregistering a global lookup object.
112: */
113: public void testDeregisterGlobalLookup() {
114: ConfigurationInterpolator.registerGlobalLookup(TEST_PREFIX,
115: StrLookup.noneLookup());
116: assertTrue("Lookup could not be deregistered",
117: ConfigurationInterpolator
118: .deregisterGlobalLookup(TEST_PREFIX));
119: ConfigurationInterpolator int2 = new ConfigurationInterpolator();
120: assertFalse("Deregistered lookup still available", int2
121: .prefixSet().contains(TEST_PREFIX));
122: }
123:
124: /**
125: * Tests deregistering an unknown lookup.
126: */
127: public void testDeregisterGlobalLookupNonExisting() {
128: assertFalse("Could deregister unknown global lookup",
129: ConfigurationInterpolator
130: .deregisterGlobalLookup(TEST_PREFIX));
131: }
132:
133: /**
134: * Tests registering a lookup object at an instance.
135: */
136: public void testRegisterLookup() {
137: int cnt = interpolator.prefixSet().size();
138: interpolator
139: .registerLookup(TEST_PREFIX, StrLookup.noneLookup());
140: assertTrue("New lookup not registered", interpolator
141: .prefixSet().contains(TEST_PREFIX));
142: assertEquals("Wrong number of registered lookups", cnt + 1,
143: interpolator.prefixSet().size());
144: ConfigurationInterpolator int2 = new ConfigurationInterpolator();
145: assertFalse("Local registration has global impact", int2
146: .prefixSet().contains(TEST_PREFIX));
147: }
148:
149: /**
150: * Tests registering a null lookup object. This should cause an exception.
151: */
152: public void testRegisterLookupNull() {
153: try {
154: interpolator.registerLookup(TEST_PREFIX, null);
155: fail("Could register null lookup!");
156: } catch (IllegalArgumentException iex) {
157: // ok
158: }
159: }
160:
161: /**
162: * Tests registering a lookup object for an undefined prefix. This should
163: * cause an exception.
164: */
165: public void testRegisterLookupNullPrefix() {
166: try {
167: interpolator.registerLookup(null, StrLookup.noneLookup());
168: fail("Could register lookup for null prefix!");
169: } catch (IllegalArgumentException iex) {
170: // ok
171: }
172: }
173:
174: /**
175: * Tests deregistering a local lookup object.
176: */
177: public void testDeregisterLookup() {
178: interpolator
179: .registerLookup(TEST_PREFIX, StrLookup.noneLookup());
180: assertTrue("Derigstration not successfull", interpolator
181: .deregisterLookup(TEST_PREFIX));
182: assertFalse("Deregistered prefix still contained", interpolator
183: .prefixSet().contains(TEST_PREFIX));
184: }
185:
186: /**
187: * Tests deregistering an unknown lookup object.
188: */
189: public void testDeregisterLookupNonExisting() {
190: assertFalse("Could deregister unknown lookup", interpolator
191: .deregisterLookup(TEST_PREFIX));
192: }
193:
194: /**
195: * Tests whether a variable can be resolved using the associated lookup
196: * object. The lookup is identified by the variable's prefix.
197: */
198: public void testLookupWithPrefix() {
199: interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
200: assertEquals("Wrong variable value", TEST_VALUE, interpolator
201: .lookup(TEST_PREFIX + ':' + TEST_NAME));
202: }
203:
204: /**
205: * Tests the behavior of the lookup method for variables with an unknown
206: * prefix. These variables should not be resolved.
207: */
208: public void testLookupWithUnknownPrefix() {
209: interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
210: assertNull("Variable could be resolved", interpolator
211: .lookup("UnknownPrefix:" + TEST_NAME));
212: assertNull("Variable with empty prefix could be resolved",
213: interpolator.lookup(":" + TEST_NAME));
214: }
215:
216: /**
217: * Tests looking up a variable without a prefix. This should trigger the
218: * default lookup object.
219: */
220: public void testLookupDefault() {
221: interpolator.setDefaultLookup(setUpTestLookup());
222: assertEquals("Wrong variable value", TEST_VALUE, interpolator
223: .lookup(TEST_NAME));
224: }
225:
226: /**
227: * Tests looking up a variable without a prefix when no default lookup is
228: * specified. Result should be null in this case.
229: */
230: public void testLookupNoDefault() {
231: assertNull("Variable could be resolved", interpolator
232: .lookup(TEST_NAME));
233: }
234:
235: /**
236: * Tests the empty variable prefix. This is a special case, but legal.
237: */
238: public void testLookupEmptyPrefix() {
239: interpolator.registerLookup("", setUpTestLookup());
240: assertEquals("Wrong variable value", TEST_VALUE, interpolator
241: .lookup(":" + TEST_NAME));
242: }
243:
244: /**
245: * Tests an empty variable name.
246: */
247: public void testLookupEmptyVarName() {
248: Map map = new HashMap();
249: map.put("", TEST_VALUE);
250: interpolator.registerLookup(TEST_PREFIX, StrLookup
251: .mapLookup(map));
252: assertEquals("Wrong variable value", TEST_VALUE, interpolator
253: .lookup(TEST_PREFIX + ":"));
254: }
255:
256: /**
257: * Tests an empty variable name without a prefix.
258: */
259: public void testLookupDefaultEmptyVarName() {
260: Map map = new HashMap();
261: map.put("", TEST_VALUE);
262: interpolator.setDefaultLookup(StrLookup.mapLookup(map));
263: assertEquals("Wrong variable value", TEST_VALUE, interpolator
264: .lookup(""));
265: }
266:
267: /**
268: * Tests looking up a null variable. Result shoult be null, too.
269: */
270: public void testLookupNull() {
271: assertNull("Could resolve null variable", interpolator
272: .lookup(null));
273: }
274:
275: /**
276: * Creates a lookup object that can resolve the test variable.
277: *
278: * @return the test lookup object
279: */
280: private StrLookup setUpTestLookup() {
281: Map map = new HashMap();
282: map.put(TEST_NAME, TEST_VALUE);
283: return StrLookup.mapLookup(map);
284: }
285:
286: /**
287: * Tests whether system properties can be correctly resolved.
288: */
289: public void testLookupSysProperties() {
290: Properties sysProps = System.getProperties();
291: for (Iterator it = sysProps.keySet().iterator(); it.hasNext();) {
292: String key = (String) it.next();
293: assertEquals(
294: "Wrong value for system property " + key,
295: sysProps.getProperty(key),
296: interpolator
297: .lookup(ConfigurationInterpolator.PREFIX_SYSPROPERTIES
298: + ":" + key));
299: }
300: }
301:
302: /**
303: * Tests whether constants can be correctly resolved.
304: */
305: public void testLookupConstants() {
306: String varName = ConfigurationInterpolator.class.getName()
307: + ".PREFIX_CONSTANTS";
308: assertEquals(
309: "Wrong constant value",
310: ConfigurationInterpolator.PREFIX_CONSTANTS,
311: interpolator
312: .lookup(ConfigurationInterpolator.PREFIX_CONSTANTS
313: + ":" + varName));
314: }
315: }
|