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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.commons.configuration;
20
import java.util.Hashtable;
21
import java.util.Properties;
23
import javax.naming.Context;
24
import javax.naming.InitialContext;
25
import javax.naming.NameNotFoundException;
26
import javax.naming.NamingException;
28
import junit.framework.TestCase;
30
import org.apache.commons.configuration.event.ConfigurationErrorListener;
33
* Test to see if the JNDIConfiguration works properly.
35
* @version $Id: TestJNDIConfiguration.java 1081926 2011-03-15 20:20:25Z oheger $
37
public class TestJNDIConfiguration extends TestCase {
39
public static final String CONTEXT_FACTORY = MockInitialContextFactory.class.getName();
41
private PotentialErrorJNDIConfiguration conf;
42
private NonStringTestHolder nonStringTestHolder;
44
/** A test error listener for counting internal errors.*/
45
private ConfigurationErrorListenerImpl listener;
47
public void setUp() throws Exception {
49
System.setProperty("java.naming.factory.initial", CONTEXT_FACTORY);
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);
56
nonStringTestHolder = new NonStringTestHolder();
57
nonStringTestHolder.setConfiguration(conf);
59
listener = new ConfigurationErrorListenerImpl();
60
conf.addErrorListener(listener);
64
* Clears the test environment. If an error listener is defined, checks
65
* whether no error event was received.
67
protected void tearDown() throws Exception
76
public void testBoolean() throws Exception {
77
nonStringTestHolder.testBoolean();
80
public void testBooleanDefaultValue() throws Exception {
81
nonStringTestHolder.testBooleanDefaultValue();
84
public void testByte() throws Exception {
85
nonStringTestHolder.testByte();
88
public void testDouble() throws Exception {
89
nonStringTestHolder.testDouble();
92
public void testDoubleDefaultValue() throws Exception {
93
nonStringTestHolder.testDoubleDefaultValue();
96
public void testFloat() throws Exception {
97
nonStringTestHolder.testFloat();
100
public void testFloatDefaultValue() throws Exception {
101
nonStringTestHolder.testFloatDefaultValue();
104
public void testInteger() throws Exception {
105
nonStringTestHolder.testInteger();
108
public void testIntegerDefaultValue() throws Exception {
109
nonStringTestHolder.testIntegerDefaultValue();
112
public void testLong() throws Exception {
113
nonStringTestHolder.testLong();
116
public void testLongDefaultValue() throws Exception {
117
nonStringTestHolder.testLongDefaultValue();
120
public void testShort() throws Exception {
121
nonStringTestHolder.testShort();
124
public void testShortDefaultValue() throws Exception {
125
nonStringTestHolder.testShortDefaultValue();
128
public void testListMissing() throws Exception {
129
nonStringTestHolder.testListMissing();
132
public void testSubset() throws Exception {
133
nonStringTestHolder.testSubset();
136
public void testProperties() throws Exception {
137
Object o = conf.getProperty("test.boolean");
139
assertEquals("true", o.toString());
142
public void testContainsKey()
144
String key = "test.boolean";
145
assertTrue("'" + key + "' not found", conf.containsKey(key));
147
conf.clearProperty(key);
148
assertFalse("'" + key + "' still found", conf.containsKey(key));
151
public void testChangePrefix()
153
assertEquals("'test.boolean' property", "true", conf.getString("test.boolean"));
154
assertEquals("'boolean' property", null, conf.getString("boolean"));
157
conf.setPrefix("test");
158
assertEquals("'test.boolean' property", null, conf.getString("test.boolean"));
159
assertEquals("'boolean' property", "true", conf.getString("boolean"));
162
public void testResetRemovedProperties() throws Exception
164
assertEquals("'test.boolean' property", "true", conf.getString("test.boolean"));
166
// remove the property
167
conf.clearProperty("test.boolean");
168
assertEquals("'test.boolean' property", null, conf.getString("test.boolean"));
170
// change the context
171
conf.setContext(new InitialContext());
174
assertEquals("'test.boolean' property", "true", conf.getString("test.boolean"));
177
public void testConstructor() throws Exception
179
// test the constructor accepting a context
180
JNDIConfiguration c = new JNDIConfiguration(new InitialContext());
182
assertEquals("'test.boolean' property", "true", c.getString("test.boolean"));
184
// test the constructor accepting a context and a prefix
185
c = new JNDIConfiguration(new InitialContext(), "test");
187
assertEquals("'boolean' property", "true", c.getString("boolean"));
191
* Configures the test config to throw an exception.
193
private PotentialErrorJNDIConfiguration setUpErrorConfig()
195
conf.installException();
196
conf.removeErrorListener((ConfigurationErrorListener) conf
197
.getErrorListeners().iterator().next());
198
return (PotentialErrorJNDIConfiguration) conf;
202
* Tests whether the expected error events have been received.
204
* @param type the expected event type
205
* @param propName the name of the property
206
* @param propValue the property value
208
private void checkErrorListener(int type, String propName, Object propValue)
210
listener.verify(type, propName, propValue);
211
assertTrue("Wrong exception class",
212
listener.getLastEvent().getCause() instanceof NamingException);
217
* Tests whether a JNDI configuration registers an error log listener.
219
public void testLogListener() throws NamingException
221
JNDIConfiguration c = new JNDIConfiguration();
222
assertEquals("No error log listener registered", 1, c
223
.getErrorListeners().size());
227
* Tests handling of errors in getKeys().
229
public void testGetKeysError()
231
assertFalse("Iteration not empty", setUpErrorConfig().getKeys()
233
checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, null,
238
* Tests handling of errors in isEmpty().
240
public void testIsEmptyError() throws NamingException
242
assertTrue("Error config not empty", setUpErrorConfig().isEmpty());
243
checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, null,
248
* Tests handling of errors in the containsKey() method.
250
public void testContainsKeyError()
252
assertFalse("Key contained after error", setUpErrorConfig()
253
.containsKey("key"));
254
checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, "key",
259
* Tests handling of errors in getProperty().
261
public void testGetPropertyError()
263
assertNull("Wrong property value after error", setUpErrorConfig()
264
.getProperty("key"));
265
checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY, "key",
270
* Tests the getKeys() method when there are cycles in the tree.
272
public void testGetKeysWithCycles() throws NamingException
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);
282
* Tests getKeys() if no data is found. This should not cause a problem and
283
* not notify the error listeners.
285
public void testGetKeysNoData()
287
conf.installException(new NameNotFoundException("Test exception"));
288
assertFalse("Got keys", conf.getKeys().hasNext());
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.
297
public static class PotentialErrorJNDIConfiguration extends
300
/** An exception to be thrown by getBaseContext(). */
301
private NamingException exception;
303
public PotentialErrorJNDIConfiguration(Context ctx)
304
throws NamingException
310
* Prepares this object to throw an exception when the JNDI context is
313
* @param nex the exception to be thrown
315
public void installException(NamingException nex)
321
* Prepares this object to throw a standard exception when the JNDI
322
* context is queried.
324
public void installException()
326
installException(new NamingException("Simulated JNDI exception!"));
330
* Returns the JNDI context. Optionally throws an exception.
332
public Context getBaseContext() throws NamingException
334
if (exception != null)
338
return super.getBaseContext();
b'\\ No newline at end of file'