1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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 }