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.sql.DataSource;
036: import javax.jms.ConnectionFactory;
037: import javax.jms.TopicConnectionFactory;
038: import javax.jms.QueueConnectionFactory;
039: import javax.jms.Connection;
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: import javax.naming.InitialContext;
049:
050: import junit.framework.Assert;
051: import junit.framework.AssertionFailedError;
052:
053: /**
054: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
055: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
056: */
057: public class ContextLookupMdbBean implements EncMdbObject,
058: MessageDrivenBean, MessageListener {
059: private MessageDrivenContext mdbContext = null;
060: private MdbInvoker mdbInvoker;
061:
062: public void setMessageDrivenContext(MessageDrivenContext ctx)
063: throws EJBException {
064: this .mdbContext = ctx;
065: try {
066: ConnectionFactory connectionFactory = (ConnectionFactory) new InitialContext()
067: .lookup("java:comp/env/jms");
068: mdbInvoker = new MdbInvoker(connectionFactory, this );
069: } catch (Exception e) {
070: throw new EJBException(e);
071: }
072: }
073:
074: public void onMessage(Message message) {
075: try {
076: // System.out.println("\n" +
077: // "***************************************\n" +
078: // "Got message: " + message + "\n" +
079: // "***************************************\n\n");
080: try {
081: message.acknowledge();
082: } catch (JMSException e) {
083: e.printStackTrace();
084: }
085: mdbInvoker.onMessage(message);
086: } catch (Throwable e) {
087: e.printStackTrace();
088: }
089: }
090:
091: public void lookupEntityBean() throws TestFailureException {
092: try {
093: try {
094: BasicBmpHome home = (BasicBmpHome) mdbContext
095: .lookup("stateless/beanReferences/bmp_entity");
096: Assert.assertNotNull("The EJBHome looked up is null",
097: home);
098:
099: BasicBmpObject object = home.createObject("Enc Bean");
100: Assert.assertNotNull("The EJBObject is null", object);
101: } catch (Exception e) {
102: Assert.fail("Received Exception " + e.getClass()
103: + " : " + e.getMessage());
104: }
105: } catch (AssertionFailedError afe) {
106: throw new TestFailureException(afe);
107: }
108: }
109:
110: public void lookupStatefulBean() throws TestFailureException {
111: try {
112: try {
113: BasicStatefulHome home = (BasicStatefulHome) mdbContext
114: .lookup("stateless/beanReferences/stateful");
115: Assert.assertNotNull("The EJBHome looked up is null",
116: home);
117:
118: BasicStatefulObject object = home
119: .createObject("Enc Bean");
120: Assert.assertNotNull("The EJBObject is null", object);
121: } catch (Exception e) {
122: Assert.fail("Received Exception " + e.getClass()
123: + " : " + e.getMessage());
124: }
125: } catch (AssertionFailedError afe) {
126: throw new TestFailureException(afe);
127: }
128: }
129:
130: public void lookupStatelessBean() throws TestFailureException {
131: try {
132: try {
133: BasicStatelessHome home = (BasicStatelessHome) mdbContext
134: .lookup("stateless/beanReferences/stateless");
135: Assert.assertNotNull("The EJBHome looked up is null",
136: home);
137:
138: BasicStatelessObject object = home.createObject();
139: Assert.assertNotNull("The EJBObject is null", object);
140: } catch (Exception e) {
141: Assert.fail("Received Exception " + e.getClass()
142: + " : " + e.getMessage());
143: }
144: } catch (AssertionFailedError afe) {
145: throw new TestFailureException(afe);
146: }
147: }
148:
149: public void lookupStatelessBusinessLocal()
150: throws TestFailureException {
151: try {
152: try {
153: BasicStatelessBusinessLocal object = (BasicStatelessBusinessLocal) mdbContext
154: .lookup("stateless/beanReferences/stateless-business-local");
155: Assert.assertNotNull("The EJB BusinessLocal is null",
156: 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 lookupStatelessBusinessRemote()
167: throws TestFailureException {
168: try {
169: try {
170: BasicStatelessBusinessRemote object = (BasicStatelessBusinessRemote) mdbContext
171: .lookup("stateless/beanReferences/stateless-business-remote");
172: Assert.assertNotNull("The EJB BusinessRemote is null",
173: object);
174: } catch (Exception e) {
175: Assert.fail("Received Exception " + e.getClass()
176: + " : " + e.getMessage());
177: }
178: } catch (AssertionFailedError afe) {
179: throw new TestFailureException(afe);
180: }
181: }
182:
183: public void lookupStatefulBusinessLocal()
184: throws TestFailureException {
185: try {
186: try {
187: BasicStatefulBusinessLocal object = (BasicStatefulBusinessLocal) mdbContext
188: .lookup("stateless/beanReferences/stateful-business-local");
189: Assert.assertNotNull("The EJB BusinessLocal is null",
190: object);
191: } catch (Exception e) {
192: Assert.fail("Received Exception " + e.getClass()
193: + " : " + e.getMessage());
194: }
195: } catch (AssertionFailedError afe) {
196: throw new TestFailureException(afe);
197: }
198: }
199:
200: public void lookupStatefulBusinessRemote()
201: throws TestFailureException {
202: try {
203: try {
204: BasicStatefulBusinessRemote object = (BasicStatefulBusinessRemote) mdbContext
205: .lookup("stateless/beanReferences/stateful-business-remote");
206: Assert.assertNotNull("The EJB BusinessRemote is null",
207: object);
208: } catch (Exception e) {
209: Assert.fail("Received Exception " + e.getClass()
210: + " : " + e.getMessage());
211: }
212: } catch (AssertionFailedError afe) {
213: throw new TestFailureException(afe);
214: }
215: }
216:
217: public void lookupStringEntry() throws TestFailureException {
218: try {
219: try {
220: String expected = new String("1");
221: String actual = (String) mdbContext
222: .lookup("stateless/references/String");
223:
224: Assert.assertNotNull("The String looked up is null",
225: actual);
226: Assert.assertEquals(expected, actual);
227:
228: } catch (Exception e) {
229: Assert.fail("Received Exception " + e.getClass()
230: + " : " + e.getMessage());
231: }
232: } catch (AssertionFailedError afe) {
233: throw new TestFailureException(afe);
234: }
235: }
236:
237: public void lookupDoubleEntry() throws TestFailureException {
238: try {
239: try {
240: Double expected = new Double(1.0D);
241: Double actual = (Double) mdbContext
242: .lookup("stateless/references/Double");
243:
244: Assert.assertNotNull("The Double looked up is null",
245: actual);
246: Assert.assertEquals(expected, actual);
247:
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 lookupLongEntry() throws TestFailureException {
258: try {
259: try {
260: Long expected = new Long(1L);
261: Long actual = (Long) mdbContext
262: .lookup("stateless/references/Long");
263:
264: Assert.assertNotNull("The Long looked up is null",
265: actual);
266: Assert.assertEquals(expected, actual);
267:
268: } catch (Exception e) {
269: Assert.fail("Received Exception " + e.getClass()
270: + " : " + e.getMessage());
271: }
272: } catch (AssertionFailedError afe) {
273: throw new TestFailureException(afe);
274: }
275: }
276:
277: public void lookupFloatEntry() throws TestFailureException {
278: try {
279: try {
280: Float expected = new Float(1.0F);
281: Float actual = (Float) mdbContext
282: .lookup("stateless/references/Float");
283:
284: Assert.assertNotNull("The Float looked up is null",
285: actual);
286: Assert.assertEquals(expected, actual);
287:
288: } catch (Exception e) {
289: Assert.fail("Received Exception " + e.getClass()
290: + " : " + e.getMessage());
291: }
292: } catch (AssertionFailedError afe) {
293: throw new TestFailureException(afe);
294: }
295: }
296:
297: public void lookupIntegerEntry() throws TestFailureException {
298: try {
299: try {
300: Integer expected = new Integer(1);
301: Integer actual = (Integer) mdbContext
302: .lookup("stateless/references/Integer");
303:
304: Assert.assertNotNull("The Integer looked up is null",
305: actual);
306: Assert.assertEquals(expected, actual);
307:
308: } catch (Exception e) {
309: Assert.fail("Received Exception " + e.getClass()
310: + " : " + e.getMessage());
311: }
312: } catch (AssertionFailedError afe) {
313: throw new TestFailureException(afe);
314: }
315: }
316:
317: public void lookupShortEntry() throws TestFailureException {
318: try {
319: try {
320: Short expected = new Short((short) 1);
321: Short actual = (Short) mdbContext
322: .lookup("stateless/references/Short");
323:
324: Assert.assertNotNull("The Short looked up is null",
325: actual);
326: Assert.assertEquals(expected, actual);
327:
328: } catch (Exception e) {
329: Assert.fail("Received Exception " + e.getClass()
330: + " : " + e.getMessage());
331: }
332: } catch (AssertionFailedError afe) {
333: throw new TestFailureException(afe);
334: }
335: }
336:
337: public void lookupBooleanEntry() throws TestFailureException {
338: try {
339: try {
340: Boolean expected = new Boolean(true);
341: Boolean actual = (Boolean) mdbContext
342: .lookup("stateless/references/Boolean");
343:
344: Assert.assertNotNull("The Boolean looked up is null",
345: actual);
346: Assert.assertEquals(expected, actual);
347:
348: } catch (Exception e) {
349: Assert.fail("Received Exception " + e.getClass()
350: + " : " + e.getMessage());
351: }
352: } catch (AssertionFailedError afe) {
353: throw new TestFailureException(afe);
354: }
355: }
356:
357: public void lookupByteEntry() throws TestFailureException {
358: try {
359: try {
360: Byte expected = new Byte((byte) 1);
361: Byte actual = (Byte) mdbContext
362: .lookup("stateless/references/Byte");
363:
364: Assert.assertNotNull("The Byte looked up is null",
365: actual);
366: Assert.assertEquals(expected, actual);
367:
368: } catch (Exception e) {
369: Assert.fail("Received Exception " + e.getClass()
370: + " : " + e.getMessage());
371: }
372: } catch (AssertionFailedError afe) {
373: throw new TestFailureException(afe);
374: }
375: }
376:
377: public void lookupCharacterEntry() throws TestFailureException {
378: try {
379: try {
380: Character expected = new Character('D');
381: Character actual = (Character) mdbContext
382: .lookup("stateless/references/Character");
383:
384: Assert.assertNotNull("The Character looked up is null",
385: actual);
386: Assert.assertEquals(expected, actual);
387:
388: } catch (Exception e) {
389: Assert.fail("Received Exception " + e.getClass()
390: + " : " + e.getMessage());
391: }
392: } catch (AssertionFailedError afe) {
393: throw new TestFailureException(afe);
394: }
395: }
396:
397: public void lookupResource() throws TestFailureException {
398: try {
399: try {
400: Object obj = mdbContext.lookup("datasource");
401: Assert.assertNotNull("The DataSource is null", obj);
402: Assert.assertTrue("Not an instance of DataSource",
403: obj instanceof DataSource);
404: } catch (Exception e) {
405: Assert.fail("Received Exception " + e.getClass()
406: + " : " + e.getMessage());
407: }
408: } catch (AssertionFailedError afe) {
409: throw new TestFailureException(afe);
410: }
411: }
412:
413: public void lookupJMSConnectionFactory()
414: throws TestFailureException {
415: try {
416: try {
417: Object obj = mdbContext.lookup("jms");
418: Assert.assertNotNull(
419: "The JMS ConnectionFactory is null", obj);
420: Assert.assertTrue(
421: "Not an instance of ConnectionFactory",
422: obj instanceof ConnectionFactory);
423: ConnectionFactory connectionFactory = (ConnectionFactory) obj;
424: testJmsConnection(connectionFactory.createConnection());
425:
426: obj = mdbContext.lookup("TopicCF");
427: Assert.assertNotNull(
428: "The JMS TopicConnectionFactory is null", obj);
429: Assert.assertTrue(
430: "Not an instance of TopicConnectionFactory",
431: obj instanceof TopicConnectionFactory);
432: TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) obj;
433: testJmsConnection(topicConnectionFactory
434: .createConnection());
435:
436: obj = mdbContext.lookup("QueueCF");
437: Assert.assertNotNull(
438: "The JMS QueueConnectionFactory is null", obj);
439: Assert.assertTrue(
440: "Not an instance of QueueConnectionFactory",
441: obj instanceof QueueConnectionFactory);
442: QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;
443: testJmsConnection(queueConnectionFactory
444: .createConnection());
445: } catch (Exception e) {
446: e.printStackTrace();
447: Assert.fail("Received Exception " + e.getClass()
448: + " : " + e.getMessage());
449: }
450: } catch (AssertionFailedError afe) {
451: throw new TestFailureException(afe);
452: }
453: }
454:
455: private void testJmsConnection(Connection connection)
456: throws JMSException {
457: Session session = connection.createSession(false,
458: Session.DUPS_OK_ACKNOWLEDGE);
459: Topic topic = session.createTopic("test");
460: MessageProducer producer = session.createProducer(topic);
461: producer.send(session.createMessage());
462: producer.close();
463: session.close();
464: connection.close();
465: }
466:
467: public void lookupPersistenceUnit() throws TestFailureException {
468: try {
469: try {
470: EntityManagerFactory emf = (EntityManagerFactory) mdbContext
471: .lookup("persistence/TestUnit");
472: Assert.assertNotNull(
473: "The EntityManagerFactory is null", emf);
474:
475: } catch (Exception e) {
476: Assert.fail("Received Exception " + e.getClass()
477: + " : " + e.getMessage());
478: }
479: } catch (AssertionFailedError afe) {
480: throw new TestFailureException(afe);
481: }
482: }
483:
484: public void lookupPersistenceContext() throws TestFailureException {
485: try {
486: try {
487: EntityManager em = (EntityManager) mdbContext
488: .lookup("persistence/TestContext");
489: Assert.assertNotNull("The EntityManager is null", em);
490:
491: // call a do nothing method to assure entity manager actually exists
492: em.getFlushMode();
493: } catch (Exception e) {
494: Assert.fail("Received Exception " + e.getClass()
495: + " : " + e.getMessage());
496: }
497: } catch (AssertionFailedError afe) {
498: throw new TestFailureException(afe);
499: }
500: }
501:
502: public void lookupMessageDrivenContext()
503: throws TestFailureException {
504: try {
505: try {
506: InitialContext ctx = new InitialContext();
507: Assert.assertNotNull("The InitialContext is null", ctx);
508:
509: // lookup in enc
510: MessageDrivenContext sctx = (MessageDrivenContext) ctx
511: .lookup("java:comp/env/mdbcontext");
512: Assert
513: .assertNotNull(
514: "The MessageDrivenContext got from java:comp/env/mdbcontext is null",
515: sctx);
516:
517: // lookup using global name
518: EJBContext ejbCtx = (EJBContext) ctx
519: .lookup("java:comp/EJBContext");
520: Assert
521: .assertNotNull(
522: "The MessageDrivenContext got from java:comp/EJBContext is null ",
523: ejbCtx);
524:
525: // verify context was set via legacy set method
526: Assert
527: .assertNotNull(
528: "The MessageDrivenContext is null from setter method",
529: mdbContext);
530: } catch (Exception e) {
531: Assert.fail("Received Exception " + e.getClass()
532: + " : " + e.getMessage());
533: }
534: } catch (AssertionFailedError afe) {
535: throw new TestFailureException(afe);
536: }
537:
538: }
539:
540: public void ejbCreate() throws javax.ejb.CreateException {
541: }
542:
543: public void ejbRemove() throws EJBException {
544: }
545: }
|