KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > ejb > local > test > LocalHomeFactoryTest


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.ejb.local.test;
19
20
21 import java.rmi.RemoteException JavaDoc;
22
23 import javax.ejb.CreateException JavaDoc;
24 import javax.ejb.EJBHome JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35 import org.sape.carbon.core.component.Lookup;
36 import org.sape.carbon.core.config.Config;
37
38 import org.sape.carbon.services.ejb.HomeFactoryException;
39 import org.sape.carbon.services.ejb.remote.RemoteHomeFactory;
40 import org.sape.carbon.services.jndi.InitialContextFactory;
41 import org.sape.carbon.services.jndi.InitialContextFactoryConfiguration;
42
43 public class LocalHomeFactoryTest extends TestCase {
44
45     /**
46      * Provides a handle to Apache-commons logger
47      */

48     private Log log = LogFactory.getLog(this.getClass());
49
50     /**
51      * Path of the local home factory test component
52      */

53     public static final String JavaDoc TEST_LOCAL_HOME_FACTORY =
54         "/ejb/test/LocalHomeFactoryTest";
55
56
57     /**
58      * Path of the remote home factory test component
59      */

60     public static final String JavaDoc TEST_REMOTE_HOME_FACTORY =
61         "/ejb/test/RemoteHomeFactoryTest";
62
63     /**
64      * Path of the remote home factory test component used to generate
65      * a <code>ClassCastException</code> error condition
66      */

67     public static final String JavaDoc TEST_MISCAST_HOME_FACTORY =
68         "/ejb/test/MiscastRemoteHomeFactoryTest";
69
70     /**
71      * Path of the remote home factory test component
72      */

73     public static final String JavaDoc TEST_INITIAL_CONTEXT_FACTORY =
74         "/ejb/test/TestInitialContextFactory";
75
76     /**
77      * Logical name of the <code>LocalGateway</code> EJB
78      */

79     public static final String JavaDoc TEST_REMOTE_EJB =
80         "org.sape.carbon.services.ejb.local.test.LocalGateway";
81
82
83     /**
84      * Logical name of the <code>LocalTester</code> EJB
85      */

86     public static final String JavaDoc TEST_LOCAL_EJB =
87         "org.sape.carbon.services.ejb.local.test.LocalTester";
88
89
90     /**
91      * Logical name of a non-existant EJB used for generating a
92      * <code>HomeFactoryNamingException</code> error condition
93      */

94     public static final String JavaDoc TEST_NONEXISTANT_EJB =
95         "org.sape.carbon.services.ejb.test.Nonexistant";
96
97
98     public LocalHomeFactoryTest(String JavaDoc name) {
99         super(name);
100     }
101
102
103     public static void main(String JavaDoc args[]) throws Exception JavaDoc{
104     }
105
106
107     /**
108      * Tests the exception case wherein an EJB lookup using a remote home
109      * factory component results in a
110      * <code>HomeFactoryClassCastException</code>.
111      */

112     public void testLocalHomeFactoryClassCastException() {
113
114         EJBHome JavaDoc ejbHome = null;
115
116         // Perform the test using the RemoteHomeFactory
117
try {
118             RemoteHomeFactory homeFactory = (RemoteHomeFactory)
119                 Lookup.getInstance().fetchComponent(TEST_MISCAST_HOME_FACTORY);
120
121             ejbHome = homeFactory.lookup(TEST_REMOTE_EJB);
122
123             fail("Lookup for: "
124                 + TEST_REMOTE_EJB
125                 + " failed to intercept HomeFactoryClassCastException");
126         } catch (HomeFactoryException hfe) {
127             // expected behaviour
128
}
129     }
130
131
132     /**
133      * Tests the exception case wherein an EJB lookup using a remote home
134      * factory component results in a <code>HomeFactoryNamingException</code>.
135      */

136     public void testLocalHomeFactoryNamingException() {
137
138         EJBHome JavaDoc ejbHome = null;
139
140         RemoteHomeFactory homeFactory = (RemoteHomeFactory)
141                 Lookup.getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY);
142
143         // Perform the test using the RemoteHomeFactory
144
try {
145
146             ejbHome = homeFactory.lookup(TEST_NONEXISTANT_EJB);
147
148             fail("Lookup for: "
149                 + TEST_NONEXISTANT_EJB
150                 + " failed to intercept HomeFactoryNamingException");
151         } catch (HomeFactoryException hfe) {
152             // expected behaviour
153
}
154
155
156         // Perform the test using the LocalHomeFactory
157
try {
158
159             ejbHome = homeFactory.lookup(TEST_REMOTE_EJB);
160
161             LocalGateway gateway = ((LocalGatewayHome) ejbHome).create();
162
163             if (log.isTraceEnabled()) {
164                 log.trace("Invoking test method on: "
165                     + TEST_REMOTE_EJB
166                     + " to validate LocalHomeFactory functionality");
167             }
168             gateway.testLocalHomeFactoryNamingException();
169
170             fail("Lookup for: "
171                 + TEST_NONEXISTANT_EJB
172                 + " failed to intercept HomeFactoryNamingException");
173         } catch (HomeFactoryException hfe) {
174             // expected behaviour
175
} catch (RemoteException JavaDoc re) {
176             fail("Failed test; caught RemoteException: " + re);
177         } catch (CreateException JavaDoc ce) {
178             fail("Failed test; caught CreateException: " + ce);
179         }
180     }
181
182
183     /**
184      * Tests the normal EJB lookup case using a remote home factory component.
185      */

