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 org.apache.openejb.test.TestFailureException;
019: import org.apache.openejb.test.stateful.BasicStatefulHome;
020: import org.apache.openejb.test.stateful.BasicStatefulObject;
021: import org.apache.openejb.test.stateful.BasicStatefulBusinessLocal;
022: import org.apache.openejb.test.stateful.BasicStatefulBusinessRemote;
023: import org.apache.openejb.test.entity.bmp.BasicBmpHome;
024: import org.apache.openejb.test.entity.bmp.BasicBmpObject;
025:
026: import javax.ejb.EJBContext;
027: import javax.ejb.SessionContext;
028: import javax.ejb.EJBException;
029: import javax.sql.DataSource;
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032: import javax.persistence.EntityManagerFactory;
033: import javax.persistence.EntityManager;
034:
035: import junit.framework.Assert;
036: import junit.framework.AssertionFailedError;
037:
038: import java.rmi.RemoteException;
039:
040: public class ContextLookupStatelessPojoBean {
041:
042: public void lookupEntityBean() throws TestFailureException {
043: try {
044: try {
045: BasicBmpHome home = (BasicBmpHome) getSessionContext()
046: .lookup("stateless/beanReferences/bmp_entity");
047: Assert.assertNotNull("The EJBHome looked up is null",
048: home);
049:
050: BasicBmpObject object = home.createObject("Enc Bean");
051: Assert.assertNotNull("The EJBObject is null", object);
052: } catch (Exception e) {
053: Assert.fail("Received Exception " + e.getClass()
054: + " : " + e.getMessage());
055: }
056: } catch (AssertionFailedError afe) {
057: throw new TestFailureException(afe);
058: }
059: }
060:
061: public void lookupStatefulBean() throws TestFailureException {
062: try {
063: try {
064: BasicStatefulHome home = (BasicStatefulHome) getSessionContext()
065: .lookup("stateless/beanReferences/stateful");
066: Assert.assertNotNull("The EJBHome looked up is null",
067: home);
068:
069: BasicStatefulObject object = home
070: .createObject("Enc Bean");
071: Assert.assertNotNull("The EJBObject is null", object);
072: } catch (Exception e) {
073: Assert.fail("Received Exception " + e.getClass()
074: + " : " + e.getMessage());
075: }
076: } catch (AssertionFailedError afe) {
077: throw new TestFailureException(afe);
078: }
079: }
080:
081: public void lookupStatelessBean() throws TestFailureException {
082: try {
083: try {
084: BasicStatelessHome home = (BasicStatelessHome) getSessionContext()
085: .lookup("stateless/beanReferences/stateless");
086: Assert.assertNotNull("The EJBHome looked up is null",
087: home);
088:
089: BasicStatelessObject object = home.createObject();
090: Assert.assertNotNull("The EJBObject is null", object);
091: } catch (Exception e) {
092: Assert.fail("Received Exception " + e.getClass()
093: + " : " + e.getMessage());
094: }
095: } catch (AssertionFailedError afe) {
096: throw new TestFailureException(afe);
097: }
098: }
099:
100: public void lookupStatelessBusinessLocal()
101: throws TestFailureException {
102: try {
103: try {
104: BasicStatelessBusinessLocal object = (BasicStatelessBusinessLocal) getSessionContext()
105: .lookup(
106: "stateless/beanReferences/stateless-business-local");
107: Assert.assertNotNull("The EJB BusinessLocal is null",
108: 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 lookupStatelessBusinessRemote()
119: throws TestFailureException {
120: try {
121: try {
122: BasicStatelessBusinessRemote object = (BasicStatelessBusinessRemote) getSessionContext()
123: .lookup(
124: "stateless/beanReferences/stateless-business-remote");
125: Assert.assertNotNull("The EJB BusinessRemote is null",
126: object);
127: } catch (Exception e) {
128: Assert.fail("Received Exception " + e.getClass()
129: + " : " + e.getMessage());
130: }
131: } catch (AssertionFailedError afe) {
132: throw new TestFailureException(afe);
133: }
134: }
135:
136: public void lookupStatefulBusinessLocal()
137: throws TestFailureException {
138: try {
139: try {
140: BasicStatefulBusinessLocal object = (BasicStatefulBusinessLocal) getSessionContext()
141: .lookup(
142: "stateless/beanReferences/stateful-business-local");
143: Assert.assertNotNull("The EJB BusinessLocal is null",
144: object);
145: } catch (Exception e) {
146: Assert.fail("Received Exception " + e.getClass()
147: + " : " + e.getMessage());
148: }
149: } catch (AssertionFailedError afe) {
150: throw new TestFailureException(afe);
151: }
152: }
153:
154: public void lookupStatefulBusinessRemote()
155: throws TestFailureException {
156: try {
157: try {
158: BasicStatefulBusinessRemote object = (BasicStatefulBusinessRemote) getSessionContext()
159: .lookup(
160: "stateless/beanReferences/stateful-business-remote");
161: Assert.assertNotNull("The EJB BusinessRemote is null",
162: object);
163: } catch (Exception e) {
164: Assert.fail("Received Exception " + e.getClass()
165: + " : " + e.getMessage());
166: }
167: } catch (AssertionFailedError afe) {
168: throw new TestFailureException(afe);
169: }
170: }
171:
172: public void lookupStringEntry() throws TestFailureException {
173: try {
174: try {
175: String expected = new String("1");
176: String actual = (String) getSessionContext().lookup(
177: "stateless/references/String");
178:
179: Assert.assertNotNull("The String looked up is null",
180: actual);
181: Assert.assertEquals(expected, actual);
182:
183: } catch (Exception e) {
184: Assert.fail("Received Exception " + e.getClass()
185: + " : " + e.getMessage());
186: }
187: } catch (AssertionFailedError afe) {
188: throw new TestFailureException(afe);
189: }
190: }
191:
192: public void lookupDoubleEntry() throws TestFailureException {
193: try {
194: try {
195: Double expected = new Double(1.0D);
196: Double actual = (Double) getSessionContext().lookup(
197: "stateless/references/Double");
198:
199: Assert.assertNotNull("The Double looked up is null",
200: actual);
201: Assert.assertEquals(expected, actual);
202:
203: } catch (Exception e) {
204: Assert.fail("Received Exception " + e.getClass()
205: + " : " + e.getMessage());
206: }
207: } catch (AssertionFailedError afe) {
208: throw new TestFailureException(afe);
209: }
210: }
211:
212: public void lookupLongEntry() throws TestFailureException {
213: try {
214: try {
215: Long expected = new Long(1L);
216: Long actual = (Long) getSessionContext().lookup(
217: "stateless/references/Long");
218:
219: Assert.assertNotNull("The Long looked up is null",
220: actual);
221: Assert.assertEquals(expected, actual);
222:
223: } catch (Exception e) {
224: Assert.fail("Received Exception " + e.getClass()
225: + " : " + e.getMessage());
226: }
227: } catch (AssertionFailedError afe) {
228: throw new TestFailureException(afe);
229: }
230: }
231:
232: public void lookupFloatEntry() throws TestFailureException {
233: try {
234: try {
235: Float expected = new Float(1.0F);
236: Float actual = (Float) getSessionContext().lookup(
237: "stateless/references/Float");
238:
239: Assert.assertNotNull("The Float looked up is null",
240: actual);
241: Assert.assertEquals(expected, actual);
242:
243: } catch (Exception e) {
244: Assert.fail("Received Exception " + e.getClass()
245: + " : " + e.getMessage());
246: }
247: } catch (AssertionFailedError afe) {
248: throw new TestFailureException(afe);
249: }
250: }
251:
252: public void lookupIntegerEntry() throws TestFailureException {
253: try {
254: try {
255: Integer expected = new Integer(1);
256: Integer actual = (Integer) getSessionContext().lookup(
257: "stateless/references/Integer");
258:
259: Assert.assertNotNull("The Integer looked up is null",
260: actual);
261: Assert.assertEquals(expected, actual);
262:
263: } catch (Exception e) {
264: Assert.fail("Received Exception " + e.getClass()
265: + " : " + e.getMessage());
266: }
267: } catch (AssertionFailedError afe) {
268: throw new TestFailureException(afe);
269: }
270: }
271:
272: public void lookupShortEntry() throws TestFailureException {
273: try {
274: try {
275: Short expected = new Short((short) 1);
276: Short actual = (Short) getSessionContext().lookup(
277: "stateless/references/Short");
278:
279: Assert.assertNotNull("The Short looked up is null",
280: actual);
281: Assert.assertEquals(expected, actual);
282:
283: } catch (Exception e) {
284: Assert.fail("Received Exception " + e.getClass()
285: + " : " + e.getMessage());
286: }
287: } catch (AssertionFailedError afe) {
288: throw new TestFailureException(afe);
289: }
290: }
291:
292: public void lookupBooleanEntry() throws TestFailureException {
293: try {
294: try {
295: Boolean expected = new Boolean(true);
296: Boolean actual = (Boolean) getSessionContext().lookup(
297: "stateless/references/Boolean");
298:
299: Assert.assertNotNull("The Boolean looked up is null",
300: actual);
301: Assert.assertEquals(expected, actual);
302:
303: } catch (Exception e) {
304: Assert.fail("Received Exception " + e.getClass()
305: + " : " + e.getMessage());
306: }
307: } catch (AssertionFailedError afe) {
308: throw new TestFailureException(afe);
309: }
310: }
311:
312: public void lookupByteEntry() throws TestFailureException {
313: try {
314: try {
315: Byte expected = new Byte((byte) 1);
316: Byte actual = (Byte) getSessionContext().lookup(
317: "stateless/references/Byte");
318:
319: Assert.assertNotNull("The Byte looked up is null",
320: actual);
321: Assert.assertEquals(expected, actual);
322:
323: } catch (Exception e) {
324: Assert.fail("Received Exception " + e.getClass()
325: + " : " + e.getMessage());
326: }
327: } catch (AssertionFailedError afe) {
328: throw new TestFailureException(afe);
329: }
330: }
331:
332: public void lookupCharacterEntry() throws TestFailureException {
333: try {
334: try {
335: Character expected = new Character('D');
336: Character actual = (Character) getSessionContext()
337: .lookup("stateless/references/Character");
338:
339: Assert.assertNotNull("The Character looked up is null",
340: actual);
341: Assert.assertEquals(expected, actual);
342:
343: } catch (Exception e) {
344: Assert.fail("Received Exception " + e.getClass()
345: + " : " + e.getMessage());
346: }
347: } catch (AssertionFailedError afe) {
348: throw new TestFailureException(afe);
349: }
350: }
351:
352: public void lookupResource() throws TestFailureException {
353: try {
354: try {
355: Object obj = getSessionContext().lookup("datasource");
356: Assert.assertNotNull("The DataSource is null", obj);
357: Assert.assertTrue("Not an instance of DataSource",
358: obj instanceof DataSource);
359: } catch (Exception e) {
360: Assert.fail("Received Exception " + e.getClass()
361: + " : " + e.getMessage());
362: }
363: } catch (AssertionFailedError afe) {
364: throw new TestFailureException(afe);
365: }
366: }
367:
368: public void lookupPersistenceUnit() throws TestFailureException {
369: try {
370: try {
371: InitialContext ctx = new InitialContext();
372: Assert.assertNotNull("The InitialContext is null", ctx);
373: EntityManagerFactory emf = (EntityManagerFactory) ctx
374: .lookup("java:comp/env/persistence/TestUnit");
375: Assert.assertNotNull(
376: "The EntityManagerFactory is null", emf);
377:
378: } catch (Exception e) {
379: Assert.fail("Received Exception " + e.getClass()
380: + " : " + e.getMessage());
381: }
382: } catch (AssertionFailedError afe) {
383: throw new TestFailureException(afe);
384: }
385: }
386:
387: public void lookupSessionContext() throws TestFailureException {
388: try {
389: try {
390: InitialContext ctx = new InitialContext();
391: Assert.assertNotNull("The InitialContext is null", ctx);
392:
393: // lookup in enc
394: SessionContext sctx = (SessionContext) ctx
395: .lookup("java:comp/env/sessioncontext");
396: Assert
397: .assertNotNull(
398: "The SessionContext got from java:comp/env/sessioncontext is null",
399: sctx);
400:
401: // lookup using global name
402: EJBContext ejbCtx = (EJBContext) ctx
403: .lookup("java:comp/EJBContext");
404: Assert
405: .assertNotNull(
406: "The SessionContext got from java:comp/EJBContext is null ",
407: ejbCtx);
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:
418: public void lookupPersistenceContext() throws TestFailureException {
419: try {
420: try {
421: InitialContext ctx = new InitialContext();
422: Assert.assertNotNull("The InitialContext is null", ctx);
423: EntityManager em = (EntityManager) ctx
424: .lookup("java:comp/env/persistence/TestContext");
425: Assert.assertNotNull("The EntityManager is null", em);
426:
427: // call a do nothing method to assure entity manager actually exists
428: em.getFlushMode();
429: } catch (Exception e) {
430: Assert.fail("Received Exception " + e.getClass()
431: + " : " + e.getMessage());
432: }
433: } catch (AssertionFailedError afe) {
434: throw new TestFailureException(afe);
435: }
436: }
437:
438: /**
439: * Set the associated session context. The container calls this method
440: * after the instance creation.
441: */
442: public SessionContext getSessionContext() throws EJBException,
443: RemoteException {
444: SessionContext ejbContext = null;
445: try {
446: ejbContext = (SessionContext) new InitialContext()
447: .lookup("java:comp/EJBContext");
448: } catch (NamingException e) {
449: // TODO Auto-generated catch block
450: e.printStackTrace();
451: }
452: return ejbContext;
453: }
454: }
|