KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

44 @Stateless JavaDoc
45 @Remote JavaDoc(ItfEntityManagerTester01.class)
46 public class SLSBEntityManagerTester01 implements ItfEntityManagerTester01 {
47
48     /**
49      * The persistence context used during the test.
50      */

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

60     private EBStore completeEBStore(final int id, final String JavaDoc name) {
61         EBStore ebstore = new EBStore();
62         ebstore.setId(id);
63         ebstore.setName(name);
64         return ebstore;
65     }
66
67     /**
68      * Creates a entity and makes it detached. After that, changes the entity
69      * detached and tries to make a merge.
70      * @param id the entity primary key.
71      * @param name the entity name.
72      * @param newName the name that the entity receives after to be detached.
73      */

74     public void mergeEBStore(final int id, final String JavaDoc name, final String JavaDoc newName) {
75         EBStore ebstore = completeEBStore(id, name);
76         em.persist(ebstore);
77         //makes the entity detached
78
em.clear();
79         //changes the entity name
80
ebstore.setName(newName);
81         EBStore ebstoreMerged = em.merge(ebstore);
82         assertEquals(newName, ebstoreMerged.getName(), "The entity was not merged.");
83     }
84
85     /**
86      * Test if the method contains verifies if the entity is in the db.
87      * @param id the entity primary key.
88      * @param name the entity name.
89      */

90     public void containsEBStore(final int id, final String JavaDoc name) {
91         EBStore ebstore = completeEBStore(id, name);
92         em.persist(ebstore);
93         assertTrue(em.contains(ebstore), "The contains methos does not work properly");
94     }
95
96     /**
97      * Verifies if the method refresh is working properly.
98      * @param id the entity primary key.
99      * @param name the entity name.
100      * @param newName the name that the entity receives after to be detached.
101      */

102     public void refreshEBStore(final int id, final String JavaDoc name, final String JavaDoc newName) {
103         EBStore ebstore = completeEBStore(id, name);
104         em.persist(ebstore);
105         //makes the entity detached
106
em.clear();
107         //changes the entity name
108
ebstore.setName(newName);
109         //makes a refresh
110
em.refresh(ebstore);
111         //verifies if the container got the values from the database.
112
assertEquals(ebstore.getName(), name, "The entity was not refreshed");
113     }
114
115     /**
116      * Removes the entity that is in the database.
117      * @param id the entity primary key.
118      */

119     public void removeEBStore(final int id) {
120         EBStore ebstore = em.find(EBStore.class, new Integer JavaDoc(id));
121         if(ebstore != null){
122             em.remove(ebstore);
123         }
124     }
125 }
126
Popular Tags