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: BasePctxLifeCMTTester00.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: * Tests container-managed transaction-scoped persistence context.
037: * In this scope, the lifecycle of a persistence context ends when the associated transaction ends.
038: * @author Eduardo Studzinski Estima de Castro
039: * @author Gisele Pinheiro Souza
040: *
041: */
042: public class BasePctxLifeCMTTester00 {
043:
044: /**
045: * Bean.
046: */
047: private ItfPCtxLifetime00 bean00;
048:
049: /**
050: * UserTransaction.
051: */
052: private UserTransaction utx;
053:
054: /**
055: * Sets the bean used in the tests.
056: * @param bean instance
057: * @throws Exception if a problem occurs.
058: */
059: public void setBean(final ItfPCtxLifetime00 bean) throws Exception {
060: bean00 = bean;
061: bean00.initEntityManager();
062: utx = getInternalUserTransaction();
063: }
064:
065: /**
066: * This test begins a transaction, creates an entity and rolls back the transaction.
067: * The entity must not exists after the rollback and the entity instance must become detached.
068: * A rollback in a transaction, which is used with the persistence context,
069: * always turns detached all entities associated with the persistence context.
070: * @input UserTransaction and entity.
071: * @output After the rollback, the bean must not exists.
072: * @throws Exception if a problem occurs.
073: */
074: public void test00() throws Exception {
075: utx.begin();
076:
077: bean00.createCheckEntity00();
078:
079: utx.rollback();
080:
081: assertFalse(bean00.existsEntity(),
082: "The bean must not exists, the transaction was rolled back.");
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 become detached, because it is an transaction 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 become detached.
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 detached, the transaction was finished.
100: bean00.checkDetached();
101: }
102:
103: /**
104: * This test begins a transaction and creates an entity. As it uses an transaction
105: * persistence context and the transaction is still open,
106: * the entity remains managed after its creation. After this step,
107: * the test rolls back the transaction and the entity must be removed.
108: * @input With a client transaction, invocation of a bean method which creates an entity.
109: * @output The entity must be removed.
110: * @throws Exception if a problem occurs.
111: */
112: public void test02() throws Exception {
113: utx.begin();
114:
115: bean00.createCheckEntity00();
116:
117: //Checks if the bean is managed, the transaction is open.
118: bean00.checkManaged();
119:
120: utx.rollback();
121:
122: assertFalse(bean00.existsEntity(),
123: "The bean must not exists, the transaction was rolled back.");
124: }
125:
126: /**
127: * This test creates an entity and verifies if it remains managed.
128: * In this test, the transaction is created by the container for each bean method invocation,
129: * as it is a transaction persistence context, it becomes detached.
130: * @input Without providing a client transaction, invocation of a bean method which creates an entity and persists it.
131: * @output A detached entity.
132: * @throws Exception if a problem occurs.
133: */
134: public void test03() throws Exception {
135: bean00.createCheckEntity00();
136:
137: assertTrue(bean00.existsEntity(), "The bean must exists.");
138:
139: bean00.checkDetached();
140: }
141:
142: /**
143: * This test creates an entity, persists the entity and verifies if it becomes detached.
144: * In this test, the transaction is created by the container for each bean method invocation,
145: * as it is a transaction persistence context, it must becomes detached.
146: * @input Without providing a client transaction, invocation of a bean method which creates an entity and persists it.
147: * @output A detached entity.
148: * @throws Exception if a problem occurs.
149: */
150: public void test04() throws Exception {
151: bean00.createCheckEntity01();
152: bean00.checkDetached();
153: }
154:
155: /**
156: * Cleans the test results.
157: * @throws Exception if a problem occurs
158: */
159: public void tearDown() throws Exception {
160: ItfPCtxLifetime00 beanRemove = getBeanRemoteInstance(
161: SFSBPCtxLifeCMT00.class, ItfPCtxLifetime00.class);
162: beanRemove.initEntityManager();
163: beanRemove.removeEntity();
164: }
165: }
|