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