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