001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.services.ejb.local.test;
019:
020: import java.rmi.RemoteException;
021:
022: import javax.ejb.CreateException;
023: import javax.ejb.EJBHome;
024: import javax.naming.Context;
025: import javax.naming.NamingException;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: import junit.framework.Test;
031: import junit.framework.TestCase;
032: import junit.framework.TestSuite;
033:
034: import org.sape.carbon.core.component.Lookup;
035: import org.sape.carbon.core.config.Config;
036:
037: import org.sape.carbon.services.ejb.HomeFactoryException;
038: import org.sape.carbon.services.ejb.remote.RemoteHomeFactory;
039: import org.sape.carbon.services.jndi.InitialContextFactory;
040: import org.sape.carbon.services.jndi.InitialContextFactoryConfiguration;
041:
042: public class LocalHomeFactoryTest extends TestCase {
043:
044: /**
045: * Provides a handle to Apache-commons logger
046: */
047: private Log log = LogFactory.getLog(this .getClass());
048:
049: /**
050: * Path of the local home factory test component
051: */
052: public static final String TEST_LOCAL_HOME_FACTORY = "/ejb/test/LocalHomeFactoryTest";
053:
054: /**
055: * Path of the remote home factory test component
056: */
057: public static final String TEST_REMOTE_HOME_FACTORY = "/ejb/test/RemoteHomeFactoryTest";
058:
059: /**
060: * Path of the remote home factory test component used to generate
061: * a <code>ClassCastException</code> error condition
062: */
063: public static final String TEST_MISCAST_HOME_FACTORY = "/ejb/test/MiscastRemoteHomeFactoryTest";
064:
065: /**
066: * Path of the remote home factory test component
067: */
068: public static final String TEST_INITIAL_CONTEXT_FACTORY = "/ejb/test/TestInitialContextFactory";
069:
070: /**
071: * Logical name of the <code>LocalGateway</code> EJB
072: */
073: public static final String TEST_REMOTE_EJB = "org.sape.carbon.services.ejb.local.test.LocalGateway";
074:
075: /**
076: * Logical name of the <code>LocalTester</code> EJB
077: */
078: public static final String TEST_LOCAL_EJB = "org.sape.carbon.services.ejb.local.test.LocalTester";
079:
080: /**
081: * Logical name of a non-existant EJB used for generating a
082: * <code>HomeFactoryNamingException</code> error condition
083: */
084: public static final String TEST_NONEXISTANT_EJB = "org.sape.carbon.services.ejb.test.Nonexistant";
085:
086: public LocalHomeFactoryTest(String name) {
087: super (name);
088: }
089:
090: public static void main(String args[]) throws Exception {
091: }
092:
093: /**
094: * Tests the exception case wherein an EJB lookup using a remote home
095: * factory component results in a
096: * <code>HomeFactoryClassCastException</code>.
097: */
098: public void testLocalHomeFactoryClassCastException() {
099:
100: EJBHome ejbHome = null;
101:
102: // Perform the test using the RemoteHomeFactory
103: try {
104: RemoteHomeFactory homeFactory = (RemoteHomeFactory) Lookup
105: .getInstance().fetchComponent(
106: TEST_MISCAST_HOME_FACTORY);
107:
108: ejbHome = homeFactory.lookup(TEST_REMOTE_EJB);
109:
110: fail("Lookup for: "
111: + TEST_REMOTE_EJB
112: + " failed to intercept HomeFactoryClassCastException");
113: } catch (HomeFactoryException hfe) {
114: // expected behaviour
115: }
116: }
117:
118: /**
119: * Tests the exception case wherein an EJB lookup using a remote home
120: * factory component results in a <code>HomeFactoryNamingException</code>.
121: */
122: public void testLocalHomeFactoryNamingException() {
123:
124: EJBHome ejbHome = null;
125:
126: RemoteHomeFactory homeFactory = (RemoteHomeFactory) Lookup
127: .getInstance().fetchComponent(TEST_REMOTE_HOME_FACTORY);
128:
129: // Perform the test using the RemoteHomeFactory
130: try {
131:
132: ejbHome = homeFactory.lookup(TEST_NONEXISTANT_EJB);
133:
134: fail("Lookup for: " + TEST_NONEXISTANT_EJB
135: + " failed to intercept HomeFactoryNamingException");
136: } catch (HomeFactoryException hfe) {
137: // expected behaviour
138: }
139:
140: // Perform the test using the LocalHomeFactory
141: try {
142:
143: ejbHome = homeFactory.lookup(TEST_REMOTE_EJB);
144:
145: LocalGateway gateway = ((LocalGatewayHome) ejbHome)
146: .create();
147:
148: if (log.isTraceEnabled()) {
149: log
150: .trace("Invoking test method on: "
151: + TEST_REMOTE_EJB
152: + " to validate LocalHomeFactory functionality");
153: }
154: gateway.testLocalHomeFactoryNamingException();
155:
156: fail("Lookup for: " + TEST_NONEXISTANT_EJB
157: + " failed to intercept HomeFactoryNamingException");
158: } catch (HomeFactoryException hfe) {
159: // expected behaviour
160: } catch (RemoteException re) {
161: fail("Failed test; caught RemoteException: " + re);
162: } catch (CreateException ce) {
163: fail("Failed test; caught CreateException: " + ce);
164: }
165: }
166:
167: /**
168: * Tests the normal EJB lookup case using a remote home factory component.
169: */
170: public void testLocalHomeFactoryLookup() {
171:
172: EJBHome ejbHome = null;
173:
174: try {
175: RemoteHomeFactory homeFactory = (RemoteHomeFactory) Lookup
176: .getInstance().fetchComponent(
177: TEST_REMOTE_HOME_FACTORY);
178:
179: ejbHome = homeFactory.lookup(TEST_REMOTE_EJB);
180: } catch (HomeFactoryException hfe) {
181: fail("Failed to lookup home interface for EJB with logical name: "
182: + TEST_REMOTE_EJB + " due to: " + hfe);
183: }
184:
185: // Perform the test using the LocalHomeFactory
186: try {
187:
188: LocalGateway gateway = ((LocalGatewayHome) ejbHome)
189: .create();
190:
191: if (log.isTraceEnabled()) {
192: log
193: .trace("Invoking test method on: "
194: + TEST_REMOTE_EJB
195: + " to validate LocalHomeFactory functionality");
196: }
197: gateway.testLocalHomeFactoryLookup();
198:
199: } catch (Exception e) {
200: fail("Lookup for local EJB: " + TEST_LOCAL_EJB
201: + " failed due to: " + e);
202: }
203: }
204:
205: /**
206: * Tests the normal EJB lookup case using a local home factory component
207: * and a programmatically specificied <code>Context</code> object.
208: */
209: public void testLocalHomeFactoryLookupWithContext() {
210:
211: EJBHome ejbHome = null;
212:
213: try {
214: InitialContextFactory factory = (InitialContextFactory) Lookup
215: .getInstance().fetchComponent(
216: TEST_INITIAL_CONTEXT_FACTORY);
217: Context context = factory.getContext();
218:
219: RemoteHomeFactory homeFactory = (RemoteHomeFactory) Lookup
220: .getInstance().fetchComponent(
221: TEST_REMOTE_HOME_FACTORY);
222:
223: ejbHome = homeFactory.lookup(TEST_REMOTE_EJB, context);
224: } catch (HomeFactoryException hfe) {
225: fail("Failed to lookup home interface for EJB with logical name: "
226: + TEST_REMOTE_EJB + " due to: " + hfe);
227: } catch (NamingException ne) {
228: fail("Failed to lookup home interface for EJB with logical name: "
229: + TEST_REMOTE_EJB + " due to: " + ne);
230: }
231:
232: // Perform the test using the LocalHomeFactory
233: try {
234:
235: LocalGateway gateway = ((LocalGatewayHome) ejbHome)
236: .create();
237:
238: if (log.isTraceEnabled()) {
239: log
240: .trace("Invoking test method on: "
241: + TEST_REMOTE_EJB
242: + " to validate LocalHomeFactory functionality");
243: }
244: gateway.testLocalHomeFactoryLookupWithContext();
245:
246: } catch (Exception e) {
247: fail("Lookup for local EJB: " + TEST_LOCAL_EJB
248: + " failed due to: " + e);
249: }
250: }
251:
252: /**
253: * Tests the normal EJB lookup case using a remote home factory component
254: * and programmatically specified credentials.
255: */
256: public void testLocalHomeFactoryLookupWithCredentials() {
257:
258: EJBHome ejbHome = null;
259:
260: try {
261: RemoteHomeFactory homeFactory = (RemoteHomeFactory) Lookup
262: .getInstance().fetchComponent(
263: TEST_REMOTE_HOME_FACTORY);
264:
265: InitialContextFactoryConfiguration factoryConfig = (InitialContextFactoryConfiguration) Config
266: .getInstance().fetchConfiguration(
267: TEST_INITIAL_CONTEXT_FACTORY);
268:
269: ejbHome = homeFactory
270: .lookup(
271: TEST_REMOTE_EJB,
272: factoryConfig
273: .getEnvironment(Context.SECURITY_PRINCIPAL),
274: factoryConfig
275: .getEnvironment(Context.SECURITY_CREDENTIALS));
276:
277: } catch (HomeFactoryException hfe) {
278: fail("Failed to lookup home interface for EJB with logical name: "
279: + TEST_REMOTE_EJB + " due to: " + hfe);
280: }
281:
282: // Perform the test using the LocalHomeFactory
283: try {
284:
285: LocalGateway gateway = ((LocalGatewayHome) ejbHome)
286: .create();
287:
288: if (log.isTraceEnabled()) {
289: log
290: .trace("Invoking test method on: "
291: + TEST_REMOTE_EJB
292: + " to validate LocalHomeFactory functionality");
293: }
294: gateway.testLocalHomeFactoryLookup();
295:
296: } catch (Exception e) {
297: fail("Lookup for local EJB: " + TEST_LOCAL_EJB
298: + " failed due to: " + e);
299: }
300: }
301:
302: /**
303: * Method called by jUnit to get all the tests in this test case.
304: * @return Test the suite of tests in this test case
305: */
306: public static Test suite() {
307:
308: TestSuite test = new TestSuite();
309:
310: test.addTest(new LocalHomeFactoryTest(
311: "testLocalHomeFactoryClassCastException"));
312: test.addTest(new LocalHomeFactoryTest(
313: "testLocalHomeFactoryNamingException"));
314: test.addTest(new LocalHomeFactoryTest(
315: "testLocalHomeFactoryLookup"));
316: test.addTest(new LocalHomeFactoryTest(
317: "testLocalHomeFactoryLookupWithContext"));
318: test.addTest(new LocalHomeFactoryTest(
319: "testLocalHomeFactoryLookupWithCredentials"));
320:
321: return test;
322: }
323: }
|