1   /*
2    * Copyright 2001-2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.configuration;
18  
19  import java.io.File;
20  import java.io.FileWriter;
21  import java.util.Collection;
22  import java.net.URL;
23  
24  import junit.framework.TestCase;
25  
26  import org.xml.sax.SAXParseException;
27  
28  /***
29   * Test the ConfigurationFactory.
30   *
31   * @version $Id: TestConfigurationFactory.java 155763 2005-03-01 10:04:49Z oheger $
32   */
33  public class TestConfigurationFactory extends TestCase
34  {
35      /*** The Files that we test with */
36      private File digesterRules = new File("conf/digesterRules.xml");
37      private File testDigesterFile =
38              new File("conf/testDigesterConfiguration.xml");
39      private File testDigesterFileReverseOrder =
40              new File("conf/testDigesterConfigurationReverseOrder.xml");
41      private File testDigesterFileNamespaceAware =
42              new File("conf/testDigesterConfigurationNamespaceAware.xml");
43      private File testDigesterFileBasePath =
44              new File("conf/testDigesterConfigurationBasePath.xml");
45      private File testDigesterFileEnhanced =
46              new File("conf/testDigesterConfiguration2.xml");
47      private File testDigesterFileComplete =
48              new File("conf/testDigesterConfiguration3.xml");
49      private File testDigesterFileOptional =
50              new File("conf/testDigesterOptionalConfiguration.xml");
51      private File testDigesterFileOptionalEx =
52              new File("conf/testDigesterOptionalConfigurationEx.xml");
53  
54      private File testDigesterBadXML = new File("conf/testDigesterBadXML.xml");
55  
56      private String testBasePath = new File("conf").getAbsolutePath();
57      
58      private File testProperties = new File("conf/test.properties");
59      private File testAbsConfig = new File("target/testAbsConfig.xml");
60  
61      private Configuration configuration;
62      private CompositeConfiguration compositeConfiguration;
63      private ConfigurationFactory factory;
64  
65      public void setUp() throws Exception
66      {
67          System.setProperty("java.naming.factory.initial", "org.apache.commons.configuration.MockStaticMemoryInitialContextFactory");
68          factory = new ConfigurationFactory();
69      }
70  
71      public void testJNDI() throws Exception
72      {
73          JNDIConfiguration jndiConfiguration = new JNDIConfiguration();
74          Object o = jndiConfiguration.getProperty("test.boolean");
75          assertNotNull(o);
76          assertEquals("true", o.toString());
77      }
78  
79      public void testLoadingConfiguration() throws Exception
80      {
81          factory.setConfigurationFileName(
82                  testDigesterFile.toString());
83  
84          compositeConfiguration =
85                  (CompositeConfiguration) factory.getConfiguration();
86  
87          assertEquals(
88                  "Verify how many configs",
89                  3,
90                  compositeConfiguration.getNumberOfConfigurations());
91          assertEquals(
92                  PropertiesConfiguration.class,
93                  compositeConfiguration.getConfiguration(0).getClass());
94          PropertiesConfiguration pc =
95                  (PropertiesConfiguration) compositeConfiguration.getConfiguration(
96                          0);
97  
98          assertNotNull(
99                  "Make sure we have a fileName:" + pc.getFileName(),
100                 pc.getFileName());
101 
102         assertTrue(
103                 "Make sure we have loades our key",
104                 compositeConfiguration.getBoolean("test.boolean"));
105         assertEquals(
106                 "I'm complex!",
107                 compositeConfiguration.getProperty(
108                         "element2.subelement.subsubelement"));
109 
110         configuration = compositeConfiguration;
111         assertEquals(
112                 "I'm complex!",
113                 configuration.getProperty("element2.subelement.subsubelement"));
114     }
115 
116     public void testLoadingConfigurationReverseOrder() throws Exception
117     {
118         factory.setConfigurationFileName(
119                 testDigesterFileReverseOrder.toString());
120 
121         configuration = factory.getConfiguration();
122 
123         assertEquals("8", configuration.getProperty("test.short"));
124 
125         factory.setConfigurationFileName(testDigesterFile.toString());
126 
127         configuration = factory.getConfiguration();
128         assertEquals("1", configuration.getProperty("test.short"));
129     }
130 
131     public void testLoadingConfigurationWithRulesXML() throws Exception
132     {
133         factory.setConfigurationFileName(testDigesterFile.toString());
134         factory.setDigesterRules(digesterRules.toURL());
135 
136         compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
137 
138         assertEquals(
139                 "Verify how many configs",
140                 3,
141                 compositeConfiguration.getNumberOfConfigurations());
142 
143         assertEquals(
144                 PropertiesConfiguration.class,
145                 compositeConfiguration.getConfiguration(0).getClass());
146 
147         PropertiesConfiguration pc =
148                 (PropertiesConfiguration) compositeConfiguration.getConfiguration(
149                         0);
150         assertNotNull(
151                 "Make sure we have a fileName:" + pc.getFileName(),
152                 pc.getFileName());
153         assertTrue(
154                 "Make sure we have loaded our key",
155                 pc.getBoolean("test.boolean"));
156 
157         assertTrue(
158                 "Make sure we have loaded our key",
159                 compositeConfiguration.getBoolean("test.boolean"));
160 
161         assertEquals(
162                 "I'm complex!",
163                 compositeConfiguration.getProperty(
164                         "element2.subelement.subsubelement"));
165 
166         configuration = compositeConfiguration;
167         assertEquals(
168                 "I'm complex!",
169                 configuration.getProperty("element2.subelement.subsubelement"));
170     }
171 
172     public void testLoadingConfigurationNamespaceAware() throws Exception
173     {
174         factory.setConfigurationFileName(testDigesterFileNamespaceAware.toString());
175         //factory.setDigesterRules(digesterRules.toURL());
176         factory.setDigesterRuleNamespaceURI("namespace-one");
177 
178         checkCompositeConfiguration();
179     }
180 
181     public void testLoadingConfigurationBasePath() throws Exception
182     {
183         factory.setConfigurationFileName(testDigesterFileBasePath.toString());
184 
185         factory.setBasePath(testBasePath);
186 
187         //factory.setDigesterRules(digesterRules.toURL());
188         //factory.setDigesterRuleNamespaceURI("namespace-one");
189 
190         checkCompositeConfiguration();
191     }
192 
193     public void testLoadingAdditional() throws Exception
194     {
195         factory.setConfigurationFileName(testDigesterFileEnhanced.toString());
196         factory.setBasePath(null);
197         checkUnionConfig();
198     }
199 
200     public void testLoadingURL() throws Exception
201     {
202         factory.setConfigurationURL(testDigesterFileEnhanced.toURL());
203         checkUnionConfig();
204     }
205 
206     public void testLoadingFromJAR() throws Exception
207     {
208         URL url = Thread.currentThread().getContextClassLoader().getResource("config-jar.xml");
209         assertNotNull("config-jar.xml not found on the classpath", url);
210         factory.setConfigurationURL(url);
211 
212         Configuration conf = factory.getConfiguration();
213         assertFalse("The configuration is empty", conf.isEmpty());
214     }
215 
216     public void testThrowingConfigurationInitializationException() throws Exception
217     {
218         factory.setConfigurationFileName(testDigesterBadXML.toString());
219         try
220         {
221             factory.getConfiguration();
222             fail("Should have throw an Exception");
223         }
224         catch (ConfigurationException cle)
225         {
226             assertTrue(cle.getCause() instanceof SAXParseException);
227         }
228     }
229 
230     // Tests if properties from all sources can be loaded
231     public void testAllConfiguration() throws Exception
232     {
233         factory.setConfigurationURL(testDigesterFileComplete.toURL());
234         Configuration config = factory.getConfiguration();
235         assertFalse(config.isEmpty());
236         assertTrue(config instanceof CompositeConfiguration);
237         CompositeConfiguration cc = (CompositeConfiguration) config;
238         assertTrue(cc.getNumberOfConfigurations() > 1);
239         // Currently fails, should be 4?  Only 2?
240         //assertEquals(4, cc.getNumberOfConfigurations());
241 
242         assertNotNull(config.getProperty("tables.table(0).fields.field(2).name"));
243         assertNotNull(config.getProperty("element2.subelement.subsubelement"));
244         assertEquals("value", config.getProperty("element3"));
245         assertEquals("foo", config.getProperty("element3[@name]"));
246         assertNotNull(config.getProperty("mail.account.user"));
247 
248         // test JNDIConfiguration
249         assertNotNull(config.getProperty("test.onlyinjndi"));
250         assertTrue(config.getBoolean("test.onlyinjndi"));
251 
252         Configuration subset = config.subset("test");
253         assertNotNull(subset.getProperty("onlyinjndi"));
254         assertTrue(subset.getBoolean("onlyinjndi"));
255 
256         // test SystemConfiguration
257         assertNotNull(config.getProperty("java.version"));
258         assertEquals(System.getProperty("java.version"), config.getString("java.version"));
259     }
260     
261     // Checks if optional configurations work
262     public void testOptionalConfigurations() throws Exception
263     {
264         factory.setConfigurationURL(testDigesterFileOptional.toURL());
265         Configuration config = factory.getConfiguration();
266         assertTrue(config.getBoolean("test.boolean"));
267         assertEquals("value", config.getProperty("element"));
268         
269         factory.setConfigurationURL(testDigesterFileOptionalEx.toURL());
270         try
271         {
272             config = factory.getConfiguration();
273             fail("Unexisting properties loaded!");
274         }
275         catch(ConfigurationException cex)
276         {
277             // fine
278         }
279     }
280     
281     // Checks if a file with an absolute path can be loaded
282     public void testLoadAbsolutePath() throws Exception
283     {
284         try
285         {
286             FileWriter out = null;
287             try
288             {
289                 out = new FileWriter(testAbsConfig);
290                 out.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
291                 out.write("<configuration>");
292                 out.write("<properties fileName=\"");
293                 out.write(testProperties.getAbsolutePath());
294                 out.write("\"/>");
295                 out.write("</configuration>");
296             }
297             finally
298             {
299                 if (out != null)
300                 {
301                     out.close();
302                 }
303             }
304 
305             factory.setConfigurationFileName(testAbsConfig.toString());
306             Configuration config = factory.getConfiguration();
307             assertTrue(config.getBoolean("configuration.loaded"));
308         }
309         finally
310         {
311             if (testAbsConfig.exists())
312             {
313                 testAbsConfig.delete();
314             }
315         }
316     }
317 
318     private void checkUnionConfig() throws Exception
319     {
320         compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
321         assertEquals(
322                 "Verify how many configs",
323                 3,
324                 compositeConfiguration.getNumberOfConfigurations());
325 
326         // Test if union was constructed correctly
327         Object prop = compositeConfiguration.getProperty("tables.table.name");
328         assertTrue(prop instanceof Collection);
329         assertEquals(3, ((Collection) prop).size());
330         assertEquals(
331                 "users",
332                 compositeConfiguration.getProperty("tables.table(0).name"));
333         assertEquals(
334                 "documents",
335                 compositeConfiguration.getProperty("tables.table(1).name"));
336         assertEquals(
337                 "tasks",
338                 compositeConfiguration.getProperty("tables.table(2).name"));
339 
340         prop =
341                 compositeConfiguration.getProperty(
342                         "tables.table.fields.field.name");
343         assertTrue(prop instanceof Collection);
344         assertEquals(17, ((Collection) prop).size());
345 
346         assertEquals(
347                 "smtp.mydomain.org",
348                 compositeConfiguration.getString("mail.host.smtp"));
349         assertEquals(
350                 "pop3.mydomain.org",
351                 compositeConfiguration.getString("mail.host.pop"));
352 
353         // This was overriden
354         assertEquals(
355                 "masterOfPost",
356                 compositeConfiguration.getString("mail.account.user"));
357         assertEquals(
358                 "topsecret",
359                 compositeConfiguration.getString("mail.account.psswd"));
360 
361         // This was overriden, too, but not in additional section
362         assertEquals(
363                 "enhanced factory",
364                 compositeConfiguration.getString("test.configuration"));
365     }
366 
367     private void checkCompositeConfiguration() throws Exception
368     {
369         compositeConfiguration = (CompositeConfiguration) factory.getConfiguration();
370 
371         assertEquals(
372                 "Verify how many configs",
373                 2,
374                 compositeConfiguration.getNumberOfConfigurations());
375 
376         assertEquals(
377                 PropertiesConfiguration.class,
378                 compositeConfiguration.getConfiguration(0).getClass());
379 
380         PropertiesConfiguration pc =
381                 (PropertiesConfiguration) compositeConfiguration.getConfiguration(
382                         0);
383         assertNotNull(
384                 "Make sure we have a fileName:" + pc.getFileName(),
385                 pc.getFileName());
386         assertTrue(
387                 "Make sure we have loaded our key",
388                 pc.getBoolean("test.boolean"));
389 
390         assertTrue(
391                 "Make sure we have loaded our key",
392                 compositeConfiguration.getBoolean("test.boolean"));
393 
394 
395         Object property = compositeConfiguration.getProperty(
396                 "element2.subelement.subsubelement");
397         assertNull("Should have returned a null", property);
398     }
399 }