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 static org.junit.Assert.assertEquals;
020    import static org.junit.Assert.assertFalse;
021    import static org.junit.Assert.assertNull;
022    import static org.junit.Assert.assertTrue;
023    
024    import java.util.HashMap;
025    import java.util.Map;
026    import java.util.Properties;
027    
028    import org.apache.commons.lang.text.StrLookup;
029    import org.junit.After;
030    import org.junit.Before;
031    import org.junit.Test;
032    
033    /**
034     * Test class for ConfigurationInterpolator.
035     *
036     * @version $Id: TestConfigurationInterpolator.java 1225653 2011-12-29 21:06:26Z oheger $
037     */
038    public class TestConfigurationInterpolator
039    {
040        /** Constant for a test variable prefix. */
041        private static final String TEST_PREFIX = "prefix";
042    
043        /** Constant for a test variable name. */
044        private static final String TEST_NAME = "varname";
045    
046        /** Constant for the value of the test variable. */
047        private static final String TEST_VALUE = "TestVariableValue";
048    
049        /** Stores the object to be tested. */
050        private ConfigurationInterpolator interpolator;
051    
052        @Before
053        public void setUp() throws Exception
054        {
055            interpolator = new ConfigurationInterpolator();
056        }
057    
058        /**
059         * Cleans the test environment. Deregisters the test lookup object if
060         * necessary.
061         */
062        @After
063        public void tearDown() throws Exception
064        {
065            ConfigurationInterpolator.deregisterGlobalLookup(TEST_PREFIX);
066        }
067    
068        /**
069         * Tests creating an instance. Does it contain some predefined lookups?
070         */
071        @Test
072        public void testInit()
073        {
074            assertNull("A default lookup is set", interpolator.getDefaultLookup());
075            assertFalse("No predefined lookups", interpolator.prefixSet().isEmpty());
076        }
077    
078        /**
079         * Tries to register a global lookup for a null prefix. This should cause an
080         * exception.
081         */
082        @Test(expected = IllegalArgumentException.class)
083        public void testRegisterGlobalLookupNullPrefix()
084        {
085            ConfigurationInterpolator.registerGlobalLookup(null, StrLookup
086                    .noneLookup());
087        }
088    
089        /**
090         * Tries to register a global null lookup. This should cause an exception.
091         */
092        @Test(expected = IllegalArgumentException.class)
093        public void testRegisterGlobalLookupNull()
094        {
095            ConfigurationInterpolator.registerGlobalLookup(TEST_PREFIX, null);
096        }
097    
098        /**
099         * Tests registering a global lookup object. This lookup object should then
100         * be available for instances created later on.
101         */
102        @Test
103        public void testRegisterGlobalLookup()
104        {
105            ConfigurationInterpolator.registerGlobalLookup(TEST_PREFIX, StrLookup
106                    .noneLookup());
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));
112        }
113    
114        /**
115         * Tests deregistering a global lookup object.
116         */
117        @Test
118        public void testDeregisterGlobalLookup()
119        {
120            ConfigurationInterpolator.registerGlobalLookup(TEST_PREFIX, StrLookup
121                    .noneLookup());
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));
127        }
128    
129        /**
130         * Tests deregistering an unknown lookup.
131         */
132        @Test
133        public void testDeregisterGlobalLookupNonExisting()
134        {
135            assertFalse("Could deregister unknown global lookup",
136                    ConfigurationInterpolator.deregisterGlobalLookup(TEST_PREFIX));
137        }
138    
139        /**
140         * Tests registering a lookup object at an instance.
141         */
142        @Test
143        public void testRegisterLookup()
144        {
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));
154        }
155    
156        /**
157         * Tests registering a null lookup object. This should cause an exception.
158         */
159        @Test(expected = IllegalArgumentException.class)
160        public void testRegisterLookupNull()
161        {
162            interpolator.registerLookup(TEST_PREFIX, null);
163        }
164    
165        /**
166         * Tests registering a lookup object for an undefined prefix. This should
167         * cause an exception.
168         */
169        @Test(expected = IllegalArgumentException.class)
170        public void testRegisterLookupNullPrefix()
171        {
172            interpolator.registerLookup(null, StrLookup.noneLookup());
173        }
174    
175        /**
176         * Tests deregistering a local lookup object.
177         */
178        @Test
179        public void testDeregisterLookup()
180        {
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));
186        }
187    
188        /**
189         * Tests deregistering an unknown lookup object.
190         */
191        @Test
192        public void testDeregisterLookupNonExisting()
193        {
194            assertFalse("Could deregister unknown lookup", interpolator
195                    .deregisterLookup(TEST_PREFIX));
196        }
197    
198        /**
199         * Tests whether a variable can be resolved using the associated lookup
200         * object. The lookup is identified by the variable's prefix.
201         */
202        @Test
203        public void testLookupWithPrefix()
204        {
205            interpolator.registerLookup(TEST_PREFIX, setUpTestLookup());
206            assertEquals("Wrong variable value", TEST_VALUE, interpolator
207                    .lookup(TEST_PREFIX + ':' + TEST_NAME));
208        }
209    
210        /**
211         * Tests the behavior of the lookup method for variables with an unknown
212         * prefix. These variables should not be resolved.
213         */
214        @Test
215        public void testLookupWithUnknownPrefix()
216        {
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));
222        }
223    
224        /**
225         * Tests looking up a variable without a prefix. This should trigger the
226         * default lookup object.
227         */
228        @Test
229        public void testLookupDefault()
230        {
231            interpolator.setDefaultLookup(setUpTestLookup());
232            assertEquals("Wrong variable value", TEST_VALUE, interpolator
233                    .lookup(TEST_NAME));
234        }
235    
236        /**
237         * Tests looking up a variable without a prefix when no default lookup is
238         * specified. Result should be null in this case.
239         */
240        @Test
241        public void testLookupNoDefault()
242        {
243            assertNull("Variable could be resolved", interpolator.lookup(TEST_NAME));
244        }
245    
246        /**
247         * Tests the empty variable prefix. This is a special case, but legal.
248         */
249        @Test
250        public void testLookupEmptyPrefix()
251        {
252            interpolator.registerLookup("", setUpTestLookup());
253            assertEquals("Wrong variable value", TEST_VALUE, interpolator
254                    .lookup(":" + TEST_NAME));
255        }
256    
257        /**
258         * Tests an empty variable name.
259         */
260        @Test
261        public void testLookupEmptyVarName()
262        {
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 + ":"));
268        }
269    
270        /**
271         * Tests an empty variable name without a prefix.
272         */
273        @Test
274        public void testLookupDefaultEmptyVarName()
275        {
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
280                    .lookup(""));
281        }
282    
283        /**
284         * Tests looking up a null variable. Result shoult be null, too.
285         */
286        @Test
287        public void testLookupNull()
288        {
289            assertNull("Could resolve null variable", interpolator.lookup(null));
290        }
291    
292        /**
293         * Creates a lookup object that can resolve the test variable.
294         *
295         * @return the test lookup object
296         */
297        private StrLookup setUpTestLookup()
298        {
299            Map<String, String> map = new HashMap<String, String>();
300            map.put(TEST_NAME, TEST_VALUE);
301            return StrLookup.mapLookup(map);
302        }
303    
304        /**
305         * Tests whether system properties can be correctly resolved.
306         */
307        @Test
308        public void testLookupSysProperties()
309        {
310            Properties sysProps = System.getProperties();
311            for (Object prop : sysProps.keySet())
312            {
313                String key = (String) prop;
314                assertEquals("Wrong value for system property " + key, sysProps
315                        .getProperty(key), interpolator
316                        .lookup(ConfigurationInterpolator.PREFIX_SYSPROPERTIES
317                                + ":" + key));
318            }
319        }
320    
321        /**
322         * Tests whether constants can be correctly resolved.
323         */
324        @Test
325        public void testLookupConstants()
326        {
327            String varName = ConfigurationInterpolator.class.getName()
328                    + ".PREFIX_CONSTANTS";
329            assertEquals("Wrong constant value",
330                    ConfigurationInterpolator.PREFIX_CONSTANTS, interpolator
331                            .lookup(ConfigurationInterpolator.PREFIX_CONSTANTS
332                                    + ":" + varName));
333        }
334    
335        /**
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.
339         */
340        @Test
341        public void testLookupDefaultAfterPrefixFails()
342        {
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));
350        }
351    }