1   /*
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
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
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.
16   */
17  
18  package org.apache.commons.configuration;
19  
20  import java.util.Iterator;
21  
22  import junit.framework.TestCase;
23  
24  public class TestNullJNDIEnvironmentValues extends TestCase
25  {
26      private JNDIConfiguration conf = null;
27  
28      public void setUp() throws Exception
29      {
30          System.setProperty("java.naming.factory.initial", TestJNDIConfiguration.CONTEXT_FACTORY);
31          
32          conf = new JNDIConfiguration();
33          conf.setThrowExceptionOnMissing(false);
34      }
35  
36      public void testThrowExceptionOnMissing()
37      {
38          assertFalse("Throw Exception Property is set!", conf.isThrowExceptionOnMissing());
39      }
40  
41      public void testSimpleGet() throws Exception
42      {
43          String s = conf.getString("test.key");
44          assertEquals("jndivalue", s);
45      }
46  
47      public void testMoreGets() throws Exception
48      {
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"));
53      }
54  
55      public void testGetMissingKey() throws Exception
56      {
57          assertNull("Missing Key is not null!", conf.getString("test.imaginarykey"));
58      }
59  
60      public void testGetMissingKeyWithDefault() throws Exception
61      {
62          String result = conf.getString("test.imaginarykey", "bob");
63          assertEquals("bob", result);
64      }
65  
66      public void testContainsKey() throws Exception
67      {
68          assertTrue(conf.containsKey("test.key"));
69          assertTrue(!conf.containsKey("test.imaginerykey"));
70      }
71      
72      public void testClearProperty()
73      {
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));
77      }
78      
79      public void testIsEmpty()
80      {
81          assertFalse("the configuration shouldn't be empty", conf.isEmpty());
82      }
83      
84      public void testGetKeys() throws Exception
85      {
86          boolean found = false;
87          Iterator it = conf.getKeys();
88  
89          assertTrue("no key found", it.hasNext());
90  
91          while (it.hasNext() && !found)
92          {
93              found = "test.boolean".equals(it.next());
94          }
95  
96          assertTrue("'test.boolean' key not found", found);
97      }
98  
99      public void testGetKeysWithUnknownPrefix()
100     {
101         // test for a unknown prefix
102         Iterator it = conf.getKeys("foo.bar");
103         assertFalse("no key should be found", it.hasNext());
104     }
105 
106     public void testGetKeysWithExistingPrefix()
107     {
108         // test for an existing prefix
109         Iterator it = conf.getKeys("test");
110         boolean found = false;
111         while (it.hasNext() && !found)
112         {
113             found = "test.boolean".equals(it.next());
114         }
115 
116         assertTrue("'test.boolean' key not found", found);
117     }
118 
119     public void testGetKeysWithKeyAsPrefix()
120     {
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)
125         {
126             found = "test.boolean".equals(it.next());
127         }
128 
129         assertTrue("'test.boolean' key not found", found);
130     }
131 
132 }