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

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/configuration/TestJNDIConfiguration.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
 
 
18
 
package org.apache.commons.configuration;
19
 
 
20
 
import java.util.Hashtable;
21
 
import java.util.Properties;
22
 
 
23
 
import javax.naming.Context;
24
 
import javax.naming.InitialContext;
25
 
import javax.naming.NameNotFoundException;
26
 
import javax.naming.NamingException;
27
 
 
28
 
import junit.framework.TestCase;
29
 
 
30
 
import org.apache.commons.configuration.event.ConfigurationErrorListener;
31
 
 
32
 
/**
33
 
 * Test to see if the JNDIConfiguration works properly.
34
 
 *
35
 
 * @version $Id: TestJNDIConfiguration.java 1081926 2011-03-15 20:20:25Z oheger $
36
 
 */
37
 
public class TestJNDIConfiguration extends TestCase {
38
 
 
39
 
    public static final String CONTEXT_FACTORY = MockInitialContextFactory.class.getName();
40
 
 
41
 
    private PotentialErrorJNDIConfiguration conf;
42
 
    private NonStringTestHolder nonStringTestHolder;
43
 
 
44
 
    /** A test error listener for counting internal errors.*/
45
 
    private ConfigurationErrorListenerImpl listener;
46
 
 
47
 
    public void setUp() throws Exception {
48
 
 
49
 
        System.setProperty("java.naming.factory.initial", CONTEXT_FACTORY);
50
 
 
51
 
        Properties props = new Properties();
52
 
        props.put("java.naming.factory.initial", CONTEXT_FACTORY);
53
 
        Context ctx = new InitialContext(props);
54
 
        conf = new PotentialErrorJNDIConfiguration(ctx);
55
 
 
56
 
        nonStringTestHolder = new NonStringTestHolder();
57
 
        nonStringTestHolder.setConfiguration(conf);
58
 
 
59
 
        listener = new ConfigurationErrorListenerImpl();
60
 
        conf.addErrorListener(listener);
61
 
    }
62
 
 
63
 
    /**
64
 
     * Clears the test environment. If an error listener is defined, checks
65
 
     * whether no error event was received.
66
 
     */
67
 
    protected void tearDown() throws Exception
68
 
    {
69
 
        if (listener != null)
70
 
        {
71
 
            listener.verify();
72
 
        }
73
 
        super.tearDown();
74
 
    }
75
 
 
76
 
    public void testBoolean() throws Exception {
77
 
        nonStringTestHolder.testBoolean();
78
 
    }
79
 
 
80
 
    public void testBooleanDefaultValue() throws Exception {
81
 
        nonStringTestHolder.testBooleanDefaultValue();
82
 
    }
83
 
 
84
 
    public void testByte() throws Exception {
85
 
        nonStringTestHolder.testByte();
86
 
    }
87
 
 
88
 
    public void testDouble() throws Exception {
89
 
        nonStringTestHolder.testDouble();
90
 
    }
91
 
 
92
 
    public void testDoubleDefaultValue() throws Exception {
93
 
        nonStringTestHolder.testDoubleDefaultValue();
94
 
    }
95
 
 
96
 
    public void testFloat() throws Exception {
97
 
        nonStringTestHolder.testFloat();
98
 
    }
99
 
 
100
 
    public void testFloatDefaultValue() throws Exception {
101
 
        nonStringTestHolder.testFloatDefaultValue();
102
 
    }
103
 
 
104
 
    public void testInteger() throws Exception {
105
 
        nonStringTestHolder.testInteger();
106
 
    }
107
 
 
108
 
    public void testIntegerDefaultValue() throws Exception {
109
 
        nonStringTestHolder.testIntegerDefaultValue();
110
 
    }
111
 
 
112
 
    public void testLong() throws Exception {
113
 
        nonStringTestHolder.testLong();
114
 
    }
115
 
 
116
 
    public void testLongDefaultValue() throws Exception {
117
 
        nonStringTestHolder.testLongDefaultValue();
118
 
    }
119
 
 
120
 
    public void testShort() throws Exception {
121
 
        nonStringTestHolder.testShort();
122
 
    }
123
 
 
124
 
