KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateless > containermanaged > entitymanager > SLSBEntityManagerTester00


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:$
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.entitymanager;
26
27 import javax.ejb.Remote JavaDoc;
28 import javax.ejb.Stateless JavaDoc;
29 import javax.persistence.EntityManager;
30 import javax.persistence.PersistenceContext;
31 import javax.persistence.PersistenceContextType;
32
33 import org.objectweb.easybeans.tests.common.ejbs.entity.ebstore.EBStore;
34
35 /**
36  * Tests if the EntityManager is creating and deleting correctly the entity
37  * beans when a persistence context transaction is used.
38  * @author Gisele Pinheiro Souza
39  * @author Eduardo Studzinski Estima de Castro
40  */

41 @Stateless JavaDoc
42 @Remote JavaDoc(ItfEntityManagerTester00.class)
43 public class SLSBEntityManagerTester00 implements ItfEntityManagerTester00 {
44
45     /**
46      * The persistence context used during the test.
47      */

48     @PersistenceContext(type = PersistenceContextType.TRANSACTION)
49     private EntityManager em;
50
51     /**
52      * Completes the entity with the values.
53      * @param id the entity primary key.
54      * @param name the entity name.
55      * @return the ebstore completed.
56      */

57     private EBStore completeEBStore(final int id, final String JavaDoc name) {
58         EBStore ebstore = new EBStore();
59         ebstore.setId(id);
60         ebstore.setName(name);
61         return ebstore;
62     }
63
64     /**
65      * Finds the entity by primaryKey.
66      * @param id the entity primary key.
67      * @return the entity.
68      */

69     public EBStore findEBStore(final int id) {
70         return em.find(EBStore.class, new Integer JavaDoc(id));
71     }
72
73     /**
74      * Removes the entity that is in the database.
75      * @param id the entity primary key.
76      */

77     public void removeEBStore(final int id) {
78         EBStore ebstore = findEBStore(id);
79         if(ebstore != null){
80             em.remove(ebstore);
81         }
82     }
83
84     /**
85      * Creates an entity beans.
86      * @param id the primary key.
87      * @param name the entity name.
88      */

89     public void createEBStoreNew(final int id, final String JavaDoc name) {
90         em.persist(completeEBStore(id, name));
91     }
92
93     /**
94      * Tries to persist the same bean twice. In the first persist, the entity
95      * status is new, in the second persist, the bean status is managed.
96      * @param id the primary key.
97      * @param name the entity name.
98      */

99     public void createEBStoreManaged(final int id, final String JavaDoc name) {
100         EBStore ebstore = completeEBStore(id, name);
101         em.persist(ebstore);
102         em.persist(ebstore);
103     }
104
105     /**
106      * Creates the entity, removes it and after tries to insert other time the
107      * removed entity.
108      * @param id the primary key.
109      * @param name the entity name.
110      */

111     public void createEBStoreRemoved(final int id, final String JavaDoc name) {
112         EBStore ebstore = completeEBStore(id, name);
113         em.persist(ebstore);
114         em.remove(ebstore);
115         em.persist(ebstore);
116     }
117
118     /**
119      * Creates the entity and tries to remove twice the same entity.
120      * @param id the primary key.
121      * @param name the entity name.
122      */

123     public void removeEBStoreRemoved(final int id, final String JavaDoc name) {
124         EBStore ebstore = completeEBStore(id, name);
125         em.persist(ebstore);
126         em.remove(ebstore);
127         em.remove(ebstore);
128     }
129
130     /**
131      * Creates the entity and , after that, tries remove the same entity.
132      * @param id the primary key.
133      * @param name the entity name.
134      */

135     public void removeEBStoreManaged(final int id, final String JavaDoc name) {
136         EBStore ebstore = completeEBStore(id, name);
137         em.persist(ebstore);
138         em.remove(ebstore);
139     }
140
141     /**
142      * Tries remove the entity that is not persistent yet.
143      * @param id the primary key.
144      * @param name the entity name.
145      */

146     public void removeEBStoreNew(final int id, final String JavaDoc name) {
147         EBStore ebstore = completeEBStore(id, name);
148         em.remove(ebstore);
149     }
150 }
151
Popular Tags