KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateful > containermanaged > persistencectx > SFSBPersistenceContextTester


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: SFSBPersistenceContextTester.java 822 2006-07-04 14:35:35Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.persistencectx;
26
27 import static org.testng.Assert.assertFalse;
28 import static org.testng.Assert.assertTrue;
29
30 import javax.ejb.Remote JavaDoc;
31 import javax.ejb.Stateful JavaDoc;
32 import javax.ejb.TransactionAttribute JavaDoc;
33 import javax.ejb.TransactionAttributeType JavaDoc;
34 import javax.persistence.EntityManager;
35 import javax.persistence.PersistenceContext;
36 import javax.persistence.PersistenceContextType;
37
38 import org.objectweb.easybeans.tests.common.ejbs.entity.ebstore.EBStore;
39
40 /**
41  * Used to test the reference in the persistence context.
42  * @author Gisele Pinheiro Souza
43  * @author Eduardo Studzinski Estima de Castro
44  */

45 @Stateful JavaDoc
46 @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRES_NEW)
47 @Remote JavaDoc(ItfPersistenceContextTester.class)
48 public class SFSBPersistenceContextTester implements ItfPersistenceContextTester {
49
50     /**
51      * EntityManager with the persistence context type = transaction.
52      */

53     @PersistenceContext(type = PersistenceContextType.TRANSACTION)
54     private EntityManager entityManagerTransaction;
55
56     /**
57      * EntityManager with the persistence context type = extended.
58      */

59     @PersistenceContext(type = PersistenceContextType.EXTENDED)
60     private EntityManager entityManagerExtended;
61
62     /**
63      * Bean that is created in a transaction-scoped persistence context.
64      */

65     private EBStore ebstoreTransaction;
66
67     /**
68      * Bean that is created in a extended persistence context.
69      */

70     private EBStore ebstoreExtended;
71
72     /**
73      * Completes a EBStore entity with the values in the parameters.
74      * @param id the primary key.
75      * @param name the entity name.
76      * @return a new entity with the values.
77      */

78     private EBStore createBean(final Integer JavaDoc id, final String JavaDoc name) {
79         EBStore ebstore = new EBStore();
80         ebstore.setId(id.intValue());
81         ebstore.setName(name);
82         return ebstore;
83     }
84
85     /**
86      * Removes the beans of the database.
87      *
88      */

89     private void removeEBStore(){
90         EBStore ebstoreTransaction = entityManagerTransaction.find(EBStore.class, ID_TRANSACTION);
91         if(ebstoreTransaction != null){
92             entityManagerTransaction.remove(ebstoreTransaction);
93         }
94
95         EBStore ebstoreExtended = entityManagerExtended.find(EBStore.class, ID_EXTENDED);
96         if(ebstoreExtended != null){
97             entityManagerExtended.remove(ebstoreExtended);
98         }
99
100     }
101
102     /**
103      * Creates a entity bean using transaction-scoped persistence context and a
104      * bean with extended persistence context.
105      */

106     public void startup() {
107         //clean the db
108
removeEBStore();
109         // creates an entity in the transaction-scoped persistence context
110
ebstoreTransaction = createBean(ID_TRANSACTION, NAME_TRANSACTION);
111         entityManagerTransaction.persist(ebstoreTransaction);
112         ebstoreTransaction = entityManagerTransaction.find(EBStore.class, ID_TRANSACTION);
113
114         // creates an entity in the extended persistence context
115
ebstoreExtended = createBean(ID_EXTENDED, NAME_EXTENDED);
116         entityManagerExtended.persist(ebstoreExtended);
117         ebstoreExtended = entityManagerExtended.find(EBStore.class, ID_EXTENDED);
118     }
119
120     /**
121      * Verifies if the bean created in transaction-scoped persistence context in
122      * the startup method, and, a bean that was goten using the find method have
123      * different reference.
124      */

125     public void testTransactionPersistenceContext() {
126         EBStore ebstoreResult = entityManagerTransaction.find(EBStore.class, ID_TRANSACTION);
127         assertFalse(ebstoreResult == ebstoreTransaction,
128                 "The entities were goten in diferents persistence context, so they should not have the same reference.");
129     }
130
131     /**
132      * Verifies if the bean created in extended persistence context in the
133      * startup method, and, a bean that was goten using the find method have the
134      * same reference.
135      */

136     public void testExtendedPersistenceContext() {
137         EBStore ebstoreResult = entityManagerExtended.find(EBStore.class, ID_EXTENDED);
138         assertTrue(ebstoreResult == ebstoreExtended,
139                 "The entities were goten in the same persistence context, so they should have the same reference.");
140     }
141
142 }
143
Popular Tags