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