KickJava   Java API By Example, From Geeks To Geeks.

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


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 OneToManyBiTest extends EJBTestCase {
31
32    public OneToManyBiTest(String JavaDoc name) {
33       super(name);
34    }
35
36    private OrderHome getOrderHome() {
37       try {
38          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
39          
40          return (OrderHome) jndiContext.lookup("commerce/Order");
41       } catch(Exception JavaDoc e) {
42          e.printStackTrace();
43          fail("Exception in getOrder: " + e.getMessage());
44       }
45       return null;
46    }
47
48    private LineItemHome getLineItemHome() {
49       try {
50          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
51          
52          return (LineItemHome) jndiContext.lookup("commerce/LineItem");
53       } catch(Exception JavaDoc e) {
54          e.printStackTrace();
55          fail("Exception in getLineItemHome: " + e.getMessage());
56       }
57       return null;
58    }
59
60    private Order a1;
61    private Order a2;
62
63    private Collection JavaDoc b1;
64    private Collection JavaDoc b2;
65          
66    private LineItem[] b1x = new LineItem[20];
67    private LineItem[] b2x = new LineItem[30];
68  
69    public void setUpEJB() throws Exception JavaDoc {
70       OrderHome orderHome = getOrderHome();
71       LineItemHome lineItemHome = getLineItemHome();
72
73       // clean out the db
74
deleteAllOrders(orderHome);
75       deleteAllLineItems(lineItemHome);
76
77       // setup the before change part of the test
78
beforeChange(orderHome, lineItemHome);
79    }
80
81   private void beforeChange(OrderHome orderHome, LineItemHome lineItemHome)
82          throws Exception JavaDoc {
83
84       // Before change:
85
a1 = orderHome.create();
86       a2 = orderHome.create();
87          
88       b1 = a1.getLineItems();
89       b2 = a2.getLineItems();
90          
91       for(int i=0; i<b1x.length; i++) {
92          b1x[i] = lineItemHome.create();
93          b1.add(b1x[i]);
94       }
95       
96       for(int i=0; i<b2x.length; i++) {
97          b2x[i] = lineItemHome.create();
98          b2.add(b2x[i]);
99       }
100       
101       // B b11, b12, ... , b1n; members of b1
102
for(int i=0; i<b1x.length; i++) {
103          assertTrue(b1.contains(b1x[i]));
104       }
105       
106       // B b21, b22, ... , b2m; members of b2
107
for(int i=0; i<b2x.length; i++) {
108          assertTrue(b2.contains(b2x[i]));
109       }
110    }
111
112    public void setUp() {
113       // get the collections again as they are only
114
// valid for the tx length, and were from last tx
115
b1 = a1.getLineItems();
116       b2 = a2.getLineItems();
117    }
118
119    // a1.setB(a2.getB());
120
public void test_a1SetB_a2GetB() {
121       // Change:
122
a1.setLineItems(a2.getLineItems());
123          
124       // Expected result:
125

126       // a2.getB().isEmpty()
127
assertTrue(a2.getLineItems().isEmpty());
128          
129       // b2.isEmpty()
130
assertTrue(b2.isEmpty());
131          
132       // b1 == a1.getB()
133
assertTrue(b1 == a1.getLineItems());
134          
135       // b2 == a2.getB()
136
assertTrue(b2 == a2.getLineItems());
137          
138       // a1.getB().contains(b21)
139
// a1.getB().contains(b22)
140
// a1.getB().contains(...)
141
// a1.getB().contains(b2m)
142
for(int i=0; i<b2x.length; i++) {
143          assertTrue(a1.getLineItems().contains(b2x[i]));
144       }
145       
146       // b11.getA() == null
147
// b12.getA() == null
148
// ....getA() == null
149
// b1n.getA() == null
150
for(int i=0; i<b1x.length; i++) {
151          assertTrue(b1x[i].getOrder() == null);
152       }
153       
154       
155       // a1.isIdentical(b21.getA())
156
// a1.isIdentical(b22.getA())
157
// a1.isIdentical(....getA())
158
// a1.isIdentical(b2m.getA()))
159
for(int i=0; i<b2x.length; i++) {
160          assertTrue(a1.isIdentical(b2x[i].getOrder()));
161       }
162    }
163
164    // b2m.setA(b1n.getA());
165
public void test_b2mSetA_b1nGetA() {
166       // Change:
167

168       // b2m.setA(b1n.getA());
169
b2x[b2x.length-1].setOrder(b1x[b1x.length-1].getOrder());
170       
171       // Expected result:
172

173       // b1.contains(b11)
174
// b1.contains(b12)
175
// b1.contains(...)
176
// b1.contains(b1n)
177
for(int i=0; i<b1x.length; i++) {
178          assertTrue(b1.contains(b1x[i]));
179       }
180
181       // b1.contains(b2m)
182
assertTrue(b1.contains(b2x[b2x.length-1]));
183
184       // b2.contains(b21)
185
// b2.contains(b22)
186
// b2.contains(...)
187
// b2.contains(b2m_1)
188
for(int i=0; i<b2x.length-1; i++) {
189          assertTrue(b2.contains(b2x[i]));
190       }
191
192       // a1.isIdentical(b11.getA())
193
// a1.isIdentical(b12.getA())
194
// a1.isIdentical(....getA())
195
// a1.isIdentical(b1n.getA())
196
for(int i=0; i<b1x.length; i++) {
197          assertTrue(a1.isIdentical(b1x[i].getOrder()));
198       }
199       
200       // a2.isIdentical(b21.getA())
201
// a2.isIdentical(b22.getA())
202
// a2.isIdentical(....getA())
203
// a2.isIdentical(b2m_1.getA())
204
for(int i=0; i<b2x.length-1; i++) {
205          assertTrue(a2.isIdentical(b2x[i].getOrder()));
206       }
207       
208       // a1.isIdentical(b2m.getA())
209
assertTrue(a1.isIdentical(b2x[b2x.length-1].getOrder()));
210    }
211    
212    // a1.getB().add(b2m);
213
public void test_a1GetB_addB2m() {
214       // Change:
215

216       // a1.getB().add(b2m);
217
a1.getLineItems().add(b2x[b2x.length-1]);
218          
219       // Expected result:
220

221       // b1.contains(b11)
222
// b1.contains(b12)
223
// b1.contains(...)
224
// b1.contains(b1n)
225
for(int i=0; i<b1x.length; i++) {
226          assertTrue(b1.contains(b1x[i]));
227       }
228
229       // b1.contains(b2m)
230
assertTrue(b1.contains(b2x[b2x.length-1]));
231
232       // b2.contains(b21)
233
// b2.contains(b22)
234
// b2.contains(...)
235
// b2.contains(b2m_1)
236
for(int i=0; i<b2x.length-1; i++) {
237          assertTrue(b2.contains(b2x[i]));
238       }
239
240       // a1.isIdentical(b11.getA())
241
// a1.isIdentical(b12.getA())
242
// a1.isIdentical(....getA())
243
// a1.isIdentical(b1n.getA())
244
for(int i=0; i<b1x.length; i++) {
245          assertTrue(a1.isIdentical(b1x[i].getOrder()));
246       }
247       
248       // a2.isIdentical(b21.getA())
249
// a2.isIdentical(b22.getA())
250
// a2.isIdentical(....getA())
251
// a2.isIdentical(b2m_1.getA())
252
for(int i=0; i<b2x.length-1; i++) {
253          assertTrue(a2.isIdentical(b2x[i].getOrder()));
254       }
255       
256       // a1.isIdentical(b2m.getA())
257
assertTrue(a1.isIdentical(b2x[b2x.length-1].getOrder()));
258    }
259    
260    // a1.getB().remove(b1n);
261
public void test_a1GetB_removeB1n() {
262       // Change:
263

264       // a1.getB().remove(b1n);
265
a1.getLineItems().remove(b1x[b1x.length-1]);
266       
267       // Expected result:
268

269       // b1n.getA() == null
270
assertTrue(b1x[b1x.length-1].getOrder() == null);
271       
272       // b1 == a1.getB()
273
assertTrue(b1 == a1.getLineItems());
274       
275       // b1.contains(b11)
276
// b1.contains(b12)
277
// b1.contains(...)
278
// b1.contains(b1n_1)
279
for(int i=0; i<b1x.length-1; i++) {
280          assertTrue(b1.contains(b1x[i]));
281       }
282
283       // !(b1.contains(b1n))
284
assertTrue(!(b1.contains(b1x[b1x.length-1])));
285    }
286
287    public void tearDownEJB() throws Exception JavaDoc {
288       OrderHome orderHome = getOrderHome();
289       LineItemHome lineItemHome = getLineItemHome();
290
291       // clean out the db
292
deleteAllOrders(orderHome);
293       deleteAllLineItems(lineItemHome);
294    }
295    
296    public void deleteAllOrders(OrderHome orderHome) throws Exception JavaDoc {
297       // delete all Orders
298
Iterator JavaDoc currentOrders = orderHome.findAll().iterator();
299       while(currentOrders.hasNext()) {
300          Order o = (Order)currentOrders.next();
301          o.remove();
302       }
303    }
304
305    public void deleteAllLineItems(LineItemHome lineItemHome) throws Exception JavaDoc {
306       // delete all LineItems
307
Iterator JavaDoc currentLineItems = lineItemHome.findAll().iterator();
308       while(currentLineItems.hasNext()) {
309          LineItem l = (LineItem)currentLineItems.next();
310          l.remove();
311       }
312    }
313 }
314
315
316
317
Popular Tags