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.io.File;
20 import java.util.Collection;
21
22 import junit.framework.TestCase;
23
24 import org.xml.sax.SAXParseException;
25
26 /***
27 * Test the ConfigurationFactory.
28 *
29 * @version $Id: TestConfigurationFactory.java,v 1.15 2004/09/21 17:18:27 ebourg Exp $
30 */
31 public class TestConfigurationFactory extends TestCase
32 {
33 /*** The Files that we test with */
34 private File digesterRules = new File("conf/digesterRules.xml");
35 private File testDigesterFile =
36 new File("conf/testDigesterConfiguration.xml");
37 private File testDigesterFileReverseOrder =
38 new File("conf/testDigesterConfigurationReverseOrder.xml");
39 private File testDigesterFileNamespaceAware =
40 new File("conf/testDigesterConfigurationNamespaceAware.xml");
41 private File testDigesterFileBasePath =
42 new File("conf/testDigesterConfigurationBasePath.xml");
43 private File testDigesterFileEnhanced =
44 new File("conf/testDigesterConfiguration2.xml");
45 private File testDigesterFileComplete =
46 new File("conf/testDigesterConfiguration3.xml");
47
48 private File testDigesterBadXML = new File("conf/testDigesterBadXML.xml");
49
50 private String testBasePath = new File("conf").getAbsolutePath();
51
52 private Configuration configuration;
53 private CompositeConfiguration compositeConfiguration;
54 private ConfigurationFactory factory;
55
56 public void setUp() throws Exception
57 {
58 System.setProperty("java.naming.factory.initial","org.apache.commons.configuration.MockStaticMemoryInitialContextFactory");
59 factory = new ConfigurationFactory();
60 }
61
62 public void testJNDI() throws Exception
63 {
64 JNDIConfiguration jndiConfiguration = new JNDIConfiguration();
65 Object o = jndiConfiguration.getProperty("test.boolean");
66 assertNotNull(o);
67 assertEquals("true",o.toString());
68 }
69
70 public void testLoadingConfiguration() throws Exception
71 {
72 factory.setConfigurationFileName(
73 testDigesterFile.toString());
74
75 compositeConfiguration =
76 (CompositeConfiguration) factory.getConfiguration();
77
78 assertEquals(
79 "Verify how many configs",
80 3,
81 compositeConfiguration.getNumberOfConfigurations());
82 assertEquals(
83 PropertiesConfiguration.class,
84 compositeConfiguration.getConfiguration(0).getClass());
85 PropertiesConfiguration pc =
86 (PropertiesConfiguration) compositeConfiguration.getConfiguration(
87 0);
88
89 assertNotNull(
90 "Make sure we have a fileName:" + pc.getFileName(),
91 pc.getFileName());
92
93 assertTrue(
94 "Make sure we have loades our key",
95 compositeConfiguration.getBoolean("test.boolean"));
96 assertEquals(
97 "I'm complex!",
98 compositeConfiguration.getProperty(
99 "element2.subelement.subsubelement"));
100
101 configuration = compositeConfiguration;
102 assertEquals(
103 "I'm complex!",
104 configuration.getProperty("element2.subelement.subsubelement"));
105 }
106
107 public void testLoadingConfigurationReverseOrder() throws Exception
108 {
109 factory.setConfigurationFileName(
110 testDigesterFileReverseOrder.toString());
111
112 configuration = factory.getConfiguration();
113
114 assertEquals("8", configuration.getProperty("test.short"));
115
116 factory.setConfigurationFileName(testDigesterFile.toString());
117
118 configuration = factory.getConfiguration();
119 assertEquals("1", configuration.getProperty("test.short"));
120 }
121
122 public void testLoadingConfigurationWithRulesXML() throws Exception
123 {
124 factory.setConfigurationFileName(testDigesterFile.toString());
125 factory.setDigesterRules(digesterRules.toURL());
126
127 compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
128
129 assertEquals(
130 "Verify how many configs",
131 3,
132 compositeConfiguration.getNumberOfConfigurations());
133
134 assertEquals(
135 PropertiesConfiguration.class,
136 compositeConfiguration.getConfiguration(0).getClass());
137
138 PropertiesConfiguration pc =
139 (PropertiesConfiguration) compositeConfiguration.getConfiguration(
140 0);
141 assertNotNull(
142 "Make sure we have a fileName:" + pc.getFileName(),
143 pc.getFileName());
144 assertTrue(
145 "Make sure we have loaded our key",
146 pc.getBoolean("test.boolean"));
147
148 assertTrue(
149 "Make sure we have loaded our key",
150 compositeConfiguration.getBoolean("test.boolean"));
151
152 assertEquals(
153 "I'm complex!",
154 compositeConfiguration.getProperty(
155 "element2.subelement.subsubelement"));
156
157 configuration = compositeConfiguration;
158 assertEquals(
159 "I'm complex!",
160 configuration.getProperty("element2.subelement.subsubelement"));
161 }
162
163 public void testLoadingConfigurationNamespaceAware() throws Exception
164 {
165 factory.setConfigurationFileName(testDigesterFileNamespaceAware.toString());
166
167 factory.setDigesterRuleNamespaceURI("namespace-one");
168
169 checkCompositeConfiguration();
170 }
171
172 public void testLoadingConfigurationBasePath() throws Exception
173 {
174 factory.setConfigurationFileName(testDigesterFileBasePath.toString());
175
176 factory.setBasePath(testBasePath);
177
178
179
180
181 checkCompositeConfiguration();
182 }
183
184 public void testLoadingAdditional() throws Exception
185 {
186 factory.setConfigurationFileName(testDigesterFileEnhanced.toString());
187 factory.setBasePath(null);
188 checkUnionConfig();
189 }
190
191 public void testLoadingURL() throws Exception
192 {
193 factory.setConfigurationURL(testDigesterFileEnhanced.toURL());
194 checkUnionConfig();
195 }
196
197 public void testThrowingConfigurationInitializationException() throws Exception
198 {
199 factory.setConfigurationFileName(testDigesterBadXML.toString());
200 try
201 {
202 factory.getConfiguration();
203 fail("Should have throw an Exception");
204 }
205 catch (ConfigurationException cle)
206 {
207 assertTrue(cle.getCause() instanceof SAXParseException);
208 }
209 }
210
211
212 public void testAllConfiguration() throws Exception
213 {
214
215 factory.setConfigurationURL(testDigesterFileComplete.toURL());
216 Configuration config = factory.getConfiguration();
217 assertFalse(config.isEmpty());
218 assertTrue(config instanceof CompositeConfiguration);
219 CompositeConfiguration cc = (CompositeConfiguration)config;
220 assertTrue(cc.getNumberOfConfigurations()>1);
221
222
223
224 assertNotNull(config.getProperty("tables.table(0).fields.field(2).name"));
225 assertNotNull(config.getProperty("element2.subelement.subsubelement"));
226 assertEquals("value", config.getProperty("element3"));
227 assertEquals("foo", config.getProperty("element3[@name]"));
228 assertNotNull(config.getProperty("mail.account.user"));
229
230 assertNotNull(config.getProperty("test.onlyinjndi"));
231 assertTrue(config.getBoolean("test.onlyinjndi"));
232
233 Configuration subset = config.subset("test");
234 assertNotNull(subset.getProperty("onlyinjndi"));
235 assertTrue(subset.getBoolean("onlyinjndi"));
236 }
237
238 private void checkUnionConfig() throws Exception
239 {
240 compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
241 assertEquals(
242 "Verify how many configs",
243 3,
244 compositeConfiguration.getNumberOfConfigurations());
245
246
247 Object prop = compositeConfiguration.getProperty("tables.table.name");
248 assertTrue(prop instanceof Collection);
249 assertEquals(3, ((Collection) prop).size());
250 assertEquals(
251 "users",
252 compositeConfiguration.getProperty("tables.table(0).name"));
253 assertEquals(
254 "documents",
255 compositeConfiguration.getProperty("tables.table(1).name"));
256 assertEquals(
257 "tasks",
258 compositeConfiguration.getProperty("tables.table(2).name"));
259
260 prop =
261 compositeConfiguration.getProperty(
262 "tables.table.fields.field.name");
263 assertTrue(prop instanceof Collection);
264 assertEquals(17, ((Collection) prop).size());
265
266 assertEquals(
267 "smtp.mydomain.org",
268 compositeConfiguration.getString("mail.host.smtp"));
269 assertEquals(
270 "pop3.mydomain.org",
271 compositeConfiguration.getString("mail.host.pop"));
272
273
274 assertEquals(
275 "masterOfPost",
276 compositeConfiguration.getString("mail.account.user"));
277 assertEquals(
278 "topsecret",
279 compositeConfiguration.getString("mail.account.psswd"));
280
281
282 assertEquals(
283 "enhanced factory",
284 compositeConfiguration.getString("test.configuration"));
285 }
286
287 private void checkCompositeConfiguration() throws Exception
288 {
289 compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
290
291 assertEquals(
292 "Verify how many configs",
293 2,
294 compositeConfiguration.getNumberOfConfigurations());
295
296 assertEquals(
297 PropertiesConfiguration.class,
298 compositeConfiguration.getConfiguration(0).getClass());
299
300 PropertiesConfiguration pc =
301 (PropertiesConfiguration) compositeConfiguration.getConfiguration(
302 0);
303 assertNotNull(
304 "Make sure we have a fileName:" + pc.getFileName(),
305 pc.getFileName());
306 assertTrue(
307 "Make sure we have loaded our key",
308 pc.getBoolean("test.boolean"));
309
310 assertTrue(
311 "Make sure we have loaded our key",
312 compositeConfiguration.getBoolean("test.boolean"));
313
314
315 Object property = compositeConfiguration.getProperty(
316 "element2.subelement.subsubelement");
317 assertNull("Should have returned a null",property);
318 }
319 }