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