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.manyToOneUnidirectional;
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: return (AHome) jndiContext
049: .lookup("relation/manyToOne/unidirectional/table/A");
050: } catch (Exception e) {
051: log.debug("failed", e);
052: fail("Exception in getTableAHome: " + e.getMessage());
053: }
054: return null;
055: }
056:
057: private BHome getTableBHome() {
058: try {
059: InitialContext jndiContext = new InitialContext();
060:
061: return (BHome) jndiContext
062: .lookup("relation/manyToOne/unidirectional/table/B");
063: } catch (Exception e) {
064: log.debug("failed", e);
065: fail("Exception in getTableBHome: " + e.getMessage());
066: }
067: return null;
068: }
069:
070: private AHome getFKAHome() {
071: try {
072: InitialContext jndiContext = new InitialContext();
073:
074: return (AHome) jndiContext
075: .lookup("relation/manyToOne/unidirectional/fk/A");
076: } catch (Exception e) {
077: log.debug("failed", e);
078: fail("Exception in getFKAHome: " + e.getMessage());
079: }
080: return null;
081: }
082:
083: private BHome getFKBHome() {
084: try {
085: InitialContext jndiContext = new InitialContext();
086:
087: return (BHome) jndiContext
088: .lookup("relation/manyToOne/unidirectional/fk/B");
089: } catch (Exception e) {
090: log.debug("failed", e);
091: fail("Exception in getFKBHome: " + e.getMessage());
092: }
093: return null;
094: }
095:
096: // b1j.setA(b2k.getA());
097: public void test_b1jSetA_b2kGetA_Table() throws Exception {
098: AHome aHome = getTableAHome();
099: BHome bHome = getTableBHome();
100: b1jSetA_b2kGetA(aHome, bHome);
101: }
102:
103: // b1j.setA(b2k.getA());
104: public void test_b1jSetA_b2kGetA_FK() throws Exception {
105: AHome aHome = getFKAHome();
106: BHome bHome = getFKBHome();
107: b1jSetA_b2kGetA(aHome, bHome);
108: }
109:
110: // b1j.setA(b2k.getA());
111: private void b1jSetA_b2kGetA(AHome aHome, BHome bHome)
112: throws Exception {
113: // Before change:
114: A a1 = aHome.create(new Integer(1));
115: A a2 = aHome.create(new Integer(2));
116:
117: B[] b1x = new B[20];
118: B[] b2x = new B[30];
119:
120: for (int i = 0; i < b1x.length; i++) {
121: b1x[i] = bHome.create(new Integer(10000 + i));
122: b1x[i].setA(a1);
123: }
124:
125: for (int i = 0; i < b2x.length; i++) {
126: b2x[i] = bHome.create(new Integer(20000 + i));
127: b2x[i].setA(a2);
128: }
129:
130: // (a1.isIdentical(b11.getA())) && ... && (a1.isIdentical(b1n.getA()
131: for (int i = 0; i < b1x.length; i++) {
132: a1.isIdentical(b1x[i].getA());
133: }
134:
135: // (a2.isIdentical(b21.getA())) && ... && (a2.isIdentical(b2m.getA()
136: for (int i = 0; i < b2x.length; i++) {
137: a2.isIdentical(b2x[i].getA());
138: }
139:
140: // Change:
141:
142: // b1j.setA(b2k.getA());
143: int j = b1x.length / 3;
144: int k = b2x.length / 2;
145: b1x[j].setA(b2x[k].getA());
146:
147: // Expected result:
148:
149: // a1.isIdentical(b11.getA())
150: // a1.isIdentical(b12.getA())
151: // ...
152: // a2.isIdentical(b1j.getA())
153: // ...
154: // a1.isIdentical(b1n.getA())
155: for (int i = 0; i < b1x.length; i++) {
156: if (i != j) {
157: assertTrue(a1.isIdentical(b1x[i].getA()));
158: } else {
159: assertTrue(a2.isIdentical(b1x[i].getA()));
160: }
161: }
162:
163: // a2.isIdentical(b21.getA())
164: // a2.isIdentical(b22.getA())
165: // ...
166: // a2.isIdentical(b2k.getA())
167: // ...
168: // a2.isIdentical(b2m.getA())
169: for (int i = 0; i < b2x.length; i++) {
170: assertTrue(a2.isIdentical(b2x[i].getA()));
171: }
172: }
173:
174: public void setUpEJB() throws Exception {
175: AHome aHome;
176: BHome bHome;
177:
178: aHome = getTableAHome();
179: bHome = getTableBHome();
180: deleteAllAsAndBs(aHome, bHome);
181:
182: aHome = getFKAHome();
183: bHome = getFKBHome();
184: deleteAllAsAndBs(aHome, bHome);
185: }
186:
187: public void tearDownEJB() throws Exception {
188: }
189:
190: public void deleteAllAsAndBs(AHome aHome, BHome bHome)
191: throws Exception {
192: // delete all As
193: Iterator currentAs = aHome.findAll().iterator();
194: while (currentAs.hasNext()) {
195: A a = (A) currentAs.next();
196: a.remove();
197: }
198:
199: // delete all Bs
200: Iterator currentBs = bHome.findAll().iterator();
201: while (currentBs.hasNext()) {
202: B b = (B) currentBs.next();
203: b.remove();
204: }
205: }
206: }
|