1 package org.apache.ojb.odmg; 2 3 import java.util.List ; 4 5 import org.apache.ojb.broker.core.proxy.ProxyHelper; 6 import org.apache.ojb.junit.ODMGTestCase; 7 import org.apache.ojb.odmg.shared.TestClassA; 8 import org.apache.ojb.odmg.shared.TestClassAWithBProxy; 9 import org.apache.ojb.odmg.shared.TestClassB; 10 import org.apache.ojb.odmg.shared.TestClassBProxy; 11 import org.apache.ojb.odmg.shared.TestClassBProxyI; 12 import org.odmg.ODMGException; 13 import org.odmg.OQLQuery; 14 import org.odmg.Transaction; 15 16 17 public class OneToOneTest extends ODMGTestCase 18 { 19 TestClassA m_a; 20 TestClassB m_b; 21 22 public static void main(String [] args) 23 { 24 String [] arr = {OneToOneTest.class.getName()}; 25 junit.textui.TestRunner.main(arr); 26 } 27 28 32 public OneToOneTest(String arg0) 33 { 34 super(arg0); 35 } 36 37 protected void setUp() throws Exception 38 { 39 super.setUp(); 40 41 m_a = new TestClassA(); 42 m_b = new TestClassB(); 43 44 m_a.setValue1("A.One"); 46 m_a.setValue2("B.Two"); 47 m_a.setValue3(3); 48 m_a.setB(m_b); 49 50 m_b.setValue1("B.One"); 52 m_b.setA(m_a); 53 } 54 55 public void testSave() 56 { 57 Transaction tx = odmg.newTransaction(); 58 tx.begin(); 59 database.makePersistent(m_a); 60 database.makePersistent(m_b); 61 tx.commit(); 62 63 assertTrue(m_a.getOid() != null); 64 assertTrue(m_b.getOid() != null); 65 } 66 67 72 public void testFKAssignForProxy() 73 { 74 try 75 { 76 Transaction tx = odmg.newTransaction(); 77 tx.begin(); 78 TestClassBProxy b = new TestClassBProxy(); 82 database.makePersistent(b); 83 tx.commit(); 84 85 tx = odmg.newTransaction(); 87 tx.begin(); 88 OQLQuery query = odmg.newOQLQuery(); 89 query.create("select bproxies from " + TestClassBProxy.class.getName() + 90 " where oid = $1"); 91 query.bind(b.getOid()); 92 List bList = (List ) query.execute(); 93 assertEquals(1, bList.size()); 94 95 TestClassBProxyI bI = (TestClassBProxyI) bList.get(0); 96 97 assertTrue(ProxyHelper.isProxy(bI)); 99 100 TestClassAWithBProxy a = new TestClassAWithBProxy(); 101 a.setBProxy(bI); 102 tx.lock(a, Transaction.WRITE); 103 tx.commit(); 104 105 String aBOid = a.getBoid(); 108 109 assertEquals("foreign key should match", b.getOid(), aBOid); 110 111 } catch(ODMGException ex) { 112 fail("ODMGException" + ex); 113 } 114 } 115 } 116 | Popular Tags |