KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateful > containermanaged > cascadeoperation > SFSBCascadeTester


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: SFSBCascadeTester.java 822 2006-07-04 14:35:35Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.cascadeoperation;
26
27 import static org.testng.Assert.assertEquals;
28 import static org.testng.Assert.assertFalse;
29 import static org.testng.Assert.assertTrue;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33
34 import javax.ejb.Remote JavaDoc;
35 import javax.ejb.Stateful JavaDoc;
36 import javax.ejb.TransactionAttribute JavaDoc;
37 import javax.ejb.TransactionAttributeType JavaDoc;
38 import javax.persistence.EntityManager;
39 import javax.persistence.PersistenceContext;
40
41 import org.objectweb.easybeans.tests.common.ejbs.entity.customer.Address;
42 import org.objectweb.easybeans.tests.common.ejbs.entity.customer.Category;
43 import org.objectweb.easybeans.tests.common.ejbs.entity.customer.Customer;
44 import org.objectweb.easybeans.tests.common.ejbs.entity.customer.Product;
45 import org.objectweb.easybeans.tests.common.ejbs.entity.customer.ProductOrder;
46
47 /**
48  * Verifies if the container can manage the different types of cascade.
49  * @author Gisele Pinheiro Souza
50  * @author Eduardo Studzinski Estima de Castro
51  */

52 @Stateful JavaDoc
53 @Remote JavaDoc(ItfCascadeTester.class)
54 @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRES_NEW)
55 public class SFSBCascadeTester implements ItfCascadeTester {
56
57     /**
58      * EntityManager used to manage the entities.
59      */

60     @PersistenceContext
61     private EntityManager entityManager;
62
63     /**
64      * The beans primary key.
65      */

66     public static final long ID = 1;
67
68     /**
69      * The alternative primary key.
70      */

71     public static final long ALTERNATIVE_ID = 2;
72
73     /**
74      * The field price of the product.
75      */

76     public static final float PRODUCT_PRICE = 2.4f;
77
78     /**
79      * The field price of the product.
80      */

81     public static final float PRODUCT_PRICE_2 = 4.6f;
82
83     /**
84      * Removes a bean from the persistence context.
85      * @param <T> the entity type.
86      * @param entityClass the entity class.
87      */

88     private <T> void removeBean(final Class JavaDoc<T> entityClass) {
89         T objBean;
90         objBean = entityManager.find(entityClass, new Long JavaDoc(ID));
91         if (objBean != null) {
92             entityManager.remove(objBean);
93         }
94     }
95
96     /**
97      * Removes all entities used in this test.
98      *
99      */

100     private void removeAll() {
101         removeBean(Category.class);
102         removeBean(Product.class);
103         removeBean(ProductOrder.class);
104         removeBean(Customer.class);
105         removeBean(Address.class);
106     }
107
108     /**
109      * Creates all entities used during the test.
110      */

111     public void startup() {
112         removeAll();
113
114         //creates the category
115
Category category = new Category();
116         category.setId(ID);
117         category.setDescription("description");
118         entityManager.persist(category);
119
120         //creates the product
121
Product product = new Product(ID, "description", PRODUCT_PRICE, category);
122         entityManager.persist(product);
123
124         //creates the address
125
Address address = new Address(ID, "street", "country", 1);
126         entityManager.persist(address);
127
128         //creates the customer
129
Customer customer = new Customer();
130         customer.setId(ID);
131         customer.setAddress(address);
132         customer.setName("customer");
133         entityManager.persist(customer);
134
135         //creates the order
136
List JavaDoc<Product> products = new ArrayList JavaDoc<Product>();
137         products.add(product);
138         ProductOrder order = new ProductOrder(ID, "description", customer, products);
139         entityManager.persist(order);
140
141     }
142
143     /**
144      * Verifies if the container can make a refresh in cascade.
145      */

146     public void verifyCascadeTypeRefresh() {
147         Category category = entityManager.find(Category.class, new Long JavaDoc(ID));
148         category.setDescription("new description");
149
150         Product product = entityManager.find(Product.class, new Long JavaDoc(ID));
151         product.setPrice(PRODUCT_PRICE_2);
152
153         entityManager.refresh(product);
154
155         assertEquals(category.getDescription(), "description", "The container did not make the refresh in cascade.");
156     }
157
158     /**
159      * Verifies if the conatiner makes the remove in cascade.
160      */

161     public void verifyCascadeTypeRemove() {
162         ProductOrder order = entityManager.find(ProductOrder.class, new Long JavaDoc(ID));
163         assertFalse(order == null, "The container did not find the order");
164
165         entityManager.remove(order);
166         entityManager.flush();
167
168         Product productResult = entityManager.find(Product.class, new Long JavaDoc(ID));
169         assertTrue(productResult == null, "The container did not make the delete in cascade.");
170     }
171
172     /**
173      * Verifies if the container makes the merge in cascade.
174      */

175     public void verifyCascadeTypeMerge() {
176         ProductOrder order = entityManager.find(ProductOrder.class, new Long JavaDoc(ID));
177         Customer customer = entityManager.find(Customer.class, new Long JavaDoc(ID));
178
179         entityManager.clear();
180         order.setDescription("new description");
181         customer.setName("new customer");
182
183         entityManager.merge(customer);
184         entityManager.flush();
185
186         assertEquals(order.getDescription(), "new description", "The container did not make the merge in cascade");
187     }
188
189     /**
190      * Verifies if the container makes the persist in cascade.
191      */

192     public void verifyCascadeTypePersist() {
193         Customer customer = entityManager.find(Customer.class, new Long JavaDoc(ID));
194         Address addressNew = new Address(ALTERNATIVE_ID, "street", "country", 1);
195         customer.setAddress(addressNew);
196
197         entityManager.persist(customer);
198         entityManager.flush();
199
200         Address addressResultAfterPersist = entityManager.find(Address.class, new Long JavaDoc(2));
201         assertFalse(addressResultAfterPersist == null, "The container did not make a persist in cascade.");
202     }
203 }
204
Popular Tags