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