001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.transaction.locking; 018 019import java.io.PrintWriter; 020 021import junit.framework.TestCase; 022 023import org.apache.commons.transaction.util.LoggerFacade; 024import org.apache.commons.transaction.util.PrintWriterLogger; 025 026/** 027 * Tests for repeatable read in locks as investigated for OJB. 028 * 029 * @version $Id: LockTestRepeatableReads.java 493628 2007-01-07 01:42:48Z joerg $ 030 */ 031public class LockTestRepeatableReads extends TestCase 032{ 033 private static final LoggerFacade logFacade = new PrintWriterLogger(new PrintWriter(System.out), 034 LockTestRepeatableReads.class.getName(), false); 035 036 protected static final long TIMEOUT = 1000000; 037 038 public static void main(String[] args) 039 { 040 String[] arr = {LockTestRepeatableReads.class.getName()}; 041 junit.textui.TestRunner.main(arr); 042 } 043 044 public LockTestRepeatableReads(String name) 045 { 046 super(name); 047 } 048 049 Object tx1; 050 Object tx2; 051 Object obj; 052 ReadWriteUpgradeLockManager lockManager; 053 054 public void setUp() throws Exception 055 { 056 super.setUp(); 057 058 lockManager = new ReadWriteUpgradeLockManager(logFacade, TIMEOUT); 059 060 // initialize the dummies 061 tx2 = new Object(); 062 tx1 = new Object(); 063 obj = new Object(); 064 } 065 066 public void tearDown() throws Exception 067 { 068 try 069 { 070 lockManager.release(tx1, obj); 071 lockManager.release(tx2, obj); 072 } 073 finally 074 { 075 super.tearDown(); 076 } 077 } 078 079 /** 080 * Test 19 081 */ 082 public void testWriteReleaseCheckRead() 083 { 084 assertTrue(lockManager.tryWriteLock(tx2, obj)); 085 assertTrue(lockManager.checkReadLock(tx2, obj)); 086 assertTrue(lockManager.release(tx2, obj)); 087 assertFalse(lockManager.hasReadLock(tx2, obj)); 088 } 089 090 /** 091 * Test 20 092 */ 093 public void testReadWriteReleaseCheckRead() 094 { 095 assertTrue(lockManager.tryReadLock(tx2, obj)); 096 assertTrue(lockManager.tryWriteLock(tx2, obj)); 097 assertTrue(lockManager.checkReadLock(tx2, obj)); 098 assertTrue(lockManager.release(tx2, obj)); 099 assertFalse(lockManager.hasReadLock(tx2, obj)); 100 } 101 102 /** 103 * Test 1 104 */ 105 public void testSingleReadLock() 106 { 107 assertTrue(lockManager.tryReadLock(tx1, obj)); 108 } 109 110 /** 111 * Test 2 112 */ 113 public void testUpgradeReadLock() 114 { 115 assertTrue(lockManager.tryReadLock(tx1, obj)); 116 assertTrue(lockManager.tryUpgradeLock(tx1, obj)); 117 } 118 119 /** 120 * Test3 121 */ 122 public void testReadThenWrite() 123 { 124 assertTrue(lockManager.tryReadLock(tx1, obj)); 125 assertTrue(lockManager.tryWriteLock(tx1, obj)); 126 } 127 128 /** 129 * Test 4 130 */ 131 public void testSingleWriteLock() 132 { 133 assertTrue(lockManager.tryWriteLock(tx1, obj)); 134 } 135 136 /** 137 * Test 5 138 */ 139 public void testWriteThenRead() 140 { 141 assertTrue(lockManager.tryWriteLock(tx1, obj)); 142 assertTrue(lockManager.tryReadLock(tx1, obj)); 143 } 144 145 /** 146 * Test 6 147 */ 148 public void testMultipleReadLock() 149 { 150 assertTrue(lockManager.tryReadLock(tx1, obj)); 151 assertTrue(lockManager.tryReadLock(tx2, obj)); 152 } 153 154 /** 155 * Test 7 156 */ 157 public void testUpgradeWithExistingReader() 158 { 159 assertTrue(lockManager.tryReadLock(tx1, obj)); 160 assertTrue(lockManager.tryUpgradeLock(tx2, obj)); 161 } 162 163 /** 164 * Test 8 165 */ 166 public void testWriteWithExistingReader() 167 { 168 assertTrue(lockManager.tryReadLock(tx1, obj)); 169 assertFalse(lockManager.tryWriteLock(tx2, obj)); 170 } 171 172 /** 173 * Test 9 174 */ 175 public void testUpgradeWithMultipleReaders() 176 { 177 assertTrue(lockManager.tryReadLock(tx1, obj)); 178 assertTrue(lockManager.tryReadLock(tx2, obj)); 179 assertTrue(lockManager.tryUpgradeLock(tx2, obj)); 180 } 181 182 /** 183 * Test 10 184 */ 185 public void testWriteWithMultipleReaders() 186 { 187 assertTrue(lockManager.tryReadLock(tx1, obj)); 188 assertTrue(lockManager.tryReadLock(tx2, obj)); 189 assertTrue(!lockManager.tryWriteLock(tx2, obj)); 190 } 191 192 /** 193 * Test 11 194 */ 195 public void testUpgradeWithMultipleReadersOn1() 196 { 197 assertTrue(lockManager.tryReadLock(tx1, obj)); 198 assertTrue(lockManager.tryReadLock(tx2, obj)); 199 assertTrue(lockManager.tryUpgradeLock(tx1, obj)); 200 } 201 202 /** 203 * Test 12 204 */ 205 public void testWriteWithMultipleReadersOn1() 206 { 207 assertTrue(lockManager.tryReadLock(tx1, obj)); 208 assertTrue(lockManager.tryReadLock(tx2, obj)); 209 assertFalse(lockManager.tryWriteLock(tx1, obj)); 210 } 211 212 /** 213 * Test 13 214 */ 215 public void testReadWithExistingWriter() 216 { 217 assertTrue(lockManager.tryWriteLock(tx1, obj)); 218 assertFalse(lockManager.tryReadLock(tx2, obj)); 219 } 220 221 /** 222 * Test 14 223 */ 224 public void testMultipleWriteLock() 225 { 226 assertTrue(lockManager.tryWriteLock(tx1, obj)); 227 assertFalse(lockManager.tryWriteLock(tx2, obj)); 228 } 229 230 /** 231 * Test 15 232 */ 233 public void testReleaseReadLock() 234 { 235 assertTrue(lockManager.tryReadLock(tx1, obj)); 236 assertTrue(lockManager.release(tx1, obj)); 237 assertTrue(lockManager.tryWriteLock(tx2, obj)); 238 } 239 240 /** 241 * Test 16 242 */ 243 public void testReleaseUpgradeLock() 244 { 245 assertTrue(lockManager.tryUpgradeLock(tx1, obj)); 246 assertTrue(lockManager.release(tx1, obj)); 247 assertTrue(lockManager.tryWriteLock(tx2, obj)); 248 } 249 250 /** 251 * Test 17 252 */ 253 public void testReleaseWriteLock() 254 { 255 assertTrue(lockManager.tryWriteLock(tx1, obj)); 256 assertTrue(lockManager.release(tx1, obj)); 257 assertTrue(lockManager.tryWriteLock(tx2, obj)); 258 } 259 260 /** 261 * Test 18 262 */ 263 public void testReadThenRead() 264 { 265 assertTrue(lockManager.tryReadLock(tx1, obj)); 266 assertTrue(lockManager.tryReadLock(tx1, obj)); 267 } 268}