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