1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.Iterator;
27
28 import org.junit.Before;
29 import org.junit.Test;
30
31 public class TestNullJNDIEnvironmentValues
32 {
33 private JNDIConfiguration conf = null;
34
35 @Before
36 public void setUp() throws Exception
37 {
38 System.setProperty("java.naming.factory.initial", TestJNDIConfiguration.CONTEXT_FACTORY);
39
40 conf = new JNDIConfiguration();
41 conf.setThrowExceptionOnMissing(false);
42 }
43
44 @Test
45 public void testThrowExceptionOnMissing()
46 {
47 assertFalse("Throw Exception Property is set!", conf.isThrowExceptionOnMissing());
48 }
49
50 @Test
51 public void testSimpleGet() throws Exception
52 {
53 String s = conf.getString("test.key");
54 assertEquals("jndivalue", s);
55 }
56
57 @Test
58 public void testMoreGets() throws Exception
59 {
60 String s = conf.getString("test.key");
61 assertEquals("jndivalue", s);
62 assertEquals("jndivalue2", conf.getString("test.key2"));
63 assertEquals(1, conf.getShort("test.short"));
64 }
65
66 @Test
67 public void testGetMissingKey() throws Exception
68 {
69 assertNull("Missing Key is not null!", conf.getString("test.imaginarykey"));
70 }
71
72 @Test
73 public void testGetMissingKeyWithDefault() throws Exception
74 {
75 String result = conf.getString("test.imaginarykey", "bob");
76 assertEquals("bob", result);
77 }
78
79 @Test
80 public void testContainsKey() throws Exception
81 {
82 assertTrue(conf.containsKey("test.key"));
83 assertTrue(!conf.containsKey("test.imaginarykey"));
84 }
85
86 @Test
87 public void testClearProperty()
88 {
89 assertNotNull("null short for the 'test.short' key", conf.getShort("test.short", null));
90 conf.clearProperty("test.short");
91 assertNull("'test.short' property not cleared", conf.getShort("test.short", null));
92 }
93
94 @Test
95 public void testIsEmpty()
96 {
97 assertFalse("the configuration shouldn't be empty", conf.isEmpty());
98 }
99
100 @Test
101 public void testGetKeys() throws Exception
102 {
103 boolean found = false;
104 Iterator<String> it = conf.getKeys();
105
106 assertTrue("no key found", it.hasNext());
107
108 while (it.hasNext() && !found)
109 {
110 found = "test.boolean".equals(it.next());
111 }
112
113 assertTrue("'test.boolean' key not found", found);
114 }
115
116 @Test
117 public void testGetKeysWithUnknownPrefix()
118 {
119
120 Iterator<String> it = conf.getKeys("foo.bar");
121 assertFalse("no key should be found", it.hasNext());
122 }
123
124 @Test
125 public void testGetKeysWithExistingPrefix()
126 {
127
128 Iterator<String> it = conf.getKeys("test");
129 boolean found = false;
130 while (it.hasNext() && !found)
131 {
132 found = "test.boolean".equals(it.next());
133 }
134
135 assertTrue("'test.boolean' key not found", found);
136 }
137
138 @Test
139 public void testGetKeysWithKeyAsPrefix()
140 {
141
142 Iterator<String> it = conf.getKeys("test.boolean");
143 boolean found = false;
144 while (it.hasNext() && !found)
145 {
146 found = "test.boolean".equals(it.next());
147 }
148
149 assertTrue("'test.boolean' key not found", found);
150 }
151 }