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