001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.relationship.oneToManyUnidirectional;
023:
024: import java.util.Collection;
025: import java.util.Iterator;
026: import javax.naming.InitialContext;
027: import junit.framework.Test;
028: import junit.framework.TestCase;
029: import org.jboss.test.util.ejb.EJBTestCase;
030: import org.jboss.test.JBossTestCase;
031:
032: public class ABTest extends EJBTestCase {
033: static org.apache.log4j.Category log = org.apache.log4j.Category
034: .getInstance(ABTest.class);
035:
036: public static Test suite() throws Exception {
037: return JBossTestCase.getDeploySetup(ABTest.class,
038: "cmp2-relationship.jar");
039: }
040:
041: public ABTest(String name) {
042: super (name);
043: }
044:
045: private AHome getTableAHome() {
046: try {
047: InitialContext jndiContext = new InitialContext();
048:
049: return (AHome) jndiContext
050: .lookup("relation/oneToMany/unidirectional/table/A");
051: } catch (Exception e) {
052: log.debug("failed", e);
053: fail("Exception in getTableAHome: " + e.getMessage());
054: }
055: return null;
056: }
057:
058: private BHome getTableBHome() {
059: try {
060: InitialContext jndiContext = new InitialContext();
061:
062: return (BHome) jndiContext
063: .lookup("relation/oneToMany/unidirectional/table/B");
064: } catch (Exception e) {
065: log.debug("failed", e);
066: fail("Exception in getTableBHome: " + e.getMessage());
067: }
068: return null;
069: }
070:
071: private AHome getFKAHome() {
072: try {
073: InitialContext jndiContext = new InitialContext();
074:
075: return (AHome) jndiContext
076: .lookup("relation/oneToMany/unidirectional/fk/A");
077: } catch (Exception e) {
078: log.debug("failed", e);
079: fail("Exception in getFKAHome: " + e.getMessage());
080: }
081: return null;
082: }
083:
084: private BHome getFKBHome() {
085: try {
086: InitialContext jndiContext = new InitialContext();
087:
088: return (BHome) jndiContext
089: .lookup("relation/oneToMany/unidirectional/fk/B");
090: } catch (Exception e) {
091: log.debug("failed", e);
092: fail("Exception in getFKBHome: " + e.getMessage());
093: }
094: return null;
095: }
096:
097: // a1.setB(a2.getB());
098: public void Xtest_a1SetB_a2GetB_Table() throws Exception {
099: AHome aHome = getTableAHome();
100: BHome bHome = getTableBHome();
101: a1SetB_a2GetB(aHome, bHome);
102: }
103:
104: // a1.setB(a2.getB());
105: public void test_a1SetB_a2GetB_FK() throws Exception {
106: AHome aHome = getFKAHome();
107: BHome bHome = getFKBHome();
108: a1SetB_a2GetB(aHome, bHome);
109: }
110:
111: // a1.setB(a2.getB());
112: private void a1SetB_a2GetB(AHome aHome, BHome bHome)
113: throws Exception {
114: // Before change:
115: A a1 = aHome.create(new Integer(1));
116: A a2 = aHome.create(new Integer(2));
117:
118: Collection b1 = a1.getB();
119: a1.getId();
120: assertTrue(b1.isEmpty());
121:
122: Collection b2 = a2.getB();
123: a2.getId();
124: assertTrue(b2.isEmpty());
125:
126: B[] b1x = new B[20];
127: B[] b2x = new B[30];
128:
129: for (int i = 0; i < b1x.length; i++) {
130: b1x[i] = bHome.create(new Integer(10000 + i));
131: b1.add(b1x[i]);
132: }
133:
134: for (int i = 0; i < b2x.length; i++) {
135: b2x[i] = bHome.create(new Integer(20000 + i));
136: b2.add(b2x[i]);
137: }
138:
139: // B b11, b12, ... , b1n; members of b1
140: for (int i = 0; i < b1x.length; i++) {
141: assertTrue(b1.contains(b1x[i]));
142: }
143:
144: // B b21, b22, ... , b2m; members of b2
145: for (int i = 0; i < b2x.length; i++) {
146: assertTrue(b2.contains(b2x[i]));
147: }
148:
149: // Change:
150: a1.setB(a2.getB());
151:
152: // Expected result:
153:
154: // a2.getB().isEmpty()
155: assertTrue(a2.getB().isEmpty());
156:
157: // b2.isEmpty()
158: assertTrue(b2.isEmpty());
159:
160: // b1 == a1.getB()
161: assertTrue(b1 == a1.getB());
162:
163: // b2 == a2.getB()
164: assertTrue(b2 == a2.getB());
165:
166: // a1.getB().contains(b21)
167: // a1.getB().contains(b22)
168: // a1.getB().contains(...)
169: // a1.getB().contains(b2m)
170: for (int i = 0; i < b2x.length; i++) {
171: assertTrue(a1.getB().contains(b2x[i]));
172: }
173: }
174:
175: // a1.getB().add(b2m);
176: public void Xtest_a1GetB_addB2m_Table() throws Exception {
177: AHome aHome = getTableAHome();
178: BHome bHome = getTableBHome();
179: a1GetB_addB2m(aHome, bHome);
180: }
181:
182: // a1.getB().add(b2m);
183: public void Xtest_a1GetB_addB2m_FK() throws Exception {
184: AHome aHome = getFKAHome();
185: BHome bHome = getFKBHome();
186: a1GetB_addB2m(aHome, bHome);
187: }
188:
189: // a1.getB().add(b2m);
190: private void a1GetB_addB2m(AHome aHome, BHome bHome)
191: throws Exception {
192: // Before change:
193: A a1 = aHome.create(new Integer(1));
194: A a2 = aHome.create(new Integer(2));
195:
196: Collection b1 = a1.getB();
197: Collection b2 = a2.getB();
198:
199: B[] b1x = new B[20];
200: B[] b2x = new B[30];
201:
202: for (int i = 0; i < b1x.length; i++) {
203: b1x[i] = bHome.create(new Integer(10000 + i));
204: b1.add(b1x[i]);
205: }
206:
207: for (int i = 0; i < b2x.length; i++) {
208: b2x[i] = bHome.create(new Integer(20000 + i));
209: b2.add(b2x[i]);
210: }
211:
212: // B b11, b12, ... , b1n; members of b1
213: for (int i = 0; i < b1x.length; i++) {
214: assertTrue(b1.contains(b1x[i]));
215: }
216:
217: // B b21, b22, ... , b2m; members of b2
218: for (int i = 0; i < b2x.length; i++) {
219: assertTrue(b2.contains(b2x[i]));
220: }
221:
222: // Change:
223:
224: // a1.getB().add(b2m);
225: a1.getB().add(b2x[b2x.length - 1]);
226:
227: // Expected result:
228:
229: // b1.contains(b11)
230: // b1.contains(b12)
231: // b1.contains(...)
232: // b1.contains(b1n)
233: for (int i = 0; i < b1x.length; i++) {
234: assertTrue(b1.contains(b1x[i]));
235: }
236:
237: // b1.contains(b2m)
238: assertTrue(b1.contains(b2x[b2x.length - 1]));
239:
240: // b2.contains(b21)
241: // b2.contains(b22)
242: // b2.contains(...)
243: // b2.contains(b2m_1)
244: for (int i = 0; i < b2x.length - 1; i++) {
245: assertTrue(b2.contains(b2x[i]));
246: }
247: }
248:
249: // a1.getB().remove(b1n);
250: public void Xtest_a1GetB_removeB1n_Table() throws Exception {
251: AHome aHome = getTableAHome();
252: BHome bHome = getTableBHome();
253: a1GetB_removeB1n(aHome, bHome);
254: }
255:
256: // a1.getB().remove(b1n);
257: public void Xtest_a1GetB_removeB1n_FK() throws Exception {
258: AHome aHome = getFKAHome();
259: BHome bHome = getFKBHome();
260: a1GetB_removeB1n(aHome, bHome);
261: }
262:
263: // a1.getB().remove(b1n);
264: private void a1GetB_removeB1n(AHome aHome, BHome bHome)
265: throws Exception {
266: // Before change:
267: A a1 = aHome.create(new Integer(1));
268: A a2 = aHome.create(new Integer(2));
269:
270: Collection b1 = a1.getB();
271: Collection b2 = a2.getB();
272:
273: B[] b1x = new B[20];
274: B[] b2x = new B[30];
275:
276: for (int i = 0; i < b1x.length; i++) {
277: b1x[i] = bHome.create(new Integer(10000 + i));
278: b1.add(b1x[i]);
279: }
280:
281: for (int i = 0; i < b2x.length; i++) {
282: b2x[i] = bHome.create(new Integer(20000 + i));
283: b2.add(b2x[i]);
284: }
285:
286: // B b11, b12, ... , b1n; members of b1
287: for (int i = 0; i < b1x.length; i++) {
288: assertTrue(b1.contains(b1x[i]));
289: }
290:
291: // B b21, b22, ... , b2m; members of b2
292: for (int i = 0; i < b2x.length; i++) {
293: assertTrue(b2.contains(b2x[i]));
294: }
295:
296: // Change:
297:
298: // a1.getB().remove(b1n);
299: a1.getB().remove(b1x[b1x.length - 1]);
300:
301: // Expected result:
302:
303: // b1 == a1.getB()
304: assertTrue(b1 == a1.getB());
305:
306: // b1.contains(b11)
307: // b1.contains(b12)
308: // b1.contains(...)
309: // b1.contains(b1n_1)
310: for (int i = 0; i < b1x.length - 1; i++) {
311: assertTrue(b1.contains(b1x[i]));
312: }
313:
314: // !(b1.contains(b1n))
315: assertTrue(!(b1.contains(b1x[b1x.length - 1])));
316: }
317:
318: public void setUpEJB() throws Exception {
319: AHome aHome;
320: BHome bHome;
321:
322: aHome = getTableAHome();
323: bHome = getTableBHome();
324: deleteAllAsAndBs(aHome, bHome);
325:
326: aHome = getFKAHome();
327: bHome = getFKBHome();
328: deleteAllAsAndBs(aHome, bHome);
329: }
330:
331: public void tearDownEJB() throws Exception {
332: }
333:
334: public void deleteAllAsAndBs(AHome aHome, BHome bHome)
335: throws Exception {
336: // delete all As
337: Iterator currentAs = aHome.findAll().iterator();
338: while (currentAs.hasNext()) {
339: A a = (A) currentAs.next();
340: a.remove();
341: }
342:
343: // delete all Bs
344: Iterator currentBs = bHome.findAll().iterator();
345: while (currentBs.hasNext()) {
346: B b = (B) currentBs.next();
347: b.remove();
348: }
349: }
350: }
|