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