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