001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test.stateless;
017:
018: import junit.framework.Assert;
019: import junit.framework.AssertionFailedError;
020: import org.apache.openejb.test.TestFailureException;
021: import org.apache.openejb.test.entity.bmp.BasicBmpHome;
022: import org.apache.openejb.test.entity.bmp.BasicBmpObject;
023: import org.apache.openejb.test.stateful.BasicStatefulHome;
024: import org.apache.openejb.test.stateful.BasicStatefulObject;
025: import org.apache.openejb.test.stateful.BasicStatefulBusinessLocal;
026: import org.apache.openejb.test.stateful.BasicStatefulBusinessRemote;
027:
028: import javax.ejb.EJBContext;
029: import javax.ejb.EJBException;
030: import javax.ejb.SessionContext;
031: import javax.persistence.EntityManagerFactory;
032: import javax.persistence.EntityManager;
033: import javax.sql.DataSource;
034: import javax.jms.ConnectionFactory;
035: import javax.jms.Connection;
036: import javax.jms.Session;
037: import javax.jms.Topic;
038: import javax.jms.MessageProducer;
039: import javax.jms.TopicConnectionFactory;
040: import javax.jms.JMSException;
041: import javax.jms.QueueConnectionFactory;
042: import javax.naming.InitialContext;
043: import java.rmi.RemoteException;
044:
045: /**
046: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
047: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
048: */
049: public class ContextLookupStatelessBean implements
050: javax.ejb.SessionBean {
051:
052: private String name;
053: private SessionContext ejbContext;
054:
055: //=============================
056: // Home interface methods
057: //
058: //
059: // Home interface methods
060: //=============================
061:
062: //=============================
063: // Remote interface methods
064: //
065: public void lookupEntityBean() throws TestFailureException {
066: try {
067: try {
068: BasicBmpHome home = (BasicBmpHome) ejbContext
069: .lookup("stateless/beanReferences/bmp_entity");
070: Assert.assertNotNull("The EJBHome looked up is null",
071: home);
072:
073: BasicBmpObject object = home.createObject("Enc Bean");
074: Assert.assertNotNull("The EJBObject is null", object);
075: } catch (Exception e) {
076: Assert.fail("Received Exception " + e.getClass()
077: + " : " + e.getMessage());
078: }
079: } catch (AssertionFailedError afe) {
080: throw new TestFailureException(afe);
081: }
082: }
083:
084: public void lookupStatefulBean() throws TestFailureException {
085: try {
086: try {
087: BasicStatefulHome home = (BasicStatefulHome) ejbContext
088: .lookup("stateless/beanReferences/stateful");
089: Assert.assertNotNull("The EJBHome looked up is null",
090: home);
091:
092: BasicStatefulObject object = home
093: .createObject("Enc Bean");
094: Assert.assertNotNull("The EJBObject is null", object);
095: } catch (Exception e) {
096: Assert.fail("Received Exception " + e.getClass()
097: + " : " + e.getMessage());
098: }
099: } catch (AssertionFailedError afe) {
100: throw new TestFailureException(afe);
101: }
102: }
103:
104: public void lookupStatelessBean() throws TestFailureException {
105: try {
106: try {
107: BasicStatelessHome home = (BasicStatelessHome) ejbContext
108: .lookup("stateless/beanReferences/stateless");
109: Assert.assertNotNull("The EJBHome looked up is null",
110: home);
111:
112: BasicStatelessObject object = home.createObject();
113: Assert.assertNotNull("The EJBObject is null", object);
114: } catch (Exception e) {
115: Assert.fail("Received Exception " + e.getClass()
116: + " : " + e.getMessage());
117: }
118: } catch (AssertionFailedError afe) {
119: throw new TestFailureException(afe);
120: }
121: }
122:
123: public void lookupStatelessBusinessLocal()
124: throws TestFailureException {
125: try {
126: try {
127: BasicStatelessBusinessLocal object = (BasicStatelessBusinessLocal) ejbContext
128: .lookup("stateless/beanReferences/stateless-business-local");
129: Assert.assertNotNull("The EJB BusinessLocal is null",
130: object);
131: } catch (Exception e) {
132: Assert.fail("Received Exception " + e.getClass()
133: + " : " + e.getMessage());
134: }
135: } catch (AssertionFailedError afe) {
136: throw new TestFailureException(afe);
137: }
138: }
139:
140: public void lookupStatelessBusinessRemote()
141: throws TestFailureException {
142: try {
143: try {
144: BasicStatelessBusinessRemote object = (BasicStatelessBusinessRemote) ejbContext
145: .lookup("stateless/beanReferences/stateless-business-remote");
146: Assert.assertNotNull("The EJB BusinessRemote is null",
147: object);
148: } catch (Exception e) {
149: Assert.fail("Received Exception " + e.getClass()
150: + " : " + e.getMessage());
151: }
152: } catch (AssertionFailedError afe) {
153: throw new TestFailureException(afe);
154: }
155: }
156:
157: public void lookupStatefulBusinessLocal()
158: throws TestFailureException {
159: try {
160: try {
161: BasicStatefulBusinessLocal object = (BasicStatefulBusinessLocal) ejbContext
162: .lookup("stateless/beanReferences/stateful-business-local");
163: Assert.assertNotNull("The EJB BusinessLocal is null",
164: object);
165: } catch (Exception e) {
166: Assert.fail("Received Exception " + e.getClass()
167: + " : " + e.getMessage());
168: }
169: } catch (AssertionFailedError afe) {
170: throw new TestFailureException(afe);
171: }
172: }
173:
174: public void lookupStatefulBusinessRemote()
175: throws TestFailureException {
176: try {
177: try {
178: BasicStatefulBusinessRemote object = (BasicStatefulBusinessRemote) ejbContext
179: .lookup("stateless/beanReferences/stateful-business-remote");
180: Assert.assertNotNull("The EJB BusinessRemote is null",
181: object);
182: } catch (Exception e) {
183: Assert.fail("Received Exception " + e.getClass()
184: + " : " + e.getMessage());
185: }
186: } catch (AssertionFailedError afe) {
187: throw new TestFailureException(afe);
188: }
189: }
190:
191: public void lookupStringEntry() throws TestFailureException {
192: try {
193: try {
194: String expected = new String("1");
195: String actual = (String) ejbContext
196: .lookup("stateless/references/String");
197:
198: Assert.assertNotNull("The String looked up is null",
199: actual);
200: Assert.assertEquals(expected, actual);
201:
202: } catch (Exception e) {
203: Assert.fail("Received Exception " + e.getClass()
204: + " : " + e.getMessage());
205: }
206: } catch (AssertionFailedError afe) {
207: throw new TestFailureException(afe);
208: }
209: }
210:
211: public void lookupDoubleEntry() throws TestFailureException {
212: try {
213: try {
214: Double expected = new Double(1.0D);
215: Double actual = (Double) ejbContext
216: .lookup("stateless/references/Double");
217:
218: Assert.assertNotNull("The Double looked up is null",
219: actual);
220: Assert.assertEquals(expected, actual);
221:
222: } catch (Exception e) {
223: Assert.fail("Received Exception " + e.getClass()
224: + " : " + e.getMessage());
225: }
226: } catch (AssertionFailedError afe) {
227: throw new TestFailureException(afe);
228: }
229: }
230:
231: public void lookupLongEntry() throws TestFailureException {
232: try {
233: try {
234: Long expected = new Long(1L);
235: Long actual = (Long) ejbContext
236: .lookup("stateless/references/Long");
237:
238: Assert.assertNotNull("The Long looked up is null",
239: actual);
240: Assert.assertEquals(expected, actual);
241:
242: } catch (Exception e) {
243: Assert.fail("Received Exception " + e.getClass()
244: + " : " + e.getMessage());
245: }
246: } catch (AssertionFailedError afe) {
247: throw new TestFailureException(afe);
248: }
249: }
250:
251: public void lookupFloatEntry() throws TestFailureException {
252: try {
253: try {
254: Float expected = new Float(1.0F);
255: Float actual = (Float) ejbContext
256: .lookup("stateless/references/Float");
257:
258: Assert.assertNotNull("The Float looked up is null",
259: actual);
260: Assert.assertEquals(expected, actual);
261:
262: } catch (Exception e) {
263: Assert.fail("Received Exception " + e.getClass()
264: + " : " + e.getMessage());
265: }
266: } catch (AssertionFailedError afe) {
267: throw new TestFailureException(afe);
268: }
269: }
270:
271: public void lookupIntegerEntry() throws TestFailureException {
272: try {
273: try {
274: Integer expected = new Integer(1);
275: Integer actual = (Integer) ejbContext
276: .lookup("stateless/references/Integer");
277:
278: Assert.assertNotNull("The Integer looked up is null",
279: actual);
280: Assert.assertEquals(expected, actual);
281:
282: } catch (Exception e) {
283: Assert.fail("Received Exception " + e.getClass()
284: + " : " + e.getMessage());
285: }
286: } catch (AssertionFailedError afe) {
287: throw new TestFailureException(afe);
288: }
289: }
290:
291: public void lookupShortEntry() throws TestFailureException {
292: try {
293: try {
294: Short expected = new Short((short) 1);
295: Short actual = (Short) ejbContext
296: .lookup("stateless/references/Short");
297:
298: Assert.assertNotNull("The Short looked up is null",
299: actual);
300: Assert.assertEquals(expected, actual);
301:
302: } catch (Exception e) {
303: Assert.fail("Received Exception " + e.getClass()
304: + " : " + e.getMessage());
305: }
306: } catch (AssertionFailedError afe) {
307: throw new TestFailureException(afe);
308: }
309: }
310:
311: public void lookupBooleanEntry() throws TestFailureException {
312: try {
313: try {
314: Boolean expected = new Boolean(true);
315: Boolean actual = (Boolean) ejbContext
316: .lookup("stateless/references/Boolean");
317:
318: Assert.assertNotNull("The Boolean looked up is null",
319: actual);
320: Assert.assertEquals(expected, actual);
321:
322: } catch (Exception e) {
323: Assert.fail("Received Exception " + e.getClass()
324: + " : " + e.getMessage());
325: }
326: } catch (AssertionFailedError afe) {
327: throw new TestFailureException(afe);
328: }
329: }
330:
331: public void lookupByteEntry() throws TestFailureException {
332: try {
333: try {
334: Byte expected = new Byte((byte) 1);
335: Byte actual = (Byte) ejbContext
336: .lookup("stateless/references/Byte");
337:
338: Assert.assertNotNull("The Byte looked up is null",
339: actual);
340: Assert.assertEquals(expected, actual);
341:
342: } catch (Exception e) {
343: Assert.fail("Received Exception " + e.getClass()
344: + " : " + e.getMessage());
345: }
346: } catch (AssertionFailedError afe) {
347: throw new TestFailureException(afe);
348: }
349: }
350:
351: public void lookupCharacterEntry() throws TestFailureException {
352: try {
353: try {
354: Character expected = new Character('D');
355: Character actual = (Character) ejbContext
356: .lookup("stateless/references/Character");
357:
358: Assert.assertNotNull("The Character looked up is null",
359: actual);
360: Assert.assertEquals(expected, actual);
361:
362: } catch (Exception e) {
363: Assert.fail("Received Exception " + e.getClass()
364: + " : " + e.getMessage());
365: }
366: } catch (AssertionFailedError afe) {
367: throw new TestFailureException(afe);
368: }
369: }
370:
371: public void lookupResource() throws TestFailureException {
372: try {
373: try {
374: Object obj = ejbContext.lookup("datasource");
375: Assert.assertNotNull("The DataSource is null", obj);
376: Assert.assertTrue("Not an instance of DataSource",
377: obj instanceof DataSource);
378: } catch (Exception e) {
379: Assert.fail("Received Exception " + e.getClass()
380: + " : " + e.getMessage());
381: }
382: } catch (AssertionFailedError afe) {
383: throw new TestFailureException(afe);
384: }
385: }
386:
387: public void lookupJMSConnectionFactory()
388: throws TestFailureException {
389: try {
390: try {
391: Object obj = ejbContext.lookup("jms");
392: Assert.assertNotNull(
393: "The JMS ConnectionFactory is null", obj);
394: Assert.assertTrue(
395: "Not an instance of ConnectionFactory",
396: obj instanceof ConnectionFactory);
397: ConnectionFactory connectionFactory = (ConnectionFactory) obj;
398: testJmsConnection(connectionFactory.createConnection());
399:
400: obj = ejbContext.lookup("TopicCF");
401: Assert.assertNotNull(
402: "The JMS TopicConnectionFactory is null", obj);
403: Assert.assertTrue(
404: "Not an instance of TopicConnectionFactory",
405: obj instanceof TopicConnectionFactory);
406: TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) obj;
407: testJmsConnection(topicConnectionFactory
408: .createConnection());
409:
410: obj = ejbContext.lookup("QueueCF");
411: Assert.assertNotNull(
412: "The JMS QueueConnectionFactory is null", obj);
413: Assert.assertTrue(
414: "Not an instance of QueueConnectionFactory",
415: obj instanceof QueueConnectionFactory);
416: QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;
417: testJmsConnection(queueConnectionFactory
418: .createConnection());
419: } catch (Exception e) {
420: e.printStackTrace();
421: Assert.fail("Received Exception " + e.getClass()
422: + " : " + e.getMessage());
423: }
424: } catch (AssertionFailedError afe) {
425: throw new TestFailureException(afe);
426: }
427: }
428:
429: private void testJmsConnection(Connection connection)
430: throws JMSException {
431: Session session = connection.createSession(false,
432: Session.DUPS_OK_ACKNOWLEDGE);
433: Topic topic = session.createTopic("test");
434: MessageProducer producer = session.createProducer(topic);
435: producer.send(session.createMessage());
436: producer.close();
437: session.close();
438: connection.close();
439: }
440:
441: public void lookupPersistenceUnit() throws TestFailureException {
442: try {
443: try {
444: EntityManagerFactory emf = (EntityManagerFactory) ejbContext
445: .lookup("persistence/TestUnit");
446: Assert.assertNotNull(
447: "The EntityManagerFactory is null", emf);
448:
449: } catch (Exception e) {
450: Assert.fail("Received Exception " + e.getClass()
451: + " : " + e.getMessage());
452: }
453: } catch (AssertionFailedError afe) {
454: throw new TestFailureException(afe);
455: }
456: }
457:
458: public void lookupPersistenceContext() throws TestFailureException {
459: try {
460: try {
461: EntityManager em = (EntityManager) ejbContext
462: .lookup("persistence/TestContext");
463: Assert.assertNotNull("The EntityManager is null", em);
464:
465: // call a do nothing method to assure entity manager actually exists
466: em.getFlushMode();
467: } catch (Exception e) {
468: Assert.fail("Received Exception " + e.getClass()
469: + " : " + e.getMessage());
470: }
471: } catch (AssertionFailedError afe) {
472: throw new TestFailureException(afe);
473: }
474: }
475:
476: public void lookupSessionContext() throws TestFailureException {
477: try {
478: try {
479: InitialContext ctx = new InitialContext();
480: Assert.assertNotNull("The InitialContext is null", ctx);
481:
482: // lookup in enc
483: SessionContext sctx = (SessionContext) ctx
484: .lookup("java:comp/env/sessioncontext");
485: Assert
486: .assertNotNull(
487: "The SessionContext got from java:comp/env/sessioncontext is null",
488: sctx);
489:
490: // lookup using global name
491: EJBContext ejbCtx = (EJBContext) ctx
492: .lookup("java:comp/EJBContext");
493: Assert
494: .assertNotNull(
495: "The SessionContext got from java:comp/EJBContext is null ",
496: ejbCtx);
497:
498: // verify context was set via legacy set method
499: Assert
500: .assertNotNull(
501: "The SessionContext is null from setter method",
502: ejbContext);
503: } catch (Exception e) {
504: Assert.fail("Received Exception " + e.getClass()
505: + " : " + e.getMessage());
506: }
507: } catch (AssertionFailedError afe) {
508: throw new TestFailureException(afe);
509: }
510:
511: }
512:
513: //
514: // Remote interface methods
515: //=============================
516:
517: //================================
518: // SessionBean interface methods
519: //
520:
521: /**
522: * Set the associated session context. The container calls this method
523: * after the instance creation.
524: */
525: public void setSessionContext(SessionContext ctx)
526: throws EJBException, RemoteException {
527: ejbContext = ctx;
528: }
529:
530: /**
531: * @throws javax.ejb.CreateException
532: */
533: public void ejbCreate() throws javax.ejb.CreateException {
534: this .name = "nameless automaton";
535: }
536:
537: /**
538: * A container invokes this method before it ends the life of the session
539: * object. This happens as a result of a client's invoking a remove
540: * operation, or when a container decides to terminate the session object
541: * after a timeout.
542: */
543: public void ejbRemove() throws EJBException, RemoteException {
544: }
545:
546: /**
547: * The activate method is called when the instance is activated
548: * from its "passive" state. The instance should acquire any resource
549: * that it has released earlier in the ejbPassivate() method.
550: */
551: public void ejbActivate() throws EJBException, RemoteException {
552: // Should never called.
553: }
554:
555: /**
556: * The passivate method is called before the instance enters
557: * the "passive" state. The instance should release any resources that
558: * it can re-acquire later in the ejbActivate() method.
559: */
560: public void ejbPassivate() throws EJBException, RemoteException {
561: // Should never called.
562: }
563:
564: //
565: // SessionBean interface methods
566: //================================
567:
568: public String remove(String arg) {
569: return arg;
570: }
571: }
|