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 CommonsLockTestCommittedReads extends OJBTestCase 13 { 14 final int testIsoLevel = LockManager.IL_READ_COMMITTED; 15 16 public static void main(String [] args) 17 { 18 String [] arr = {CommonsLockTestCommittedReads.class.getName()}; 19 junit.textui.TestRunner.main(arr); 20 } 21 22 public CommonsLockTestCommittedReads(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 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 assertFalse(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 |