001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: BasePctxLifeCMETester00.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.ejbs.base.persistencectxlife;
025:
026: import static org.ow2.easybeans.tests.common.helper.EJBHelper.getBeanRemoteInstance;
027: import static org.ow2.easybeans.tests.common.helper.TransactionHelper.getInternalUserTransaction;
028: import static org.testng.Assert.assertFalse;
029: import static org.testng.Assert.assertTrue;
030:
031: import javax.transaction.UserTransaction;
032:
033: import org.ow2.easybeans.tests.common.ejbs.stateful.containermanaged.persistencectxlife.SFSBPCtxLifeCMT00;
034:
035: /**
036: * Base class for container-managed extended-scoped persistence context tests.
037: * @author Eduardo Studzinski Estima de Castro
038: * @author Gisele Pinheiro Souza
039: *
040: */
041: public class BasePctxLifeCMETester00 {
042:
043: /**
044: * Bean.
045: */
046: private ItfPCtxLifetime00 bean00;
047:
048: /**
049: * UserTransaction.
050: */
051: private UserTransaction utx;
052:
053: /**
054: * Sets the bean used in the tests.
055: * @param bean instance
056: * @throws Exception if a problem occurs.
057: */
058: public void setBean(final ItfPCtxLifetime00 bean) throws Exception {
059: bean00 = bean;
060: bean00.initEntityManager();
061: utx = getInternalUserTransaction();
062: }
063:
064: /**
065: * This test begins a transaction, creates an entity and rolls back the transaction.
066: * The entity must not exists after the rollback and the entity instance must become detached.
067: * A rollback in a transaction, which is used with the persistence context,
068: * always turns detached all entities associated with the persistence context.
069: * @input UserTransaction and entity.
070: * @output After the rollback, the bean must not exists.
071: * @throws Exception if a problem occurs.
072: */
073: public void test00() throws Exception {
074: utx.begin();
075:
076: bean00.createCheckEntity00();
077:
078: utx.rollback();
079:
080: assertFalse(bean00.existsEntity(),
081: "The bean must not exists, the transaction was rolled back.");
082: bean00.checkDetached();
083: }
084:
085: /**
086: * This test begins a transaction, creates an entity and commits the transaction. The entity must
087: * exists after the commit and it must remains managed, because it is an extended persistence context.
088: * @input With a client transaction, invocation of a bean method which creates an entity.
089: * @output After the commit, the bean must remains managed.
090: * @throws Exception if a problem occurs.
091: */
092: public void test01() throws Exception {
093: utx.begin();
094:
095: bean00.createCheckEntity00();
096:
097: utx.commit();
098:
099: //Checks if the bean is managed, this is an extended context.
100: bean00.checkManaged();
101: }
102:
103: /**
104: * This test begins a transaction and creates an entity. As it uses an extended persistence context,
105: * the entity must be managed after its creation. After this step,
106: * the test rolls back the transaction and the entity must become detached.
107: * @input With a client transaction, invocation of a bean method which creates an entity.
108: * @output An detached entity.
109: * @throws Exception if a problem occurs.
110: */
111: public void test02() throws Exception {
112: utx.begin();
113:
114: bean00.createCheckEntity00();
115:
116: //Checs if the bean is managed
117: bean00.checkManaged();
118:
119: utx.rollback();
120:
121: //Checs if the bean is detached
122: bean00.checkDetached();
123: }
124:
125: /**
126: * This test creates an entity and verifies if it remains managed.
127: * In this test, the transaction is created by the container for each bean method invocation,
128: * however, as it is an extended persistence context, it must remains managed.
129: * @input Without providing a client transaction, invocation of a bean method which creates an entity and persists it.
130: * @output A managed entity.
131: * @throws Exception if a problem occurs.
132: */
133: public void test03() throws Exception {
134: bean00.createCheckEntity00();
135:
136: assertTrue(bean00.existsEntity(), "The bean must exists.");
137:
138: bean00.checkManaged();
139: }
140:
141: /**
142: * This test creates an entity, persists the entity and verifies if it remains managed.
143: * In this test, the transaction is created by the container for each bean method invocation,
144: * however, as it is an extended persistence context, it must remains managed.
145: * @input Without providing a client transaction, invocation of a bean method which creates an entity and persists it.
146: * @output A managed entity.
147: * @throws Exception if a problem occurs.
148: */
149: public void test04() throws Exception {
150: //Creates and persist.
151: bean00.createCheckEntity00();
152: //Persists again.
153: bean00.persistEntity();
154:
155: bean00.checkManaged();
156: }
157:
158: /**
159: * Cleans the test results.
160: * @throws Exception if a problem occurs
161: */
162: public void tearDown() throws Exception {
163: ItfPCtxLifetime00 beanRemove = getBeanRemoteInstance(
164: SFSBPCtxLifeCMT00.class, ItfPCtxLifetime00.class);
165: beanRemove.initEntityManager();
166: beanRemove.removeEntity();
167: }
168:
169: }
|