1 package org.apache.ojb.broker.locking; 2 3 import org.apache.ojb.broker.Article; 4 import org.apache.ojb.broker.metadata.ClassDescriptor; 5 import org.apache.ojb.broker.metadata.MetadataManager; 6 import org.apache.ojb.junit.OJBTestCase; 7 8 14 public class LockTestRepeatableReads extends OJBTestCase 15 { 16 final int testIsoLevel = LockManager.IL_REPEATABLE_READ; 17 18 public static void main(String [] args) 19 { 20 String [] arr = {LockTestRepeatableReads.class.getName()}; 21 junit.textui.TestRunner.main(arr); 22 } 23 24 public LockTestRepeatableReads(String name) 25 { 26 super(name); 27 } 28 29 Object tx1; 30 Object tx2; 31 Article obj; 32 LockManager lockManager; 33 int oldIsolationLevel; 34 ClassDescriptor cld; 35 36 public void setUp() throws Exception 37 { 38 super.setUp(); 39 40 cld = MetadataManager.getInstance().getRepository().getDescriptorFor(Article.class); 42 oldIsolationLevel = cld.getIsolationLevel(); 43 cld.setIsolationLevel(testIsoLevel); 44 45 lockManager = LockManagerHelper.getLockManagerSpecifiedByConfiguration(); 46 47 tx2 = new Object (); 49 tx1 = new Object (); 50 51 obj = Article.createInstance(); 52 } 53 54 public void tearDown() throws Exception 55 { 56 cld.setIsolationLevel(oldIsolationLevel); 58 try 59 { 60 lockManager.releaseLock(tx1, obj); 61 lockManager.releaseLock(tx2, obj); 62 } 63 finally 64 { 65 super.tearDown(); 66 } 67 } 68 69 72 public void testWriteReleaseCheckRead() 73 { 74 assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel)); 75 assertTrue(lockManager.hasRead(tx2, obj)); 76 assertTrue(lockManager.releaseLock(tx2, obj)); 77 assertFalse(lockManager.hasRead(tx2, obj)); 78 } 79 80 83 public void testReadWriteReleaseCheckRead() 84 { 85 assertTrue(lockManager.readLock(tx2, obj, testIsoLevel)); 86 assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel)); 87 assertTrue(lockManager.hasRead(tx2, obj)); 88 assertTrue(lockManager.releaseLock(tx2, obj)); 89 assertFalse(lockManager.hasRead(tx2, obj)); 90 } 91 92 95 public void testSingleReadLock() 96 { 97 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 98 } 99 100 103 public void testUpgradeReadLock() 104 { 105 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 106 assertTrue(lockManager.upgradeLock(tx1, obj, testIsoLevel)); 107 } 108 109 112 public void testReadThenWrite() 113 { 114 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 115 assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel)); 116 } 117 118 121 public void testSingleWriteLock() 122 { 123 assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel)); 124 } 125 126 129 public void testWriteThenRead() 130 { 131 assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel)); 132 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 133 } 134 135 138 public void testMultipleReadLock() 139 { 140 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 141 assertTrue(lockManager.readLock(tx2, obj, testIsoLevel)); 142 } 143 144 147 public void testUpgradeWithExistingReader() 148 { 149 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 150 assertTrue(!lockManager.upgradeLock(tx2, obj, testIsoLevel)); 151 } 152 153 156 public void testWriteWithExistingReader() 157 { 158 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 159 assertTrue(!lockManager.writeLock(tx2, obj, testIsoLevel)); 160 } 161 162 165 public void testUpgradeWithMultipleReaders() 166 { 167 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 168 assertTrue(lockManager.readLock(tx2, obj, testIsoLevel)); 169 assertTrue(!lockManager.upgradeLock(tx2, obj, testIsoLevel)); 170 } 171 172 175 public void testWriteWithMultipleReaders() 176 { 177 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 178 assertTrue(lockManager.readLock(tx2, obj, testIsoLevel)); 179 assertTrue(!lockManager.writeLock(tx2, obj, testIsoLevel)); 180 } 181 182 185 public void testUpgradeWithMultipleReadersOn1() 186 { 187 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 188 assertTrue(lockManager.readLock(tx2, obj, testIsoLevel)); 189 assertTrue(!lockManager.upgradeLock(tx1, obj, testIsoLevel)); 190 } 191 192 195 public void testWriteWithMultipleReadersOn1() 196 { 197 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 198 assertTrue(lockManager.readLock(tx2, obj, testIsoLevel)); 199 assertTrue(!lockManager.writeLock(tx1, obj, testIsoLevel)); 200 } 201 202 205 public void testReadWithExistingWriter() 206 { 207 assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel)); 208 assertTrue(!lockManager.readLock(tx2, obj, testIsoLevel)); 209 } 210 211 214 public void testMultipleWriteLock() 215 { 216 assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel)); 217 assertTrue(!lockManager.writeLock(tx2, obj, testIsoLevel)); 218 } 219 220 223 public void testReleaseReadLock() 224 { 225 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 226 assertTrue(lockManager.releaseLock(tx1, obj)); 227 assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel)); 228 } 229 230 233 public void testReleaseUpgradeLock() 234 { 235 assertTrue(lockManager.upgradeLock(tx1, obj, testIsoLevel)); 236 assertTrue(lockManager.releaseLock(tx1, obj)); 237 assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel)); 238 } 239 240 243 public void testReleaseWriteLock() 244 { 245 assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel)); 246 assertTrue(lockManager.releaseLock(tx1, obj)); 247 assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel)); 248 } 249 250 253 public void testReadThenRead() 254 { 255 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 256 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 257 } 258 } 259 | Popular Tags |