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 12 public class CommonsLockTestSerializable extends OJBTestCase 13 { 14 final int testIsoLevel = LockManager.IL_SERIALIZABLE; 15 16 public static void main(String [] args) 17 { 18 String [] arr = {CommonsLockTestSerializable.class.getName()}; 19 junit.textui.TestRunner.main(arr); 20 } 21 22 public CommonsLockTestSerializable(String name) 23 { 24 super(name); 25 } 26 27 Object tx1; 28 Object tx2; 29 org.apache.ojb.broker.Article obj; 30 LockManager lockManager; 31 int oldIsolationLevel; 32 ClassDescriptor cld; 33 34 public void setUp() throws Exception 35 { 36 super.setUp(); 37 38 cld = MetadataManager.getInstance().getRepository().getDescriptorFor(org.apache.ojb.broker.Article.class); 40 oldIsolationLevel = cld.getIsolationLevel(); 41 cld.setIsolationLevel(testIsoLevel); 42 43 lockManager = LockManagerHelper.getCommonsLockManager(); 44 45 tx2 = new Object (); 47 tx1 = new Object (); 48 49 obj = org.apache.ojb.broker.Article.createInstance(); 50 } 51 52 public void tearDown() throws Exception 53 { 54 cld.setIsolationLevel(oldIsolationLevel); 56 try 57 { 58 lockManager.releaseLock(tx1, obj); 59 lockManager.releaseLock(tx2, obj); 60 } 61 finally 62 { 63 super.tearDown(); 64 } 65 } 66 67 70 public void testWriteReleaseCheckRead() 71 { 72 assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel)); 73 assertTrue(lockManager.hasRead(tx2, obj)); 74 assertTrue(lockManager.releaseLock(tx2, obj)); 75 assertFalse(lockManager.hasRead(tx2, obj)); 76 } 77 78 81 public void testReadWriteReleaseCheckRead() 82 { 83 assertTrue(lockManager.readLock(tx2, obj, testIsoLevel)); 84 assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel)); 85 assertTrue(lockManager.hasRead(tx2, obj)); 86 assertTrue(lockManager.releaseLock(tx2, obj)); 87 assertFalse(lockManager.hasRead(tx2, obj)); 88 } 89 90 93 public void testSingleReadLock() 94 { 95 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 96 } 97 98 101 public void testUpgradeReadLock() 102 { 103 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 104 assertTrue(lockManager.upgradeLock(tx1, obj, testIsoLevel)); 105 } 106 107 110 public void testReadThenWrite() 111 { 112 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 113 assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel)); 114 } 115 116 119 public void testSingleWriteLock() 120 { 121 assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel)); 122 } 123 124 127 public void testWriteThenRead() 128 { 129 assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel)); 130 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 131 } 132 133 136 public void testMultipleReadLock() 137 { 138 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 139 assertTrue(!lockManager.readLock(tx2, obj, testIsoLevel)); 140 } 141 142 145 public void testUpgradeWithExistingReader() 146 { 147 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 148 assertTrue(!lockManager.upgradeLock(tx2, obj, testIsoLevel)); 149 } 150 151 154 public void testWriteWithExistingReader() 155 { 156 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 157 assertTrue(!lockManager.writeLock(tx2, obj, testIsoLevel)); 158 } 159 160 163 public void testUpgradeWithMultipleReaders() 164 { 165 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 166 assertTrue(!lockManager.readLock(tx2, obj, testIsoLevel)); 167 assertTrue(!lockManager.upgradeLock(tx2, obj, testIsoLevel)); 168 } 169 170 173 public void testWriteWithMultipleReaders() 174 { 175 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 176 assertTrue(!lockManager.readLock(tx2, obj, testIsoLevel)); 177 assertTrue(!lockManager.writeLock(tx2, obj, testIsoLevel)); 178 } 179 180 183 public void testUpgradeWithMultipleReadersOn1() 184 { 185 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 186 assertTrue(!lockManager.readLock(tx2, obj, testIsoLevel)); 187 assertTrue(lockManager.upgradeLock(tx1, obj, testIsoLevel)); 188 } 189 190 193 public void testWriteWithMultipleReadersOn1() 194 { 195 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 196 assertTrue(!lockManager.readLock(tx2, obj, testIsoLevel)); 197 assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel)); 198 } 199 200 203 public void testReadWithExistingWriter() 204 { 205 assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel)); 206 assertTrue(!lockManager.readLock(tx2, obj, testIsoLevel)); 207 } 208 209 212 public void testMultipleWriteLock() 213 { 214 assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel)); 215 assertTrue(!lockManager.writeLock(tx2, obj, testIsoLevel)); 216 } 217 218 221 public void testReleaseReadLock() 222 { 223 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 224 assertTrue(lockManager.releaseLock(tx1, obj)); 225 assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel)); 226 } 227 228 231 public void testReleaseUpgradeLock() 232 { 233 assertTrue(lockManager.upgradeLock(tx1, obj, testIsoLevel)); 234 assertTrue(lockManager.releaseLock(tx1, obj)); 235 assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel)); 236 } 237 238 241 public void testReleaseWriteLock() 242 { 243 assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel)); 244 assertTrue(lockManager.releaseLock(tx1, obj)); 245 assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel)); 246 } 247 248 251 public void testReadThenRead() 252 { 253 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 254 assertTrue(lockManager.readLock(tx1, obj, testIsoLevel)); 255 } 256 } 257 | Popular Tags |