1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Map;
24
25 import junit.framework.TestCase;
26
27 /***
28 * Test class for EnvironmentConfiguration.
29 *
30 * @author Oliver Heger
31 * @version $Id: TestEnvironmentConfiguration.java 587636 2007-10-23 20:03:47Z oheger $
32 */
33 public class TestEnvironmentConfiguration extends TestCase
34 {
35 /*** Stores the configuration to be tested. */
36 private EnvironmentConfiguration config;
37
38 protected void setUp() throws Exception
39 {
40 super.setUp();
41 config = new EnvironmentConfiguration();
42 }
43
44 /***
45 * Helper method for checking that the configuration contains some
46 * environment properties. (We expect that at least some properties are set
47 * in each environment.)
48 */
49 private void checkProperties()
50 {
51 boolean found = false;
52 assertFalse("No properties found", config.isEmpty());
53 for (Iterator it = config.getKeys(); it.hasNext();)
54 {
55 String key = (String) it.next();
56 assertTrue("Key not found: " + key, config.containsKey(key));
57 assertNotNull("No value for property " + key, config.getString(key));
58 found = true;
59 }
60 assertTrue("No property keys returned", found);
61 }
62
63 /***
64 * Tests whether a newly created configuration contains some properties. (We
65 * expect that at least some properties are set in each environment.)
66 */
67 public void testInit()
68 {
69 checkProperties();
70 }
71
72 /***
73 * Tests extracting properties for JDK before 1.5. This method should work
74 * on later JDKs, too, so we can test it always.
75 */
76 public void testExtractProperties14()
77 {
78 config.extractProperties14();
79 checkProperties();
80 }
81
82 /***
83 * Tests whether a collection with properties is correctly processed.
84 */
85 public void testExtractPropertiesFromCollection()
86 {
87 final int count = 8;
88 final String prop = "property";
89 final String value = "value";
90
91 Collection env = new ArrayList(count);
92 for (int i = 0; i < count; i++)
93 {
94 env.add(prop + i + "=" + value + i);
95 }
96 env.add("irregularProperty");
97 config.extractPropertiesFromCollection(env);
98
99 Map props = new HashMap();
100 for (Iterator it = config.getKeys(); it.hasNext();)
101 {
102 String key = (String) it.next();
103 props.put(key, config.getString(key));
104 }
105 assertEquals("Wrong number of properties", count, props.size());
106 for (int i = 0; i < count; i++)
107 {
108 assertEquals("Wrong value for property " + i, value + i, props
109 .get(prop + i));
110 }
111 assertFalse("Irregular property found", config
112 .containsKey("irregularProperty"));
113 }
114
115 /***
116 * Tests removing properties. This should not be possible.
117 */
118 public void testClearProperty()
119 {
120 String key = (String) config.getKeys().next();
121 try
122 {
123 config.clearProperty(key);
124 fail("Could remove a property!");
125 }
126 catch (UnsupportedOperationException uoex)
127 {
128
129 }
130 }
131
132 /***
133 * Tests removing all properties. This should not be possible.
134 */
135 public void testClear()
136 {
137 try
138 {
139 config.clear();
140 fail("Could remove properties!");
141 }
142 catch (UnsupportedOperationException uoex)
143 {
144
145 }
146 }
147
148 /***
149 * Tries to add another property. This should cause an exception.
150 */
151 public void testAddProperty()
152 {
153 try
154 {
155 config.addProperty("JAVA_HOME", "C://java");
156 fail("Could add a property!");
157 }
158 catch (UnsupportedOperationException uoex)
159 {
160
161 }
162 }
163
164 /***
165 * Tries to set the value of a property. This should cause an exception.
166 */
167 public void testSetProperty()
168 {
169 try
170 {
171 config.setProperty("JAVA_HOME", "C://java");
172 fail("Could set a property!");
173 }
174 catch (UnsupportedOperationException uoex)
175 {
176
177 }
178 }
179 }