KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > commerce > ManyToManyBiTest


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.commerce;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import junit.framework.TestCase;
28 import net.sourceforge.junitejb.EJBTestCase;
29
30 public class ManyToManyBiTest extends EJBTestCase {
31
32    public ManyToManyBiTest(String JavaDoc name) {
33       super(name);
34    }
35
36    private ProductHome getProductHome() {
37       try {
38          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
39
40          return (ProductHome) jndiContext.lookup("commerce/Product");
41       } catch(Exception JavaDoc e) {
42          e.printStackTrace();
43          fail("Exception in getProduct: " + e.getMessage());
44       }
45       return null;
46    }
47
48    private ProductCategoryHome getProductCategoryHome() {
49       try {
50          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
51
52          return (ProductCategoryHome)
53                jndiContext.lookup("commerce/ProductCategory");
54       } catch(Exception JavaDoc e) {
55          e.printStackTrace();
56          fail("Exception in getProductCategory: " + e.getMessage());
57       }
58       return null;
59    }
60
61    private Product a1;
62    private Product a2;
63    private Product a3;
64    private Product a4;
65    private Product a5;
66
67    private ProductCategory b1;
68    private ProductCategory b2;
69    private ProductCategory b3;
70    private ProductCategory b4;
71    private ProductCategory b5;
72
73    public void setUpEJB() throws Exception JavaDoc {
74       ProductHome productHome = getProductHome();
75       ProductCategoryHome productCategoryHome = getProductCategoryHome();
76
77       // clean out the db
78
deleteAllProducts(productHome);
79       deleteAllProductCategories(productCategoryHome);
80
81       // setup the before change part of the test
82
beforeChange(productHome, productCategoryHome);
83    }
84
85    private void beforeChange(
86          ProductHome productHome,
87          ProductCategoryHome productCategoryHome) throws Exception JavaDoc {
88
89       // Before change:
90
a1 = productHome.create();
91       a2 = productHome.create();
92       a3 = productHome.create();
93       a4 = productHome.create();
94       a5 = productHome.create();
95
96       b1 = productCategoryHome.create();
97       b2 = productCategoryHome.create();
98       b3 = productCategoryHome.create();
99       b4 = productCategoryHome.create();
100       b5 = productCategoryHome.create();
101
102       a1.getProductCategories().add(b1);
103       a1.getProductCategories().add(b2);
104       a2.getProductCategories().add(b1);
105       a2.getProductCategories().add(b2);
106       a2.getProductCategories().add(b3);
107       a3.getProductCategories().add(b2);
108       a3.getProductCategories().add(b3);
109       a3.getProductCategories().add(b4);
110       a4.getProductCategories().add(b3);
111       a4.getProductCategories().add(b4);
112       a4.getProductCategories().add(b5);
113       a5.getProductCategories().add(b4);
114       a5.getProductCategories().add(b5);
115
116       assertTrue(a1.getProductCategories().contains(b1));
117       assertTrue(a1.getProductCategories().contains(b2));
118       assertTrue(a2.getProductCategories().contains(b1));
119       assertTrue(a2.getProductCategories().contains(b2));
120       assertTrue(a2.getProductCategories().contains(b3));
121       assertTrue(a3.getProductCategories().contains(b2));
122       assertTrue(a3.getProductCategories().contains(b3));
123       assertTrue(a3.getProductCategories().contains(b4));
124       assertTrue(a4.getProductCategories().contains(b3));
125       assertTrue(a4.getProductCategories().contains(b4));
126       assertTrue(a4.getProductCategories().contains(b5));
127       assertTrue(a5.getProductCategories().contains(b4));
128       assertTrue(a5.getProductCategories().contains(b5));
129
130       assertTrue(b1.getProducts().contains(a1));
131       assertTrue(b1.getProducts().contains(a2));
132       assertTrue(b2.getProducts().contains(a1));
133       assertTrue(b2.getProducts().contains(a2));
134       assertTrue(b2.getProducts().contains(a3));
135       assertTrue(b3.getProducts().contains(a2));
136       assertTrue(b3.getProducts().contains(a3));
137       assertTrue(b3.getProducts().contains(a4));
138       assertTrue(b4.getProducts().contains(a3));
139       assertTrue(b4.getProducts().contains(a4));
140       assertTrue(b4.getProducts().contains(a5));
141       assertTrue(b5.getProducts().contains(a4));
142       assertTrue(b5.getProducts().contains(a5));
143    }
144
145
146    // a1.setB(a3.getB());
147
public void test_a1SetB_a3GetB() {
148       // Change:
149
a1.setProductCategories(a3.getProductCategories());
150
151       // Expected result:
152
assertTrue(!a1.getProductCategories().contains(b1));
153       assertTrue(a1.getProductCategories().contains(b2));
154       assertTrue(a1.getProductCategories().contains(b3));
155       assertTrue(a1.getProductCategories().contains(b4));
156
157       assertTrue(a2.getProductCategories().contains(b1));
158       assertTrue(a2.getProductCategories().contains(b2));
159       assertTrue(a2.getProductCategories().contains(b3));
160
161       assertTrue(a3.getProductCategories().contains(b2));
162       assertTrue(a3.getProductCategories().contains(b3));
163       assertTrue(a3.getProductCategories().contains(b4));
164
165       assertTrue(a4.getProductCategories().contains(b3));
166       assertTrue(a4.getProductCategories().contains(b4));
167       assertTrue(a4.getProductCategories().contains(b5));
168
169       assertTrue(a5.getProductCategories().contains(b4));
170       assertTrue(a5.getProductCategories().contains(b5));
171
172
173       assertTrue(!b1.getProducts().contains(a1));
174       assertTrue(b1.getProducts().contains(a2));
175
176       assertTrue(b2.getProducts().contains(a1));
177       assertTrue(b2.getProducts().contains(a2));
178       assertTrue(b2.getProducts().contains(a3));
179
180       assertTrue(b3.getProducts().contains(a1));
181       assertTrue(b3.getProducts().contains(a2));
182       assertTrue(b3.getProducts().contains(a3));
183       assertTrue(b3.getProducts().contains(a4));
184
185       assertTrue(b4.getProducts().contains(a1));
186       assertTrue(b4.getProducts().contains(a3));
187       assertTrue(b4.getProducts().contains(a4));
188       assertTrue(b4.getProducts().contains(a5));
189
190       assertTrue(b5.getProducts().contains(a4));
191       assertTrue(b5.getProducts().contains(a5));
192    }
193
194    // a1.getB().add(b3);
195
public void test_a1GetB_addB3() {
196       // Change:
197
a1.getProductCategories().add(b3);
198
199       // Expected result:
200
assertTrue(a1.getProductCategories().contains(b1));
201       assertTrue(a1.getProductCategories().contains(b2));
202       assertTrue(a1.getProductCategories().contains(b3));
203
204       assertTrue(a2.getProductCategories().contains(b1));
205       assertTrue(a2.getProductCategories().contains(b2));
206       assertTrue(a2.getProductCategories().contains(b3));
207
208       assertTrue(a3.getProductCategories().contains(b2));
209       assertTrue(a3.getProductCategories().contains(b3));
210       assertTrue(a3.getProductCategories().contains(b4));
211
212       assertTrue(a4.getProductCategories().contains(b3));
213       assertTrue(a4.getProductCategories().contains(b4));
214       assertTrue(a4.getProductCategories().contains(b5));
215
216       assertTrue(a5.getProductCategories().contains(b4));
217       assertTrue(a5.getProductCategories().contains(b5));
218
219
220       assertTrue(b1.getProducts().contains(a1));
221       assertTrue(b1.getProducts().contains(a2));
222
223       assertTrue(b2.getProducts().contains(a1));
224       assertTrue(b2.getProducts().contains(a2));
225       assertTrue(b2.getProducts().contains(a3));
226
227       assertTrue(b3.getProducts().contains(a1));
228       assertTrue(b3.getProducts().contains(a2));
229       assertTrue(b3.getProducts().contains(a3));
230       assertTrue(b3.getProducts().contains(a4));
231
232       assertTrue(b4.getProducts().contains(a3));
233       assertTrue(b4.getProducts().contains(a4));
234       assertTrue(b4.getProducts().contains(a5));
235
236       assertTrue(b5.getProducts().contains(a4));
237       assertTrue(b5.getProducts().contains(a5));
238    }
239
240    // a2.getB().remove(b2);
241
public void test_a2GetB_removeB2() {
242       // Change:
243
a2.getProductCategories().remove(b2);
244
245       // Expected result:
246
assertTrue(a1.getProductCategories().contains(b1));
247       assertTrue(a1.getProductCategories().contains(b2));
248
249       assertTrue(a2.getProductCategories().contains(b1));
250       assertTrue(!a2.getProductCategories().contains(b2));
251       assertTrue(a2.getProductCategories().contains(b3));
252
253       assertTrue(a3.getProductCategories().contains(b2));
254       assertTrue(a3.getProductCategories().contains(b3));
255       assertTrue(a3.getProductCategories().contains(b4));
256
257       assertTrue(a4.getProductCategories().contains(b3));
258       assertTrue(a4.getProductCategories().contains(b4));
259       assertTrue(a4.getProductCategories().contains(b5));
260
261       assertTrue(a5.getProductCategories().contains(b4));
262       assertTrue(a5.getProductCategories().contains(b5));
263
264
265       assertTrue(b1.getProducts().contains(a1));
266       assertTrue(b1.getProducts().contains(a2));
267
268       assertTrue(b2.getProducts().contains(a1));
269       assertTrue(!b2.getProducts().contains(a2));
270       assertTrue(b2.getProducts().contains(a3));
271
272       assertTrue(b3.getProducts().contains(a2));
273       assertTrue(b3.getProducts().contains(a3));
274       assertTrue(b3.getProducts().contains(a4));
275
276       assertTrue(b4.getProducts().contains(a3));
277       assertTrue(b4.getProducts().contains(a4));
278       assertTrue(b4.getProducts().contains(a5));
279
280       assertTrue(b5.getProducts().contains(a4));
281       assertTrue(b5.getProducts().contains(a5));
282    }
283
284    public void tearDownEJB() throws Exception JavaDoc {
285    }
286
287    public void deleteAllProducts(ProductHome productHome) throws Exception JavaDoc {
288       // delete all Products
289
Iterator JavaDoc currentProducts = productHome.findAll().iterator();
290       while(currentProducts.hasNext()) {
291          Product p = (Product)currentProducts.next();
292          p.remove();
293       }
294    }
295
296    public void deleteAllProductCategories(ProductCategoryHome productCategoryHome) throws Exception JavaDoc {
297       // delete all ProductCategories
298
Iterator JavaDoc currentProductCategories = productCategoryHome.findAll().iterator();
299       while(currentProductCategories.hasNext()) {
300          ProductCategory pc = (ProductCategory)currentProductCategories.next();
301          pc.remove();
302       }
303    }
304
305
306 }
307
308
309
310
Popular Tags