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.oneToOneUnidirectional;
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/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/oneToOne/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/oneToOne/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/oneToOne/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: 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:
147: public void setUpEJB() throws Exception {
148: AHome aHome;
149: BHome bHome;
150:
151: aHome = getTableAHome();
152: bHome = getTableBHome();
153: deleteAllAsAndBs(aHome, bHome);
154:
155: aHome = getFKAHome();
156: bHome = getFKBHome();
157: deleteAllAsAndBs(aHome, bHome);
158: }
159:
160: public void tearDownEJB() throws Exception {
161: }
162:
163: public void deleteAllAsAndBs(AHome aHome, BHome bHome)
164: throws Exception {
165: // delete all As
166: Iterator currentAs = aHome.findAll().iterator();
167: while (currentAs.hasNext()) {
168: A a = (A) currentAs.next();
169: a.remove();
170: }
171:
172: // delete all Bs
173: Iterator currentBs = bHome.findAll().iterator();
174: while (currentBs.hasNext()) {
175: B b = (B) currentBs.next();
176: b.remove();
177: }
178: }
179: }
|