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;
22
import junit.framework.TestCase;
24
public class TestNullJNDIEnvironmentValues extends TestCase
26
private JNDIConfiguration conf = null;
28
public void setUp() throws Exception
30
System.setProperty("java.naming.factory.initial", TestJNDIConfiguration.CONTEXT_FACTORY);
32
conf = new JNDIConfiguration();
33
conf.setThrowExceptionOnMissing(false);
36
public void testThrowExceptionOnMissing()
38
assertFalse("Throw Exception Property is set!", conf.isThrowExceptionOnMissing());
41
public void testSimpleGet() throws Exception
43
String s = conf.getString("test.key");
44
assertEquals("jndivalue", s);
47
public void testMoreGets() throws Exception
49
String s = conf.getString("test.key");
50
assertEquals("jndivalue", s);
51
assertEquals("jndivalue2", conf.getString("test.key2"));
52
assertEquals(1, conf.getShort("test.short"));
55
public void testGetMissingKey() throws Exception
57
assertNull("Missing Key is not null!", conf.getString("test.imaginarykey"));
60
public void testGetMissingKeyWithDefault() throws Exception
62
String result = conf.getString("test.imaginarykey", "bob");
63
assertEquals("bob", result);
66
public void testContainsKey() throws Exception
68
assertTrue(conf.containsKey("test.key"));
69
assertTrue(!conf.containsKey("test.imaginarykey"));
72
public void testClearProperty()
74
assertNotNull("null short for the 'test.short' key", conf.getShort("test.short", null));
75
conf.clearProperty("test.short");
76
assertNull("'test.short' property not cleared", conf.getShort("test.short", null));
79
public void testIsEmpty()
81
assertFalse("the configuration shouldn't be empty", conf.isEmpty());
84
public void testGetKeys() throws Exception
86
boolean found = false;
87
Iterator it = conf.getKeys();
89
assertTrue("no key found", it.hasNext());
91
while (it.hasNext() && !found)
93
found = "test.boolean".equals(it.next());
96
assertTrue("'test.boolean' key not found", found);
99
public void testGetKeysWithUnknownPrefix()
101
// test for a unknown prefix
102
Iterator it = conf.getKeys("foo.bar");
103
assertFalse("no key should be found", it.hasNext());
106
public void testGetKeysWithExistingPrefix()
108
// test for an existing prefix
109
Iterator it = conf.getKeys("test");
110
boolean found = false;
111
while (it.hasNext() && !found)
113
found = "test.boolean".equals(it.next());
116
assertTrue("'test.boolean' key not found", found);
119
public void testGetKeysWithKeyAsPrefix()
121
// test for a prefix matching exactly the key of a property
122
Iterator it = conf.getKeys("test.boolean");
123
boolean found = false;
124
while (it.hasNext() && !found)
126
found = "test.boolean".equals(it.next());
129
assertTrue("'test.boolean' key not found", found);