KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > ejb > remote > test > RemoteHomeFactoryTest


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.remote.test;
19
20
21 import javax.ejb.EJBHome JavaDoc;
22 import javax.naming.Context JavaDoc;
23 import javax.naming.NamingException JavaDoc;
24
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28
29 import org.sape.carbon.core.component.Lookup;
30 import org.sape.carbon.core.config.Config;
31
32 import org.sape.carbon.services.ejb.HomeFactoryException;
33 import org.sape.carbon.services.ejb.remote.RemoteHomeFactory;
34 import org.sape.carbon.services.jndi.InitialContextFactory;
35 import org.sape.carbon.services.jndi.InitialContextFactoryConfiguration;
36
37 public class RemoteHomeFactoryTest extends TestCase {
38
39     /**
40      * Path of the remote home factory test component
41      */

42     public static final String JavaDoc TEST_REMOTE_HOME_FACTORY =
43         "/ejb/test/RemoteHomeFactoryTest";
44
45
46     /**
47      * Path of the RMI-IIOP-disabled remote home factory test component
48      */

49     public static final String JavaDoc TEST_RMIIIOP_DISABLED_HOME_FACTORY =
50         "/ejb/test/RmiIiopDisabledRemoteHomeFactoryTest";
51
52     /**
53      * Path of the remote home factory test component used to generate
54      * a <code>ClassCastException</code> error condition
55      */

56     public static final String JavaDoc TEST_MISCAST_HOME_FACTORY =
57         "/ejb/test/MiscastRemoteHomeFactoryTest";
58
59     /**
60      * Path of the remote home factory test component
61      */

62     public static final String JavaDoc TEST_INITIAL_CONTEXT_FACTORY =
63         "/ejb/test/TestInitialContextFactory";
64
65
66     /**
67      * Logical name of the <code>Tester</code> EJB
68      */

69     public static final String JavaDoc TEST_REMOTE_EJB =
70         "org.sape.carbon.services.ejb.remote.test.Tester";
71
72
73     /**
74      * Logical name of a non-existant EJB used for generating a
75      * <code>HomeFactoryNamingException</code> error condition
76      */

77     public static final String JavaDoc TEST_NONEXISTANT_EJB =
78         "org.sape.carbon.services.ejb.test.Nonexistant";
79
80
81     public RemoteHomeFactoryTest(String JavaDoc name) {
82         super(name);
83     }
84
85
86     public static void main(String JavaDoc args[]) throws Exception JavaDoc{
87     }
88
89
90     /**
91      * Tests the exception case wherein an EJB lookup using a remote home
92      * factory component results in a
93      * <code>HomeFactoryClassCastException</code>.
94      */

95     public void testRemoteHomeFactoryClassCastException() {
96
97         EJBHome JavaDoc ejbHome = null;
98
99         // Perform the test using the RemoteHomeFactory
100
try {
101             RemoteHomeFactory homeFactory = (RemoteHomeFactory)
102                 Lookup.getInstance().fetchComponent(TEST_MISCAST_HOME_FACTORY);
103
104             ejbHome = homeFactory.lookup(TEST_REMOTE_EJB);
105
106             fail("Lookup for: "
107                 + TEST_REMOTE_EJB
108                 + " failed to intercept HomeFactoryClassCastException");
109         } catch (HomeFactoryException hfe) {
110             // expected behaviour
111
}
112     }
113
114
115     /**
116      * Tests the exception case wherein an EJB lookup using a remote home
117      * factory component results in a <code>HomeFactoryNamingException</code>.
118      */

119     public void testRemoteHomeFactoryNamingException() {
120
121         EJBHome JavaDoc ejbHome = null;
122
123         RemoteHomeFactory homeFactory = (RemoteHomeFactory)
124                 Lookup.getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY);
125
126         // Perform the test using the RemoteHomeFactory
127
try {
128
129             ejbHome = homeFactory.lookup(TEST_NONEXISTANT_EJB);
130
131             fail("Lookup for: "
132                 + TEST_NONEXISTANT_EJB
133                 + " failed to intercept HomeFactoryNamingException");
134         } catch (HomeFactoryException hfe) {
135             // expected behaviour
136
}
137     }
138
139
140     /**
141      * Tests the normal EJB lookup case using a remote home factory component.
142      */

