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