    public void testShortDefaultValue() throws Exception {
125
 
        nonStringTestHolder.testShortDefaultValue();
126
 
    }
127
 
 
128
 
    public void testListMissing() throws Exception {
129
 
        nonStringTestHolder.testListMissing();
130
 
    }
131
 
 
132
 
    public void testSubset() throws Exception {
133
 
        nonStringTestHolder.testSubset();
134
 
    }
135
 
 
136
 
    public void testProperties() throws Exception {
137
 
        Object o = conf.getProperty("test.boolean");
138
 
        assertNotNull(o);
139
 
        assertEquals("true", o.toString());
140
 
    }
141
 
 
142
 
    public void testContainsKey()
143
 
    {
144
 
        String key = "test.boolean";
145
 
        assertTrue("'" + key + "' not found", conf.containsKey(key));
146
 
 
147
 
        conf.clearProperty(key);
148
 
        assertFalse("'" + key + "' still found", conf.containsKey(key));
149
 
    }
150
 
 
151
 
    public void testChangePrefix()
152
 
    {
153
 
        assertEquals("'test.boolean' property", "true", conf.getString("test.boolean"));
154
 
        assertEquals("'boolean' property", null, conf.getString("boolean"));
155
 
 
156
 
        // change the prefix
157
 
        conf.setPrefix("test");
158
 
        assertEquals("'test.boolean' property", null, conf.getString("test.boolean"));
159
 
        assertEquals("'boolean' property", "true", conf.getString("boolean"));
160
 
    }
161
 
 
162
 
    public void testResetRemovedProperties() throws Exception
163
 
    {
164
 
        assertEquals("'test.boolean' property", "true", conf.getString("test.boolean"));
165
 
 
166
 
        // remove the property
167
 
        conf.clearProperty("test.boolean");
168
 
        assertEquals("'test.boolean' property", null, conf.getString("test.boolean"));
169
 
 
170
 
        // change the context
171
 
        conf.setContext(new InitialContext());
172
 
 
173
 
        // get the property
174
 
        assertEquals("'test.boolean' property", "true", conf.getString("test.boolean"));
175
 
    }
176
 
 
177
 
    public void testConstructor() throws Exception
178
 
    {
179
 
        // test the constructor accepting a context
180
 
        JNDIConfiguration c = new JNDIConfiguration(new InitialContext());
181
 
 
182
 
        assertEquals("'test.boolean' property", "true", c.getString("test.boolean"));
183
 
 
184
 
        // test the constructor accepting a context and a prefix
185
 
        c = new JNDIConfiguration(new InitialContext(), "test");
186
 
 
187
 
        assertEquals("'boolean' property", "true", c.getString("boolean"));
188
 
    }
189
 
 
190
 
    /**
191
 
     * Configures the test config to throw an exception.
192
 
     */
193
 
    private PotentialErrorJNDIConfiguration setUpErrorConfig()
194
 
    {
195
 
        conf.installException();
196
 
        conf.removeErrorListener((ConfigurationErrorListener) conf
197
 
                .getErrorListeners().iterator().next());
198
 
        return (PotentialErrorJNDIConfiguration) conf;
199
 
    }
200
 
 
201
 
    /**
202
 
     * Tests whether the expected error events have been received.
203
 
     *
204
 
     * @param type the expected event type
205
 
     * @param propName the name of the property
206
 
     * @param propValue the property value
207
 
     */
208
 
    private void checkErrorListener(int type, String propName, Object propValue)
209
 
    {
210
 
        listener.verify(type, propName, propValue);
211
 
        assertTrue("Wrong exception class",
212
 
                listener.getLastEvent().getCause() instanceof NamingException);
213
 
        listener = null;
214
 
    }
215
 
 
216
 
    /**
217
 
     * Tests whether a JNDI configuration registers an error log listener.
218
 
     */
219
 
    public void testLogListener() throws NamingException
220
 
    {
221
 
        JNDIConfiguration c = new JNDIConfiguration();
222
 
        assertEquals("No error log listener registered", 1, c
223
 
                .getErrorListeners().size());
224
 
    }
225
 
 
226
 
