KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > base > persistencectxlife > BasePctxLifeCMTTester00


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: BasePctxLifeCMTTester00.java 851 2006-07-12 12:16:22Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife;
26
27 import static org.objectweb.easybeans.tests.common.helper.EJBHelper.getBeanRemoteInstance;
28 import static org.objectweb.easybeans.tests.common.helper.TransactionHelper.getInternalUserTransaction;
29 import static org.testng.Assert.assertFalse;
30 import static org.testng.Assert.assertTrue;
31
32 import javax.transaction.UserTransaction JavaDoc;
33
34 import org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.persistencectxlife.SFSBPCtxLifeCMT00;
35
36 /**
37  * Tests container-managed transaction-scoped persistence context.
38  * In this scope, the lifecycle of a persistence context ends when the associated transaction ends.
39  * @author Eduardo Studzinski Estima de Castro
40  * @author Gisele Pinheiro Souza
41  *
42  */

43 public class BasePctxLifeCMTTester00 {
44
45     /**
46      * Bean.
47      */

48     private ItfPCtxLifetime00 bean00;
49
50     /**
51      * UserTransaction.
52      */

53     private UserTransaction utx;
54
55     /**
56      * Sets the bean used in the tests.
57      * @param bean instance
58      * @throws Exception if a problem occurs.
59      */

60     public void setBean(final ItfPCtxLifetime00 bean) throws Exception JavaDoc{
61         bean00 = bean;
62         bean00.initEntityManager();
63         utx = getInternalUserTransaction();
64     }
65
66     /**
67      * This test begins a transaction, creates an entity and rolls back the transaction.
68      * The entity must not exists after the rollback and the entity instance must become detached.
69      * A rollback in a transaction, which is used with the persistence context,
70      * always turns detached all entities associated with the persistence context.
71      * @input UserTransaction and entity.
72      * @output After the rollback, the bean must not exists.
73      * @throws Exception if a problem occurs.
74      */

75     public void test00() throws Exception JavaDoc{
76         utx.begin();
77
78         bean00.createCheckEntity00();
79
80         utx.rollback();
81
82         assertFalse(bean00.existsEntity(), "The bean must not exists, the transaction was rolled back.");
83     }
84
85     /**
86      * This test begins a transaction, creates an entity and commits the transaction. The entity must
87      * exists after the commit and it must become detached, because it is an transaction persistence context.
88      * @input With a client transaction, invocation of a bean method which creates an entity.
89      * @output After the commit, the bean must become detached.
90      * @throws Exception if a problem occurs.
91      */

92     public void test01() throws Exception JavaDoc{
93         utx.begin();
94
95         bean00.createCheckEntity00();
96
97         utx.commit();
98
99         //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 JavaDoc{
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(), "The bean must not exists, the transaction was rolled back.");
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      * as it is a transaction persistence context, it becomes detached.
129      * @input Without providing a client transaction, invocation of a bean method which creates an entity and persists it.
130      * @output A detached entity.
131      * @throws Exception if a problem occurs.
132      */

133     public void test03() throws Exception JavaDoc{
134         bean00.createCheckEntity00();
135
136         assertTrue(bean00.existsEntity(), "The bean must exists.");
137
138         bean00.checkDetached();
139     }
140
141     /**
142      * This test creates an entity, persists the entity and verifies if it becomes detached.
143      * In this test, the transaction is created by the container for each bean method invocation,
144      * as it is a transaction persistence context, it must becomes detached.
145      * @input Without providing a client transaction, invocation of a bean method which creates an entity and persists it.
146      * @output A detached entity.
147      * @throws Exception if a problem occurs.
148      */

149     public void test04() throws Exception JavaDoc{
150         bean00.createCheckEntity01();
151         bean00.checkDetached();
152     }
153
154     /**
155      * Cleans the test results.
156      * @throws Exception if a problem occurs
157      */

158     public void tearDown() throws Exception JavaDoc{
159         ItfPCtxLifetime00 beanRemove = getBeanRemoteInstance(SFSBPCtxLifeCMT00.class, ItfPCtxLifetime00.class);
160         beanRemove.initEntityManager();
161         beanRemove.removeEntity();
162     }
163 }
164
Popular Tags