KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > OneToOneTest


1 package org.apache.ojb.odmg;
2
3 import java.util.List JavaDoc;
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 JavaDoc[] args)
23     {
24         String JavaDoc[] arr = {OneToOneTest.class.getName()};
25         junit.textui.TestRunner.main(arr);
26     }
27
28     /**
29      * Constructor for OneToOneTest.
30      * @param arg0
31      */

32     public OneToOneTest(String JavaDoc arg0)
33     {
34         super(arg0);
35     }
36
37     protected void setUp() throws Exception JavaDoc
38     {
39         super.setUp();
40
41         m_a = new TestClassA();
42         m_b = new TestClassB();
43
44         // init a
45
m_a.setValue1("A.One");
46         m_a.setValue2("B.Two");
47         m_a.setValue3(3);
48         m_a.setB(m_b);
49
50         // init b
51
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     /**
68      * This method tests the correct assignment of a foreign
69      * key field in case that the referenced object is a
70      * dynamic proxy
71      */

72     public void testFKAssignForProxy()
73     {
74         try
75         {
76             Transaction tx = odmg.newTransaction();
77             tx.begin();
78             //create a TestClassBProxy and persist it
79
//so that when loading it again we will
80
//get a dynamic proxy object
81
TestClassBProxy b = new TestClassBProxy();
82             database.makePersistent(b);
83             tx.commit();
84
85             //reload the object created in the previous step
86
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 JavaDoc bList = (List JavaDoc) query.execute();
93             assertEquals(1, bList.size());
94
95             TestClassBProxyI bI = (TestClassBProxyI) bList.get(0);
96
97             //bI should now be a dynamic proxy
98
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             //on commit the foreign key in "a" should have been set to
106
//bOid
107
String JavaDoc 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