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 java.util.Iterator;
021:
022: import junit.framework.TestCase;
023:
024: public class TestNullJNDIEnvironmentValues extends TestCase {
025: private JNDIConfiguration conf = null;
026:
027: public void setUp() throws Exception {
028: System.setProperty("java.naming.factory.initial",
029: TestJNDIConfiguration.CONTEXT_FACTORY);
030:
031: conf = new JNDIConfiguration();
032: conf.setThrowExceptionOnMissing(false);
033: }
034:
035: public void testThrowExceptionOnMissing() {
036: assertFalse("Throw Exception Property is set!", conf
037: .isThrowExceptionOnMissing());
038: }
039:
040: public void testSimpleGet() throws Exception {
041: String s = conf.getString("test.key");
042: assertEquals("jndivalue", s);
043: }
044:
045: public void testMoreGets() throws Exception {
046: String s = conf.getString("test.key");
047: assertEquals("jndivalue", s);
048: assertEquals("jndivalue2", conf.getString("test.key2"));
049: assertEquals(1, conf.getShort("test.short"));
050: }
051:
052: public void testGetMissingKey() throws Exception {
053: assertNull("Missing Key is not null!", conf
054: .getString("test.imaginarykey"));
055: }
056:
057: public void testGetMissingKeyWithDefault() throws Exception {
058: String result = conf.getString("test.imaginarykey", "bob");
059: assertEquals("bob", result);
060: }
061:
062: public void testContainsKey() throws Exception {
063: assertTrue(conf.containsKey("test.key"));
064: assertTrue(!conf.containsKey("test.imaginarykey"));
065: }
066:
067: public void testClearProperty() {
068: assertNotNull("null short for the 'test.short' key", conf
069: .getShort("test.short", null));
070: conf.clearProperty("test.short");
071: assertNull("'test.short' property not cleared", conf.getShort(
072: "test.short", null));
073: }
074:
075: public void testIsEmpty() {
076: assertFalse("the configuration shouldn't be empty", conf
077: .isEmpty());
078: }
079:
080: public void testGetKeys() throws Exception {
081: boolean found = false;
082: Iterator it = conf.getKeys();
083:
084: assertTrue("no key found", it.hasNext());
085:
086: while (it.hasNext() && !found) {
087: found = "test.boolean".equals(it.next());
088: }
089:
090: assertTrue("'test.boolean' key not found", found);
091: }
092:
093: public void testGetKeysWithUnknownPrefix() {
094: // test for a unknown prefix
095: Iterator it = conf.getKeys("foo.bar");
096: assertFalse("no key should be found", it.hasNext());
097: }
098:
099: public void testGetKeysWithExistingPrefix() {
100: // test for an existing prefix
101: Iterator it = conf.getKeys("test");
102: boolean found = false;
103: while (it.hasNext() && !found) {
104: found = "test.boolean".equals(it.next());
105: }
106:
107: assertTrue("'test.boolean' key not found", found);
108: }
109:
110: public void testGetKeysWithKeyAsPrefix() {
111: // test for a prefix matching exactly the key of a property
112: Iterator it = conf.getKeys("test.boolean");
113: boolean found = false;
114: while (it.hasNext() && !found) {
115: found = "test.boolean".equals(it.next());
116: }
117:
118: assertTrue("'test.boolean' key not found", found);
119: }
120:
121: }
|