KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateful > containermanaged > flushoperation > SFSBEntityManagerFlushTester


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: SFSBEntityManagerFlushTester.java 460 2006-05-17 09:04:35Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.flushoperation;
26
27 import static org.testng.Assert.assertEquals;
28
29 import javax.ejb.Remote JavaDoc;
30 import javax.ejb.Stateful JavaDoc;
31 import javax.ejb.TransactionAttribute JavaDoc;
32 import javax.ejb.TransactionAttributeType JavaDoc;
33 import javax.persistence.EntityManager;
34 import javax.persistence.FlushModeType;
35 import javax.persistence.PersistenceContext;
36
37 import org.objectweb.easybeans.tests.common.ejbs.entity.ebstore.EBStore;
38
39 /**
40  * Verifies if the persistence context is making correctly the flush method.
41  * There are two ways to make the flush: auto and commit.
42  * @author Gisele Pinheiro Souza
43  * @author Eduardo Studzinski Estima de Castro
44  */

45 @Stateful JavaDoc
46 @Remote JavaDoc(ItfEntityManagerFlushTester.class)
47 public class SFSBEntityManagerFlushTester implements ItfEntityManagerFlushTester {
48
49     /**
50      * The persistence context used during the test.
51      */

52     @PersistenceContext
53     private EntityManager entityManager;
54
55     /**
56      * Removes the entity from the database.
57      *
58      */

59     @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
60     private void removeEntity() {
61         EBStore ebstore = entityManager.find(EBStore.class, ENTITY_ID);
62         if (ebstore != null) {
63             entityManager.remove(ebstore);
64         }
65     }
66
67     /**
68      * Removes the entity in the database avoiding an insertion error and
69      * inserts a new entity to make the tests.
70      */

71     public void startup() {
72         removeEntity();
73         EBStore ebstore = new EBStore();
74         ebstore.setId(ENTITY_ID.intValue());
75         ebstore.setName(ENTITY_NAME);
76         entityManager.persist(ebstore);
77     }
78
79     /**
80      * Verifies if the flush mode value is auto.
81      */

82     public void verifyDefaultFlushMode() {
83         assertEquals(entityManager.getFlushMode(), FlushModeType.AUTO,
84                 "The flush mode is not beginning with the default value");
85     }
86
87     /**
88      * Sets the flush mode to AUTO and verifies if the container makes a flush when a query method is called.
89      */

90     @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
91     public void setFlushModeAuto() {
92         entityManager.setFlushMode(FlushModeType.AUTO);
93         assertEquals(entityManager.getFlushMode(), FlushModeType.AUTO,
94         "The flush mode was not set.");
95
96         EBStore ebstoreBeforeChange = entityManager.find(EBStore.class, ENTITY_ID);
97         ebstoreBeforeChange.setName(ENTITY_NAME_2);
98         // forces a flush
99
entityManager.createQuery("SELECT e FROM EBStore e");
100         // verifies if the flush was made
101
EBStore ebstoreAfterChange = entityManager.find(EBStore.class, ENTITY_ID);
102         assertEquals(ebstoreAfterChange.getName(), ENTITY_NAME_2, "The container did not make a flush after the query");
103     }
104
105     /**
106      * Sets the flush mode to COMMIT and verifies if the container does not make
107      * a flush when a query method is called.The flush must be called only in
108      * the commit.
109      */

110     @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
111     public void setFlushModeCommit() {
112         entityManager.setFlushMode(FlushModeType.COMMIT);
113         assertEquals(entityManager.getFlushMode(), FlushModeType.COMMIT,
114         "The flush mode was not set.");
115
116         EBStore ebstoreBeforeChange = entityManager.find(EBStore.class, ENTITY_ID);
117         ebstoreBeforeChange.setName(ENTITY_NAME_2);
118         // forces a flush
119
entityManager.createQuery("SELECT e FROM EBStore e");
120         // verifies if the flush was not made
121
EBStore ebstoreAfterChange = entityManager.find(EBStore.class, ENTITY_ID);
122         assertEquals(ebstoreAfterChange.getName(), ENTITY_NAME, "The container made a flush after the query");
123     }
124
125 }
126
Popular Tags