143     public void testRemoteHomeFactoryLookup() throws Exception JavaDoc {
144
145         TesterHome ejbHome = null;
146
147         try {
148             RemoteHomeFactory homeFactory = (RemoteHomeFactory)
149                 Lookup.getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY);
150
151             ejbHome = (TesterHome) homeFactory.lookup(TEST_REMOTE_EJB);
152             
153             ejbHome.create().testMe();
154         } catch (HomeFactoryException hfe) {
155             fail("Failed to lookup home interface for EJB with logical name: "
156                 + TEST_REMOTE_EJB
157                 + " due to: "
158                 + hfe);
159         }
160     }
161
162
163     /**
164      * Tests the normal EJB lookup case using an RMI-IIOP-disabled remote home
165      * factory component
166      */

167     public void testRemoteHomeFactoryLookupWithoutRmiIiop() {
168
169         EJBHome JavaDoc ejbHome = null;
170
171         try {
172             RemoteHomeFactory homeFactory = (RemoteHomeFactory)
173                 Lookup.getInstance().fetchComponent(
174                     TEST_RMIIIOP_DISABLED_HOME_FACTORY);
175
176             ejbHome = homeFactory.lookup(TEST_REMOTE_EJB);
177         } catch (HomeFactoryException hfe) {
178             fail("Failed to lookup home interface for EJB with logical name: "
179                 + TEST_REMOTE_EJB
180                 + " due to: "
181                 + hfe);
182         }
183     }
184
185
186     /**
187      * Tests the normal EJB lookup case using a local home factory component
188      * and a programmatically specificied <code>Context</code> object.
189      */

190     public void testRemoteHomeFactoryLookupWithContext() {
191
192         EJBHome JavaDoc ejbHome = null;
193
194         try {
195             InitialContextFactory factory = (InitialContextFactory)
196                 Lookup.getInstance().fetchComponent(TEST_INITIAL_CONTEXT_FACTORY);
197             Context JavaDoc context = factory.getContext();
198
199             RemoteHomeFactory homeFactory = (RemoteHomeFactory)
200                 Lookup.getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY);
201
202             ejbHome = homeFactory.lookup(TEST_REMOTE_EJB, context);
203         } catch (HomeFactoryException hfe) {
204             fail("Failed to lookup home interface for EJB with logical name: "
205                 + TEST_REMOTE_EJB
206                 + " due to: "
207                 + hfe);
208         } catch (NamingException JavaDoc ne) {
209             fail("Failed to lookup home interface for EJB with logical name: "
210                 + TEST_REMOTE_EJB
211                 + " due to: "
212                 + ne);
213         }
214     }
215
216
217     /**
218      * Tests the normal EJB lookup case using a remote home factory component
219      * and programmatically specified credentials.
220      */

221     public void testRemoteHomeFactoryLookupWithCredentials() {
222
223         EJBHome JavaDoc ejbHome = null;
224
225         try {
226             RemoteHomeFactory homeFactory = (RemoteHomeFactory)
227                 Lookup.getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY);
228
229             InitialContextFactoryConfiguration factoryConfig =
230                 (InitialContextFactoryConfiguration)
231                 Config.getInstance().fetchConfiguration(TEST_INITIAL_CONTEXT_FACTORY);
232
233             ejbHome = homeFactory.lookup(TEST_REMOTE_EJB,
234                 factoryConfig.getEnvironment(Context.SECURITY_PRINCIPAL),
235                 factoryConfig.getEnvironment(Context.SECURITY_CREDENTIALS));
236
237         } catch (HomeFactoryException hfe) {
238             fail("Failed to lookup home interface for EJB with logical name: "
239                 + TEST_REMOTE_EJB
240                 + " due to: "
241                 + hfe);
242         }
243     }
244
245
246     /**
247      * Method called by jUnit to get all the tests in this test case.
248      * @return Test the suite of tests in this test case
249      */

250     public static Test suite() {
251
252         TestSuite test = new TestSuite();
253
254         test.addTest(new RemoteHomeFactoryTest(
255             "testRemoteHomeFactoryClassCastException"));
256         test.addTest(new RemoteHomeFactoryTest(
257             "testRemoteHomeFactoryNamingException"));
258         test.addTest(new RemoteHomeFactoryTest(
259             "testRemoteHomeFactoryLookup"));
260         test.addTest(new RemoteHomeFactoryTest(
261             "testRemoteHomeFactoryLookupWithoutRmiIiop"));
262         test.addTest(new RemoteHomeFactoryTest(
263             "testRemoteHomeFactoryLookupWithContext"));
264         test.addTest(new RemoteHomeFactoryTest(
265             "testRemoteHomeFactoryLookupWithCredentials"));
266
267         return test;
268     }
269 }
270
Popular Tags