1 package org.apache.ojb.broker; 2 3 import org.apache.ojb.junit.PBTestCase; 4 5 8 public class OptimisticLockingTest extends PBTestCase 9 { 10 11 public static void main(String [] args) 12 { 13 String [] arr = {OptimisticLockingTest.class.getName()}; 14 junit.textui.TestRunner.main(arr); 15 } 16 17 public OptimisticLockingTest(String name) 18 { 19 super(name); 20 } 21 22 23 public void testVersionLock() throws Exception 24 { 25 LockedByVersion obj = new LockedByVersion(); 26 obj.setValue("original"); 27 Identity oid = new Identity(obj, broker); 28 broker.beginTransaction(); 29 broker.store(obj); 30 broker.commitTransaction(); 31 32 broker.clearCache(); 33 LockedByVersion copy1 = (LockedByVersion) broker.getObjectByIdentity(oid); 34 broker.clearCache(); 35 LockedByVersion copy2 = (LockedByVersion) broker.getObjectByIdentity(oid); 36 37 copy1.setValue("copy 1"); 38 copy2.setValue("copy 2"); 39 assertEquals("Expect same version number", copy1.getVersion(), copy2.getVersion()); 40 41 broker.beginTransaction(); 42 broker.store(copy1); 43 broker.commitTransaction(); 44 assertTrue("Expect different version number", copy1.getVersion() != copy2.getVersion()); 45 try 46 { 47 broker.beginTransaction(); 50 broker.store(copy2); 51 broker.commitTransaction(); 52 } 53 catch (OptimisticLockException ex) 54 { 55 assertTrue(true); 56 broker.abortTransaction(); 58 return; 59 } 60 fail("Should throw an Optimistic Lock exception"); 61 } 62 63 67 public void testLockHandling() throws Exception 68 { 69 LockedByVersion obj = new LockedByVersion(); 70 obj.setValue("original"); 71 Identity oid = new Identity(obj, broker); 72 broker.beginTransaction(); 73 broker.store(obj); 74 broker.commitTransaction(); 75 76 broker.clearCache(); 77 LockedByVersion copy1 = (LockedByVersion) broker.getObjectByIdentity(oid); 78 broker.clearCache(); 79 LockedByVersion copy2 = (LockedByVersion) broker.getObjectByIdentity(oid); 80 81 copy1.setValue("copy 1"); 82 copy2.setValue("copy 2"); 83 assertEquals("Expect same version number", copy1.getVersion(), copy2.getVersion()); 84 85 broker.beginTransaction(); 86 broker.store(copy1); 87 broker.commitTransaction(); 88 assertTrue("Expect different version number", copy1.getVersion() != copy2.getVersion()); 89 try 90 { 91 broker.beginTransaction(); 94 broker.store(copy2); 95 broker.commitTransaction(); 96 } 97 catch (OptimisticLockException ex) 98 { 99 Object conflictingObject = ex.getSourceObject(); 101 102 broker.removeFromCache(conflictingObject); 104 Object syncronizedObject = broker.getObjectByIdentity(new Identity(conflictingObject, broker)); 105 106 ((LockedByVersion) syncronizedObject).setValue("copy 3"); 108 if(!broker.isInTransaction()) broker.beginTransaction(); 109 broker.store(syncronizedObject); 110 broker.commitTransaction(); 111 return; 112 } 113 fail("Should throw an Optimistic Lock exception"); 114 } 115 116 117 118 public void testTimestampLock() throws Exception 119 { 120 LockedByTimestamp obj = new LockedByTimestamp(); 121 obj.setValue("original"); 122 Identity oid = new Identity(obj, broker); 123 broker.beginTransaction(); 124 broker.store(obj); 125 broker.commitTransaction(); 126 127 broker.clearCache(); 128 LockedByTimestamp copy1 = (LockedByTimestamp) broker.getObjectByIdentity(oid); 129 broker.clearCache(); 130 LockedByTimestamp copy2 = (LockedByTimestamp) broker.getObjectByIdentity(oid); 131 132 138 Thread.sleep(50); 139 140 copy1.setValue("copy 1"); 141 copy2.setValue("copy 2"); 142 assertEquals("Expect same version number", copy1.getTimestamp(), copy2.getTimestamp()); 143 144 broker.beginTransaction(); 145 broker.store(copy1); 146 broker.commitTransaction(); 147 assertTrue("Expect different version number", copy1.getTimestamp() != copy2.getTimestamp()); 148 try 149 { 150 broker.beginTransaction(); 153 broker.store(copy2); 154 broker.commitTransaction(); 155 156 fail("Should throw an Optimistic Lock exception"); 157 } 158 catch (OptimisticLockException ex) 159 { 160 assertTrue(true); 161 broker.abortTransaction(); 162 } 163 164 if(!broker.isInTransaction()) broker.beginTransaction(); 165 broker.delete(copy1); 166 broker.commitTransaction(); 167 168 try 169 { 170 broker.beginTransaction(); 171 broker.delete(copy1); 172 broker.commitTransaction(); 173 } 174 catch (OptimisticLockException e) 175 { 176 broker.abortTransaction(); 181 return; 182 } 183 fail("Should throw an Optimistic Lock exception"); 184 } 185 186 } 187 | Popular Tags |