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