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:
018: package org.apache.commons.configuration;
019:
020: import junit.framework.TestCase;
021:
022: import javax.naming.Context;
023: import javax.naming.InitialContext;
024: import javax.naming.NamingException;
025:
026: import org.apache.commons.configuration.event.ConfigurationErrorEvent;
027: import org.apache.commons.configuration.event.ConfigurationErrorListener;
028:
029: /**
030: * Test to see if the JNDIConfiguration works properly.
031: *
032: * @version $Id: TestJNDIConfiguration.java 506128 2007-02-11 20:43:08Z oheger $
033: */
034: public class TestJNDIConfiguration extends TestCase {
035:
036: public static final String CONTEXT_FACTORY = MockInitialContextFactory.class
037: .getName();
038:
039: private JNDIConfiguration conf;
040: private NonStringTestHolder nonStringTestHolder;
041:
042: /** A test error listener for counting internal errors.*/
043: private TestErrorListener listener;
044:
045: public void setUp() throws Exception {
046:
047: System.setProperty("java.naming.factory.initial",
048: CONTEXT_FACTORY);
049:
050: conf = new PotentialErrorJNDIConfiguration();
051:
052: nonStringTestHolder = new NonStringTestHolder();
053: nonStringTestHolder.setConfiguration(conf);
054:
055: listener = new TestErrorListener();
056: conf.addErrorListener(listener);
057: }
058:
059: /**
060: * Clears the test environment. If an error listener is defined, checks
061: * whether no error event was received.
062: */
063: protected void tearDown() throws Exception {
064: if (listener != null) {
065: listener.verify();
066: }
067: super .tearDown();
068: }
069:
070: public void testBoolean() throws Exception {
071: nonStringTestHolder.testBoolean();
072: }
073:
074: public void testBooleanDefaultValue() throws Exception {
075: nonStringTestHolder.testBooleanDefaultValue();
076: }
077:
078: public void testByte() throws Exception {
079: nonStringTestHolder.testByte();
080: }
081:
082: public void testDouble() throws Exception {
083: nonStringTestHolder.testDouble();
084: }
085:
086: public void testDoubleDefaultValue() throws Exception {
087: nonStringTestHolder.testDoubleDefaultValue();
088: }
089:
090: public void testFloat() throws Exception {
091: nonStringTestHolder.testFloat();
092: }
093:
094: public void testFloatDefaultValue() throws Exception {
095: nonStringTestHolder.testFloatDefaultValue();
096: }
097:
098: public void testInteger() throws Exception {
099: nonStringTestHolder.testInteger();
100: }
101:
102: public void testIntegerDefaultValue() throws Exception {
103: nonStringTestHolder.testIntegerDefaultValue();
104: }
105:
106: public void testLong() throws Exception {
107: nonStringTestHolder.testLong();
108: }
109:
110: public void testLongDefaultValue() throws Exception {
111: nonStringTestHolder.testLongDefaultValue();
112: }
113:
114: public void testShort() throws Exception {
115: nonStringTestHolder.testShort();
116: }
117:
118: public void testShortDefaultValue() throws Exception {
119: nonStringTestHolder.testShortDefaultValue();
120: }
121:
122: public void testListMissing() throws Exception {
123: nonStringTestHolder.testListMissing();
124: }
125:
126: public void testSubset() throws Exception {
127: nonStringTestHolder.testSubset();
128: }
129:
130: public void testProperties() throws Exception {
131: Object o = conf.getProperty("test.boolean");
132: assertNotNull(o);
133: assertEquals("true", o.toString());
134: }
135:
136: public void testContainsKey() {
137: String key = "test.boolean";
138: assertTrue("'" + key + "' not found", conf.containsKey(key));
139:
140: conf.clearProperty(key);
141: assertFalse("'" + key + "' still found", conf.containsKey(key));
142: }
143:
144: public void testChangePrefix() {
145: assertEquals("'test.boolean' property", "true", conf
146: .getString("test.boolean"));
147: assertEquals("'boolean' property", null, conf
148: .getString("boolean"));
149:
150: // change the prefix
151: conf.setPrefix("test");
152: assertEquals("'test.boolean' property", null, conf
153: .getString("test.boolean"));
154: assertEquals("'boolean' property", "true", conf
155: .getString("boolean"));
156: }
157:
158: public void testResetRemovedProperties() throws Exception {
159: assertEquals("'test.boolean' property", "true", conf
160: .getString("test.boolean"));
161:
162: // remove the property
163: conf.clearProperty("test.boolean");
164: assertEquals("'test.boolean' property", null, conf
165: .getString("test.boolean"));
166:
167: // change the context
168: conf.setContext(new InitialContext());
169:
170: // get the property
171: assertEquals("'test.boolean' property", "true", conf
172: .getString("test.boolean"));
173: }
174:
175: public void testConstructor() throws Exception {
176: // test the constructor accepting a context
177: conf = new JNDIConfiguration(new InitialContext());
178:
179: assertEquals("'test.boolean' property", "true", conf
180: .getString("test.boolean"));
181:
182: // test the constructor accepting a context and a prefix
183: conf = new JNDIConfiguration(new InitialContext(), "test");
184:
185: assertEquals("'boolean' property", "true", conf
186: .getString("boolean"));
187: }
188:
189: /**
190: * Configures the test config to throw an exception.
191: */
192: private PotentialErrorJNDIConfiguration setUpErrorConfig() {
193: ((PotentialErrorJNDIConfiguration) conf).failOnGetCtx = true;
194: conf.removeErrorListener((ConfigurationErrorListener) conf
195: .getErrorListeners().iterator().next());
196: return (PotentialErrorJNDIConfiguration) conf;
197: }
198:
199: /**
200: * Tests whether the expected error events have been received.
201: *
202: * @param type the expected event type
203: * @param propName the name of the property
204: * @param propValue the property value
205: */
206: private void checkErrorListener(int type, String propName,
207: Object propValue) {
208: listener.verify(type, propName, propValue);
209: listener = null;
210: }
211:
212: /**
213: * Tests whether a JNDI configuration registers an error log listener.
214: */
215: public void testLogListener() throws NamingException {
216: conf = new JNDIConfiguration();
217: assertEquals("No error log listener registered", 1, conf
218: .getErrorListeners().size());
219: }
220:
221: /**
222: * Tests handling of errors in getKeys().
223: */
224: public void testGetKeysError() {
225: assertFalse("Iteration not empty", setUpErrorConfig().getKeys()
226: .hasNext());
227: checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY,
228: null, null);
229: }
230:
231: /**
232: * Tests handling of errors in isEmpty().
233: */
234: public void testIsEmptyError() throws NamingException {
235: assertTrue("Error config not empty", setUpErrorConfig()
236: .isEmpty());
237: checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY,
238: null, null);
239: }
240:
241: /**
242: * Tests handling of errors in the containsKey() method.
243: */
244: public void testContainsKeyError() {
245: assertFalse("Key contained after error", setUpErrorConfig()
246: .containsKey("key"));
247: checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY,
248: "key", null);
249: }
250:
251: /**
252: * Tests handling of errors in getProperty().
253: */
254: public void testGetPropertyError() {
255: assertNull("Wrong property value after error",
256: setUpErrorConfig().getProperty("key"));
257: checkErrorListener(AbstractConfiguration.EVENT_READ_PROPERTY,
258: "key", null);
259: }
260:
261: /**
262: * A special JNDI configuration implementation that can be configured to
263: * throw an exception when accessing the base context. Used for testing the
264: * exception handling.
265: */
266: static class PotentialErrorJNDIConfiguration extends
267: JNDIConfiguration {
268: /** A flag whether an exception should be thrown. */
269: boolean failOnGetCtx;
270:
271: public PotentialErrorJNDIConfiguration() throws NamingException {
272: super ();
273: }
274:
275: public Context getBaseContext() throws NamingException {
276: if (failOnGetCtx) {
277: throw new NamingException("Simulated JNDI exception!");
278: }
279: return super .getBaseContext();
280: }
281: }
282:
283: /**
284: * A test listener implementation that is used for counting and testing
285: * internal errors.
286: */
287: static class TestErrorListener implements
288: ConfigurationErrorListener {
289: /** Stores the last received error event. */
290: ConfigurationErrorEvent event;
291:
292: /** Stores the number of calls. */
293: int errorCount;
294:
295: public void configurationError(ConfigurationErrorEvent event) {
296: this .event = event;
297: errorCount++;
298: }
299:
300: /**
301: * Checks whether no error event was received.
302: */
303: public void verify() {
304: assertEquals("Error events received", 0, errorCount);
305: }
306:
307: /**
308: * Checks whether an expected error event was received.
309: *
310: * @param type the type of the event
311: * @param propName the name of the property
312: * @param propValue the value of the property
313: */
314: public void verify(int type, String propName, Object propValue) {
315: assertEquals("Wrong number of error events", 1, errorCount);
316: assertEquals("Wrong event type", type, event.getType());
317: assertTrue(
318: "Wrong property name",
319: (propName == null) ? event.getPropertyName() == null
320: : propName.equals(event.getPropertyName()));
321: assertTrue(
322: "Wrong property value",
323: (propValue == null) ? event.getPropertyValue() == null
324: : propValue
325: .equals(event.getPropertyValue()));
326: assertTrue("Wrong exception class",
327: event.getCause() instanceof NamingException);
328: }
329: }
330: }
|