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 static org.junit.Assert.assertEquals;
21
import static org.junit.Assert.assertFalse;
22
import static org.junit.Assert.assertNotNull;
23
import static org.junit.Assert.assertNull;
24
import static org.junit.Assert.assertTrue;
26
import java.util.Iterator;
28
import org.junit.Before;
29
import org.junit.Test;
31
public class TestNullJNDIEnvironmentValues
33
private JNDIConfiguration conf = null;
36
public void setUp() throws Exception
38
System.setProperty("java.naming.factory.initial", TestJNDIConfiguration.CONTEXT_FACTORY);
40
conf = new JNDIConfiguration();
41
conf.setThrowExceptionOnMissing(false);
45
public void testThrowExceptionOnMissing()
47
assertFalse("Throw Exception Property is set!", conf.isThrowExceptionOnMissing());
51
public void testSimpleGet() throws Exception
53
String s = conf.getString("test.key");
54
assertEquals("jndivalue", s);
58
public void testMoreGets() throws Exception
60
String s = conf.getString("test.key");
61
assertEquals("jndivalue", s);
62
assertEquals("jndivalue2", conf.getString("test.key2"));
63
assertEquals(1, conf.getShort("test.short"));
67
public void testGetMissingKey() throws Exception
69
assertNull("Missing Key is not null!", conf.getString("test.imaginarykey"));
73
public void testGetMissingKeyWithDefault() throws Exception
75
String result = conf.getString("test.imaginarykey", "bob");
76
assertEquals("bob", result);
80
public void testContainsKey() throws Exception
82
assertTrue(conf.containsKey("test.key"));
83
assertTrue(!conf.containsKey("test.imaginarykey"));
87
public void testClearProperty()
89
assertNotNull("null short for the 'test.short' key", conf.getShort("test.short", null));
90
conf.clearProperty("test.short");
91
assertNull("'test.short' property not cleared", conf.getShort("test.short", null));
95
public void testIsEmpty()
97
assertFalse("the configuration shouldn't be empty", conf.isEmpty());
101
public void testGetKeys() throws Exception
103
boolean found = false;
104
Iterator<String> it = conf.getKeys();
106
assertTrue("no key found", it.hasNext());
108
while (it.hasNext() && !found)
110
found = "test.boolean".equals(it.next());
113
assertTrue("'test.boolean' key not found", found);
117
public void testGetKeysWithUnknownPrefix()
119
// test for a unknown prefix
120
Iterator<String> it = conf.getKeys("foo.bar");
121
assertFalse("no key should be found", it.hasNext());
125
public void testGetKeysWithExistingPrefix()
127
// test for an existing prefix
128
Iterator<String> it = conf.getKeys("test");
129
boolean found = false;
130
while (it.hasNext() && !found)
132
found = "test.boolean".equals(it.next());
135
assertTrue("'test.boolean' key not found", found);
139
public void testGetKeysWithKeyAsPrefix()
141
// test for a prefix matching exactly the key of a property
142
Iterator<String> it = conf.getKeys("test.boolean");
143
boolean found = false;
144
while (it.hasNext() && !found)
146
found = "test.boolean".equals(it.next());
149
assertTrue("'test.boolean' key not found", found);