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