001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: SFSBEntityManagerFlushTester.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.ejbs.stateful.containermanaged.flushoperation;
025:
026: import static org.testng.Assert.assertEquals;
027:
028: import javax.ejb.Remote;
029: import javax.ejb.Stateful;
030: import javax.ejb.TransactionAttribute;
031: import javax.ejb.TransactionAttributeType;
032: import javax.persistence.EntityManager;
033: import javax.persistence.FlushModeType;
034: import javax.persistence.PersistenceContext;
035:
036: import org.ow2.easybeans.tests.common.ejbs.entity.ebstore.EBStore;
037:
038: /**
039: * Verifies if the persistence context is making correctly the flush method.
040: * There are two ways to make the flush: auto and commit.
041: * @author Gisele Pinheiro Souza
042: * @author Eduardo Studzinski Estima de Castro
043: */
044: @Stateful
045: @Remote(ItfEntityManagerFlushTester.class)
046: public class SFSBEntityManagerFlushTester implements
047: ItfEntityManagerFlushTester {
048:
049: /**
050: * The persistence context used during the test.
051: */
052: @PersistenceContext
053: private EntityManager entityManager;
054:
055: /**
056: * Removes the entity from the database.
057: *
058: */
059: @TransactionAttribute(TransactionAttributeType.REQUIRED)
060: private void removeEntity() {
061: EBStore ebstore = entityManager.find(EBStore.class, ENTITY_ID);
062: if (ebstore != null) {
063: entityManager.remove(ebstore);
064: }
065: }
066:
067: /**
068: * Removes the entity in the database avoiding an insertion error and
069: * inserts a new entity to make the tests.
070: */
071: public void startup() {
072: removeEntity();
073: EBStore ebstore = new EBStore();
074: ebstore.setId(ENTITY_ID.intValue());
075: ebstore.setName(ENTITY_NAME);
076: entityManager.persist(ebstore);
077: }
078:
079: /**
080: * Verifies if the flush mode value is auto.
081: */
082: public void verifyDefaultFlushMode() {
083: assertEquals(entityManager.getFlushMode(), FlushModeType.AUTO,
084: "The flush mode is not beginning with the default value");
085: }
086:
087: /**
088: * Sets the flush mode to AUTO and verifies if the container makes a flush when a query method is called.
089: */
090: @TransactionAttribute(TransactionAttributeType.REQUIRED)
091: public void setFlushModeAuto() {
092: entityManager.setFlushMode(FlushModeType.AUTO);
093: assertEquals(entityManager.getFlushMode(), FlushModeType.AUTO,
094: "The flush mode was not set.");
095:
096: EBStore ebstoreBeforeChange = entityManager.find(EBStore.class,
097: ENTITY_ID);
098: ebstoreBeforeChange.setName(ENTITY_NAME_2);
099: // forces a flush
100: entityManager.createQuery("SELECT e FROM EBStore e");
101: // verifies if the flush was made
102: EBStore ebstoreAfterChange = entityManager.find(EBStore.class,
103: ENTITY_ID);
104: assertEquals(ebstoreAfterChange.getName(), ENTITY_NAME_2,
105: "The container did not make a flush after the query");
106: }
107:
108: /**
109: * Sets the flush mode to COMMIT and verifies if the container does not make
110: * a flush when a query method is called.The flush must be called only in
111: * the commit.
112: */
113: @TransactionAttribute(TransactionAttributeType.REQUIRED)
114: public void setFlushModeCommit() {
115: entityManager.setFlushMode(FlushModeType.COMMIT);
116: assertEquals(entityManager.getFlushMode(),
117: FlushModeType.COMMIT, "The flush mode was not set.");
118:
119: EBStore ebstoreBeforeChange = entityManager.find(EBStore.class,
120: ENTITY_ID);
121: ebstoreBeforeChange.setName(ENTITY_NAME_2);
122: // forces a flush
123: entityManager.createQuery("SELECT e FROM EBStore e");
124: // verifies if the flush was not made
125: EBStore ebstoreAfterChange = entityManager.find(EBStore.class,
126: ENTITY_ID);
127: assertEquals(ebstoreAfterChange.getName(), ENTITY_NAME,
128: "The container made a flush after the query");
129: }
130:
131: }
|