01: package org.apache.ojb.broker.sequence;
02:
03: import org.apache.commons.lang.builder.ToStringBuilder;
04: import org.apache.ojb.broker.Identity;
05: import org.apache.ojb.junit.PBTestCase;
06:
07: /**
08: * Test autoincrement behaviour of PK and non PK fields.
09: *
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: public static void main(String[] args) {
15: String[] arr = { AutoIncrementTest.class.getName() };
16: junit.textui.TestRunner.main(arr);
17: }
18:
19: public void testAutoIncrement() {
20: IncrementObject obj_1 = new IncrementObject();
21: IncrementObject obj_2 = new IncrementObject();
22: broker.beginTransaction();
23: broker.store(obj_1);
24: broker.store(obj_2);
25: broker.commitTransaction();
26: // we assume different autogenerated values > 0
27: assertTrue(obj_1.getIntId() < obj_2.getIntId());
28: assertTrue(obj_1.getIntegerId().intValue() < obj_2
29: .getIntegerId().intValue());
30: assertTrue(!obj_1.getStr().equals(obj_2.getStr()));
31: assertTrue(obj_2.getIntId() > 0);
32: assertTrue(obj_2.getIntegerId().intValue() > 0);
33: assertTrue((new Integer(obj_2.getStr())).intValue() > 0);
34:
35: Identity oid = new Identity(obj_2, broker);
36: IncrementObject newObj_2 = (IncrementObject) broker
37: .getObjectByIdentity(oid);
38: broker.beginTransaction();
39: broker.store(obj_1);
40: broker.store(obj_2);
41: broker.commitTransaction();
42: assertEquals(obj_2.getIntId(), newObj_2.getIntId());
43: assertEquals(obj_2.getIntegerId(), newObj_2.getIntegerId());
44: assertEquals(obj_2.getStr(), newObj_2.getStr());
45: }
46:
47: public static class IncrementObject {
48: int intId;
49: Integer integerId;
50: String str;
51:
52: public IncrementObject() {
53: }
54:
55: public Integer getIntegerId() {
56: return integerId;
57: }
58:
59: public void setIntegerId(Integer integerId) {
60: this .integerId = integerId;
61: }
62:
63: public int getIntId() {
64: return intId;
65: }
66:
67: public void setIntId(int intId) {
68: this .intId = intId;
69: }
70:
71: public String getStr() {
72: return str;
73: }
74:
75: public void setStr(String str) {
76: this .str = str;
77: }
78:
79: public String toString() {
80: return ToStringBuilder.reflectionToString(this);
81: }
82: }
83: }
|