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.entity.bmp;
017:
018: import java.rmi.RemoteException;
019:
020: import javax.ejb.EJBException;
021: import javax.ejb.EntityContext;
022: import javax.ejb.RemoveException;
023: import javax.naming.InitialContext;
024: import javax.persistence.EntityManagerFactory;
025: import javax.persistence.EntityManager;
026: import javax.sql.DataSource;
027: import javax.jms.ConnectionFactory;
028: import javax.jms.Connection;
029: import javax.jms.Session;
030: import javax.jms.Topic;
031: import javax.jms.MessageProducer;
032: import javax.jms.TopicConnectionFactory;
033: import javax.jms.QueueConnectionFactory;
034: import javax.jms.JMSException;
035:
036: import junit.framework.Assert;
037: import junit.framework.AssertionFailedError;
038:
039: import org.apache.openejb.test.TestFailureException;
040: import org.apache.openejb.test.stateful.BasicStatefulHome;
041: import org.apache.openejb.test.stateful.BasicStatefulObject;
042: import org.apache.openejb.test.stateful.BasicStatefulBusinessLocal;
043: import org.apache.openejb.test.stateful.BasicStatefulBusinessRemote;
044: import org.apache.openejb.test.stateless.BasicStatelessHome;
045: import org.apache.openejb.test.stateless.BasicStatelessObject;
046: import org.apache.openejb.test.stateless.BasicStatelessBusinessLocal;
047: import org.apache.openejb.test.stateless.BasicStatelessBusinessRemote;
048:
049: /**
050: *
051: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
052: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
053: */
054: public class EncBmpBean implements javax.ejb.EntityBean {
055:
056: private int primaryKey;
057: private String firstName;
058: private String lastName;
059: private EntityContext ejbContext;
060:
061: //=============================
062: // Home interface methods
063: //
064:
065: /**
066: * Maps to EncBmpHome.findEmptyCollection
067: *
068: * @return
069: * @exception javax.ejb.FinderException
070: * @see EncBmpHome#findEmptyCollection()
071: */
072: public java.util.Collection ejbFindEmptyCollection()
073: throws javax.ejb.FinderException, java.rmi.RemoteException {
074: return new java.util.Vector();
075: }
076:
077: /**
078: * Maps to EncBmpHome.findByPrimaryKey
079: *
080: * @param primaryKey
081: * @return
082: * @exception javax.ejb.FinderException
083: * @see EncBmpHome#findByPrimaryKey(Integer)
084: */
085: public Integer ejbFindByPrimaryKey(Integer primaryKey)
086: throws javax.ejb.FinderException {
087: return new Integer(-1);
088: }
089:
090: /**
091: * Maps to EncBmpHome.create
092: *
093: * @param name
094: * @return
095: * @exception javax.ejb.CreateException
096: * @see EncBmpHome#create
097: */
098: public Integer ejbCreate(String name)
099: throws javax.ejb.CreateException {
100: return new Integer(-1);
101: }
102:
103: public void ejbPostCreate(String name)
104: throws javax.ejb.CreateException {
105: }
106:
107: //
108: // Home interface methods
109: //=============================
110:
111: //=============================
112: // Remote interface methods
113: //
114:
115: public void lookupEntityBean() throws TestFailureException {
116: try {
117: try {
118: InitialContext ctx = new InitialContext();
119: Assert.assertNotNull("The InitialContext is null", ctx);
120:
121: BasicBmpHome home = (BasicBmpHome) javax.rmi.PortableRemoteObject
122: .narrow(
123: ctx
124: .lookup("java:comp/env/entity/bmp/beanReferences/bmp_entity"),
125: BasicBmpHome.class);
126: Assert.assertNotNull("The EJBHome looked up is null",
127: home);
128:
129: BasicBmpObject object = home.createObject("Enc Bean");
130: Assert.assertNotNull("The EJBObject is null", object);
131: } catch (Exception e) {
132: Assert.fail("Received Exception " + e.getClass()
133: + " : " + e.getMessage());
134: }
135: } catch (AssertionFailedError afe) {
136: throw new TestFailureException(afe);
137: }
138: }
139:
140: public void lookupStatefulBean() throws TestFailureException {
141: try {
142: try {
143: InitialContext ctx = new InitialContext();
144: Assert.assertNotNull("The InitialContext is null", ctx);
145:
146: BasicStatefulHome home = (BasicStatefulHome) javax.rmi.PortableRemoteObject
147: .narrow(
148: ctx
149: .lookup("java:comp/env/entity/bmp/beanReferences/stateful"),
150: BasicStatefulHome.class);
151: Assert.assertNotNull("The EJBHome looked up is null",
152: home);
153:
154: BasicStatefulObject object = home
155: .createObject("Enc Bean");
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 lookupStatelessBean() throws TestFailureException {
167: try {
168: try {
169: InitialContext ctx = new InitialContext();
170: Assert.assertNotNull("The InitialContext is null", ctx);
171:
172: BasicStatelessHome home = (BasicStatelessHome) javax.rmi.PortableRemoteObject
173: .narrow(
174: ctx
175: .lookup("java:comp/env/entity/bmp/beanReferences/stateless"),
176: BasicStatelessHome.class);
177: Assert.assertNotNull("The EJBHome looked up is null",
178: home);
179:
180: BasicStatelessObject object = home.createObject();
181: Assert.assertNotNull("The EJBObject is null", object);
182: } catch (Exception e) {
183: Assert.fail("Received Exception " + e.getClass()
184: + " : " + e.getMessage());
185: }
186: } catch (AssertionFailedError afe) {
187: throw new TestFailureException(afe);
188: }
189: }
190:
191: public void lookupStatelessBusinessLocal()
192: throws TestFailureException {
193: try {
194: try {
195: InitialContext ctx = new InitialContext();
196: Assert.assertNotNull("The InitialContext is null", ctx);
197:
198: Object o = ctx
199: .lookup("java:comp/env/entity/bmp/beanReferences/stateless-business-local");
200: BasicStatelessBusinessLocal object = (BasicStatelessBusinessLocal) o;
201: Assert.assertNotNull("The EJB BusinessLocal is null",
202: object);
203: } catch (Exception e) {
204: e.printStackTrace();
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 lookupStatelessBusinessRemote()
214: throws TestFailureException {
215: try {
216: try {
217: InitialContext ctx = new InitialContext();
218: Assert.assertNotNull("The InitialContext is null", ctx);
219:
220: BasicStatelessBusinessRemote object = (BasicStatelessBusinessRemote) javax.rmi.PortableRemoteObject
221: .narrow(
222: ctx
223: .lookup("java:comp/env/entity/bmp/beanReferences/stateless-business-remote"),
224: BasicStatelessBusinessRemote.class);
225: Assert.assertNotNull("The EJB BusinessRemote is null",
226: object);
227: } catch (Exception e) {
228: Assert.fail("Received Exception " + e.getClass()
229: + " : " + e.getMessage());
230: }
231: } catch (AssertionFailedError afe) {
232: throw new TestFailureException(afe);
233: }
234: }
235:
236: public void lookupStatefulBusinessLocal()
237: throws TestFailureException {
238: try {
239: try {
240: InitialContext ctx = new InitialContext();
241: Assert.assertNotNull("The InitialContext is null", ctx);
242:
243: BasicStatefulBusinessLocal object = (BasicStatefulBusinessLocal) javax.rmi.PortableRemoteObject
244: .narrow(
245: ctx
246: .lookup("java:comp/env/entity/bmp/beanReferences/stateful-business-local"),
247: BasicStatefulBusinessLocal.class);
248: Assert.assertNotNull("The EJB BusinessLocal is null",
249: object);
250: } catch (Exception e) {
251: Assert.fail("Received Exception " + e.getClass()
252: + " : " + e.getMessage());
253: }
254: } catch (AssertionFailedError afe) {
255: throw new TestFailureException(afe);
256: }
257: }
258:
259: public void lookupStatefulBusinessRemote()
260: throws TestFailureException {
261: try {
262: try {
263: InitialContext ctx = new InitialContext();
264: Assert.assertNotNull("The InitialContext is null", ctx);
265:
266: BasicStatefulBusinessRemote object = (BasicStatefulBusinessRemote) javax.rmi.PortableRemoteObject
267: .narrow(
268: ctx
269: .lookup("java:comp/env/entity/bmp/beanReferences/stateful-business-remote"),
270: BasicStatefulBusinessRemote.class);
271: Assert.assertNotNull("The EJB BusinessRemote is null",
272: object);
273: } catch (Exception e) {
274: Assert.fail("Received Exception " + e.getClass()
275: + " : " + e.getMessage());
276: }
277: } catch (AssertionFailedError afe) {
278: throw new TestFailureException(afe);
279: }
280: }
281:
282: public void lookupStringEntry() throws TestFailureException {
283: try {
284: try {
285: InitialContext ctx = new InitialContext();
286: Assert.assertNotNull("The InitialContext is null", ctx);
287:
288: String expected = new String("1");
289: String actual = (String) ctx
290: .lookup("java:comp/env/entity/bmp/references/String");
291:
292: Assert.assertNotNull("The String looked up is null",
293: actual);
294: Assert.assertEquals(expected, actual);
295:
296: } catch (Exception e) {
297: Assert.fail("Received Exception " + e.getClass()
298: + " : " + e.getMessage());
299: }
300: } catch (AssertionFailedError afe) {
301: throw new TestFailureException(afe);
302: }
303: }
304:
305: public void lookupDoubleEntry() throws TestFailureException {
306: try {
307: try {
308: InitialContext ctx = new InitialContext();
309: Assert.assertNotNull("The InitialContext is null", ctx);
310:
311: Double expected = new Double(1.0D);
312: Double actual = (Double) ctx
313: .lookup("java:comp/env/entity/bmp/references/Double");
314:
315: Assert.assertNotNull("The Double looked up is null",
316: actual);
317: Assert.assertEquals(expected, actual);
318:
319: } catch (Exception e) {
320: Assert.fail("Received Exception " + e.getClass()
321: + " : " + e.getMessage());
322: }
323: } catch (AssertionFailedError afe) {
324: throw new TestFailureException(afe);
325: }
326: }
327:
328: public void lookupLongEntry() throws TestFailureException {
329: try {
330: try {
331: InitialContext ctx = new InitialContext();
332: Assert.assertNotNull("The InitialContext is null", ctx);
333:
334: Long expected = new Long(1L);
335: Long actual = (Long) ctx
336: .lookup("java:comp/env/entity/bmp/references/Long");
337:
338: Assert.assertNotNull("The Long looked up is null",
339: actual);
340: Assert.assertEquals(expected, actual);
341:
342: } catch (Exception e) {
343: Assert.fail("Received Exception " + e.getClass()
344: + " : " + e.getMessage());
345: }
346: } catch (AssertionFailedError afe) {
347: throw new TestFailureException(afe);
348: }
349: }
350:
351: public void lookupFloatEntry() throws TestFailureException {
352: try {
353: try {
354: InitialContext ctx = new InitialContext();
355: Assert.assertNotNull("The InitialContext is null", ctx);
356:
357: Float expected = new Float(1.0F);
358: Float actual = (Float) ctx
359: .lookup("java:comp/env/entity/bmp/references/Float");
360:
361: Assert.assertNotNull("The Float looked up is null",
362: actual);
363: Assert.assertEquals(expected, actual);
364:
365: } catch (Exception e) {
366: Assert.fail("Received Exception " + e.getClass()
367: + " : " + e.getMessage());
368: }
369: } catch (AssertionFailedError afe) {
370: throw new TestFailureException(afe);
371: }
372: }
373:
374: public void lookupIntegerEntry() throws TestFailureException {
375: try {
376: try {
377: InitialContext ctx = new InitialContext();
378: Assert.assertNotNull("The InitialContext is null", ctx);
379:
380: Integer expected = new Integer(1);
381: Integer actual = (Integer) ctx
382: .lookup("java:comp/env/entity/bmp/references/Integer");
383:
384: Assert.assertNotNull("The Integer 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 lookupShortEntry() throws TestFailureException {
398: try {
399: try {
400: InitialContext ctx = new InitialContext();
401: Assert.assertNotNull("The InitialContext is null", ctx);
402:
403: Short expected = new Short((short) 1);
404: Short actual = (Short) ctx
405: .lookup("java:comp/env/entity/bmp/references/Short");
406:
407: Assert.assertNotNull("The Short looked up is null",
408: actual);
409: Assert.assertEquals(expected, actual);
410:
411: } catch (Exception e) {
412: Assert.fail("Received Exception " + e.getClass()
413: + " : " + e.getMessage());
414: }
415: } catch (AssertionFailedError afe) {
416: throw new TestFailureException(afe);
417: }
418: }
419:
420: public void lookupBooleanEntry() throws TestFailureException {
421: try {
422: try {
423: InitialContext ctx = new InitialContext();
424: Assert.assertNotNull("The InitialContext is null", ctx);
425:
426: Boolean expected = new Boolean(true);
427: Boolean actual = (Boolean) ctx
428: .lookup("java:comp/env/entity/bmp/references/Boolean");
429:
430: Assert.assertNotNull("The Boolean looked up is null",
431: actual);
432: Assert.assertEquals(expected, actual);
433:
434: } catch (Exception e) {
435: Assert.fail("Received Exception " + e.getClass()
436: + " : " + e.getMessage());
437: }
438: } catch (AssertionFailedError afe) {
439: throw new TestFailureException(afe);
440: }
441: }
442:
443: public void lookupByteEntry() throws TestFailureException {
444: try {
445: try {
446: InitialContext ctx = new InitialContext();
447: Assert.assertNotNull("The InitialContext is null", ctx);
448:
449: Byte expected = new Byte((byte) 1);
450: Byte actual = (Byte) ctx
451: .lookup("java:comp/env/entity/bmp/references/Byte");
452:
453: Assert.assertNotNull("The Byte looked up is null",
454: actual);
455: Assert.assertEquals(expected, actual);
456:
457: } catch (Exception e) {
458: Assert.fail("Received Exception " + e.getClass()
459: + " : " + e.getMessage());
460: }
461: } catch (AssertionFailedError afe) {
462: throw new TestFailureException(afe);
463: }
464: }
465:
466: public void lookupCharacterEntry() throws TestFailureException {
467: try {
468: try {
469: InitialContext ctx = new InitialContext();
470: Assert.assertNotNull("The InitialContext is null", ctx);
471:
472: Character expected = new Character('D');
473: Character actual = (Character) ctx
474: .lookup("java:comp/env/entity/bmp/references/Character");
475:
476: Assert.assertNotNull("The Character looked up is null",
477: actual);
478: Assert.assertEquals(expected, actual);
479:
480: } catch (Exception e) {
481: Assert.fail("Received Exception " + e.getClass()
482: + " : " + e.getMessage());
483: }
484: } catch (AssertionFailedError afe) {
485: throw new TestFailureException(afe);
486: }
487: }
488:
489: public void lookupResource() throws TestFailureException {
490: try {
491: try {
492: InitialContext ctx = new InitialContext();
493: Assert.assertNotNull("The InitialContext is null", ctx);
494: Object obj = ctx.lookup("java:comp/env/datasource");
495: Assert.assertNotNull("The DataSource is null", obj);
496: Assert.assertTrue("Not an instance of DataSource",
497: obj instanceof DataSource);
498: } catch (Exception e) {
499: Assert.fail("Received Exception " + e.getClass()
500: + " : " + e.getMessage());
501: }
502: } catch (AssertionFailedError afe) {
503: throw new TestFailureException(afe);
504: }
505: }
506:
507: public void lookupJMSConnectionFactory()
508: throws TestFailureException {
509: try {
510: try {
511: InitialContext ctx = new InitialContext();
512: Assert.assertNotNull("The InitialContext is null", ctx);
513: Object obj = ctx.lookup("java:comp/env/jms");
514: Assert.assertNotNull(
515: "The JMS ConnectionFactory is null", obj);
516: Assert.assertTrue(
517: "Not an instance of ConnectionFactory",
518: obj instanceof ConnectionFactory);
519: ConnectionFactory connectionFactory = (ConnectionFactory) obj;
520: testJmsConnection(connectionFactory.createConnection());
521:
522: obj = ctx.lookup("java:comp/env/TopicCF");
523: Assert.assertNotNull(
524: "The JMS TopicConnectionFactory is null", obj);
525: Assert.assertTrue(
526: "Not an instance of TopicConnectionFactory",
527: obj instanceof TopicConnectionFactory);
528: TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) obj;
529: testJmsConnection(topicConnectionFactory
530: .createConnection());
531:
532: obj = ctx.lookup("java:comp/env/QueueCF");
533: Assert.assertNotNull(
534: "The JMS QueueConnectionFactory is null", obj);
535: Assert.assertTrue(
536: "Not an instance of QueueConnectionFactory",
537: obj instanceof QueueConnectionFactory);
538: QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;
539: testJmsConnection(queueConnectionFactory
540: .createConnection());
541: } catch (Exception e) {
542: e.printStackTrace();
543: Assert.fail("Received Exception " + e.getClass()
544: + " : " + e.getMessage());
545: }
546: } catch (AssertionFailedError afe) {
547: throw new TestFailureException(afe);
548: }
549: }
550:
551: private void testJmsConnection(Connection connection)
552: throws JMSException {
553: Session session = connection.createSession(false,
554: Session.DUPS_OK_ACKNOWLEDGE);
555: Topic topic = session.createTopic("test");
556: MessageProducer producer = session.createProducer(topic);
557: producer.send(session.createMessage());
558: producer.close();
559: session.close();
560: connection.close();
561: }
562:
563: public void lookupPersistenceUnit() throws TestFailureException {
564: try {
565: try {
566: InitialContext ctx = new InitialContext();
567: Assert.assertNotNull("The InitialContext is null", ctx);
568: EntityManagerFactory emf = (EntityManagerFactory) ctx
569: .lookup("java:comp/env/persistence/TestUnit");
570: Assert.assertNotNull(
571: "The EntityManagerFactory is null", emf);
572:
573: } catch (Exception e) {
574: Assert.fail("Received Exception " + e.getClass()
575: + " : " + e.getMessage());
576: }
577: } catch (AssertionFailedError afe) {
578: throw new TestFailureException(afe);
579: }
580: }
581:
582: public void lookupPersistenceContext() throws TestFailureException {
583: try {
584: try {
585: InitialContext ctx = new InitialContext();
586: Assert.assertNotNull("The InitialContext is null", ctx);
587: EntityManager em = (EntityManager) ctx
588: .lookup("java:comp/env/persistence/TestContext");
589: Assert.assertNotNull("The EntityManager is null", em);
590:
591: // call a do nothing method to assure entity manager actually exists
592: em.getFlushMode();
593: } catch (Exception e) {
594: Assert.fail("Received Exception " + e.getClass()
595: + " : " + e.getMessage());
596: }
597: } catch (AssertionFailedError afe) {
598: throw new TestFailureException(afe);
599: }
600: }
601:
602: //
603: // Remote interface methods
604: //=============================
605:
606: //================================
607: // EntityBean interface methods
608: //
609:
610: /**
611: * A container invokes this method to instruct the
612: * instance to synchronize its state by loading it state from the
613: * underlying database.
614: */
615: public void ejbLoad() throws EJBException, RemoteException {
616: }
617:
618: /**
619: * Set the associated entity context. The container invokes this method
620: * on an instance after the instance has been created.
621: */
622: public void setEntityContext(EntityContext ctx)
623: throws EJBException, RemoteException {
624: ejbContext = ctx;
625: }
626:
627: /**
628: * Unset the associated entity context. The container calls this method
629: * before removing the instance.
630: */
631: public void unsetEntityContext() throws EJBException,
632: RemoteException {
633: }
634:
635: /**
636: * A container invokes this method to instruct the
637: * instance to synchronize its state by storing it to the underlying
638: * database.
639: */
640: public void ejbStore() throws EJBException, RemoteException {
641: }
642:
643: /**
644: * A container invokes this method before it removes the EJB object
645: * that is currently associated with the instance. This method
646: * is invoked when a client invokes a remove operation on the
647: * enterprise Bean's home interface or the EJB object's remote interface.
648: * This method transitions the instance from the ready state to the pool
649: * of available instances.
650: */
651: public void ejbRemove() throws RemoveException, EJBException,
652: RemoteException {
653: }
654:
655: /**
656: * A container invokes this method when the instance
657: * is taken out of the pool of available instances to become associated
658: * with a specific EJB object. This method transitions the instance to
659: * the ready state.
660: */
661: public void ejbActivate() throws EJBException, RemoteException {
662: }
663:
664: /**
665: * A container invokes this method on an instance before the instance
666: * becomes disassociated with a specific EJB object. After this method
667: * completes, the container will place the instance into the pool of
668: * available instances.
669: */
670: public void ejbPassivate() throws EJBException, RemoteException {
671: }
672: //
673: // EntityBean interface methods
674: //================================
675: }
|