    /**
227
 
     * Tests handling of errors in getKeys().
228
 
     */
229
 
    public void testGetKeysError()
230
 
    {
231
 
        assertFalse("Iteration not empty", setUpErrorConfig().getKeys()
232
 
                .hasNext());
233
 
        checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, null,
234
 
                null);
235
 
    }
236
 
 
237
 
    /**
238
 
     * Tests handling of errors in isEmpty().
239
 
     */
240
 
    public void testIsEmptyError() throws NamingException
241
 
    {
242
 
        assertTrue("Error config not empty", setUpErrorConfig().isEmpty());
243
 
        checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, null,
244
 
                null);
245
 
    }
246
 
 
247
 
    /**
248
 
     * Tests handling of errors in the containsKey() method.
249
 
     */
250
 
    public void testContainsKeyError()
251
 
    {
252
 
        assertFalse("Key contained after error", setUpErrorConfig()
253
 
                .containsKey("key"));
254
 
        checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, "key",
255
 
                null);
256
 
    }
257
 
 
258
 
    /**
259
 
     * Tests handling of errors in getProperty().
260
 
     */
261
 
    public void testGetPropertyError()
262
 
    {
263
 
        assertNull("Wrong property value after error", setUpErrorConfig()
264
 
                .getProperty("key"));
265
 
        checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, "key",
266
 
                null);
267
 
    }
268
 
 
269
 
    /**
270
 
     * Tests the getKeys() method when there are cycles in the tree.
271
 
     */
272
 
    public void testGetKeysWithCycles() throws NamingException
273
 
    {
274
 
        Hashtable env = new Hashtable();
275
 
        env.put(MockInitialContextFactory.PROP_CYCLES, Boolean.TRUE);
276
 
        InitialContext initCtx = new InitialContext(env);
277
 
        JNDIConfiguration c = new JNDIConfiguration(initCtx);
278
 
        c.getKeys("cycle");
279
 
    }
280
 
 
281
 
    /**
282
 
     * Tests getKeys() if no data is found. This should not cause a problem and
283
 
     * not notify the error listeners.
284
 
     */
285
 
    public void testGetKeysNoData()
286
 
    {
287
 
        conf.installException(new NameNotFoundException("Test exception"));
288
 
        assertFalse("Got keys", conf.getKeys().hasNext());
289
 
        listener.verify();
290
 
    }
291
 
 
292
 
    /**
293
 
     * A special JNDI configuration implementation that can be configured to
294
 
     * throw an exception when accessing the base context. Used for testing the
295
 
     * exception handling.
296
 
     */
297
 
    public static class PotentialErrorJNDIConfiguration extends
298
 
            JNDIConfiguration
299
 
    {
300
 
        /** An exception to be thrown by getBaseContext(). */
301
 
        private NamingException exception;
302
 
 
303
 
        public PotentialErrorJNDIConfiguration(Context ctx)
304
 
                throws NamingException
305
 
        {
306
 
            super(ctx);
307
 
        }
308
 
 
309
 
        /**
310
 
         * Prepares this object to throw an exception when the JNDI context is
311
 
         * queried.
312
 
         *
313
 
         * @param nex the exception to be thrown
314
 
         */
315
 
        public void installException(NamingException nex)
316
 
        {
317
 
            exception = nex;
318
 
        }
319
 
 
320
 
        /**
321
 
         * Prepares this object to throw a standard exception when the JNDI
322
 
         * context is queried.
323
 
         */
324
 
        public void installException()
325
 
        {
326
 
            installException(new NamingException("Simulated JNDI exception!"));
327
 
        }
328
 
 
329
 
        /**
330
 
         * Returns the JNDI context. Optionally throws an exception.
331
 
         */
332
 
        public Context getBaseContext() throws NamingException
333
 
        {
334
 
            if (exception != null)
335
 
            {
336
 
                throw exception;
337
 
            }
338
 
            return super.getBaseContext();
339
 
        }
340
 
    }
341
 
}
 
 
b'\\ No newline at end of file'