KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > jndi > test > InitialContextFactoryTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.jndi.test;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28
29 import junit.extensions.ActiveTestSuite;
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33
34 import org.sape.carbon.core.component.Lookup;
35
36 import org.sape.carbon.services.jndi.InitialContextFactory;
37 import org.sape.carbon.services.jndi.InitialContextFactoryConfiguration;
38
39
40 /**
41  *
42  * Copyright 2003 Sapient
43  * @since carbon 2.1
44  * @author Douglas Voet, October 2003
45  * @version $Revision: 1.3 $($Author: dvoet $ / $Date: 2003/10/30 19:28:56 $)
46  */

47 public class InitialContextFactoryTest extends TestCase {
48
49     public InitialContextFactoryTest(String JavaDoc name) {
50         super(name);
51     }
52     
53     public void testGetInstance() throws NamingException JavaDoc {
54         InitialContextFactory factory = (InitialContextFactory)
55             Lookup.getInstance().fetchComponent("/jndi/test/InitialContextFactoryTest");
56             
57         InitialContext JavaDoc context = factory.getContext();
58         
59         // test environment
60
Hashtable JavaDoc liveEnvironment = context.getEnvironment();
61         Map JavaDoc configEnvironment =
62             ((InitialContextFactoryConfiguration) factory).getEnvironment();
63             
64         checkResults(configEnvironment, liveEnvironment);
65     }
66     
67     public void testGetInstanceWithParams() throws NamingException JavaDoc {
68         InitialContextFactory factory = (InitialContextFactory)
69             Lookup.getInstance().fetchComponent("/jndi/test/InitialContextFactoryTest");
70             
71         Map JavaDoc customProperties = new HashMap JavaDoc();
72         customProperties.put("dummy.property", "foo");
73         
74         InitialContext JavaDoc context = factory.getContext(customProperties);
75         
76         // test environment
77
Hashtable JavaDoc liveEnvironment = context.getEnvironment();
78         checkResults(customProperties, liveEnvironment);
79     }
80     
81     public void testGetInstanceIgnoreConfig() throws NamingException JavaDoc {
82         InitialContextFactory factory = (InitialContextFactory)
83             Lookup.getInstance().fetchComponent("/jndi/test/InitialContextFactoryTest");
84             
85         Map JavaDoc customProperties = new HashMap JavaDoc(
86             ((InitialContextFactoryConfiguration) factory).getEnvironment());
87         customProperties.put("dummy.property", "foo");
88         
89         InitialContext JavaDoc context = factory.getContext(customProperties);
90         
91         // test environment
92
Hashtable JavaDoc liveEnvironment = context.getEnvironment();
93         checkResults(customProperties, liveEnvironment);
94     }
95     
96     public void testNullParameters() throws NamingException JavaDoc {
97         InitialContextFactory factory = (InitialContextFactory)
98             Lookup.getInstance().fetchComponent("/jndi/test/InitialContextFactoryTest");
99             
100         // 1st check getInstance(null)
101
InitialContext JavaDoc context = factory.getContext(null);
102         
103         // test environment
104
Hashtable JavaDoc liveEnvironment = context.getEnvironment();
105         Map JavaDoc configEnvironment =
106             ((InitialContextFactoryConfiguration) factory).getEnvironment();
107             
108         checkResults(configEnvironment, liveEnvironment);
109         
110         // 2nd check getInstanceIgnoreConfig(null)
111
context = factory.getContextIgnoreConfig(null);
112     }
113     
114     private void checkResults(Map JavaDoc expected, Map JavaDoc actual) {
115         Iterator JavaDoc expectedIterator = expected.entrySet().iterator();
116         while (expectedIterator.hasNext()) {
117             Map.Entry JavaDoc expectedEntry = (Map.Entry JavaDoc) expectedIterator.next();
118             
119             assertTrue("unexpected environment property value: property [" +
120                 expectedEntry.getKey() + "] expected value [" +
121                 expectedEntry.getValue() + "] actual value [" +
122                 actual.get(expectedEntry.getKey()) + "]",
123                 expectedEntry.getValue().equals(actual.get(expectedEntry.getKey())));
124         }
125     }
126     
127     /**
128      * Method called by jUnit to get all the tests in this test case.
129      * @return Test the suite of tests in this test case
130      */

131     public static Test suite() {
132         TestSuite masterSuite = new TestSuite();
133         // add single threaded tests
134
Test singleThreadedTests = getSingleThreadedTests();
135         if (singleThreadedTests != null) {
136             masterSuite.addTest(singleThreadedTests);
137         }
138         // add multi threaded tests
139
Test multiThreadedTests = getMultiThreadedTests();
140         if (multiThreadedTests != null) {
141             masterSuite.addTest(multiThreadedTests);
142         }
143         return masterSuite;
144     }
145
146     /**
147      * Single threaded tests:
148      *
149      * testIDNotFound
150      * testIDsNotSkipped
151      * testInvalidConfiguration
152      * testInvalidConnectionFactory
153      *
154      * @return Test the suite of single threaded tests in this test case
155      */

156     private static Test getSingleThreadedTests() {
157         TestSuite suite = new TestSuite();
158         
159         suite.addTest(new InitialContextFactoryTest("testGetInstance"));
160         suite.addTest(new InitialContextFactoryTest("testGetInstanceWithParams"));
161         suite.addTest(new InitialContextFactoryTest("testGetInstanceIgnoreConfig"));
162         suite.addTest(new InitialContextFactoryTest("testNullParameters"));
163
164         return suite;
165     }
166
167     /**
168      * Multi-Threaded tests
169      *
170      * testMultiComponentAccess1
171      * testMultiComponentAccess2
172      * testMultiComponentAccess3
173      *
174      * @return Test the suite of multi-threaded tests in this test case
175      */

176     private static Test getMultiThreadedTests() {
177         TestSuite suite = new ActiveTestSuite();
178
179         return suite;
180     }
181
182     /**
183      * This method will add the give test to the give suite the specified
184      * number of times. This is best used for multi-threaded tests where
185      * suite is an instance of ActiveTestSuite and you want to run the same test in
186      * multiple threads.
187      * @param suite the suite to add the test to.
188      * @param testName the name of the test to add.
189      * @param number the number of times to add the test to the suite
190      */

191     private static void addTest(
192         TestSuite suite,
193         String JavaDoc testName,
194         int number) {
195         for (int count = 0; count < number; count++) {
196             suite.addTest(new InitialContextFactoryTest(testName));
197         }
198     }
199 }
Popular Tags