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