KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

222       // b1.contains(b11)
223
// b1.contains(b12)
224
// b1.contains(...)
225
// b1.contains(b1n)
226
for(int i=0; i<b1x.length; i++) {
227          assertTrue(b1.contains(b1x[i]));
228       }
229
230       // b1.contains(b2m)
231
assertTrue(b1.contains(b2x[b2x.length-1]));
232
233       // b2.contains(b21)
234
// b2.contains(b22)
235
// b2.contains(...)
236
// b2.contains(b2m_1)
237
for(int i=0; i<b2x.length-1; i++) {
238          assertTrue(b2.contains(b2x[i]));
239       }
240    }
241    
242    // a1.getB().remove(b1n);
243
public void Xtest_a1GetB_removeB1n_Table() throws Exception JavaDoc {
244       AHome aHome = getTableAHome();
245       BHome bHome = getTableBHome();
246       a1GetB_removeB1n(aHome, bHome);
247    }
248
249    // a1.getB().remove(b1n);
250
public void Xtest_a1GetB_removeB1n_FK() throws Exception JavaDoc {
251       AHome aHome = getFKAHome();
252       BHome bHome = getFKBHome();
253       a1GetB_removeB1n(aHome, bHome);
254    }
255
256    // a1.getB().remove(b1n);
257
private void a1GetB_removeB1n(AHome aHome, BHome bHome) throws Exception JavaDoc {
258       // Before change:
259
A a1 = aHome.create(new Integer JavaDoc(1));
260       A a2 = aHome.create(new Integer JavaDoc(2));
261       
262       Collection JavaDoc b1 = a1.getB();
263       Collection JavaDoc b2 = a2.getB();
264       
265       B[] b1x = new B[20];
266       B[] b2x = new B[30];
267       
268       for(int i=0; i<b1x.length; i++) {
269          b1x[i] = bHome.create(new Integer JavaDoc(10000 + i));
270          b1.add(b1x[i]);
271       }
272       
273       for(int i=0; i<b2x.length; i++) {
274          b2x[i] = bHome.create(new Integer JavaDoc(20000 + i));
275          b2.add(b2x[i]);
276       }
277          
278       // B b11, b12, ... , b1n; members of b1
279
for(int i=0; i<b1x.length; i++) {
280          assertTrue(b1.contains(b1x[i]));
281       }
282       
283       // B b21, b22, ... , b2m; members of b2
284
for(int i=0; i<b2x.length; i++) {
285          assertTrue(b2.contains(b2x[i]));
286       }
287       
288       // Change:
289

290       // a1.getB().remove(b1n);
291
a1.getB().remove(b1x[b1x.length-1]);
292       
293       // Expected result:
294

295       // b1 == a1.getB()
296
assertTrue(b1 == a1.getB());
297          
298       // b1.contains(b11)
299
// b1.contains(b12)
300
// b1.contains(...)
301
// b1.contains(b1n_1)
302
for(int i=0; i<b1x.length-1; i++) {
303          assertTrue(b1.contains(b1x[i]));
304       }
305
306       // !(b1.contains(b1n))
307
assertTrue(!(b1.contains(b1x[b1x.length-1])));
308    }
309
310    public void setUpEJB() throws Exception JavaDoc {
311       AHome aHome;
312       BHome bHome;
313
314       aHome = getTableAHome();
315       bHome = getTableBHome();
316       deleteAllAsAndBs(aHome, bHome);
317
318       aHome = getFKAHome();
319       bHome = getFKBHome();
320       deleteAllAsAndBs(aHome, bHome);
321    }
322    
323    public void tearDownEJB() throws Exception JavaDoc {
324    }
325    
326    public void deleteAllAsAndBs(AHome aHome, BHome bHome) throws Exception JavaDoc {
327       // delete all As
328
Iterator JavaDoc currentAs = aHome.findAll().iterator();
329       while(currentAs.hasNext()) {
330          A a = (A)currentAs.next();
331          a.remove();
332       }
333
334       // delete all Bs
335
Iterator JavaDoc currentBs = bHome.findAll().iterator();
336       while(currentBs.hasNext()) {
337          B b = (B)currentBs.next();
338          b.remove();
339       }
340    }
341 }
342
343
344
345
Popular Tags