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