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