001: package org.apache.ojb.odmg;
002:
003: import java.util.List;
004:
005: import org.apache.ojb.broker.core.proxy.ProxyHelper;
006: import org.apache.ojb.junit.ODMGTestCase;
007: import org.apache.ojb.odmg.shared.TestClassA;
008: import org.apache.ojb.odmg.shared.TestClassAWithBProxy;
009: import org.apache.ojb.odmg.shared.TestClassB;
010: import org.apache.ojb.odmg.shared.TestClassBProxy;
011: import org.apache.ojb.odmg.shared.TestClassBProxyI;
012: import org.odmg.ODMGException;
013: import org.odmg.OQLQuery;
014: import org.odmg.Transaction;
015:
016: public class OneToOneTest extends ODMGTestCase {
017: TestClassA m_a;
018: TestClassB m_b;
019:
020: public static void main(String[] args) {
021: String[] arr = { OneToOneTest.class.getName() };
022: junit.textui.TestRunner.main(arr);
023: }
024:
025: /**
026: * Constructor for OneToOneTest.
027: * @param arg0
028: */
029: public OneToOneTest(String arg0) {
030: super (arg0);
031: }
032:
033: protected void setUp() throws Exception {
034: super .setUp();
035:
036: m_a = new TestClassA();
037: m_b = new TestClassB();
038:
039: // init a
040: m_a.setValue1("A.One");
041: m_a.setValue2("B.Two");
042: m_a.setValue3(3);
043: m_a.setB(m_b);
044:
045: // init b
046: m_b.setValue1("B.One");
047: m_b.setA(m_a);
048: }
049:
050: public void testSave() {
051: Transaction tx = odmg.newTransaction();
052: tx.begin();
053: database.makePersistent(m_a);
054: database.makePersistent(m_b);
055: tx.commit();
056:
057: assertTrue(m_a.getOid() != null);
058: assertTrue(m_b.getOid() != null);
059: }
060:
061: /**
062: * This method tests the correct assignment of a foreign
063: * key field in case that the referenced object is a
064: * dynamic proxy
065: */
066: public void testFKAssignForProxy() {
067: try {
068: Transaction tx = odmg.newTransaction();
069: tx.begin();
070: //create a TestClassBProxy and persist it
071: //so that when loading it again we will
072: //get a dynamic proxy object
073: TestClassBProxy b = new TestClassBProxy();
074: database.makePersistent(b);
075: tx.commit();
076:
077: //reload the object created in the previous step
078: tx = odmg.newTransaction();
079: tx.begin();
080: OQLQuery query = odmg.newOQLQuery();
081: query.create("select bproxies from "
082: + TestClassBProxy.class.getName()
083: + " where oid = $1");
084: query.bind(b.getOid());
085: List bList = (List) query.execute();
086: assertEquals(1, bList.size());
087:
088: TestClassBProxyI bI = (TestClassBProxyI) bList.get(0);
089:
090: //bI should now be a dynamic proxy
091: assertTrue(ProxyHelper.isProxy(bI));
092:
093: TestClassAWithBProxy a = new TestClassAWithBProxy();
094: a.setBProxy(bI);
095: tx.lock(a, Transaction.WRITE);
096: tx.commit();
097:
098: //on commit the foreign key in "a" should have been set to
099: //bOid
100: String aBOid = a.getBoid();
101:
102: assertEquals("foreign key should match", b.getOid(), aBOid);
103:
104: } catch (ODMGException ex) {
105: fail("ODMGException" + ex);
106: }
107: }
108: }
|