001: package org.apache.ojb.broker;
002:
003: import org.apache.ojb.junit.PBTestCase;
004:
005: /**
006: * This TestClass tests OJB facilities for optimistic locking.
007: */
008: public class OptimisticLockingTest extends PBTestCase {
009:
010: public static void main(String[] args) {
011: String[] arr = { OptimisticLockingTest.class.getName() };
012: junit.textui.TestRunner.main(arr);
013: }
014:
015: public OptimisticLockingTest(String name) {
016: super (name);
017: }
018:
019: /** Test optimistic Lock by version.*/
020: public void testVersionLock() throws Exception {
021: LockedByVersion obj = new LockedByVersion();
022: obj.setValue("original");
023: Identity oid = new Identity(obj, broker);
024: broker.beginTransaction();
025: broker.store(obj);
026: broker.commitTransaction();
027:
028: broker.clearCache();
029: LockedByVersion copy1 = (LockedByVersion) broker
030: .getObjectByIdentity(oid);
031: broker.clearCache();
032: LockedByVersion copy2 = (LockedByVersion) broker
033: .getObjectByIdentity(oid);
034:
035: copy1.setValue("copy 1");
036: copy2.setValue("copy 2");
037: assertEquals("Expect same version number", copy1.getVersion(),
038: copy2.getVersion());
039:
040: broker.beginTransaction();
041: broker.store(copy1);
042: broker.commitTransaction();
043: assertTrue("Expect different version number", copy1
044: .getVersion() != copy2.getVersion());
045: try {
046: // as copy1 has already been stored the version info of copy2
047: // is out of sync with the database !
048: broker.beginTransaction();
049: broker.store(copy2);
050: broker.commitTransaction();
051: } catch (OptimisticLockException ex) {
052: assertTrue(true);
053: //LoggerFactory.getDefaultLogger().debug(ex);
054: broker.abortTransaction();
055: return;
056: }
057: fail("Should throw an Optimistic Lock exception");
058: }
059:
060: /**
061: * demonstrates how OptimisticLockExceptions can be used
062: * to handle resynchronization of conflicting instances.
063: */
064: public void testLockHandling() throws Exception {
065: LockedByVersion obj = new LockedByVersion();
066: obj.setValue("original");
067: Identity oid = new Identity(obj, broker);
068: broker.beginTransaction();
069: broker.store(obj);
070: broker.commitTransaction();
071:
072: broker.clearCache();
073: LockedByVersion copy1 = (LockedByVersion) broker
074: .getObjectByIdentity(oid);
075: broker.clearCache();
076: LockedByVersion copy2 = (LockedByVersion) broker
077: .getObjectByIdentity(oid);
078:
079: copy1.setValue("copy 1");
080: copy2.setValue("copy 2");
081: assertEquals("Expect same version number", copy1.getVersion(),
082: copy2.getVersion());
083:
084: broker.beginTransaction();
085: broker.store(copy1);
086: broker.commitTransaction();
087: assertTrue("Expect different version number", copy1
088: .getVersion() != copy2.getVersion());
089: try {
090: // as copy1 has already been stored the version info of copy2
091: // is out of sync with the database !
092: broker.beginTransaction();
093: broker.store(copy2);
094: broker.commitTransaction();
095: } catch (OptimisticLockException ex) {
096: // obtain conflicting object from exception
097: Object conflictingObject = ex.getSourceObject();
098:
099: // get a synchronized instance
100: broker.removeFromCache(conflictingObject);
101: Object syncronizedObject = broker
102: .getObjectByIdentity(new Identity(
103: conflictingObject, broker));
104:
105: // modify synchronized copy and call store again without trouble
106: ((LockedByVersion) syncronizedObject).setValue("copy 3");
107: if (!broker.isInTransaction())
108: broker.beginTransaction();
109: broker.store(syncronizedObject);
110: broker.commitTransaction();
111: return;
112: }
113: fail("Should throw an Optimistic Lock exception");
114: }
115:
116: /** Test optimistic Lock by timestamp.*/
117: public void testTimestampLock() throws Exception {
118: LockedByTimestamp obj = new LockedByTimestamp();
119: obj.setValue("original");
120: Identity oid = new Identity(obj, broker);
121: broker.beginTransaction();
122: broker.store(obj);
123: broker.commitTransaction();
124:
125: broker.clearCache();
126: LockedByTimestamp copy1 = (LockedByTimestamp) broker
127: .getObjectByIdentity(oid);
128: broker.clearCache();
129: LockedByTimestamp copy2 = (LockedByTimestamp) broker
130: .getObjectByIdentity(oid);
131:
132: /*
133: //mysql timestamp does not support milliseconds
134: arminw:
135: For proper test we need millisecond precision, so if mysql does not support
136: this, better we let fail this test for mysql
137: */
138: Thread.sleep(50);
139:
140: copy1.setValue("copy 1");
141: copy2.setValue("copy 2");
142: assertEquals("Expect same version number",
143: copy1.getTimestamp(), copy2.getTimestamp());
144:
145: broker.beginTransaction();
146: broker.store(copy1);
147: broker.commitTransaction();
148: assertTrue("Expect different version number", copy1
149: .getTimestamp() != copy2.getTimestamp());
150: try {
151: // as copy1 has already been stored the timestamp info
152: // of copy2 is out of sync with the database !
153: broker.beginTransaction();
154: broker.store(copy2);
155: broker.commitTransaction();
156:
157: fail("Should throw an Optimistic Lock exception");
158: } catch (OptimisticLockException ex) {
159: assertTrue(true);
160: broker.abortTransaction();
161: }
162:
163: if (!broker.isInTransaction())
164: broker.beginTransaction();
165: broker.delete(copy1);
166: broker.commitTransaction();
167:
168: try {
169: broker.beginTransaction();
170: broker.delete(copy1);
171: broker.commitTransaction();
172: } catch (OptimisticLockException e) {
173: // BRJ: exception thrown if object has been modified or deleted
174: //
175: // fail("If an object which use optimistic locking was deleted two times, OJB" +
176: // " should not throw an optimistic locking exception: "+e.getMessage());
177: broker.abortTransaction();
178: return;
179: }
180: fail("Should throw an Optimistic Lock exception");
181: }
182:
183: }
|