KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > relationship > manyToManyUnidirectional > 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.manyToManyUnidirectional;
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 getAHome() {
45       try {
46          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
47
48          return (AHome) jndiContext.lookup("relation/manyToMany/unidirectional/A");
49       } catch(Exception JavaDoc e) {
50          log.debug("failed", e);
51          fail("Exception in getAHome: " + e.getMessage());
52       }
53       return null;
54    }
55
56    private BHome getBHome() {
57       try {
58          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
59
60          return (BHome) jndiContext.lookup("relation/manyToMany/unidirectional/B");
61       } catch(Exception JavaDoc e) {
62          log.debug("failed", e);
63          fail("Exception in getBHome: " + e.getMessage());
64       }
65       return null;
66    }
67
68    // a1.setB(a3.getB());
69
public void test_a1SetB_a3GetB() throws Exception JavaDoc {
70       AHome aHome = getAHome();
71       BHome bHome = getBHome();
72
73       // Before change:
74
A a1 = aHome.create(new Integer JavaDoc(1));
75       A a2 = aHome.create(new Integer JavaDoc(2));
76       A a3 = aHome.create(new Integer JavaDoc(3));
77       A a4 = aHome.create(new Integer JavaDoc(4));
78       A a5 = aHome.create(new Integer JavaDoc(5));
79
80       B b1 = bHome.create(new Integer JavaDoc(-1));
81       B b2 = bHome.create(new Integer JavaDoc(-2));
82       B b3 = bHome.create(new Integer JavaDoc(-3));
83       B b4 = bHome.create(new Integer JavaDoc(-4));
84       B b5 = bHome.create(new Integer JavaDoc(-5));
85       
86       a1.getB().add(b1);
87       a1.getB().add(b2);
88       a2.getB().add(b1);
89       a2.getB().add(b2);
90       a2.getB().add(b3);
91       a3.getB().add(b2);
92       a3.getB().add(b3);
93       a3.getB().add(b4);
94       a4.getB().add(b3);
95       a4.getB().add(b4);
96       a4.getB().add(b5);
97       a5.getB().add(b4);
98       a5.getB().add(b5);
99
100       assertTrue(a1.getB().contains(b1));
101       assertTrue(a1.getB().contains(b2));
102       assertTrue(a2.getB().contains(b1));
103       assertTrue(a2.getB().contains(b2));
104       assertTrue(a2.getB().contains(b3));
105       assertTrue(a3.getB().contains(b2));
106       assertTrue(a3.getB().contains(b3));
107       assertTrue(a3.getB().contains(b4));
108       assertTrue(a4.getB().contains(b3));
109       assertTrue(a4.getB().contains(b4));
110       assertTrue(a4.getB().contains(b5));
111       assertTrue(a5.getB().contains(b4));
112       assertTrue(a5.getB().contains(b5));
113
114       // Change:
115
a1.setB(a3.getB());
116       
117       // Expected result:
118
assertTrue(!a1.getB().contains(b1));
119       assertTrue(a1.getB().contains(b2));
120       assertTrue(a1.getB().contains(b3));
121       assertTrue(a1.getB().contains(b4));
122       
123       assertTrue(a2.getB().contains(b1));
124       assertTrue(a2.getB().contains(b2));
125       assertTrue(a2.getB().contains(b3));
126       
127       assertTrue(a3.getB().contains(b2));
128       assertTrue(a3.getB().contains(b3));
129       assertTrue(a3.getB().contains(b4));
130       
131       assertTrue(a4.getB().contains(b3));
132       assertTrue(a4.getB().contains(b4));
133       assertTrue(a4.getB().contains(b5));
134       
135       assertTrue(a5.getB().contains(b4));
136       assertTrue(a5.getB().contains(b5));
137    }
138    
139    // a1.getB().add(b3);
140
public void test_a1GetB_addB3() throws Exception JavaDoc {
141       AHome aHome = getAHome();
142       BHome bHome = getBHome();
143
144       // Before change:
145
A a1 = aHome.create(new Integer JavaDoc(1));
146       A a2 = aHome.create(new Integer JavaDoc(2));
147       A a3 = aHome.create(new Integer JavaDoc(3));
148       A a4 = aHome.create(new Integer JavaDoc(4));
149       A a5 = aHome.create(new Integer JavaDoc(5));
150
151       B b1 = bHome.create(new Integer JavaDoc(-1));
152       B b2 = bHome.create(new Integer JavaDoc(-2));
153       B b3 = bHome.create(new Integer JavaDoc(-3));
154       B b4 = bHome.create(new Integer JavaDoc(-4));
155       B b5 = bHome.create(new Integer JavaDoc(-5));
156       
157       a1.getB().add(b1);
158       a1.getB().add(b2);
159       a2.getB().add(b1);
160       a2.getB().add(b2);
161       a2.getB().add(b3);
162       a3.getB().add(b2);
163       a3.getB().add(b3);
164       a3.getB().add(b4);
165       a4.getB().add(b3);
166       a4.getB().add(b4);
167       a4.getB().add(b5);
168       a5.getB().add(b4);
169       a5.getB().add(b5);
170
171       assertTrue(a1.getB().contains(b1));
172       assertTrue(a1.getB().contains(b2));
173       assertTrue(a2.getB().contains(b1));
174       assertTrue(a2.getB().contains(b2));
175       assertTrue(a2.getB().contains(b3));
176       assertTrue(a3.getB().contains(b2));
177       assertTrue(a3.getB().contains(b3));
178       assertTrue(a3.getB().contains(b4));
179       assertTrue(a4.getB().contains(b3));
180       assertTrue(a4.getB().contains(b4));
181       assertTrue(a4.getB().contains(b5));
182       assertTrue(a5.getB().contains(b4));
183       assertTrue(a5.getB().contains(b5));
184       
185       // Change:
186
a1.getB().add(b3);
187       
188       // Expected result:
189
assertTrue(a1.getB().contains(b1));
190       assertTrue(a1.getB().contains(b2));
191       assertTrue(a1.getB().contains(b3));
192       
193       assertTrue(a2.getB().contains(b1));
194       assertTrue(a2.getB().contains(b2));
195       assertTrue(a2.getB().contains(b3));
196       
197       assertTrue(a3.getB().contains(b2));
198       assertTrue(a3.getB().contains(b3));
199       assertTrue(a3.getB().contains(b4));
200       
201       assertTrue(a4.getB().contains(b3));
202       assertTrue(a4.getB().contains(b4));
203       assertTrue(a4.getB().contains(b5));
204       
205       assertTrue(a5.getB().contains(b4));
206       assertTrue(a5.getB().contains(b5));
207    }
208
209    // a2.getB().remove(b2);
210
public void test_a2GetB_removeB2() throws Exception JavaDoc {
211       AHome aHome = getAHome();
212       BHome bHome = getBHome();
213
214       // Before change:
215
A a1 = aHome.create(new Integer JavaDoc(1));
216       A a2 = aHome.create(new Integer JavaDoc(2));
217       A a3 = aHome.create(new Integer JavaDoc(3));
218       A a4 = aHome.create(new Integer JavaDoc(4));
219       A a5 = aHome.create(new Integer JavaDoc(5));
220
221       B b1 = bHome.create(new Integer JavaDoc(-1));
222       B b2 = bHome.create(new Integer JavaDoc(-2));
223       B b3 = bHome.create(new Integer JavaDoc(-3));
224       B b4 = bHome.create(new Integer JavaDoc(-4));
225       B b5 = bHome.create(new Integer JavaDoc(-5));
226       
227       a1.getB().add(b1);
228       a1.getB().add(b2);
229       a2.getB().add(b1);
230       a2.getB().add(b2);
231       a2.getB().add(b3);
232       a3.getB().add(b2);
233       a3.getB().add(b3);
234       a3.getB().add(b4);
235       a4.getB().add(b3);
236       a4.getB().add(b4);
237       a4.getB().add(b5);
238       a5.getB().add(b4);
239       a5.getB().add(b5);
240
241       assertTrue(a1.getB().contains(b1));
242       assertTrue(a1.getB().contains(b2));
243       assertTrue(a2.getB().contains(b1));
244       assertTrue(a2.getB().contains(b2));
245       assertTrue(a2.getB().contains(b3));
246       assertTrue(a3.getB().contains(b2));
247       assertTrue(a3.getB().contains(b3));
248       assertTrue(a3.getB().contains(b4));
249       assertTrue(a4.getB().contains(b3));
250       assertTrue(a4.getB().contains(b4));
251       assertTrue(a4.getB().contains(b5));
252       assertTrue(a5.getB().contains(b4));
253       assertTrue(a5.getB().contains(b5));
254       
255       // Change:
256
a2.getB().remove(b2);
257       
258       // Expected result:
259
assertTrue(a1.getB().contains(b1));
260       assertTrue(a1.getB().contains(b2));
261       
262       assertTrue(a2.getB().contains(b1));
263       assertTrue(!a2.getB().contains(b2));
264       assertTrue(a2.getB().contains(b3));
265       
266       assertTrue(a3.getB().contains(b2));
267       assertTrue(a3.getB().contains(b3));
268       assertTrue(a3.getB().contains(b4));
269       
270       assertTrue(a4.getB().contains(b3));
271       assertTrue(a4.getB().contains(b4));
272       assertTrue(a4.getB().contains(b5));
273       
274       assertTrue(a5.getB().contains(b4));
275       assertTrue(a5.getB().contains(b5));
276    }
277
278    public void setUpEJB() throws Exception JavaDoc {
279       deleteAllAsAndBs(getAHome(), getBHome());
280    }
281    
282    public void tearDownEJB() throws Exception JavaDoc {
283    }
284    
285    public void deleteAllAsAndBs(AHome aHome, BHome bHome) throws Exception JavaDoc {
286       // delete all As
287
Iterator JavaDoc currentAs = aHome.findAll().iterator();
288       while(currentAs.hasNext()) {
289          A a = (A)currentAs.next();
290          a.remove();
291       }
292       
293       // delete all Bs
294
Iterator JavaDoc currentBs = bHome.findAll().iterator();
295       while(currentBs.hasNext()) {
296          B b = (B)currentBs.next();
297          b.remove();
298       }
299    }
300 }
301
302
303
304
Popular Tags