KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > javasesupport > ResourceLevelTransTester


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: ResourceLevelTransTester.java 505 2006-05-24 14:28:38Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.javasesupport;
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.persistence.EntityManager;
34 import javax.persistence.EntityManagerFactory;
35 import javax.persistence.Persistence;
36 import javax.persistence.RollbackException;
37
38 import org.objectweb.easybeans.log.JLog;
39 import org.objectweb.easybeans.tests.common.ejbs.entity.book.Book;
40
41 /**
42  * Verifies if the container supports clients j2ee.
43  * @author Gisele Pinheiro Souza
44  * @author Eduardo Studzinski Estima de Castro
45  */

46 public class ResourceLevelTransTester {
47
48     /**
49      * The entity ID.
50      */

51     public static final int ID = 1;
52
53     /**
54      * The entity name.
55      */

56     public static final String JavaDoc ENTITY_NAME = "test";
57
58     /**
59      * The EntityManagerFactory that is used during test execution.
60      */

61     private EntityManagerFactory entityManagerFactory;
62
63     /**
64      * The logger.
65      */

66     private JLog logger;
67
68     /**
69      * Creates the EntityManagerFactory and cleans the db.
70      */

71     public ResourceLevelTransTester() {
72         // gets the EntitymanagerFactory for the persistence unite book
73
entityManagerFactory = Persistence.createEntityManagerFactory("book");
74         EntityManager entityManager = entityManagerFactory.createEntityManager();
75
76         Book bookResult = entityManager.find(Book.class, new Integer JavaDoc(ID));
77         if (bookResult != null) {
78             entityManager.remove(bookResult);
79         }
80     }
81
82     /**
83      * Verifies if the rollback works.
84      */

85     public void resourceLevelRollback() {
86         EntityManager entityManager = entityManagerFactory.createEntityManager();
87         // begins the transaction
88
entityManager.getTransaction().begin();
89         // creates a book
90
Book book = new Book();
91         book.setId(ID);
92         book.setName(ENTITY_NAME);
93         entityManager.persist(book);
94         // makes a rollback
95
entityManager.getTransaction().rollback();
96         entityManager.close();
97         // verifies if the container made the rollback
98
Book bookResult = entityManager.find(Book.class, new Integer JavaDoc(ID));
99         assertNull(bookResult, "The container did not make the rollback");
100     }
101
102     /**
103      * Verifies if the serRollbackOnly works.
104      */

105     public void setRollbackOnly() {
106         EntityManager entityManager = entityManagerFactory.createEntityManager();
107         // begins the transaction and sets the rollbackonly
108
entityManager.getTransaction().begin();
109         entityManager.getTransaction().setRollbackOnly();
110         // creates a book
111
Book book = new Book();
112         book.setId(ID);
113         book.setName(ENTITY_NAME);
114         entityManager.persist(book);
115         // verifies if the container registered the rollback.
116
assertTrue(entityManager.getTransaction().getRollbackOnly(),
117                 "The transaction is marked as rollback, but the container returned false for the method getRollbackOnly()");
118
119         // tries to make the commit, the container must throw an exception.
120
try {
121             entityManager.getTransaction().commit();
122             fail("The method setRollbackOnly was called, so the transaction cannot make the commit.");
123         } catch (RollbackException e) {
124             logger.info("The bean threw an expected exception");
125         }
126         entityManager.close();
127
128         // verifies if the transaction was rolled back
129
Book bookResult = entityManager.find(Book.class, new Integer JavaDoc(ID));
130         assertNull(bookResult, "The container did not make the rollback");
131     }
132
133     /**
134      * Verifies if the commit works.
135      */

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