KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateful > containermanaged > resourceleveltrans > SFSBResourceLevelTransTester


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: SFSBResourceLevelTransTester.java 465 2006-05-18 14:50:37Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.resourceleveltrans;
26
27 import static org.testng.Assert.assertFalse;
28 import static org.testng.Assert.assertNotNull;
29 import static org.testng.Assert.assertNull;
30 import static org.testng.Assert.assertTrue;
31 import static org.testng.Assert.fail;
32
33 import javax.ejb.Remote JavaDoc;
34 import javax.ejb.Stateful JavaDoc;
35 import javax.ejb.TransactionAttribute JavaDoc;
36 import javax.ejb.TransactionAttributeType JavaDoc;
37 import javax.persistence.EntityManager;
38 import javax.persistence.EntityManagerFactory;
39 import javax.persistence.PersistenceUnit;
40 import javax.persistence.RollbackException;
41
42 import org.objectweb.easybeans.log.JLog;
43 import org.objectweb.easybeans.tests.common.ejbs.entity.book.Book;
44
45 /**
46  * Verifies if the bean can use a resource-level transaction.
47  * @author Gisele Pinheiro Souza
48  * @author Eduardo Studzinski Estima de Castro
49  */

50 @Stateful JavaDoc
51 @Remote JavaDoc(ItfResourceLevelTransTester.class)
52 @TransactionAttribute JavaDoc(TransactionAttributeType.NOT_SUPPORTED)
53 public class SFSBResourceLevelTransTester implements ItfResourceLevelTransTester {
54
55     /**
56      * The entity ID.
57      */

58     public static final int ID = 1;
59
60     /**
61      * The entity name.
62      */

63     public static final String JavaDoc ENTITY_NAME = "test";
64
65     /**
66      * The EntityManagerFactory that is used during test execution.
67      */

68     @PersistenceUnit
69     private EntityManagerFactory entityManagerFactory;
70
71     /**
72      * The logger.
73      */

74     private JLog logger;
75
76     /**
77      * Cleans the db.
78      */

79     public void startup() {
80         EntityManager entityManager = entityManagerFactory.createEntityManager();
81         Book bookResult = entityManager.find(Book.class, new Integer JavaDoc(ID));
82         if (bookResult != null) {
83             entityManager.remove(bookResult);
84         }
85     }
86
87     /**
88      * Verifies if the rollback works.
89      */

90     public void resourceLevelRollback() {
91         EntityManager entityManager = entityManagerFactory.createEntityManager();
92         // begins the transaction
93
entityManager.getTransaction().begin();
94         // creates a book
95
Book book = new Book();
96         book.setId(ID);
97         book.setName(ENTITY_NAME);
98         entityManager.persist(book);
99         // makes a rollback
100
entityManager.getTransaction().rollback();
101         entityManager.close();
102         // verifies if the container made the rollback
103
Book bookResult = entityManager.find(Book.class, new Integer JavaDoc(ID));
104         assertNull(bookResult, "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.createEntityManager();
112         // begins the transaction and sets the rollbackonly
113
entityManager.getTransaction().begin();
114         entityManager.getTransaction().setRollbackOnly();
115         // creates a book
116
Book book = new Book();
117         book.setId(ID);
118         book.setName(ENTITY_NAME);
119         entityManager.persist(book);
120         // verifies if the container registered the rollback.
121
assertTrue(entityManager.getTransaction().getRollbackOnly(),
122                 "The transaction is marked as rollback, but the container returned false for the method getRollbackOnly()");
123
124         // tries to make the commit, the container must throw an exception.
125
try {
126             entityManager.getTransaction().commit();
127             fail("The method setRollbackOnly was called, so the transaction cannot make the commit.");
128         } catch (RollbackException e) {
129             logger.info("The bean threw an expected exception");
130         }
131         entityManager.close();
132
133         // verifies if the transaction was rolled back
134
Book bookResult = entityManager.find(Book.class, new Integer JavaDoc(ID));
135         assertNull(bookResult, "The container did not make the rollback");
136     }
137
138     /**
139      * Verifies if the commit works.
140      */

141     public void resourceLevelCommit() {
142         EntityManager entityManager = entityManagerFactory.createEntityManager();
143         // begins the trasnaction
144
entityManager.getTransaction().begin();
145         // inserts a book
146
Book book = new Book();
147         book.setId(ID);
148         book.setName(ENTITY_NAME);
149         entityManager.persist(book);
150         // verifies if the transaction is marked as ACTIVE
151
assertTrue(entityManager.getTransaction().isActive(),
152                 "The transaction is active, but the container returned false when the method isActive was called.");
153         // verify if the rollbackonly is not set.
154
assertFalse(entityManager.getTransaction().getRollbackOnly(),
155                 "The transaction is not marked as rollback, but the container returned true for the method getRollbackOnly()");
156         // makes a commit
157
entityManager.getTransaction().commit();
158         entityManager.close();
159         // verifies if the bean is persistent.
160
Book bookResult = entityManager.find(Book.class, new Integer JavaDoc(ID));
161         assertNotNull(bookResult, "The container did not make the rollback");
162
163     }
164 }
165
Popular Tags