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: ResourceLevelTransTester.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.javasesupport;
025:
026: import static org.testng.Assert.assertFalse;
027: import static org.testng.Assert.assertNotNull;
028: import static org.testng.Assert.assertNull;
029: import static org.testng.Assert.assertTrue;
030: import static org.testng.Assert.fail;
031:
032: import javax.persistence.EntityManager;
033: import javax.persistence.EntityManagerFactory;
034: import javax.persistence.Persistence;
035: import javax.persistence.RollbackException;
036:
037: import org.ow2.easybeans.tests.common.ejbs.entity.book.Book;
038: import org.ow2.util.log.Log;
039:
040: /**
041: * Verifies if the container supports clients j2ee.
042: * @author Gisele Pinheiro Souza
043: * @author Eduardo Studzinski Estima de Castro
044: */
045: public class ResourceLevelTransTester {
046:
047: /**
048: * The entity ID.
049: */
050: public static final int ID = 1;
051:
052: /**
053: * The entity name.
054: */
055: public static final String ENTITY_NAME = "test";
056:
057: /**
058: * The EntityManagerFactory that is used during test execution.
059: */
060: private EntityManagerFactory entityManagerFactory;
061:
062: /**
063: * The logger.
064: */
065: private Log logger;
066:
067: /**
068: * Creates the EntityManagerFactory and cleans the db.
069: */
070: public ResourceLevelTransTester() {
071: // gets the EntitymanagerFactory for the persistence unite book
072: entityManagerFactory = Persistence
073: .createEntityManagerFactory("book");
074: EntityManager entityManager = entityManagerFactory
075: .createEntityManager();
076:
077: Book bookResult = entityManager.find(Book.class,
078: new Integer(ID));
079: if (bookResult != null) {
080: entityManager.remove(bookResult);
081: }
082: }
083:
084: /**
085: * Verifies if the rollback works.
086: */
087: public void resourceLevelRollback() {
088: EntityManager entityManager = entityManagerFactory
089: .createEntityManager();
090: // begins the transaction
091: entityManager.getTransaction().begin();
092: // creates a book
093: Book book = new Book();
094: book.setId(ID);
095: book.setName(ENTITY_NAME);
096: entityManager.persist(book);
097: // makes a rollback
098: entityManager.getTransaction().rollback();
099: entityManager.close();
100: // verifies if the container made the rollback
101: Book bookResult = entityManager.find(Book.class,
102: new Integer(ID));
103: assertNull(bookResult,
104: "The container did not make the rollback");
105: }
106:
107: /**
108: * Verifies if the serRollbackOnly works.
109: */
110: public void setRollbackOnly() {
111: EntityManager entityManager = entityManagerFactory
112: .createEntityManager();
113: // begins the transaction and sets the rollbackonly
114: entityManager.getTransaction().begin();
115: entityManager.getTransaction().setRollbackOnly();
116: // creates a book
117: Book book = new Book();
118: book.setId(ID);
119: book.setName(ENTITY_NAME);
120: entityManager.persist(book);
121: // verifies if the container registered the rollback.
122: assertTrue(
123: entityManager.getTransaction().getRollbackOnly(),
124: "The transaction is marked as rollback, but the container returned false for the method getRollbackOnly()");
125:
126: // tries to make the commit, the container must throw an exception.
127: try {
128: entityManager.getTransaction().commit();
129: fail("The method setRollbackOnly was called, so the transaction cannot make the commit.");
130: } catch (RollbackException e) {
131: logger.info("The bean threw an expected exception");
132: }
133: entityManager.close();
134:
135: // verifies if the transaction was rolled back
136: Book bookResult = entityManager.find(Book.class,
137: new Integer(ID));
138: assertNull(bookResult,
139: "The container did not make the rollback");
140: }
141:
142: /**
143: * Verifies if the commit works.
144: */
145: public void resourceLevelCommit() {
146: EntityManager entityManager = entityManagerFactory
147: .createEntityManager();
148: // begins the trasnaction
149: entityManager.getTransaction().begin();
150: // inserts a book
151: Book book = new Book();
152: book.setId(ID);
153: book.setName(ENTITY_NAME);
154: entityManager.persist(book);
155: // verifies if the transaction is marked as ACTIVE
156: assertTrue(
157: entityManager.getTransaction().isActive(),
158: "The transaction is active, but the container returned false when the method isActive was called.");
159: // verify if the rollbackonly is not set.
160: assertFalse(
161: entityManager.getTransaction().getRollbackOnly(),
162: "The transaction is not marked as rollback, but the container returned true for the method getRollbackOnly()");
163: // makes a commit
164: entityManager.getTransaction().commit();
165: entityManager.close();
166: // verifies if the bean is persistent.
167: Book bookResult = entityManager.find(Book.class,
168: new Integer(ID));
169: assertNotNull(bookResult,
170: "The container did not make the rollback");
171:
172: }
173: }
|