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 junit.framework.Assert;
019: import junit.framework.AssertionFailedError;
020: import org.apache.openejb.test.TestFailureException;
021: import org.apache.openejb.test.stateful.BasicStatefulHome;
022: import org.apache.openejb.test.stateful.BasicStatefulObject;
023: import org.apache.openejb.test.stateful.BasicStatefulBusinessLocal;
024: import org.apache.openejb.test.stateful.BasicStatefulBusinessRemote;
025: import org.apache.openejb.test.stateless.BasicStatelessHome;
026: import org.apache.openejb.test.stateless.BasicStatelessObject;
027: import org.apache.openejb.test.stateless.BasicStatelessBusinessLocal;
028: import org.apache.openejb.test.stateless.BasicStatelessBusinessRemote;
029:
030: import javax.ejb.EJBException;
031: import javax.ejb.EntityContext;
032: import javax.ejb.RemoveException;
033: import javax.persistence.EntityManagerFactory;
034: import javax.persistence.EntityManager;
035: import javax.sql.DataSource;
036: import javax.jms.ConnectionFactory;
037: import javax.jms.Connection;
038: import javax.jms.Session;
039: import javax.jms.Topic;
040: import javax.jms.MessageProducer;
041: import javax.jms.TopicConnectionFactory;
042: import javax.jms.QueueConnectionFactory;
043: import javax.jms.JMSException;
044: import java.rmi.RemoteException;
045:
046: /**
047: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
048: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
049: */
050: public class ContextLookupBmpBean implements javax.ejb.EntityBean {
051:
052: private int primaryKey;
053: private String firstName;
054: private String lastName;
055: private EntityContext ejbContext;
056:
057: //=============================
058: // Home interface methods
059: //
060:
061: /**
062: * Maps to EncBmpHome.findEmptyCollection
063: *
064: * @return
065: * @throws javax.ejb.FinderException
066: * @see EncBmpHome#findEmptyCollection()
067: */
068: public java.util.Collection ejbFindEmptyCollection()
069: throws javax.ejb.FinderException, java.rmi.RemoteException {
070: return new java.util.Vector();
071: }
072:
073: /**
074: * Maps to EncBmpHome.findByPrimaryKey
075: *
076: * @param primaryKey
077: * @return
078: * @throws javax.ejb.FinderException
079: * @see EncBmpHome#findByPrimaryKey(Integer)
080: */
081: public Integer ejbFindByPrimaryKey(Integer primaryKey)
082: throws javax.ejb.FinderException {
083: return new Integer(-1);
084: }
085:
086: /**
087: * Maps to EncBmpHome.create
088: *
089: * @param name
090: * @return
091: * @throws javax.ejb.CreateException
092: * @see EncBmpHome#create
093: */
094: public Integer ejbCreate(String name)
095: throws javax.ejb.CreateException {
096: return new Integer(-1);
097: }
098:
099: public void ejbPostCreate(String name)
100: throws javax.ejb.CreateException {
101: }
102:
103: //
104: // Home interface methods
105: //=============================
106:
107: //=============================
108: // Remote interface methods
109: //
110:
111: public void lookupEntityBean() throws TestFailureException {
112: try {
113: try {
114: BasicBmpHome home = (BasicBmpHome) ejbContext
115: .lookup("entity/bmp/beanReferences/bmp_entity");
116: Assert.assertNotNull("The EJBHome looked up is null",
117: home);
118:
119: BasicBmpObject object = home.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 lookupStatefulBean() throws TestFailureException {
131: try {
132: try {
133: BasicStatefulHome home = (BasicStatefulHome) ejbContext
134: .lookup("entity/bmp/beanReferences/stateful");
135: Assert.assertNotNull("The EJBHome looked up is null",
136: home);
137:
138: BasicStatefulObject object = home
139: .createObject("Enc Bean");
140: Assert.assertNotNull("The EJBObject is null", object);
141: } catch (Exception e) {
142: Assert.fail("Received Exception " + e.getClass()
143: + " : " + e.getMessage());
144: }
145: } catch (AssertionFailedError afe) {
146: throw new TestFailureException(afe);
147: }
148: }
149:
150: public void lookupStatelessBean() throws TestFailureException {
151: try {
152: try {
153: BasicStatelessHome home = (BasicStatelessHome) ejbContext
154: .lookup("entity/bmp/beanReferences/stateless");
155: Assert.assertNotNull("The EJBHome looked up is null",
156: home);
157:
158: BasicStatelessObject object = home.createObject();
159: Assert.assertNotNull("The EJBObject is null", object);
160: } catch (Exception e) {
161: Assert.fail("Received Exception " + e.getClass()
162: + " : " + e.getMessage());
163: }
164: } catch (AssertionFailedError afe) {
165: throw new TestFailureException(afe);
166: }
167: }
168:
169: public void lookupStatelessBusinessLocal()
170: throws TestFailureException {
171: try {
172: try {
173: BasicStatelessBusinessLocal object = (BasicStatelessBusinessLocal) ejbContext
174: .lookup("entity/bmp/beanReferences/stateless-business-local");
175: Assert.assertNotNull("The EJB BusinessLocal is null",
176: object);
177: } catch (Exception e) {
178: Assert.fail("Received Exception " + e.getClass()
179: + " : " + e.getMessage());
180: }
181: } catch (AssertionFailedError afe) {
182: throw new TestFailureException(afe);
183: }
184: }
185:
186: public void lookupStatelessBusinessRemote()
187: throws TestFailureException {
188: try {
189: try {
190: BasicStatelessBusinessRemote object = (BasicStatelessBusinessRemote) ejbContext
191: .lookup("entity/bmp/beanReferences/stateless-business-remote");
192: Assert.assertNotNull("The EJB BusinessRemote is null",
193: object);
194: } catch (Exception e) {
195: Assert.fail("Received Exception " + e.getClass()
196: + " : " + e.getMessage());
197: }
198: } catch (AssertionFailedError afe) {
199: throw new TestFailureException(afe);
200: }
201: }
202:
203: public void lookupStatefulBusinessLocal()
204: throws TestFailureException {
205: try {
206: try {
207: BasicStatefulBusinessLocal object = (BasicStatefulBusinessLocal) ejbContext
208: .lookup("entity/bmp/beanReferences/stateful-business-local");
209: Assert.assertNotNull("The EJB BusinessLocal is null",
210: object);
211: } catch (Exception e) {
212: Assert.fail("Received Exception " + e.getClass()
213: + " : " + e.getMessage());
214: }
215: } catch (AssertionFailedError afe) {
216: throw new TestFailureException(afe);
217: }
218: }
219:
220: public void lookupStatefulBusinessRemote()
221: throws TestFailureException {
222: try {
223: try {
224: BasicStatefulBusinessRemote object = (BasicStatefulBusinessRemote) ejbContext
225: .lookup("entity/bmp/beanReferences/stateful-business-remote");
226: Assert.assertNotNull("The EJB BusinessRemote is null",
227: object);
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 lookupStringEntry() throws TestFailureException {
238: try {
239: try {
240: String expected = new String("1");
241: String actual = (String) ejbContext
242: .lookup("entity/bmp/references/String");
243:
244: Assert.assertNotNull("The String 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 lookupDoubleEntry() throws TestFailureException {
258: try {
259: try {
260: Double expected = new Double(1.0D);
261: Double actual = (Double) ejbContext
262: .lookup("entity/bmp/references/Double");
263:
264: Assert.assertNotNull("The Double 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 lookupLongEntry() throws TestFailureException {
278: try {
279: try {
280: Long expected = new Long(1L);
281: Long actual = (Long) ejbContext
282: .lookup("entity/bmp/references/Long");
283:
284: Assert.assertNotNull("The Long 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 lookupFloatEntry() throws TestFailureException {
298: try {
299: try {
300: Float expected = new Float(1.0F);
301: Float actual = (Float) ejbContext
302: .lookup("entity/bmp/references/Float");
303:
304: Assert.assertNotNull("The Float 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 lookupIntegerEntry() throws TestFailureException {
318: try {
319: try {
320: Integer expected = new Integer(1);
321: Integer actual = (Integer) ejbContext
322: .lookup("entity/bmp/references/Integer");
323:
324: Assert.assertNotNull("The Integer 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 lookupShortEntry() throws TestFailureException {
338: try {
339: try {
340: Short expected = new Short((short) 1);
341: Short actual = (Short) ejbContext
342: .lookup("entity/bmp/references/Short");
343:
344: Assert.assertNotNull("The Short 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 lookupBooleanEntry() throws TestFailureException {
358: try {
359: try {
360: Boolean expected = new Boolean(true);
361: Boolean actual = (Boolean) ejbContext
362: .lookup("entity/bmp/references/Boolean");
363:
364: Assert.assertNotNull("The Boolean 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 lookupByteEntry() throws TestFailureException {
378: try {
379: try {
380: Byte expected = new Byte((byte) 1);
381: Byte actual = (Byte) ejbContext
382: .lookup("entity/bmp/references/Byte");
383:
384: Assert.assertNotNull("The Byte 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 lookupCharacterEntry() throws TestFailureException {
398: try {
399: try {
400: Character expected = new Character('D');
401: Character actual = (Character) ejbContext
402: .lookup("entity/bmp/references/Character");
403:
404: Assert.assertNotNull("The Character looked up is null",
405: actual);
406: Assert.assertEquals(expected, actual);
407:
408: } catch (Exception e) {
409: Assert.fail("Received Exception " + e.getClass()
410: + " : " + e.getMessage());
411: }
412: } catch (AssertionFailedError afe) {
413: throw new TestFailureException(afe);
414: }
415: }
416:
417: public void lookupResource() throws TestFailureException {
418: try {
419: try {
420: Object obj = ejbContext.lookup("datasource");
421: Assert.assertNotNull("The DataSource is null", obj);
422: Assert.assertTrue("Not an instance of DataSource",
423: obj instanceof DataSource);
424: } catch (Exception e) {
425: Assert.fail("Received Exception " + e.getClass()
426: + " : " + e.getMessage());
427: }
428: } catch (AssertionFailedError afe) {
429: throw new TestFailureException(afe);
430: }
431: }
432:
433: public void lookupJMSConnectionFactory()
434: throws TestFailureException {
435: try {
436: try {
437: Object obj = ejbContext.lookup("jms");
438: Assert.assertNotNull(
439: "The JMS ConnectionFactory is null", obj);
440: Assert.assertTrue(
441: "Not an instance of ConnectionFactory",
442: obj instanceof ConnectionFactory);
443: ConnectionFactory connectionFactory = (ConnectionFactory) obj;
444: testJmsConnection(connectionFactory.createConnection());
445:
446: obj = ejbContext.lookup("TopicCF");
447: Assert.assertNotNull(
448: "The JMS TopicConnectionFactory is null", obj);
449: Assert.assertTrue(
450: "Not an instance of TopicConnectionFactory",
451: obj instanceof TopicConnectionFactory);
452: TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) obj;
453: testJmsConnection(topicConnectionFactory
454: .createConnection());
455:
456: obj = ejbContext.lookup("QueueCF");
457: Assert.assertNotNull(
458: "The JMS QueueConnectionFactory is null", obj);
459: Assert.assertTrue(
460: "Not an instance of QueueConnectionFactory",
461: obj instanceof QueueConnectionFactory);
462: QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;
463: testJmsConnection(queueConnectionFactory
464: .createConnection());
465: } catch (Exception e) {
466: e.printStackTrace();
467: Assert.fail("Received Exception " + e.getClass()
468: + " : " + e.getMessage());
469: }
470: } catch (AssertionFailedError afe) {
471: throw new TestFailureException(afe);
472: }
473: }
474:
475: private void testJmsConnection(Connection connection)
476: throws JMSException {
477: Session session = connection.createSession(false,
478: Session.DUPS_OK_ACKNOWLEDGE);
479: Topic topic = session.createTopic("test");
480: MessageProducer producer = session.createProducer(topic);
481: producer.send(session.createMessage());
482: producer.close();
483: session.close();
484: connection.close();
485: }
486:
487: public void lookupPersistenceUnit() throws TestFailureException {
488: try {
489: try {
490: EntityManagerFactory emf = (EntityManagerFactory) ejbContext
491: .lookup("persistence/TestUnit");
492: Assert.assertNotNull(
493: "The EntityManagerFactory is null", emf);
494:
495: } catch (Exception e) {
496: Assert.fail("Received Exception " + e.getClass()
497: + " : " + e.getMessage());
498: }
499: } catch (AssertionFailedError afe) {
500: throw new TestFailureException(afe);
501: }
502: }
503:
504: public void lookupPersistenceContext() throws TestFailureException {
505: try {
506: try {
507: EntityManager em = (EntityManager) ejbContext
508: .lookup("persistence/TestContext");
509: Assert.assertNotNull("The EntityManager is null", em);
510:
511: // call a do nothing method to assure entity manager actually exists
512: em.getFlushMode();
513: } catch (Exception e) {
514: Assert.fail("Received Exception " + e.getClass()
515: + " : " + e.getMessage());
516: }
517: } catch (AssertionFailedError afe) {
518: throw new TestFailureException(afe);
519: }
520: }
521:
522: //
523: // Remote interface methods
524: //=============================
525:
526: //================================
527: // EntityBean interface methods
528: //
529:
530: /**
531: * A container invokes this method to instruct the
532: * instance to synchronize its state by loading it state from the
533: * underlying database.
534: */
535: public void ejbLoad() throws EJBException, RemoteException {
536: }
537:
538: /**
539: * Set the associated entity context. The container invokes this method
540: * on an instance after the instance has been created.
541: */
542: public void setEntityContext(EntityContext ctx)
543: throws EJBException, RemoteException {
544: ejbContext = ctx;
545: }
546:
547: /**
548: * Unset the associated entity context. The container calls this method
549: * before removing the instance.
550: */
551: public void unsetEntityContext() throws EJBException,
552: RemoteException {
553: }
554:
555: /**
556: * A container invokes this method to instruct the
557: * instance to synchronize its state by storing it to the underlying
558: * database.
559: */
560: public void ejbStore() throws EJBException, RemoteException {
561: }
562:
563: /**
564: * A container invokes this method before it removes the EJB object
565: * that is currently associated with the instance. This method
566: * is invoked when a client invokes a remove operation on the
567: * enterprise Bean's home interface or the EJB object's remote interface.
568: * This method transitions the instance from the ready state to the pool
569: * of available instances.
570: */
571: public void ejbRemove() throws RemoveException, EJBException,
572: RemoteException {
573: }
574:
575: /**
576: * A container invokes this method when the instance
577: * is taken out of the pool of available instances to become associated
578: * with a specific EJB object. This method transitions the instance to
579: * the ready state.
580: */
581: public void ejbActivate() throws EJBException, RemoteException {
582: }
583:
584: /**
585: * A container invokes this method on an instance before the instance
586: * becomes disassociated with a specific EJB object. After this method
587: * completes, the container will place the instance into the pool of
588: * available instances.
589: */
590: public void ejbPassivate() throws EJBException, RemoteException {
591: }
592: //
593: // EntityBean interface methods
594: //================================
595: }
|