~ubuntu-branches/ubuntu/saucy/commons-configuration/saucy

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/configuration/interpol/TestConfigurationInterpolator.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-07-01 16:29:44 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20130701162944-98waq5gogha5gpn1
Tags: 1.9-1
* New upstream release
* debian/control:
  - Updated Standards-Version to 3.9.4 (no changes)
  - Use canonical URLs for the Vcs-* fields
  - Added new build dependencies (libjavacc-maven-plugin-java, junit4)
  - Upgraded the dependency on the Servlet API (2.5 -> 3.0)
  - Removed the dependency on the Activation Framework (glassfish-activation)
  - Replaced the dependency on glassfish-mail with libgnumail-java
  - Removed the unused dependencies:
    liblog4j1.2-java-doc, libmaven-assembly-plugin-java
  - Replaced the dependency on libcommons-jexl-java by libcommons-jexl2-java
* debian/watch: Changed to point the official Apache distribution server
* Removed the obsolete file debian/ant.properties
* Installed the upstream changelog in the binary packages
* Added the report plugins to maven.ignoreRules
* Added the classpath attribute to the jar manifest

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
8
 *
 
9
 *     http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
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.
 
16
 */
 
17
package org.apache.commons.configuration.interpol;
 
18
 
 
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;
 
23
 
 
24
import java.util.HashMap;
 
25
import java.util.Map;
 
26
import java.util.Properties;
 
27
 
 
28
import org.apache.commons.lang.text.StrLookup;
 
29
import org.junit.After;
 
30
import org.junit.Before;
 
31
import org.junit.Test;
 
32
 
 
33
/**
 
34
 * Test class for ConfigurationInterpolator.
 
35
 *
 
36
 * @version $Id: TestConfigurationInterpolator.java 1225653 2011-12-29 21:06:26Z oheger $
 
37
 */
 
38
public class TestConfigurationInterpolator
 
39
{
 
40
    /** Constant for a test variable prefix. */
 
41
    private static final String TEST_PREFIX = "prefix";
 
42
 
 
43
    /** Constant for a test variable name. */
 
44
    private static final String TEST_NAME = "varname";
 
45
 
 
46
    /** Constant for the value of the test variable. */
 
47
    private static final String TEST_VALUE = "TestVariableValue";
 
48
 
 
49
    /** Stores the object to be tested. */
 
50
    private ConfigurationInterpolator interpolator;
 
51
 
 
52
    @Before
 
53
    public void setUp() throws Exception
 
54
    {
 
55
        interpolator = new ConfigurationInterpolator();
 
56
    }
 
57
 
 
58
    /**
 
59
     * Cleans the test environment. Deregisters the test lookup object if
 
60
     * necessary.
 
61
     */
 
62
    @After
 
63
    public void tearDown() throws Exception
 
64
    {
 
65
        ConfigurationInterpolator.deregisterGlobalLookup(TEST_PREFIX);
 
66
    }
 
67
 
 
68
    /**
 
69
     * Tests creating an instance. Does it contain some predefined lookups?
 
70
     */
 
71
    @Test
 
72
    public void testInit()
 
73
    {
 
74
        assertNull("A default lookup is set", interpolator.getDefaultLookup());
 
75
        assertFalse("No predefined lookups", interpolator.prefixSet().isEmpty());
 
76
    }
 
77
 
 
78
    /**
 
79
     * Tries to register a global lookup for a null prefix. This should cause an
 
80
     * exception.
 
81
     */
 
82
    @Test(expected = IllegalArgumentException.class)
 
83
    public void testRegisterGlobalLookupNullPrefix()
 
84
    {
 
85
        ConfigurationInterpolator.registerGlobalLookup(null, StrLookup
 
86
                .noneLookup());
 
87
    }
 
88
 
 
89
    /**
 
90
     * Tries to register a global null lookup. This should cause an exception.
 
91
     */
 
92
    @Test(expected = IllegalArgumentException.class)
 
93
    public void testRegisterGlobalLookupNull()
 
94
    {
 
95
        ConfigurationInterpolator.registerGlobalLookup(TEST_PREFIX, null);
 
96
    }
 
97
 
 
98
    /**
 
99
     * 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
}