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 javax.ejb.CreateException;
020: import javax.ejb.EJBException;
021: import javax.ejb.MessageDrivenBean;
022: import javax.ejb.MessageDrivenContext;
023: import javax.jms.ConnectionFactory;
024: import javax.jms.JMSException;
025: import javax.jms.Message;
026: import javax.jms.MessageListener;
027: import javax.jms.MessageProducer;
028: import javax.jms.QueueConnectionFactory;
029: import javax.jms.Session;
030: import javax.jms.Topic;
031: import javax.jms.TopicConnectionFactory;
032: import javax.naming.InitialContext;
033: import javax.persistence.EntityManager;
034: import javax.persistence.EntityManagerFactory;
035: import javax.sql.DataSource;
036:
037: import junit.framework.Assert;
038: import junit.framework.AssertionFailedError;
039: import org.apache.openejb.test.TestFailureException;
040: import org.apache.openejb.test.entity.bmp.BasicBmpHome;
041: import org.apache.openejb.test.stateful.BasicStatefulBusinessLocal;
042: import org.apache.openejb.test.stateful.BasicStatefulBusinessRemote;
043: import org.apache.openejb.test.stateful.BasicStatefulHome;
044: import org.apache.openejb.test.stateless.BasicStatelessBusinessLocal;
045: import org.apache.openejb.test.stateless.BasicStatelessBusinessRemote;
046: import org.apache.openejb.test.stateless.BasicStatelessHome;
047:
048: /**
049: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
050: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
051: */
052: public class SetterInjectionMdbBean implements EncMdbObject,
053: MessageDrivenBean, MessageListener {
054: private MessageDrivenContext ejbContextField;
055: private BasicBmpHome bmpHomeField;
056: private BasicStatefulHome statefulHomeField;
057: private BasicStatelessHome statelessHomeField;
058: private String striingField;
059: private Double dooubleField;
060: private Long loongField;
061: private Float flooatField;
062: private Integer inteegerField;
063: private Short shoortField;
064: private Boolean boooleanField;
065: private Byte byyteField;
066: private Character chaaracterField;
067: private DataSource daataSourceField;
068: private ConnectionFactory coonnectionFactory;
069: private QueueConnectionFactory queueCoonnectionFactory;
070: private TopicConnectionFactory topicCoonnectionFactory;
071: private EntityManagerFactory emfField;
072: private EntityManager emField;
073: private EntityManager eemField;
074: private EntityManager pemField;
075: private BasicStatelessBusinessLocal statelessBusinessLocalField;
076: private BasicStatelessBusinessRemote statelessBusinessRemoteField;
077: private BasicStatefulBusinessLocal statefulBusinessLocalField;
078: private BasicStatefulBusinessRemote statefulBusinessRemoteField;
079:
080: private MessageDrivenContext mdbContext = null;
081: private MdbInvoker mdbInvoker;
082:
083: public void setMessageDrivenContext(MessageDrivenContext ctx)
084: throws EJBException {
085: this .mdbContext = ctx;
086: try {
087: ConnectionFactory connectionFactory = (ConnectionFactory) new InitialContext()
088: .lookup("java:comp/env/jms");
089: mdbInvoker = new MdbInvoker(connectionFactory, this );
090: } catch (Exception e) {
091: throw new EJBException(e);
092: }
093: }
094:
095: public void onMessage(Message message) {
096: try {
097: // System.out.println("\n" +
098: // "***************************************\n" +
099: // "Got message: " + message + "\n" +
100: // "***************************************\n\n");
101: try {
102: message.acknowledge();
103: } catch (JMSException e) {
104: e.printStackTrace();
105: }
106: mdbInvoker.onMessage(message);
107: } catch (Throwable e) {
108: e.printStackTrace();
109: }
110: }
111:
112: public BasicStatefulBusinessLocal getStatefulBusinessLocal() {
113: return statefulBusinessLocalField;
114: }
115:
116: public void setStatefulBusinessLocal(
117: BasicStatefulBusinessLocal statefulBusinessLocal) {
118: this .statefulBusinessLocalField = statefulBusinessLocal;
119: }
120:
121: public BasicStatefulBusinessRemote getStatefulBusinessRemote() {
122: return statefulBusinessRemoteField;
123: }
124:
125: public void setStatefulBusinessRemote(
126: BasicStatefulBusinessRemote statefulBusinessRemote) {
127: this .statefulBusinessRemoteField = statefulBusinessRemote;
128: }
129:
130: public BasicStatelessBusinessLocal getStatelessBusinessLocal() {
131: return statelessBusinessLocalField;
132: }
133:
134: public void setStatelessBusinessLocal(
135: BasicStatelessBusinessLocal statelessBusinessLocal) {
136: this .statelessBusinessLocalField = statelessBusinessLocal;
137: }
138:
139: public BasicStatelessBusinessRemote getStatelessBusinessRemote() {
140: return statelessBusinessRemoteField;
141: }
142:
143: public void setStatelessBusinessRemote(
144: BasicStatelessBusinessRemote statelessBusinessRemote) {
145: this .statelessBusinessRemoteField = statelessBusinessRemote;
146: }
147:
148: public BasicBmpHome getBmpHome() {
149: return bmpHomeField;
150: }
151:
152: public void setBmpHome(BasicBmpHome bmpHome) {
153: this .bmpHomeField = bmpHome;
154: }
155:
156: public Boolean getBooolean() {
157: return boooleanField;
158: }
159:
160: public void setBooolean(Boolean booolean) {
161: this .boooleanField = booolean;
162: }
163:
164: public Byte getByyte() {
165: return byyteField;
166: }
167:
168: public void setByyte(Byte byyte) {
169: this .byyteField = byyte;
170: }
171:
172: public Character getChaaracter() {
173: return chaaracterField;
174: }
175:
176: public void setChaaracter(Character chaaracter) {
177: this .chaaracterField = chaaracter;
178: }
179:
180: public DataSource getDaataSource() {
181: return daataSourceField;
182: }
183:
184: public void setDaataSource(DataSource daataSource) {
185: this .daataSourceField = daataSource;
186: }
187:
188: public ConnectionFactory getCoonnectionFactory() {
189: return coonnectionFactory;
190: }
191:
192: public void setCoonnectionFactory(
193: ConnectionFactory coonnectionFactory) {
194: this .coonnectionFactory = coonnectionFactory;
195: }
196:
197: public QueueConnectionFactory getQueueCoonnectionFactory() {
198: return queueCoonnectionFactory;
199: }
200:
201: public void setQueueCoonnectionFactory(
202: QueueConnectionFactory queueCoonnectionFactory) {
203: this .queueCoonnectionFactory = queueCoonnectionFactory;
204: }
205:
206: public TopicConnectionFactory getTopicCoonnectionFactory() {
207: return topicCoonnectionFactory;
208: }
209:
210: public void setTopicCoonnectionFactory(
211: TopicConnectionFactory topicCoonnectionFactory) {
212: this .topicCoonnectionFactory = topicCoonnectionFactory;
213: }
214:
215: public Double getDoouble() {
216: return dooubleField;
217: }
218:
219: public void setDoouble(Double doouble) {
220: this .dooubleField = doouble;
221: }
222:
223: public EntityManager getEem() {
224: return eemField;
225: }
226:
227: public void setEem(EntityManager eem) {
228: this .eemField = eem;
229: }
230:
231: public MessageDrivenContext getEjbContext() {
232: return ejbContextField;
233: }
234:
235: public void setEjbContext(MessageDrivenContext ejbContext) {
236: this .ejbContextField = ejbContext;
237: }
238:
239: public EntityManager getEm() {
240: return emField;
241: }
242:
243: public void setEm(EntityManager em) {
244: this .emField = em;
245: }
246:
247: public EntityManagerFactory getEmf() {
248: return emfField;
249: }
250:
251: public void setEmf(EntityManagerFactory emf) {
252: this .emfField = emf;
253: }
254:
255: public Float getFlooat() {
256: return flooatField;
257: }
258:
259: public void setFlooat(Float flooat) {
260: this .flooatField = flooat;
261: }
262:
263: public Integer getInteeger() {
264: return inteegerField;
265: }
266:
267: public void setInteeger(Integer inteeger) {
268: this .inteegerField = inteeger;
269: }
270:
271: public Long getLoong() {
272: return loongField;
273: }
274:
275: public void setLoong(Long loong) {
276: this .loongField = loong;
277: }
278:
279: public EntityManager getPem() {
280: return pemField;
281: }
282:
283: public void setPem(EntityManager pem) {
284: this .pemField = pem;
285: }
286:
287: public Short getShoort() {
288: return shoortField;
289: }
290:
291: public void setShoort(Short shoort) {
292: this .shoortField = shoort;
293: }
294:
295: public BasicStatefulHome getStatefulHome() {
296: return statefulHomeField;
297: }
298:
299: public void setStatefulHome(BasicStatefulHome statefulHome) {
300: this .statefulHomeField = statefulHome;
301: }
302:
303: public BasicStatelessHome getStatelessHome() {
304: return statelessHomeField;
305: }
306:
307: public void setStatelessHome(BasicStatelessHome statelessHome) {
308: this .statelessHomeField = statelessHome;
309: }
310:
311: public String getStriing() {
312: return striingField;
313: }
314:
315: public void setStriing(String striing) {
316: this .striingField = striing;
317: }
318:
319: public void ejbCreate() throws CreateException {
320: }
321:
322: public void lookupEntityBean() throws TestFailureException {
323: try {
324: Assert.assertNotNull("The EJBObject is null", bmpHomeField);
325: } catch (AssertionFailedError afe) {
326: throw new TestFailureException(afe);
327: }
328: }
329:
330: public void lookupStatefulBean() throws TestFailureException {
331: try {
332: Assert.assertNotNull("The EJBObject is null",
333: statefulHomeField);
334: } catch (AssertionFailedError afe) {
335: throw new TestFailureException(afe);
336: }
337: }
338:
339: public void lookupStatelessBean() throws TestFailureException {
340: try {
341: Assert.assertNotNull("The EJBObject is null",
342: statelessHomeField);
343: } catch (AssertionFailedError afe) {
344: throw new TestFailureException(afe);
345: }
346: }
347:
348: public void lookupStatelessBusinessLocal()
349: throws TestFailureException {
350: try {
351: Assert.assertNotNull("The EJB BusinessLocal is null",
352: statelessBusinessLocalField);
353: } catch (AssertionFailedError afe) {
354: throw new TestFailureException(afe);
355: }
356: }
357:
358: public void lookupStatelessBusinessRemote()
359: throws TestFailureException {
360: try {
361: Assert.assertNotNull("The EJB BusinessRemote is null",
362: statelessBusinessRemoteField);
363: } catch (AssertionFailedError afe) {
364: throw new TestFailureException(afe);
365: }
366: }
367:
368: public void lookupStatefulBusinessLocal()
369: throws TestFailureException {
370: try {
371: Assert.assertNotNull("The EJB BusinessLocal is null",
372: statefulBusinessLocalField);
373: } catch (AssertionFailedError afe) {
374: throw new TestFailureException(afe);
375: }
376: }
377:
378: public void lookupStatefulBusinessRemote()
379: throws TestFailureException {
380: try {
381: Assert.assertNotNull("The EJB BusinessRemote is null",
382: statefulBusinessRemoteField);
383: } catch (AssertionFailedError afe) {
384: throw new TestFailureException(afe);
385: }
386: }
387:
388: public void lookupStringEntry() throws TestFailureException {
389: try {
390: String expected = new String("1");
391: Assert.assertNotNull("The String looked up is null",
392: striingField);
393: Assert.assertEquals(expected, striingField);
394: } catch (AssertionFailedError afe) {
395: throw new TestFailureException(afe);
396: }
397: }
398:
399: public void lookupDoubleEntry() throws TestFailureException {
400: try {
401: Double expected = new Double(1.0D);
402:
403: Assert.assertNotNull("The Double looked up is null",
404: dooubleField);
405: Assert.assertEquals(expected, dooubleField);
406:
407: } catch (AssertionFailedError afe) {
408: throw new TestFailureException(afe);
409: }
410: }
411:
412: public void lookupLongEntry() throws TestFailureException {
413: try {
414: Long expected = new Long(1L);
415:
416: Assert.assertNotNull("The Long looked up is null",
417: loongField);
418: Assert.assertEquals(expected, loongField);
419: } catch (AssertionFailedError afe) {
420: throw new TestFailureException(afe);
421: }
422: }
423:
424: public void lookupFloatEntry() throws TestFailureException {
425: try {
426: Float expected = new Float(1.0F);
427:
428: Assert.assertNotNull("The Float looked up is null",
429: flooatField);
430: Assert.assertEquals(expected, flooatField);
431: } catch (AssertionFailedError afe) {
432: throw new TestFailureException(afe);
433: }
434: }
435:
436: public void lookupIntegerEntry() throws TestFailureException {
437: try {
438: Integer expected = new Integer(1);
439:
440: Assert.assertNotNull("The Integer looked up is null",
441: inteegerField);
442: Assert.assertEquals(expected, inteegerField);
443:
444: } catch (AssertionFailedError afe) {
445: throw new TestFailureException(afe);
446: }
447: }
448:
449: public void lookupShortEntry() throws TestFailureException {
450: try {
451: Short expected = new Short((short) 1);
452:
453: Assert.assertNotNull("The Short looked up is null",
454: shoortField);
455: Assert.assertEquals(expected, shoortField);
456: } catch (AssertionFailedError afe) {
457: throw new TestFailureException(afe);
458: }
459: }
460:
461: public void lookupBooleanEntry() throws TestFailureException {
462: try {
463: Boolean expected = new Boolean(true);
464:
465: Assert.assertNotNull("The Boolean looked up is null",
466: boooleanField);
467: Assert.assertEquals(expected, boooleanField);
468: } catch (AssertionFailedError afe) {
469: throw new TestFailureException(afe);
470: }
471: }
472:
473: public void lookupByteEntry() throws TestFailureException {
474: try {
475: Byte expected = new Byte((byte) 1);
476:
477: Assert.assertNotNull("The Byte looked up is null",
478: byyteField);
479: Assert.assertEquals(expected, byyteField);
480: } catch (AssertionFailedError afe) {
481: throw new TestFailureException(afe);
482: }
483: }
484:
485: public void lookupCharacterEntry() throws TestFailureException {
486: try {
487: Character expected = new Character('D');
488:
489: Assert.assertNotNull("The Character looked up is null",
490: chaaracterField);
491: Assert.assertEquals(expected, chaaracterField);
492: } catch (AssertionFailedError afe) {
493: throw new TestFailureException(afe);
494: }
495: }
496:
497: public void lookupResource() throws TestFailureException {
498: try {
499: Assert.assertNotNull("The DataSource is null",
500: daataSourceField);
501: } catch (AssertionFailedError afe) {
502: throw new TestFailureException(afe);
503: }
504: }
505:
506: public void lookupJMSConnectionFactory()
507: throws TestFailureException {
508: try {
509: try {
510: testJmsConnection(coonnectionFactory.createConnection());
511: testJmsConnection(queueCoonnectionFactory
512: .createConnection());
513: testJmsConnection(topicCoonnectionFactory
514: .createConnection());
515: } catch (Exception e) {
516: e.printStackTrace();
517: Assert.fail("Received Exception " + e.getClass()
518: + " : " + e.getMessage());
519: }
520: } catch (AssertionFailedError afe) {
521: throw new TestFailureException(afe);
522: }
523: }
524:
525: private void testJmsConnection(javax.jms.Connection connection)
526: throws JMSException {
527: Session session = connection.createSession(false,
528: Session.DUPS_OK_ACKNOWLEDGE);
529: Topic topic = session.createTopic("test");
530: MessageProducer producer = session.createProducer(topic);
531: producer.send(session.createMessage());
532: producer.close();
533: session.close();
534: connection.close();
535: }
536:
537: public void lookupPersistenceUnit() throws TestFailureException {
538: try {
539: Assert.assertNotNull("The EntityManagerFactory is null",
540: emfField);
541: } catch (AssertionFailedError afe) {
542: throw new TestFailureException(afe);
543: }
544: }
545:
546: public void lookupPersistenceContext() throws TestFailureException {
547: try {
548: Assert.assertNotNull("The EntityManager is null", emField);
549:
550: try {
551: // call a do nothing method to assure entity manager actually exists
552: emField.getFlushMode();
553: } catch (Exception e) {
554: Assert.fail("Received Exception " + e.getClass()
555: + " : " + e.getMessage());
556: }
557: } catch (AssertionFailedError afe) {
558: throw new TestFailureException(afe);
559: }
560: }
561:
562: public void lookupMessageDrivenContext()
563: throws TestFailureException {
564: try {
565: Assert.assertNotNull("The MessageDrivenContext is null",
566: mdbContext);
567: } catch (AssertionFailedError afe) {
568: throw new TestFailureException(afe);
569: }
570:
571: }
572:
573: public void ejbRemove() throws EJBException {
574: }
575: }
|