KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > sequence > AutoIncrementTest


1 package org.apache.ojb.broker.sequence;
2
3 import org.apache.commons.lang.builder.ToStringBuilder;
4 import org.apache.ojb.broker.Identity;
5 import org.apache.ojb.junit.PBTestCase;
6
7 /**
8  * Test autoincrement behaviour of PK and non PK fields.
9  *
10  * @author <a HREF="mailto:arminw@apache.org">Armin Waibel</a>
11  * @version $Id: AutoIncrementTest.java,v 1.2 2004/04/19 16:39:35 arminw Exp $
12  */

13 public class AutoIncrementTest extends PBTestCase
14 {
15    public static void main(String JavaDoc[] args)
16     {
17         String JavaDoc[] arr = {AutoIncrementTest.class.getName()};
18         junit.textui.TestRunner.main(arr);
19     }
20
21     public void testAutoIncrement()
22     {
23         IncrementObject obj_1 = new IncrementObject();
24         IncrementObject obj_2 = new IncrementObject();
25         broker.beginTransaction();
26         broker.store(obj_1);
27         broker.store(obj_2);
28         broker.commitTransaction();
29         // we assume different autogenerated values > 0
30
assertTrue(obj_1.getIntId() < obj_2.getIntId());
31         assertTrue(obj_1.getIntegerId().intValue() < obj_2.getIntegerId().intValue());
32         assertTrue(!obj_1.getStr().equals(obj_2.getStr()));
33         assertTrue(obj_2.getIntId() > 0);
34         assertTrue(obj_2.getIntegerId().intValue() > 0);
35         assertTrue((new Integer JavaDoc(obj_2.getStr())).intValue() > 0);
36
37         Identity oid = new Identity(obj_2, broker);
38         IncrementObject newObj_2 = (IncrementObject) broker.getObjectByIdentity(oid);
39         broker.beginTransaction();
40         broker.store(obj_1);
41         broker.store(obj_2);
42         broker.commitTransaction();
43         assertEquals(obj_2.getIntId(), newObj_2.getIntId());
44         assertEquals(obj_2.getIntegerId(), newObj_2.getIntegerId());
45         assertEquals(obj_2.getStr(), newObj_2.getStr());
46     }
47
48     public static class IncrementObject
49     {
50         int intId;
51         Integer JavaDoc integerId;
52         String JavaDoc str;
53
54         public IncrementObject()
55         {
56         }
57
58         public Integer JavaDoc getIntegerId()
59         {
60             return integerId;
61         }
62
63         public void setIntegerId(Integer JavaDoc integerId)
64         {
65             this.integerId = integerId;
66         }
67
68         public int getIntId()
69         {
70             return intId;
71         }
72
73         public void setIntId(int intId)
74         {
75             this.intId = intId;
76         }
77
78         public String JavaDoc getStr()
79         {
80             return str;
81         }
82
83         public void setStr(String JavaDoc str)
84         {
85             this.str = str;
86         }
87
88         public String JavaDoc toString()
89         {
90             return ToStringBuilder.reflectionToString(this);
91         }
92     }
93 }
94
Popular Tags