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.stateful;
017:
018: import junit.framework.Assert;
019: import junit.framework.AssertionFailedError;
020: import org.apache.openejb.test.TestFailureException;
021:
022: import javax.ejb.SessionContext;
023: import javax.ejb.Remove;
024: import javax.naming.InitialContext;
025: import javax.persistence.EntityManager;
026: import javax.transaction.UserTransaction;
027:
028: /**
029: * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
030: */
031: public class PersistenceContextStatefulBean {
032:
033: private EntityManager extendedEntityManager;
034:
035: // Used for testing propigation
036: private static EntityManager inheritedDelegate;
037: private SessionContext ejbContext;
038:
039: public void setSessionContext(SessionContext ctx) {
040: ejbContext = ctx;
041: }
042:
043: @Remove
044: public void remove() {
045: }
046:
047: public String remove(String arg) {
048: return arg;
049: }
050:
051: public void testPersistenceContext() throws TestFailureException {
052: try {
053: try {
054: InitialContext ctx = new InitialContext();
055: Assert.assertNotNull("The InitialContext is null", ctx);
056: EntityManager em = (EntityManager) ctx
057: .lookup("java:comp/env/persistence/TestContext");
058: Assert.assertNotNull("The EntityManager is null", em);
059:
060: // call a do nothing method to assure entity manager actually exists
061: em.getFlushMode();
062: } catch (Exception e) {
063: Assert.fail("Received Exception " + e.getClass()
064: + " : " + e.getMessage());
065: }
066: } catch (AssertionFailedError afe) {
067: throw new TestFailureException(afe);
068: }
069: }
070:
071: public void testExtendedPersistenceContext()
072: throws TestFailureException {
073: try {
074: try {
075: InitialContext ctx = new InitialContext();
076: Assert.assertNotNull("The InitialContext is null", ctx);
077: EntityManager em = (EntityManager) ctx
078: .lookup("java:comp/env/persistence/ExtendedTestContext");
079: Assert.assertNotNull("The EntityManager is null", em);
080:
081: // call a do nothing method to assure entity manager actually exists
082: em.getFlushMode();
083:
084: if (extendedEntityManager != null) {
085: Assert
086: .assertSame(
087: "Extended entity manager should be the same instance that was found last time",
088: extendedEntityManager, em);
089: Assert
090: .assertSame(
091: "Extended entity manager delegate should be the same instance that was found last time",
092: extendedEntityManager.getDelegate(),
093: em.getDelegate());
094: }
095: extendedEntityManager = em;
096:
097: UserTransaction userTransaction = ejbContext
098: .getUserTransaction();
099: userTransaction.begin();
100: try {
101: em.getFlushMode();
102: } finally {
103: userTransaction.commit();
104: }
105: } catch (Exception e) {
106: e.printStackTrace();
107: Assert.fail("Received Exception " + e.getClass()
108: + " : " + e.getMessage());
109: }
110: } catch (AssertionFailedError afe) {
111: throw new TestFailureException(afe);
112: }
113: }
114:
115: public void testPropagatedPersistenceContext()
116: throws TestFailureException {
117: try {
118: try {
119: InitialContext ctx = new InitialContext();
120: Assert.assertNotNull("The InitialContext is null", ctx);
121: EntityManager em = (EntityManager) ctx
122: .lookup("java:comp/env/persistence/ExtendedTestContext");
123: Assert.assertNotNull("The EntityManager is null", em);
124:
125: // call a do nothing method to assure entity manager actually exists
126: em.getFlushMode();
127:
128: // get the raw entity manager so we can test it below
129: inheritedDelegate = (EntityManager) em.getDelegate();
130:
131: // The extended entity manager is not propigated to a non-extended entity manager unless there is a transaction
132: EntityManager nonExtendedEm = (EntityManager) ctx
133: .lookup("java:comp/env/persistence/TestContext");
134: nonExtendedEm.getFlushMode();
135: EntityManager nonExtendedDelegate = ((EntityManager) nonExtendedEm
136: .getDelegate());
137: Assert.assertTrue(
138: "non-extended entity manager should be open",
139: nonExtendedDelegate.isOpen());
140: Assert
141: .assertNotSame(
142: "Extended non-extended entity manager shound not be the same instance as extendend entity manager when accessed out side of a transactions",
143: inheritedDelegate, nonExtendedDelegate);
144:
145: // When the non-extended entity manager is accessed within a transaction is should see the stateful extended context.
146: //
147: // Note: this code also tests EBJ 3.0 Persistence spec 5.9.1 "UserTransaction is begun within the method, the
148: // container associates the persistence context with the JTA transaction and calls EntityManager.joinTransaction."
149: // If our the extended entity manager were not associted with the transaction, the non-extended entity manager would
150: // not see it.
151: UserTransaction userTransaction = ejbContext
152: .getUserTransaction();
153: userTransaction.begin();
154: try {
155: Assert
156: .assertSame(
157: "Extended non-extended entity manager to be same instance as extendend entity manager",
158: inheritedDelegate, nonExtendedEm
159: .getDelegate());
160: } finally {
161: userTransaction.commit();
162: }
163:
164: // When a stateful bean with an extended entity manager creates another stateful bean, the new bean will
165: // inherit the extended entity manager (assuming it contains an extended entity manager for the same persistence
166: // unit).
167: PersistenceContextStatefulHome home = (PersistenceContextStatefulHome) ejbContext
168: .getEJBHome();
169: PersistenceContextStatefulObject object = home.create();
170:
171: // test the new stateful bean recieved the context
172: object.testPropgation();
173:
174: // remove the bean
175: object.remove();
176: } catch (Exception e) {
177: Assert.fail("Received Exception " + e.getClass()
178: + " : " + e.getMessage());
179: }
180: } catch (AssertionFailedError afe) {
181: throw new TestFailureException(afe);
182: }
183: }
184:
185: public void testPropgation() throws TestFailureException {
186: if (inheritedDelegate == null)
187: return;
188: try {
189: try {
190: InitialContext ctx = new InitialContext();
191: Assert.assertNotNull("The InitialContext is null", ctx);
192: EntityManager em = (EntityManager) ctx
193: .lookup("java:comp/env/persistence/ExtendedTestContext");
194: Assert.assertNotNull("The EntityManager is null", em);
195:
196: // call a do nothing method to assure entity manager actually exists
197: em.getFlushMode();
198:
199: EntityManager delegate = (EntityManager) em
200: .getDelegate();
201: Assert
202: .assertSame(
203: "Extended entity manager delegate should be the same instance that was found last time",
204: inheritedDelegate, delegate);
205: } catch (Exception e) {
206: e.printStackTrace();
207: Assert.fail("Received Exception " + e.getClass()
208: + " : " + e.getMessage());
209: }
210: } catch (AssertionFailedError afe) {
211: throw new TestFailureException(afe);
212: }
213: }
214:
215: }
|