KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > configuration > TestConfigurationFactoryWithJNDI


1 /*
2  * Copyright 1999-2004 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 JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.cactus.ServletTestCase;
24 import org.apache.commons.collections.IteratorUtils;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 public class TestConfigurationFactoryWithJNDI extends ServletTestCase
29 {
30     private File JavaDoc testDigesterFile = new File JavaDoc("conf/testDigesterConfigurationWJNDI.xml");
31     private static Log log = LogFactory.getLog(TestConfigurationFactoryWithJNDI.class);
32
33     public void testLoadingWithDigester() throws Exception JavaDoc
34     {
35         ConfigurationFactory cf = new ConfigurationFactory();
36         cf.setConfigurationFileName(testDigesterFile.toString());
37         CompositeConfiguration compositeConfiguration = (CompositeConfiguration) cf.getConfiguration();
38
39         assertEquals("Verify how many configs", 4, compositeConfiguration.getNumberOfConfigurations());
40
41         assertEquals(JNDIConfiguration.class, compositeConfiguration.getConfiguration(1).getClass());
42         assertEquals(PropertiesConfiguration.class, compositeConfiguration.getConfiguration(2).getClass());
43         //assertEquals(DOM4JConfiguration.class, compositeConfiguration.getConfiguration(3).getClass());
44
PropertiesConfiguration pc = (PropertiesConfiguration) compositeConfiguration.getConfiguration(2);
45
46         assertNotNull("Make sure we have a fileName:" + pc.getFileName(), pc.getFileName());
47
48         assertTrue("Make sure we have loaded our key", compositeConfiguration.getBoolean("test.boolean"));
49         assertEquals("I'm complex!", compositeConfiguration.getProperty("element2.subelement.subsubelement"));
50
51         assertEquals("Make sure the JNDI config overwrites everything else!", "80", compositeConfiguration.getString("test.overwrite"));
52     }
53
54     /**
55      * Verify the getKeys() method works.
56      *
57      * @throws Exception
58      */

59     public void testGetKeys() throws Exception JavaDoc
60     {
61         ConfigurationFactory cf = new ConfigurationFactory();
62         cf.setConfigurationFileName(testDigesterFile.toString());
63
64         Configuration c = cf.getConfiguration();
65
66         List JavaDoc iteratedList = IteratorUtils.toList(c.getKeys());
67         assertTrue(iteratedList.contains("test.jndi"));
68     }
69
70     /**
71      * Test that a simple key works with JNDI
72      *
73      * @throws Exception
74      */

75     public void testGetKeysWithString() throws Exception JavaDoc
76     {
77         String JavaDoc KEY = "test";
78         ConfigurationFactory cf = new ConfigurationFactory();
79         cf.setConfigurationFileName(testDigesterFile.toString());
80
81         Configuration c = cf.getConfiguration();
82
83         List JavaDoc iteratedList = IteratorUtils.toList(c.getKeys(KEY));
84
85         assertTrue("Size:" + iteratedList.size(), iteratedList.size() > 0);
86         assertTrue(iteratedList.contains("test.jndi"));
87         for (Iterator JavaDoc i = iteratedList.iterator(); i.hasNext();)
88         {
89             String JavaDoc foundKey = (String JavaDoc) i.next();
90             assertTrue(foundKey.startsWith(KEY));
91         }
92     }
93
94     /**
95      * Verify that if a key is made of multiple parts, we still find
96      * the correct JNDI Context.
97      *
98      * @throws Exception
99      */

100     public void testGetKeysWithString2() throws Exception JavaDoc
101     {
102         String JavaDoc KEY = "test.deep";
103         ConfigurationFactory cf = new ConfigurationFactory();
104         cf.setConfigurationFileName(testDigesterFile.toString());
105
106         Configuration c = cf.getConfiguration();
107
108         List JavaDoc iteratedList = IteratorUtils.toList(c.getKeys(KEY));
109
110         assertTrue("Size:" + iteratedList.size(), iteratedList.size() == 2);
111         assertTrue(iteratedList.contains("test.deep.somekey"));
112         for (Iterator JavaDoc i = iteratedList.iterator(); i.hasNext();)
113         {
114             String JavaDoc foundKey = (String JavaDoc) i.next();
115             assertTrue(foundKey.startsWith(KEY));
116         }
117     }
118
119 }
120
Popular Tags