KickJava   Java API By Example, From Geeks To Geeks.

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


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: BasePCtxLifetimeCM00.java 455 2006-05-16 07:29:27Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife;
26
27 import static org.testng.Assert.assertFalse;
28 import static org.testng.Assert.assertTrue;
29
30 import javax.ejb.TransactionAttribute JavaDoc;
31 import javax.ejb.TransactionAttributeType JavaDoc;
32 import javax.persistence.EntityManager;
33
34 import org.objectweb.easybeans.tests.common.ejbs.entity.ebstore.EBStore;
35
36 /**
37  * @author Eduardo Studzinski Estima de Castro
38  * @author Gisele Pinheiro Souza
39  */

40 public class BasePCtxLifetimeCM00 implements ItfPCtxLifetime00 {
41
42     /**
43      * Manager.
44      */

45     private EntityManager em;
46
47     /**
48      * Hashcode.
49      */

50     private int id = this.hashCode();
51
52     /**
53      * Entity.
54      */

55     private EBStore eb;
56
57     /**
58      * @see org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife.ItfPCtxLifetime00
59      * @param em instance
60      */

61     public void initEntityManager(final EntityManager em) {
62         this.em = em;
63     }
64
65     /**
66      * @see org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife.ItfPCtxLifetime00
67      * @param id entity id
68      */

69     public void setEntityID(final int id) {
70         this.id = id;
71     }
72
73     /**
74      * @see org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife.ItfPCtxLifetime00
75      */

76     @SuppressWarnings JavaDoc("boxing")
77     @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
78     public void createCheckEntity00() {
79         eb = new EBStore(id);
80         em.persist(eb);
81
82         assertTrue(em.contains(eb), "The entity instance should be managed.");
83
84         EBStore ebFind = em.find(EBStore.class, id);
85
86         assertTrue(ebFind == eb, "The entity instances should be equals, "
87                 + "they are in the same persistence context.");
88     }
89
90     /**
91      * @see org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife.ItfPCtxLifetime00
92      */

93     @SuppressWarnings JavaDoc("boxing")
94     @TransactionAttribute JavaDoc(TransactionAttributeType.NEVER)
95     public void createCheckEntity01() {
96         eb = new EBStore(id);
97         em.persist(eb);
98
99         assertFalse(em.contains(eb), "The entity instance should be detached.");
100     }
101
102     /**
103      * Checks if an entity is contained in the entity manager.
104      * @return true if exists
105      */

106     @SuppressWarnings JavaDoc("boxing")
107     @TransactionAttribute JavaDoc(TransactionAttributeType.MANDATORY)
108     public boolean containsEntity() {
109         return em.contains(eb);
110     }
111
112     /**
113      * Checks if an entity exists.
114      * @return true if exists
115      */

116     @SuppressWarnings JavaDoc("boxing")
117     @TransactionAttribute JavaDoc(TransactionAttributeType.NEVER)
118     public boolean existsEntity() {
119         return em.find(EBStore.class, id) != null;
120     }
121
122     /**
123      * @see org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife.ItfPCtxLifetime00
124      */

125     @SuppressWarnings JavaDoc("boxing")
126     @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
127     public void checkManaged() {
128         assertTrue(em.contains(eb), "The entity instance should be managed.");
129
130         EBStore ebFind = em.find(EBStore.class, id);
131
132         assertTrue(ebFind == eb, "The entity instances should be equals, "
133                 + "they are in the same persistence context.");
134     }
135
136     /**
137      * @see org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife.ItfPCtxLifetime00
138      */

139     @SuppressWarnings JavaDoc("boxing")
140     @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
141     public void checkDetached() {
142         assertFalse(em.contains(eb), "The entity instance should be detached.");
143     }
144
145     /**
146      * @see org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife.ItfPCtxLifetime00
147      */

148     @SuppressWarnings JavaDoc("boxing")
149     @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRES_NEW)
150     public void removeEntity() {
151         EBStore ebFind = em.find(EBStore.class, id);
152         if (ebFind != null) {
153             em.remove(ebFind);
154         }
155     }
156
157     /**
158      * @see org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife.ItfPCtxLifetime00
159      */

160     public void initEntityManager() {
161         //Must be overrided.
162
}
163
164     /**
165      * @see org.objectweb.easybeans.tests.common.ejbs.base.persistencectxlife.ItfPCtxLifetime00
166      */

167     public void persistEntity() {
168         em.persist(eb);
169     }
170 }
171
Popular Tags