View Javadoc
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.configuration2;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.util.Iterator;
27  
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  public class TestNullJNDIEnvironmentValues {
32      private JNDIConfiguration conf;
33  
34      @BeforeEach
35      public void setUp() throws Exception {
36          System.setProperty("java.naming.factory.initial", TestJNDIConfiguration.CONTEXT_FACTORY);
37  
38          conf = new JNDIConfiguration();
39          conf.setThrowExceptionOnMissing(false);
40      }
41  
42      @Test
43      public void testClearProperty() {
44          assertNotNull(conf.getShort("test.short", null));
45          conf.clearProperty("test.short");
46          assertNull(conf.getShort("test.short", null));
47      }
48  
49      @Test
50      public void testContainsKey() throws Exception {
51          assertTrue(conf.containsKey("test.key"));
52          assertFalse(conf.containsKey("test.imaginarykey"));
53      }
54  
55      @Test
56      public void testGetKeys() throws Exception {
57          boolean found = false;
58          final Iterator<String> it = conf.getKeys();
59  
60          assertTrue(it.hasNext());
61  
62          while (it.hasNext() && !found) {
63              found = "test.boolean".equals(it.next());
64          }
65  
66          assertTrue(found);
67      }
68  
69      @Test
70      public void testGetKeysWithExistingPrefix() {
71          // test for an existing prefix
72          final Iterator<String> it = conf.getKeys("test");
73          boolean found = false;
74          while (it.hasNext() && !found) {
75              found = "test.boolean".equals(it.next());
76          }
77  
78          assertTrue(found);
79      }
80  
81      @Test
82      public void testGetKeysWithKeyAsPrefix() {
83          // test for a prefix matching exactly the key of a property
84          final Iterator<String> it = conf.getKeys("test.boolean");
85          boolean found = false;
86          while (it.hasNext() && !found) {
87              found = "test.boolean".equals(it.next());
88          }
89  
90          assertTrue(found);
91      }
92  
93      @Test
94      public void testGetKeysWithUnknownPrefix() {
95          // test for a unknown prefix
96          final Iterator<String> it = conf.getKeys("foo.bar");
97          assertFalse(it.hasNext());
98      }
99  
100     @Test
101     public void testGetMissingKey() throws Exception {
102         assertNull(conf.getString("test.imaginarykey"));
103     }
104 
105     @Test
106     public void testGetMissingKeyWithDefault() throws Exception {
107         final String result = conf.getString("test.imaginarykey", "bob");
108         assertEquals("bob", result);
109     }
110 
111     @Test
112     public void testIsEmpty() {
113         assertFalse(conf.isEmpty());
114     }
115 
116     @Test
117     public void testMoreGets() throws Exception {
118         final String s = conf.getString("test.key");
119         assertEquals("jndivalue", s);
120         assertEquals("jndivalue2", conf.getString("test.key2"));
121         assertEquals(1, conf.getShort("test.short"));
122     }
123 
124     @Test
125     public void testSimpleGet() throws Exception {
126         final String s = conf.getString("test.key");
127         assertEquals("jndivalue", s);
128     }
129 
130     @Test
131     public void testThrowExceptionOnMissing() {
132         assertFalse(conf.isThrowExceptionOnMissing());
133     }
134 }