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.oneToOneBidirectional;
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/oneToOne/bidirectional/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/oneToOne/bidirectional/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/oneToOne/bidirectional/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/oneToOne/bidirectional/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: private A a1;
098: private A a2;
099: private B b1;
100: private B b2;
101:
102: protected void beforeChange(AHome aHome, BHome bHome)
103: throws Exception {
104: a1 = aHome.create(new Integer(1));
105: a2 = aHome.create(new Integer(2));
106: b1 = bHome.create(new Integer(10));
107: b2 = bHome.create(new Integer(20));
108: a1.setB(b1);
109: a2.setB(b2);
110:
111: assertTrue(b1.isIdentical(a1.getB()));
112: assertTrue(b2.isIdentical(a2.getB()));
113: }
114:
115: // a1.setB(a2.getB());
116: public void test_a1SetB_a2GetB_Table() throws Exception {
117: AHome aHome = getTableAHome();
118: BHome bHome = getTableBHome();
119:
120: beforeChange(aHome, bHome);
121: a1SetB_a2GetB(aHome, bHome);
122: }
123:
124: // a1.setB(a2.getB());
125: public void test_a1SetB_a2GetB_FK() throws Exception {
126: AHome aHome = getFKAHome();
127: BHome bHome = getFKBHome();
128: beforeChange(aHome, bHome);
129: a1SetB_a2GetB(aHome, bHome);
130: }
131:
132: // a1.setB(a2.getB());
133: protected void a1SetB_a2GetB(AHome aHome, BHome bHome)
134: throws Exception {
135: // Change:
136: a1.setB(a2.getB());
137:
138: // Expected result:
139:
140: // b2.isIdentical(a1.getB())
141: assertTrue(b2.isIdentical(a1.getB()));
142:
143: // a2.getB() == null
144: assertNull(a2.getB());
145:
146: // b1.getA() == null
147: assertNull(b1.getA());
148:
149: // a1.isIdentical(b2.getA())
150: assertTrue(a1.isIdentical(b2.getA()));
151: }
152:
153: public void setUpEJB() throws Exception {
154: AHome aHome;
155: BHome bHome;
156:
157: aHome = getTableAHome();
158: bHome = getTableBHome();
159: deleteAllAsAndBs(aHome, bHome);
160:
161: aHome = getFKAHome();
162: bHome = getFKBHome();
163: deleteAllAsAndBs(aHome, bHome);
164: }
165:
166: public void tearDownEJB() throws Exception {
167: }
168:
169: public void deleteAllAsAndBs(AHome aHome, BHome bHome)
170: throws Exception {
171: // delete all As
172: Iterator currentAs = aHome.findAll().iterator();
173: while (currentAs.hasNext()) {
174: A a = (A) currentAs.next();
175: a.remove();
176: }
177:
178: // delete all Bs
179: Iterator currentBs = bHome.findAll().iterator();
180: while (currentBs.hasNext()) {
181: B b = (B) currentBs.next();
182: b.remove();
183: }
184: }
185: }
|