KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > relationship > oneToManyBidirectional > ABTest


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.relationship.oneToManyBidirectional;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import junit.framework.Test;
28 import net.sourceforge.junitejb.EJBTestCase;
29 import org.jboss.test.JBossTestCase;
30
31 public class ABTest extends EJBTestCase {
32     static org.jboss.logging.Logger log =
33        org.jboss.logging.Logger.getLogger(ABTest.class);
34
35     public static Test suite() throws Exception JavaDoc {
36         return JBossTestCase.getDeploySetup(ABTest.class, "cmp2-relationship.jar");
37    }
38
39    public ABTest(String JavaDoc name) {
40       super(name);
41    }
42
43    private AHome getTableAHome() {
44       try {
45          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
46          
47          return (AHome) jndiContext.lookup("relation/oneToMany/bidirectional/table/A");
48       } catch(Exception JavaDoc e) {
49          log.debug("failed", e);
50          fail("Exception in getTableAHome: " + e.getMessage());
51       }
52       return null;
53    }
54
55    private BHome getTableBHome() {
56       try {
57          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
58          
59          return (BHome) jndiContext.lookup("relation/oneToMany/bidirectional/table/B");
60       } catch(Exception JavaDoc e) {
61          log.debug("failed", e);
62          fail("Exception in getTableBHome: " + e.getMessage());
63       }
64       return null;
65    }
66
67    private AHome getFKAHome() {
68       try {
69          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
70
71          return (AHome) jndiContext.lookup("relation/oneToMany/bidirectional/fk/A");
72       } catch(Exception JavaDoc e) {
73          log.debug("failed", e);
74          fail("Exception in getFKAHome: " + e.getMessage());
75       }
76       return null;
77    }
78
79    private BHome getFKBHome() {
80       try {
81          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
82
83          return (BHome) jndiContext.lookup("relation/oneToMany/bidirectional/fk/B");
84       } catch(Exception JavaDoc e) {
85          log.debug("failed", e);
86          fail("Exception in getFKBHome: " + e.getMessage());
87       }
88       return null;
89    }
90
91    // a1.setB(a2.getB());
92
public void test_a1SetB_a2GetB_Table() throws Exception JavaDoc {
93       AHome aHome = getTableAHome();
94       BHome bHome = getTableBHome();
95       a1SetB_a2GetB(aHome, bHome);
96    }
97
98    // a1.setB(a2.getB());
99
public void test_a1SetB_a2GetB_FK() throws Exception JavaDoc {
100       AHome aHome = getFKAHome();
101       BHome bHome = getFKBHome();
102       a1SetB_a2GetB(aHome, bHome);
103    }
104
105    // a1.setB(a2.getB());
106
private void a1SetB_a2GetB(AHome aHome, BHome bHome) throws Exception JavaDoc {
107       // Before change:
108
A a1 = aHome.create(new Integer JavaDoc(1));
109       A a2 = aHome.create(new Integer JavaDoc(2));
110       
111       Collection JavaDoc b1 = a1.getB();
112       Collection JavaDoc b2 = a2.getB();
113       
114       B[] b1x = new B[20];
115       B[] b2x = new B[30];
116       
117       for(int i=0; i<b1x.length; i++) {
118          b1x[i] = bHome.create(new Integer JavaDoc(10000 + i));
119          b1.add(b1x[i]);
120       }
121       
122       for(int i=0; i<b2x.length; i++) {
123          b2x[i] = bHome.create(new Integer JavaDoc(20000 + i));
124          b2.add(b2x[i]);
125       }
126       
127       // B b11, b12, ... , b1n; members of b1
128
for(int i=0; i<b1x.length; i++) {
129          assertTrue(b1.contains(b1x[i]));
130       }
131       
132       // B b21, b22, ... , b2m; members of b2
133
for(int i=0; i<b2x.length; i++) {
134          assertTrue(b2.contains(b2x[i]));
135       }
136       
137       // Change:
138
a1.setB(a2.getB());
139       
140       // Expected result:
141

142       // a2.getB().isEmpty()
143
assertTrue(a2.getB().isEmpty());
144       
145       // b2.isEmpty()
146
assertTrue(b2.isEmpty());
147       
148       // b1 == a1.getB()
149
assertTrue(b1 == a1.getB());
150       
151       // b2 == a2.getB()
152
assertTrue(b2 == a2.getB());
153       
154       // a1.getB().contains(b21)
155
// a1.getB().contains(b22)
156
// a1.getB().contains(...)
157
// a1.getB().contains(b2m)
158
for(int i=0; i<b2x.length; i++) {
159          assertTrue(a1.getB().contains(b2x[i]));
160       }
161       
162       // b11.getA() == null
163
// b12.getA() == null
164
// ....getA() == null
165
// b1n.getA() == null
166
for(int i=0; i<b1x.length; i++) {
167          assertTrue(b1x[i].getA() == null);
168       }
169       
170       
171       // a1.isIdentical(b21.getA())
172
// a1.isIdentical(b22.getA())
173
// a1.isIdentical(....getA())
174
// a1.isIdentical(b2m.getA()))
175
for(int i=0; i<b2x.length; i++) {
176          assertTrue(a1.isIdentical(b2x[i].getA()));
177       }
178    }
179    
180    // b2m.setA(b1n.getA());
181
public void test_b2mSetA_b1nGetA_Table() throws Exception JavaDoc {
182       AHome aHome = getTableAHome();
183       BHome bHome = getTableBHome();
184       b2mSetA_b1nGetA(aHome, bHome);
185    }
186
187    // b2m.setA(b1n.getA());
188
public void test_b2mSetA_b1nGetA_FK() throws Exception JavaDoc {
189       AHome aHome = getFKAHome();
190       BHome bHome = getFKBHome();
191       b2mSetA_b1nGetA(aHome, bHome);
192    }
193
194    // b2m.setA(b1n.getA());
195
public void b2mSetA_b1nGetA(AHome aHome, BHome bHome) throws Exception JavaDoc {
196       // Before change:
197
A a1 = aHome.create(new Integer JavaDoc(1));
198       A a2 = aHome.create(new Integer JavaDoc(2));
199       
200       Collection JavaDoc b1 = a1.getB();
201       Collection JavaDoc b2 = a2.getB();
202       
203       B[] b1x = new B[20];
204       B[] b2x = new B[30];
205       
206       for(int i=0; i<b1x.length; i++) {
207          b1x[i] = bHome.create(new Integer JavaDoc(10000 + i));
208          b1.add(b1x[i]);
209       }
210       
211       for(int i=0; i<b2x.length; i++) {
212          b2x[i] = bHome.create(new Integer JavaDoc(20000 + i));
213          b2.add(b2x[i]);
214       }
215       
216       // B b11, b12, ... , b1n; members of b1
217
for(int i=0; i<b1x.length; i++) {
218          assertTrue(b1.contains(b1x[i]));
219       }
220       
221       // B b21, b22, ... , b2m; members of b2
222
for(int i=0; i<b2x.length; i++) {
223          assertTrue(b2.contains(b2x[i]));
224       }
225       
226       // Change:
227

228       // b2m.setA(b1n.getA());
229
b2x[b2x.length-1].setA(b1x[b1x.length-1].getA());
230       
231       // Expected result:
232

233       // b1.contains(b11)
234
// b1.contains(b12)
235
// b1.contains(...)
236
// b1.contains(b1n)
237
for(int i=0; i<b1x.length; i++) {
238          assertTrue(b1.contains(b1x[i]));
239       }
240
241       // b1.contains(b2m)
242
assertTrue(b1.contains(b2x[b2x.length-1]));
243
244       // b2.contains(b21)
245
// b2.contains(b22)
246
// b2.contains(...)
247
// b2.contains(b2m_1)
248
for(int i=0; i<b2x.length-1; i++) {
249          assertTrue(b2.contains(b2x[i]));
250       }
251
252       // a1.isIdentical(b11.getA())
253
// a1.isIdentical(b12.getA())
254
// a1.isIdentical(....getA())
255
// a1.isIdentical(b1n.getA())
256
for(int i=0; i<b1x.length; i++) {
257          assertTrue(a1.isIdentical(b1x[i].getA()));
258       }
259       
260       // a2.isIdentical(b21.getA())
261
// a2.isIdentical(b22.getA())
262
// a2.isIdentical(....getA())
263
// a2.isIdentical(b2m_1.getA())
264
for(int i=0; i<b2x.length-1; i++) {
265          assertTrue(a2.isIdentical(b2x[i].getA()));
266       }
267       
268       // a1.isIdentical(b2m.getA())
269
assertTrue(a1.isIdentical(b2x[b2x.length-1].getA()));
270    }
271    
272    // a1.getB().add(b2m);
273
public void test_a1GetB_addB2m_Table() throws Exception JavaDoc {
274       AHome aHome = getTableAHome();
275       BHome bHome = getTableBHome();
276       a1GetB_addB2m(aHome, bHome);
277    }
278
279    // a1.getB().add(b2m);
280
public void test_a1GetB_addB2m_FK() throws Exception JavaDoc {
281       AHome aHome = getFKAHome();
282       BHome bHome = getFKBHome();
283       a1GetB_addB2m(aHome, bHome);
284    }
285
286    // a1.getB().add(b2m);
287
private void a1GetB_addB2m(AHome aHome, BHome bHome) throws Exception JavaDoc {
288       // Before change:
289
A a1 = aHome.create(new Integer JavaDoc(1));
290       A a2 = aHome.create(new Integer JavaDoc(2));
291       
292       Collection JavaDoc b1 = a1.getB();
293       Collection JavaDoc b2 = a2.getB();
294       
295       B[] b1x = new B[20];
296       B[] b2x = new B[30];
297       
298       for(int i=0; i<b1x.length; i++) {
299          b1x[i] = bHome.create(new Integer JavaDoc(10000 + i));
300          b1.add(b1x[i]);
301       }
302       
303       for(int i=0; i<b2x.length; i++) {
304          b2x[i] = bHome.create(new Integer JavaDoc(20000 + i));
305          b2.add(b2x[i]);
306       }
307       
308       // B b11, b12, ... , b1n; members of b1
309
for(int i=0; i<b1x.length; i++) {
310          assertTrue(b1.contains(b1x[i]));
311       }
312       
313       // B b21, b22, ... , b2m; members of b2
314
for(int i=0; i<b2x.length; i++) {
315          assertTrue(b2.contains(b2x[i]));
316       }
317       
318       // Change:
319

320       // a1.getB().add(b2m);
321
a1.getB().add(b2x[b2x.length-1]);
322       
323       // Expected result:
324

325       // b1.contains(b11)
326
// b1.contains(b12)
327
// b1.contains(...)
328
// b1.contains(b1n)
329
for(int i=0; i<b1x.length; i++) {
330          assertTrue(b1.contains(b1x[i]));
331       }
332
333       // b1.contains(b2m)
334
assertTrue(b1.contains(b2x[b2x.length-1]));
335
336       // b2.contains(b21)
337
// b2.contains(b22)
338
// b2.contains(...)
339
// b2.contains(b2m_1)
340
for(int i=0; i<b2x.length-1; i++) {
341          assertTrue(b2.contains(b2x[i]));
342       }
343
344       // a1.isIdentical(b11.getA())
345
// a1.isIdentical(b12.getA())
346
// a1.isIdentical(....getA())
347
// a1.isIdentical(b1n.getA())
348
for(int i=0; i<b1x.length; i++) {
349          assertTrue(a1.isIdentical(b1x[i].getA()));
350       }
351       
352       // a2.isIdentical(b21.getA())
353
// a2.isIdentical(b22.getA())
354
// a2.isIdentical(....getA())
355
// a2.isIdentical(b2m_1.getA())
356
for(int i=0; i<b2x.length-1; i++) {
357          assertTrue(a2.isIdentical(b2x[i].getA()));
358       }
359       
360       // a1.isIdentical(b2m.getA())
361
assertTrue(a1.isIdentical(b2x[b2x.length-1].getA()));
362    }
363    
364    // a1.getB().remove(b1n);
365
public void test_a1GetB_removeB1n_Table() throws Exception JavaDoc {
366       AHome aHome = getTableAHome();
367       BHome bHome = getTableBHome();
368       a1GetB_removeB1n(aHome, bHome);
369    }
370
371    // a1.getB().remove(b1n);
372
public void test_a1GetB_removeB1n_FK() throws Exception JavaDoc {
373       AHome aHome = getFKAHome();
374       BHome bHome = getFKBHome();
375       a1GetB_removeB1n(aHome, bHome);
376    }
377
378    // a1.getB().remove(b1n);
379
private void a1GetB_removeB1n(AHome aHome, BHome bHome) throws Exception JavaDoc {
380       // Before change:
381
A a1 = aHome.create(new Integer JavaDoc(1));
382       A a2 = aHome.create(new Integer JavaDoc(2));
383       
384       Collection JavaDoc b1 = a1.getB();
385       Collection JavaDoc b2 = a2.getB();
386       
387       B[] b1x = new B[20];
388       B[] b2x = new B[30];
389       
390       for(int i=0; i<b1x.length; i++) {
391          b1x[i] = bHome.create(new Integer JavaDoc(10000 + i));
392          b1.add(b1x[i]);
393       }
394       
395       for(int i=0; i<b2x.length; i++) {
396          b2x[i] = bHome.create(new Integer JavaDoc(20000 + i));
397          b2.add(b2x[i]);
398       }
399          
400       // B b11, b12, ... , b1n; members of b1
401
for(int i=0; i<b1x.length; i++) {
402          assertTrue(b1.contains(b1x[i]));
403       }
404       
405       // B b21, b22, ... , b2m; members of b2
406
for(int i=0; i<b2x.length; i++) {
407          assertTrue(b2.contains(b2x[i]));
408       }
409       
410       // Change:
411

412       // a1.getB().remove(b1n);
413
a1.getB().remove(b1x[b1x.length-1]);
414       
415       // Expected result:
416

417       // b1n.getA() == null
418
assertTrue(b1x[b1x.length-1].getA() == null);
419          
420       // b1 == a1.getB()
421
assertTrue(b1 == a1.getB());
422       
423       // b1.contains(b11)
424
// b1.contains(b12)
425
// b1.contains(...)
426
// b1.contains(b1n_1)
427
for(int i=0; i<b1x.length-1; i++) {
428          assertTrue(b1.contains(b1x[i]));
429       }
430
431       // !(b1.contains(b1n))
432
assertTrue(!(b1.contains(b1x[b1x.length-1])));
433    }
434
435    public void setUpEJB() throws Exception JavaDoc {
436       AHome aHome;
437       BHome bHome;
438
439       aHome = getTableAHome();
440       bHome = getTableBHome();
441       deleteAllAsAndBs(aHome, bHome);
442
443       aHome = getFKAHome();
444       bHome = getFKBHome();
445       deleteAllAsAndBs(aHome, bHome);
446    }
447
448    public void tearDownEJB() throws Exception JavaDoc {
449    }
450
451    public void deleteAllAsAndBs(AHome aHome, BHome bHome) throws Exception JavaDoc {
452       // delete all As
453
Iterator JavaDoc currentAs = aHome.findAll().iterator();
454       while(currentAs.hasNext()) {
455          A a = (A)currentAs.next();
456          a.remove();
457       }
458
459       // delete all Bs
460
Iterator JavaDoc currentBs = bHome.findAll().iterator();
461       while(currentBs.hasNext()) {
462          B b = (B)currentBs.next();
463          b.remove();
464       }
465    }
466 }
467
468
469
470
Popular Tags