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.Iterator;
21
import java.util.NoSuchElementException;
23
import junit.framework.TestCase;
25
public class TestJNDIEnvironmentValues extends TestCase
27
private JNDIConfiguration conf = null;
29
public void setUp() throws Exception
31
System.setProperty("java.naming.factory.initial", TestJNDIConfiguration.CONTEXT_FACTORY);
33
conf = new JNDIConfiguration();
34
conf.setThrowExceptionOnMissing(true);
37
public void testThrowExceptionOnMissing()
39
assertTrue("Throw Exception Property is not set!", conf.isThrowExceptionOnMissing());
42
public void testSimpleGet() throws Exception
44
String s = conf.getString("test.key");
45
assertEquals("jndivalue", s);
48
public void testMoreGets() throws Exception
50
String s = conf.getString("test.key");
51
assertEquals("jndivalue", s);
52
assertEquals("jndivalue2", conf.getString("test.key2"));
53
assertEquals(1, conf.getShort("test.short"));
56
public void testGetMissingKey() throws Exception
60
conf.getString("test.imaginarykey");
61
fail("Should have thrown NoSuchElementException");
63
catch (NoSuchElementException e)
65
assertTrue(e.getMessage(), e.getMessage().indexOf("test.imaginarykey") != -1);
69
public void testGetMissingKeyWithDefault() throws Exception
71
String result = conf.getString("test.imaginarykey", "bob");
72
assertEquals("bob", result);
75
public void testContainsKey() throws Exception
77
assertTrue(conf.containsKey("test.key"));
78
assertTrue(!conf.containsKey("test.imaginarykey"));
81
public void testClearProperty()
83
assertNotNull("null short for the 'test.short' key", conf.getShort("test.short", null));
84
conf.clearProperty("test.short");
85
assertNull("'test.short' property not cleared", conf.getShort("test.short", null));
88
public void testIsEmpty()
90
assertFalse("the configuration shouldn't be empty", conf.isEmpty());
93
public void testGetKeys() throws Exception
95
boolean found = false;
96
Iterator it = conf.getKeys();
98
assertTrue("no key found", it.hasNext());
100
while (it.hasNext() && !found)
102
found = "test.boolean".equals(it.next());
105
assertTrue("'test.boolean' key not found", found);
108
public void testGetKeysWithUnknownPrefix()
110
// test for a unknown prefix
111
Iterator it = conf.getKeys("foo.bar");
112
assertFalse("no key should be found", it.hasNext());
115
public void testGetKeysWithExistingPrefix()
117
// test for an existing prefix
118
Iterator it = conf.getKeys("test");
119
boolean found = false;
120
while (it.hasNext() && !found)
122
found = "test.boolean".equals(it.next());
125
assertTrue("'test.boolean' key not found", found);
128
public void testGetKeysWithKeyAsPrefix()
130
// test for a prefix matching exactly the key of a property
131
Iterator it = conf.getKeys("test.boolean");
132
boolean found = false;
133
while (it.hasNext() && !found)
135
found = "test.boolean".equals(it.next());
138
assertTrue("'test.boolean' key not found", found);