1   /*
2    * Copyright 2002-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.configuration;
18  
19  import java.util.Iterator;
20  
21  import junit.framework.TestCase;
22  
23  public class TestNullJNDIEnvironmentValues extends TestCase
24  {
25      private JNDIConfiguration conf = null;
26  
27      public void setUp() throws Exception
28      {
29          System.setProperty("java.naming.factory.initial", TestJNDIConfiguration.CONTEXT_FACTORY);
30          
31          conf = new JNDIConfiguration();
32          conf.setThrowExceptionOnMissing(false);
33      }
34  
35      public void testThrowExceptionOnMissing()
36      {
37          assertFalse("Throw Exception Property is set!", conf.isThrowExceptionOnMissing());
38      }
39  
40      public void testSimpleGet() throws Exception
41      {
42          String s = conf.getString("test.key");
43          assertEquals("jndivalue", s);
44      }
45  
46      public void testMoreGets() throws Exception
47      {
48          String s = conf.getString("test.key");
49          assertEquals("jndivalue", s);
50          assertEquals("jndivalue2", conf.getString("test.key2"));
51          assertEquals(1, conf.getShort("test.short"));
52      }
53  
54      public void testGetMissingKey() throws Exception
55      {
56          assertNull("Missing Key is not null!", conf.getString("test.imaginarykey"));
57      }
58  
59      public void testGetMissingKeyWithDefault() throws Exception
60      {
61          String result = conf.getString("test.imaginarykey", "bob");
62          assertEquals("bob", result);
63      }
64  
65      public void testContainsKey() throws Exception
66      {
67          assertTrue(conf.containsKey("test.key"));
68          assertTrue(!conf.containsKey("test.imaginerykey"));
69      }
70      
71      public void testClearProperty()
72      {
73          assertNotNull("null short for the 'test.short' key", conf.getShort("test.short", null));
74          conf.clearProperty("test.short");
75          assertNull("'test.short' property not cleared", conf.getShort("test.short", null));
76      }
77      
78      public void testIsEmpty()
79      {
80          assertFalse("the configuration shouldn't be empty", conf.isEmpty());
81      }
82      
83      public void testGetKeys() throws Exception
84      {
85          boolean found = false;
86          Iterator it = conf.getKeys();
87  
88          assertTrue("no key found", it.hasNext());
89  
90          while (it.hasNext() && !found)
91          {
92              found = "test.boolean".equals(it.next());
93          }
94  
95          assertTrue("'test.boolean' key not found", found);
96      }
97  
98      public void testGetKeysWithUnknownPrefix()
99      {
100         // test for a unknown prefix
101         Iterator it = conf.getKeys("foo.bar");
102         assertFalse("no key should be found", it.hasNext());
103     }
104 
105     public void testGetKeysWithExistingPrefix()
106     {
107         // test for an existing prefix
108         Iterator it = conf.getKeys("test");
109         boolean found = false;
110         while (it.hasNext() && !found)
111         {
112             found = "test.boolean".equals(it.next());
113         }
114 
115         assertTrue("'test.boolean' key not found", found);
116     }
117 
118     public void testGetKeysWithKeyAsPrefix()
119     {
120         // test for a prefix matching exactly the key of a property
121         Iterator it = conf.getKeys("test.boolean");
122         boolean found = false;
123         while (it.hasNext() && !found)
124         {
125             found = "test.boolean".equals(it.next());
126         }
127 
128         assertTrue("'test.boolean' key not found", found);
129     }
130 
131 }