KickJava   Java API By Example, From Geeks To Geeks.

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


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: BasePctxLifeCMETester00.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  * Base class for container-managed extended-scoped persistence context tests.
38  * @author Eduardo Studzinski Estima de Castro
39  * @author Gisele Pinheiro Souza
40  *
41  */

42 public class BasePctxLifeCMETester00 {
43
44     /**
45      * Bean.
46      */

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

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

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

74     public void test00() throws Exception JavaDoc{
75         utx.begin();
76
77         bean00.createCheckEntity00();
78
79         utx.rollback();
80
81         assertFalse(bean00.existsEntity(), "The bean must not exists, the transaction was rolled back.");
82         bean00.checkDetached();
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 remains managed, because it is an extended 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 remains managed.
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 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 JavaDoc{
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 JavaDoc{
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 JavaDoc{
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 JavaDoc{
163         ItfPCtxLifetime00 beanRemove = getBeanRemoteInstance(SFSBPCtxLifeCMT00.class, ItfPCtxLifetime00.class);
164         beanRemove.initEntityManager();
165         beanRemove.removeEntity();
166     }
167
168 }
169
Popular Tags