KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Author: Matthew Baird
3  * mattbaird@yahoo.com
4  */

5 package org.apache.ojb.odmg;
6
7 import junit.framework.TestCase;
8 import org.apache.ojb.broker.Table_1Object;
9 import org.apache.ojb.broker.Table_2Object;
10 import org.apache.ojb.broker.TestHelper;
11 import org.odmg.Database;
12 import org.odmg.Implementation;
13 import org.odmg.Transaction;
14
15 /**
16  * @author MBaird mattbaird@yahoo.com
17  * Equivalent to the PB test of the same name, but redone using ODMG api.
18  *
19  * works for HSQLDB
20  * fails for MSSQL :(
21  *
22  * @see org.apache.ojb.broker.AutoIncrementWithRelatedObjectTest
23  */

24 public class AutoIncrementWithRelatedObjectTest extends TestCase
25 {
26     private static Class JavaDoc CLASS = AutoIncrementWithRelatedObjectTest.class;
27     private String JavaDoc databaseName;
28
29     public static void main(String JavaDoc[] args)
30     {
31         String JavaDoc[] arr = {CLASS.getName()};
32         junit.textui.TestRunner.main(arr);
33     }
34
35     /**
36      * since java defaults the value of an int to 0, there might be a problem storing an
37      * object that uses int's as foreign keys. If this doesn't work, it means we can't use int's
38      * as foreign keys in ODMG. :(
39      */

40     public void testCreateWithoutRelatedObject() throws Exception JavaDoc
41     {
42         Implementation odmg = OJB.getInstance();
43         Database db = odmg.newDatabase();
44         db.open(databaseName, Database.OPEN_READ_WRITE);
45         try
46         {
47             Transaction tx = odmg.newTransaction();
48             tx.begin();
49             Table_1Object table1Ojb = new Table_1Object();
50             db.makePersistent(table1Ojb);
51             tx.commit();
52         }
53         finally
54         {
55             db.close();
56         }
57     }
58
59     /**
60      * do the create with a related object to prove it works in ODMG mode.
61      */

62     public void testCreateWithRelatedObject() throws Exception JavaDoc
63     {
64         Implementation odmg = OJB.getInstance();
65         Database db = odmg.newDatabase();
66         db.open(databaseName, Database.OPEN_READ_WRITE);
67         try
68         {
69             Transaction tx = odmg.newTransaction();
70             tx.begin();
71             Table_1Object table1Obj = new Table_1Object();
72             Table_2Object table2Obj = new Table_2Object();
73             table1Obj.setTable2Object(table2Obj);
74             db.makePersistent(table2Obj);
75             db.makePersistent(table1Obj);
76             tx.commit();
77         }
78         finally
79         {
80             db.close();
81         }
82     }
83
84     /**
85      * Insert the method's description here.
86      * Creation date: (24.12.2000 00:33:40)
87      */

88     public AutoIncrementWithRelatedObjectTest(String JavaDoc name)
89     {
90         super(name);
91     }
92
93     /**
94      * Insert the method's description here.
95      * Creation date: (06.12.2000 21:58:53)
96      */

97     public void setUp()
98     {
99         databaseName = TestHelper.DEF_DATABASE_NAME;
100     }
101
102     /**
103      * Insert the method's description here.
104      * Creation date: (06.12.2000 21:59:14)
105      */

106     public void tearDown()
107     {
108     }
109 }
110
Popular Tags