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.stateful;
017:
018: import java.rmi.RemoteException;
019:
020: import javax.ejb.EJBContext;
021: import javax.ejb.EJBException;
022: import javax.ejb.SessionContext;
023: import javax.ejb.SessionSynchronization;
024: import javax.ejb.CreateException;
025: import javax.naming.InitialContext;
026: import javax.persistence.EntityManagerFactory;
027: import javax.persistence.EntityManager;
028: import javax.sql.DataSource;
029: import javax.transaction.UserTransaction;
030: import javax.jms.ConnectionFactory;
031: import javax.jms.Connection;
032: import javax.jms.Session;
033: import javax.jms.Topic;
034: import javax.jms.MessageProducer;
035: import javax.jms.TopicConnectionFactory;
036: import javax.jms.QueueConnectionFactory;
037: import javax.jms.JMSException;
038:
039: import junit.framework.Assert;
040: import junit.framework.AssertionFailedError;
041:
042: import org.apache.openejb.test.TestFailureException;
043: import org.apache.openejb.test.entity.bmp.BasicBmpHome;
044: import org.apache.openejb.test.entity.bmp.BasicBmpObject;
045: import org.apache.openejb.test.stateless.BasicStatelessHome;
046: import org.apache.openejb.test.stateless.BasicStatelessObject;
047: import org.apache.openejb.test.stateless.BasicStatelessBusinessLocal;
048: import org.apache.openejb.test.stateless.BasicStatelessBusinessRemote;
049:
050: /**
051: *
052: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
053: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
054: */
055: public class EncStatefulBean implements javax.ejb.SessionBean,
056: SessionSynchronization {
057:
058: private String name;
059: private SessionContext ejbContext;
060:
061: //=============================
062: // Home interface methods
063: //
064: /**
065: * Maps to EncStatefulHome.create
066: *
067: * @param name
068: * @exception javax.ejb.CreateException
069: * @see EncStatefulHome#create
070: */
071: public void ejbCreate(String name) throws CreateException {
072: this .name = name;
073: }
074:
075: //
076: // Home interface methods
077: //=============================
078:
079: //=============================
080: // Remote interface methods
081: //
082:
083: public void lookupEntityBean() throws TestFailureException {
084: try {
085: try {
086: InitialContext ctx = new InitialContext();
087: Assert.assertNotNull("The InitialContext is null", ctx);
088:
089: BasicBmpHome home = (BasicBmpHome) javax.rmi.PortableRemoteObject
090: .narrow(
091: ctx
092: .lookup("java:comp/env/stateful/beanReferences/bmp_entity"),
093: BasicBmpHome.class);
094: Assert.assertNotNull("The EJBHome looked up is null",
095: home);
096:
097: BasicBmpObject object = home.createObject("Enc Bean");
098: Assert.assertNotNull("The EJBObject is null", object);
099: } catch (Exception e) {
100: Assert.fail("Received Exception " + e.getClass()
101: + " : " + e.getMessage());
102: }
103: } catch (AssertionFailedError afe) {
104: throw new TestFailureException(afe);
105: }
106: }
107:
108: public void lookupStatefulBean() throws TestFailureException {
109: try {
110: try {
111: InitialContext ctx = new InitialContext();
112: Assert.assertNotNull("The InitialContext is null", ctx);
113:
114: BasicStatefulHome home = (BasicStatefulHome) javax.rmi.PortableRemoteObject
115: .narrow(
116: ctx
117: .lookup("java:comp/env/stateful/beanReferences/stateful"),
118: BasicStatefulHome.class);
119: Assert.assertNotNull("The EJBHome looked up is null",
120: home);
121:
122: BasicStatefulObject object = home
123: .createObject("Enc Bean");
124: Assert.assertNotNull("The EJBObject is null", object);
125: } catch (Exception e) {
126: Assert.fail("Received Exception " + e.getClass()
127: + " : " + e.getMessage());
128: }
129: } catch (AssertionFailedError afe) {
130: throw new TestFailureException(afe);
131: }
132: }
133:
134: public void lookupStatelessBean() throws TestFailureException {
135: try {
136: try {
137: InitialContext ctx = new InitialContext();
138: Assert.assertNotNull("The InitialContext is null", ctx);
139:
140: BasicStatelessHome home = (BasicStatelessHome) javax.rmi.PortableRemoteObject
141: .narrow(
142: ctx
143: .lookup("java:comp/env/stateful/beanReferences/stateless"),
144: BasicStatelessHome.class);
145: Assert.assertNotNull("The EJBHome looked up is null",
146: home);
147:
148: BasicStatelessObject object = home.createObject();
149: Assert.assertNotNull("The EJBObject is null", object);
150: } catch (Exception e) {
151: Assert.fail("Received Exception " + e.getClass()
152: + " : " + e.getMessage());
153: }
154: } catch (AssertionFailedError afe) {
155: throw new TestFailureException(afe);
156: }
157: }
158:
159: public void lookupStatelessBusinessLocal()
160: throws TestFailureException {
161: try {
162: try {
163: InitialContext ctx = new InitialContext();
164: Assert.assertNotNull("The InitialContext is null", ctx);
165:
166: Object o = ctx
167: .lookup("java:comp/env/stateful/beanReferences/stateless-business-local");
168: BasicStatelessBusinessLocal object = (BasicStatelessBusinessLocal) o;
169: Assert.assertNotNull("The EJB BusinessLocal is null",
170: object);
171: } catch (Exception e) {
172: e.printStackTrace();
173: Assert.fail("Received Exception " + e.getClass()
174: + " : " + e.getMessage());
175: }
176: } catch (AssertionFailedError afe) {
177: throw new TestFailureException(afe);
178: }
179: }
180:
181: public void lookupStatelessBusinessRemote()
182: throws TestFailureException {
183: try {
184: try {
185: InitialContext ctx = new InitialContext();
186: Assert.assertNotNull("The InitialContext is null", ctx);
187:
188: BasicStatelessBusinessRemote object = (BasicStatelessBusinessRemote) javax.rmi.PortableRemoteObject
189: .narrow(
190: ctx
191: .lookup("java:comp/env/stateful/beanReferences/stateless-business-remote"),
192: BasicStatelessBusinessRemote.class);
193: Assert.assertNotNull("The EJB BusinessRemote is null",
194: object);
195: } catch (Exception e) {
196: Assert.fail("Received Exception " + e.getClass()
197: + " : " + e.getMessage());
198: }
199: } catch (AssertionFailedError afe) {
200: throw new TestFailureException(afe);
201: }
202: }
203:
204: public void lookupStatefulBusinessLocal()
205: throws TestFailureException {
206: try {
207: try {
208: InitialContext ctx = new InitialContext();
209: Assert.assertNotNull("The InitialContext is null", ctx);
210:
211: BasicStatefulBusinessLocal object = (BasicStatefulBusinessLocal) javax.rmi.PortableRemoteObject
212: .narrow(
213: ctx
214: .lookup("java:comp/env/stateful/beanReferences/stateful-business-local"),
215: BasicStatefulBusinessLocal.class);
216: Assert.assertNotNull("The EJB BusinessLocal is null",
217: object);
218: } catch (Exception e) {
219: Assert.fail("Received Exception " + e.getClass()
220: + " : " + e.getMessage());
221: }
222: } catch (AssertionFailedError afe) {
223: throw new TestFailureException(afe);
224: }
225: }
226:
227: public void lookupStatefulBusinessRemote()
228: throws TestFailureException {
229: try {
230: try {
231: InitialContext ctx = new InitialContext();
232: Assert.assertNotNull("The InitialContext is null", ctx);
233:
234: BasicStatefulBusinessRemote object = (BasicStatefulBusinessRemote) javax.rmi.PortableRemoteObject
235: .narrow(
236: ctx
237: .lookup("java:comp/env/stateful/beanReferences/stateful-business-remote"),
238: BasicStatefulBusinessRemote.class);
239: Assert.assertNotNull("The EJB BusinessRemote is null",
240: object);
241: } catch (Exception e) {
242: Assert.fail("Received Exception " + e.getClass()
243: + " : " + e.getMessage());
244: }
245: } catch (AssertionFailedError afe) {
246: throw new TestFailureException(afe);
247: }
248: }
249:
250: public void lookupStringEntry() throws TestFailureException {
251: try {
252: try {
253: InitialContext ctx = new InitialContext();
254: Assert.assertNotNull("The InitialContext is null", ctx);
255:
256: String expected = new String("1");
257: String actual = (String) ctx
258: .lookup("java:comp/env/stateful/references/String");
259:
260: Assert.assertNotNull("The String looked up is null",
261: actual);
262: Assert.assertEquals(expected, actual);
263:
264: } catch (Exception e) {
265: Assert.fail("Received Exception " + e.getClass()
266: + " : " + e.getMessage());
267: }
268: } catch (AssertionFailedError afe) {
269: throw new TestFailureException(afe);
270: }
271: }
272:
273: public void lookupDoubleEntry() throws TestFailureException {
274: try {
275: try {
276: InitialContext ctx = new InitialContext();
277: Assert.assertNotNull("The InitialContext is null", ctx);
278:
279: Double expected = new Double(1.0D);
280: Double actual = (Double) ctx
281: .lookup("java:comp/env/stateful/references/Double");
282:
283: Assert.assertNotNull("The Double looked up is null",
284: actual);
285: Assert.assertEquals(expected, actual);
286:
287: } catch (Exception e) {
288: Assert.fail("Received Exception " + e.getClass()
289: + " : " + e.getMessage());
290: }
291: } catch (AssertionFailedError afe) {
292: throw new TestFailureException(afe);
293: }
294: }
295:
296: public void lookupLongEntry() throws TestFailureException {
297: try {
298: try {
299: InitialContext ctx = new InitialContext();
300: Assert.assertNotNull("The InitialContext is null", ctx);
301:
302: Long expected = new Long(1L);
303: Long actual = (Long) ctx
304: .lookup("java:comp/env/stateful/references/Long");
305:
306: Assert.assertNotNull("The Long looked up is null",
307: actual);
308: Assert.assertEquals(expected, actual);
309:
310: } catch (Exception e) {
311: Assert.fail("Received Exception " + e.getClass()
312: + " : " + e.getMessage());
313: }
314: } catch (AssertionFailedError afe) {
315: throw new TestFailureException(afe);
316: }
317: }
318:
319: public void lookupFloatEntry() throws TestFailureException {
320: try {
321: try {
322: InitialContext ctx = new InitialContext();
323: Assert.assertNotNull("The InitialContext is null", ctx);
324:
325: Float expected = new Float(1.0F);
326: Float actual = (Float) ctx
327: .lookup("java:comp/env/stateful/references/Float");
328:
329: Assert.assertNotNull("The Float looked up is null",
330: actual);
331: Assert.assertEquals(expected, actual);
332:
333: } catch (Exception e) {
334: Assert.fail("Received Exception " + e.getClass()
335: + " : " + e.getMessage());
336: }
337: } catch (AssertionFailedError afe) {
338: throw new TestFailureException(afe);
339: }
340: }
341:
342: public void lookupIntegerEntry() throws TestFailureException {
343: try {
344: try {
345: InitialContext ctx = new InitialContext();
346: Assert.assertNotNull("The InitialContext is null", ctx);
347:
348: Integer expected = new Integer(1);
349: Integer actual = (Integer) ctx
350: .lookup("java:comp/env/stateful/references/Integer");
351:
352: Assert.assertNotNull("The Integer looked up is null",
353: actual);
354: Assert.assertEquals(expected, actual);
355:
356: } catch (Exception e) {
357: Assert.fail("Received Exception " + e.getClass()
358: + " : " + e.getMessage());
359: }
360: } catch (AssertionFailedError afe) {
361: throw new TestFailureException(afe);
362: }
363: }
364:
365: public void lookupShortEntry() throws TestFailureException {
366: try {
367: try {
368: InitialContext ctx = new InitialContext();
369: Assert.assertNotNull("The InitialContext is null", ctx);
370:
371: Short expected = new Short((short) 1);
372: Short actual = (Short) ctx
373: .lookup("java:comp/env/stateful/references/Short");
374:
375: Assert.assertNotNull("The Short looked up is null",
376: actual);
377: Assert.assertEquals(expected, actual);
378:
379: } catch (Exception e) {
380: Assert.fail("Received Exception " + e.getClass()
381: + " : " + e.getMessage());
382: }
383: } catch (AssertionFailedError afe) {
384: throw new TestFailureException(afe);
385: }
386: }
387:
388: public void lookupBooleanEntry() throws TestFailureException {
389: try {
390: try {
391: InitialContext ctx = new InitialContext();
392: Assert.assertNotNull("The InitialContext is null", ctx);
393:
394: Boolean expected = new Boolean(true);
395: Boolean actual = (Boolean) ctx
396: .lookup("java:comp/env/stateful/references/Boolean");
397:
398: Assert.assertNotNull("The Boolean looked up is null",
399: actual);
400: Assert.assertEquals(expected, actual);
401:
402: } catch (Exception e) {
403: Assert.fail("Received Exception " + e.getClass()
404: + " : " + e.getMessage());
405: }
406: } catch (AssertionFailedError afe) {
407: throw new TestFailureException(afe);
408: }
409: }
410:
411: public void lookupByteEntry() throws TestFailureException {
412: try {
413: try {
414: InitialContext ctx = new InitialContext();
415: Assert.assertNotNull("The InitialContext is null", ctx);
416:
417: Byte expected = new Byte((byte) 1);
418: Byte actual = (Byte) ctx
419: .lookup("java:comp/env/stateful/references/Byte");
420:
421: Assert.assertNotNull("The Byte looked up is null",
422: actual);
423: Assert.assertEquals(expected, actual);
424:
425: } catch (Exception e) {
426: Assert.fail("Received Exception " + e.getClass()
427: + " : " + e.getMessage());
428: }
429: } catch (AssertionFailedError afe) {
430: throw new TestFailureException(afe);
431: }
432: }
433:
434: public void lookupCharacterEntry() throws TestFailureException {
435: try {
436: try {
437: InitialContext ctx = new InitialContext();
438: Assert.assertNotNull("The InitialContext is null", ctx);
439:
440: Character expected = new Character('D');
441: Character actual = (Character) ctx
442: .lookup("java:comp/env/stateful/references/Character");
443:
444: Assert.assertNotNull("The Character looked up is null",
445: actual);
446: Assert.assertEquals(expected, actual);
447:
448: } catch (Exception e) {
449: Assert.fail("Received Exception " + e.getClass()
450: + " : " + e.getMessage());
451: }
452: } catch (AssertionFailedError afe) {
453: throw new TestFailureException(afe);
454: }
455: }
456:
457: public void lookupResource() throws TestFailureException {
458: try {
459: try {
460: InitialContext ctx = new InitialContext();
461: Assert.assertNotNull("The InitialContext is null", ctx);
462: Object obj = ctx.lookup("java:comp/env/datasource");
463: Assert.assertNotNull("The DataSource is null", obj);
464: Assert.assertTrue("Not an instance of DataSource",
465: obj instanceof DataSource);
466: } catch (Exception e) {
467: Assert.fail("Received Exception " + e.getClass()
468: + " : " + e.getMessage());
469: }
470: } catch (AssertionFailedError afe) {
471: throw new TestFailureException(afe);
472: }
473: }
474:
475: public void lookupJMSConnectionFactory()
476: throws TestFailureException {
477: try {
478: try {
479: InitialContext ctx = new InitialContext();
480: Assert.assertNotNull("The InitialContext is null", ctx);
481: Object obj = ctx.lookup("java:comp/env/jms");
482: Assert.assertNotNull(
483: "The JMS ConnectionFactory is null", obj);
484: Assert.assertTrue(
485: "Not an instance of ConnectionFactory",
486: obj instanceof ConnectionFactory);
487: ConnectionFactory connectionFactory = (ConnectionFactory) obj;
488: testJmsConnection(connectionFactory.createConnection());
489:
490: obj = ctx.lookup("java:comp/env/TopicCF");
491: Assert.assertNotNull(
492: "The JMS TopicConnectionFactory is null", obj);
493: Assert.assertTrue(
494: "Not an instance of TopicConnectionFactory",
495: obj instanceof TopicConnectionFactory);
496: TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) obj;
497: testJmsConnection(topicConnectionFactory
498: .createConnection());
499:
500: obj = ctx.lookup("java:comp/env/QueueCF");
501: Assert.assertNotNull(
502: "The JMS QueueConnectionFactory is null", obj);
503: Assert.assertTrue(
504: "Not an instance of QueueConnectionFactory",
505: obj instanceof QueueConnectionFactory);
506: QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;
507: testJmsConnection(queueConnectionFactory
508: .createConnection());
509: } catch (Exception e) {
510: e.printStackTrace();
511: Assert.fail("Received Exception " + e.getClass()
512: + " : " + e.getMessage());
513: }
514: } catch (AssertionFailedError afe) {
515: throw new TestFailureException(afe);
516: }
517: }
518:
519: private void testJmsConnection(Connection connection)
520: throws JMSException {
521: Session session = connection.createSession(false,
522: Session.DUPS_OK_ACKNOWLEDGE);
523: Topic topic = session.createTopic("test");
524: MessageProducer producer = session.createProducer(topic);
525: producer.send(session.createMessage());
526: producer.close();
527: session.close();
528: connection.close();
529: }
530:
531: public void lookupPersistenceUnit() throws TestFailureException {
532: try {
533: try {
534: InitialContext ctx = new InitialContext();
535: Assert.assertNotNull("The InitialContext is null", ctx);
536: EntityManagerFactory emf = (EntityManagerFactory) ctx
537: .lookup("java:comp/env/persistence/TestUnit");
538: Assert.assertNotNull(
539: "The EntityManagerFactory is null", emf);
540:
541: } catch (Exception e) {
542: Assert.fail("Received Exception " + e.getClass()
543: + " : " + e.getMessage());
544: }
545: } catch (AssertionFailedError afe) {
546: throw new TestFailureException(afe);
547: }
548: }
549:
550: public void lookupPersistenceContext() throws TestFailureException {
551: try {
552: try {
553: InitialContext ctx = new InitialContext();
554: Assert.assertNotNull("The InitialContext is null", ctx);
555: EntityManager em = (EntityManager) ctx
556: .lookup("java:comp/env/persistence/TestContext");
557: Assert.assertNotNull("The EntityManager is null", em);
558:
559: // call a do nothing method to assure entity manager actually exists
560: em.getFlushMode();
561: } catch (Exception e) {
562: Assert.fail("Received Exception " + e.getClass()
563: + " : " + e.getMessage());
564: }
565: } catch (AssertionFailedError afe) {
566: throw new TestFailureException(afe);
567: }
568: }
569:
570: public void lookupSessionContext() throws TestFailureException {
571: try {
572: try {
573: InitialContext ctx = new InitialContext();
574: Assert.assertNotNull("The InitialContext is null", ctx);
575:
576: // lookup in enc
577: SessionContext sctx = (SessionContext) ctx
578: .lookup("java:comp/env/sessioncontext");
579: Assert
580: .assertNotNull(
581: "The SessionContext got from java:comp/env/sessioncontext is null",
582: sctx);
583:
584: // lookup using global name
585: EJBContext ejbCtx = (EJBContext) ctx
586: .lookup("java:comp/EJBContext");
587: Assert
588: .assertNotNull(
589: "The SessionContext got from java:comp/EJBContext is null ",
590: ejbCtx);
591:
592: // verify context was set via legacy set method
593: Assert
594: .assertNotNull(
595: "The SessionContext is null from setter method",
596: ejbContext);
597: } catch (Exception e) {
598: Assert.fail("Received Exception " + e.getClass()
599: + " : " + e.getMessage());
600: }
601: } catch (AssertionFailedError afe) {
602: throw new TestFailureException(afe);
603: }
604:
605: }
606:
607: public String remove(String arg) {
608: return arg;
609: }
610:
611: //
612: // Remote interface methods
613: //=============================
614:
615: //=================================
616: // SessionBean interface methods
617: //
618: /**
619: * Set the associated session context. The container calls this method
620: * after the instance creation.
621: */
622: public void setSessionContext(SessionContext ctx)
623: throws EJBException, RemoteException {
624: ejbContext = ctx;
625: }
626:
627: /**
628: * A container invokes this method before it ends the life of the session
629: * object. This happens as a result of a client's invoking a remove
630: * operation, or when a container decides to terminate the session object
631: * after a timeout.
632: */
633: public void ejbRemove() throws EJBException, RemoteException {
634: }
635:
636: /**
637: * The activate method is called when the instance is activated
638: * from its "passive" state. The instance should acquire any resource
639: * that it has released earlier in the ejbPassivate() method.
640: */
641: public void ejbActivate() throws EJBException, RemoteException {
642: }
643:
644: /**
645: * The passivate method is called before the instance enters
646: * the "passive" state. The instance should release any resources that
647: * it can re-acquire later in the ejbActivate() method.
648: */
649: public void ejbPassivate() throws EJBException, RemoteException {
650: }
651:
652: //
653: // SessionBean interface methods
654: //==================================
655:
656: //============================================
657: // SessionSynchronization interface methods
658: //
659: /**
660: * The afterBegin method notifies a session Bean instance that a new
661: * transaction has started, and that the subsequent business methods on the
662: * instance will be invoked in the context of the transaction.
663: */
664: public void afterBegin() throws EJBException, RemoteException {
665: }
666:
667: /**
668: * The beforeCompletion method notifies a session Bean instance that
669: * a transaction is about to be committed. The instance can use this
670: * method, for example, to write any cached data to a database.
671: */
672: public void beforeCompletion() throws EJBException, RemoteException {
673: }
674:
675: /**
676: * The afterCompletion method notifies a session Bean instance that a
677: * transaction commit protocol has completed, and tells the instance
678: * whether the transaction has been committed or rolled back.
679: */
680: public void afterCompletion(boolean committed) throws EJBException,
681: RemoteException {
682: }
683: //
684: // SessionSynchronization interface methods
685: //============================================
686:
687: }
|