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: SFSBPersistenceContextTester.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.ejbs.stateful.containermanaged.persistencectx;
025:
026: import static org.testng.Assert.assertFalse;
027: import static org.testng.Assert.assertTrue;
028:
029: import javax.ejb.Remote;
030: import javax.ejb.Stateful;
031: import javax.ejb.TransactionAttribute;
032: import javax.ejb.TransactionAttributeType;
033: import javax.persistence.EntityManager;
034: import javax.persistence.PersistenceContext;
035: import javax.persistence.PersistenceContextType;
036:
037: import org.ow2.easybeans.tests.common.ejbs.entity.ebstore.EBStore;
038:
039: /**
040: * Used to test the reference in the persistence context.
041: * @author Gisele Pinheiro Souza
042: * @author Eduardo Studzinski Estima de Castro
043: */
044: @Stateful
045: @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
046: @Remote(ItfPersistenceContextTester.class)
047: public class SFSBPersistenceContextTester implements
048: ItfPersistenceContextTester {
049:
050: /**
051: * EntityManager with the persistence context type = transaction.
052: */
053: @PersistenceContext(type=PersistenceContextType.TRANSACTION)
054: private EntityManager entityManagerTransaction;
055:
056: /**
057: * EntityManager with the persistence context type = extended.
058: */
059: @PersistenceContext(type=PersistenceContextType.EXTENDED)
060: private EntityManager entityManagerExtended;
061:
062: /**
063: * Bean that is created in a transaction-scoped persistence context.
064: */
065: private EBStore ebstoreTransaction;
066:
067: /**
068: * Bean that is created in a extended persistence context.
069: */
070: private EBStore ebstoreExtended;
071:
072: /**
073: * Completes a EBStore entity with the values in the parameters.
074: * @param id the primary key.
075: * @param name the entity name.
076: * @return a new entity with the values.
077: */
078: private EBStore createBean(final Integer id, final String name) {
079: EBStore ebstore = new EBStore();
080: ebstore.setId(id.intValue());
081: ebstore.setName(name);
082: return ebstore;
083: }
084:
085: /**
086: * Removes the beans of the database.
087: *
088: */
089: private void removeEBStore() {
090: EBStore ebstoreTransaction = entityManagerTransaction.find(
091: EBStore.class, ID_TRANSACTION);
092: if (ebstoreTransaction != null) {
093: entityManagerTransaction.remove(ebstoreTransaction);
094: }
095:
096: EBStore ebstoreExtended = entityManagerExtended.find(
097: EBStore.class, ID_EXTENDED);
098: if (ebstoreExtended != null) {
099: entityManagerExtended.remove(ebstoreExtended);
100: }
101:
102: }
103:
104: /**
105: * Creates a entity bean using transaction-scoped persistence context and a
106: * bean with extended persistence context.
107: */
108: public void startup() {
109: //clean the db
110: removeEBStore();
111: // creates an entity in the transaction-scoped persistence context
112: ebstoreTransaction = createBean(ID_TRANSACTION,
113: NAME_TRANSACTION);
114: entityManagerTransaction.persist(ebstoreTransaction);
115: ebstoreTransaction = entityManagerTransaction.find(
116: EBStore.class, ID_TRANSACTION);
117:
118: // creates an entity in the extended persistence context
119: ebstoreExtended = createBean(ID_EXTENDED, NAME_EXTENDED);
120: entityManagerExtended.persist(ebstoreExtended);
121: ebstoreExtended = entityManagerExtended.find(EBStore.class,
122: ID_EXTENDED);
123: }
124:
125: /**
126: * Verifies if the bean created in transaction-scoped persistence context in
127: * the startup method, and, a bean that was goten using the find method have
128: * different reference.
129: */
130: public void testTransactionPersistenceContext() {
131: EBStore ebstoreResult = entityManagerTransaction.find(
132: EBStore.class, ID_TRANSACTION);
133: assertFalse(
134: ebstoreResult == ebstoreTransaction,
135: "The entities were goten in diferents persistence context, so they should not have the same reference.");
136: }
137:
138: /**
139: * Verifies if the bean created in extended persistence context in the
140: * startup method, and, a bean that was goten using the find method have the
141: * same reference.
142: */
143: public void testExtendedPersistenceContext() {
144: EBStore ebstoreResult = entityManagerExtended.find(
145: EBStore.class, ID_EXTENDED);
146: assertTrue(
147: ebstoreResult == ebstoreExtended,
148: "The entities were goten in the same persistence context, so they should have the same reference.");
149: }
150:
151: }
|