186     public void testLocalHomeFactoryLookup() {
187
188         EJBHome JavaDoc ejbHome = null;
189
190         try {
191             RemoteHomeFactory homeFactory = (RemoteHomeFactory)
192                 Lookup.getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY);
193
194             ejbHome = homeFactory.lookup(TEST_REMOTE_EJB);
195         } catch (HomeFactoryException hfe) {
196             fail("Failed to lookup home interface for EJB with logical name: "
197                 + TEST_REMOTE_EJB
198                 + " due to: "
199                 + hfe);
200         }
201
202         // Perform the test using the LocalHomeFactory
203
try {
204
205             LocalGateway gateway = ((LocalGatewayHome) ejbHome).create();
206
207             if (log.isTraceEnabled()) {
208                 log.trace("Invoking test method on: "
209                     + TEST_REMOTE_EJB
210                     + " to validate LocalHomeFactory functionality");
211             }
212             gateway.testLocalHomeFactoryLookup();
213
214         } catch (Exception JavaDoc e) {
215             fail("Lookup for local EJB: "
216                 + TEST_LOCAL_EJB
217                 + " failed due to: "
218                 + e);
219         }
220     }
221
222
223     /**
224      * Tests the normal EJB lookup case using a local home factory component
225      * and a programmatically specificied <code>Context</code> object.
226      */

227     public void testLocalHomeFactoryLookupWithContext() {
228
229         EJBHome JavaDoc ejbHome = null;
230
231         try {
232             InitialContextFactory factory = (InitialContextFactory)
233                 Lookup.getInstance().fetchComponent(TEST_INITIAL_CONTEXT_FACTORY);
234             Context JavaDoc context = factory.getContext();
235
236             RemoteHomeFactory homeFactory = (RemoteHomeFactory)
237                 Lookup.getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY);
238
239             ejbHome = homeFactory.lookup(TEST_REMOTE_EJB, context);
240         } catch (HomeFactoryException hfe) {
241             fail("Failed to lookup home interface for EJB with logical name: "
242                 + TEST_REMOTE_EJB
243                 + " due to: "
244                 + hfe);
245         } catch (NamingException JavaDoc ne) {
246             fail("Failed to lookup home interface for EJB with logical name: "
247                 + TEST_REMOTE_EJB
248                 + " due to: "
249                 + ne);
250         }
251
252         // Perform the test using the LocalHomeFactory
253
try {
254
255             LocalGateway gateway = ((LocalGatewayHome) ejbHome).create();
256
257             if (log.isTraceEnabled()) {
258                 log.trace("Invoking test method on: "
259                     + TEST_REMOTE_EJB
260                     + " to validate LocalHomeFactory functionality");
261             }
262             gateway.testLocalHomeFactoryLookupWithContext();
263
264         } catch (Exception JavaDoc e) {
265             fail("Lookup for local EJB: "
266                 + TEST_LOCAL_EJB
267                 + " failed due to: "
268                 + e);
269         }
270     }
271
272
273     /**
274      * Tests the normal EJB lookup case using a remote home factory component
275      * and programmatically specified credentials.
276      */

277     public void testLocalHomeFactoryLookupWithCredentials() {
278
279         EJBHome JavaDoc ejbHome = null;
280
281         try {
282             RemoteHomeFactory homeFactory = (RemoteHomeFactory)
283                 Lookup.getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY);
284                 
285             InitialContextFactoryConfiguration factoryConfig =
286                 (InitialContextFactoryConfiguration)
287                 Config.getInstance().fetchConfiguration(TEST_INITIAL_CONTEXT_FACTORY);
288
289             ejbHome = homeFactory.lookup(TEST_REMOTE_EJB,
290                 factoryConfig.getEnvironment(Context.SECURITY_PRINCIPAL),
291                 factoryConfig.getEnvironment(Context.SECURITY_CREDENTIALS));
292                 
293         } catch (HomeFactoryException hfe) {
294             fail("Failed to lookup home interface for EJB with logical name: "
295                 + TEST_REMOTE_EJB
296                 + " due to: "
297                 + hfe);
298         }
299
300         // Perform the test using the LocalHomeFactory
301
try {
302
303             LocalGateway gateway = ((LocalGatewayHome) ejbHome).create();
304
305             if (log.isTraceEnabled()) {
306                 log.trace("Invoking test method on: "
307                     + TEST_REMOTE_EJB
308                     + " to validate LocalHomeFactory functionality");
309             }
310             gateway.testLocalHomeFactoryLookup();
311
312         } catch (Exception JavaDoc e) {
313             fail("Lookup for local EJB: "
314                 + TEST_LOCAL_EJB
315                 + " failed due to: "
316                 + e);
317         }
318     }
319
320
321     /**
322      * Method called by jUnit to get all the tests in this test case.
323      * @return Test the suite of tests in this test case
324      */

325     public static Test suite() {
326
327         TestSuite test = new TestSuite();
328
329         test.addTest(new LocalHomeFactoryTest(
330             "testLocalHomeFactoryClassCastException"));
331         test.addTest(new LocalHomeFactoryTest(
332             "testLocalHomeFactoryNamingException"));
333         test.addTest(new LocalHomeFactoryTest(
334             "testLocalHomeFactoryLookup"));
335         test.addTest(new LocalHomeFactoryTest(
336             "testLocalHomeFactoryLookupWithContext"));
337         test.addTest(new LocalHomeFactoryTest(
338             "testLocalHomeFactoryLookupWithCredentials"));
339
340         return test;
341     }
342 }
343
Popular Tags