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: */package org.apache.commons.transaction.locking;
017:
018: import java.io.PrintWriter;
019:
020: import junit.framework.TestCase;
021:
022: import org.apache.commons.transaction.util.LoggerFacade;
023: import org.apache.commons.transaction.util.PrintWriterLogger;
024:
025: /**
026: * Tests for repeatable read in locks as investigated for OJB.
027: *
028: * @version $Id: LockTestRepeatableReads.java 493628 2007-01-07 01:42:48Z joerg $
029: */
030: public class LockTestRepeatableReads extends TestCase {
031: private static final LoggerFacade logFacade = new PrintWriterLogger(
032: new PrintWriter(System.out), LockTestRepeatableReads.class
033: .getName(), false);
034:
035: protected static final long TIMEOUT = 1000000;
036:
037: public static void main(String[] args) {
038: String[] arr = { LockTestRepeatableReads.class.getName() };
039: junit.textui.TestRunner.main(arr);
040: }
041:
042: public LockTestRepeatableReads(String name) {
043: super (name);
044: }
045:
046: Object tx1;
047: Object tx2;
048: Object obj;
049: ReadWriteUpgradeLockManager lockManager;
050:
051: public void setUp() throws Exception {
052: super .setUp();
053:
054: lockManager = new ReadWriteUpgradeLockManager(logFacade,
055: TIMEOUT);
056:
057: // initialize the dummies
058: tx2 = new Object();
059: tx1 = new Object();
060: obj = new Object();
061: }
062:
063: public void tearDown() throws Exception {
064: try {
065: lockManager.release(tx1, obj);
066: lockManager.release(tx2, obj);
067: } finally {
068: super .tearDown();
069: }
070: }
071:
072: /**
073: * Test 19
074: */
075: public void testWriteReleaseCheckRead() {
076: assertTrue(lockManager.tryWriteLock(tx2, obj));
077: assertTrue(lockManager.checkReadLock(tx2, obj));
078: assertTrue(lockManager.release(tx2, obj));
079: assertFalse(lockManager.hasReadLock(tx2, obj));
080: }
081:
082: /**
083: * Test 20
084: */
085: public void testReadWriteReleaseCheckRead() {
086: assertTrue(lockManager.tryReadLock(tx2, obj));
087: assertTrue(lockManager.tryWriteLock(tx2, obj));
088: assertTrue(lockManager.checkReadLock(tx2, obj));
089: assertTrue(lockManager.release(tx2, obj));
090: assertFalse(lockManager.hasReadLock(tx2, obj));
091: }
092:
093: /**
094: * Test 1
095: */
096: public void testSingleReadLock() {
097: assertTrue(lockManager.tryReadLock(tx1, obj));
098: }
099:
100: /**
101: * Test 2
102: */
103: public void testUpgradeReadLock() {
104: assertTrue(lockManager.tryReadLock(tx1, obj));
105: assertTrue(lockManager.tryUpgradeLock(tx1, obj));
106: }
107:
108: /**
109: * Test3
110: */
111: public void testReadThenWrite() {
112: assertTrue(lockManager.tryReadLock(tx1, obj));
113: assertTrue(lockManager.tryWriteLock(tx1, obj));
114: }
115:
116: /**
117: * Test 4
118: */
119: public void testSingleWriteLock() {
120: assertTrue(lockManager.tryWriteLock(tx1, obj));
121: }
122:
123: /**
124: * Test 5
125: */
126: public void testWriteThenRead() {
127: assertTrue(lockManager.tryWriteLock(tx1, obj));
128: assertTrue(lockManager.tryReadLock(tx1, obj));
129: }
130:
131: /**
132: * Test 6
133: */
134: public void testMultipleReadLock() {
135: assertTrue(lockManager.tryReadLock(tx1, obj));
136: assertTrue(lockManager.tryReadLock(tx2, obj));
137: }
138:
139: /**
140: * Test 7
141: */
142: public void testUpgradeWithExistingReader() {
143: assertTrue(lockManager.tryReadLock(tx1, obj));
144: assertTrue(lockManager.tryUpgradeLock(tx2, obj));
145: }
146:
147: /**
148: * Test 8
149: */
150: public void testWriteWithExistingReader() {
151: assertTrue(lockManager.tryReadLock(tx1, obj));
152: assertFalse(lockManager.tryWriteLock(tx2, obj));
153: }
154:
155: /**
156: * Test 9
157: */
158: public void testUpgradeWithMultipleReaders() {
159: assertTrue(lockManager.tryReadLock(tx1, obj));
160: assertTrue(lockManager.tryReadLock(tx2, obj));
161: assertTrue(lockManager.tryUpgradeLock(tx2, obj));
162: }
163:
164: /**
165: * Test 10
166: */
167: public void testWriteWithMultipleReaders() {
168: assertTrue(lockManager.tryReadLock(tx1, obj));
169: assertTrue(lockManager.tryReadLock(tx2, obj));
170: assertTrue(!lockManager.tryWriteLock(tx2, obj));
171: }
172:
173: /**
174: * Test 11
175: */
176: public void testUpgradeWithMultipleReadersOn1() {
177: assertTrue(lockManager.tryReadLock(tx1, obj));
178: assertTrue(lockManager.tryReadLock(tx2, obj));
179: assertTrue(lockManager.tryUpgradeLock(tx1, obj));
180: }
181:
182: /**
183: * Test 12
184: */
185: public void testWriteWithMultipleReadersOn1() {
186: assertTrue(lockManager.tryReadLock(tx1, obj));
187: assertTrue(lockManager.tryReadLock(tx2, obj));
188: assertFalse(lockManager.tryWriteLock(tx1, obj));
189: }
190:
191: /**
192: * Test 13
193: */
194: public void testReadWithExistingWriter() {
195: assertTrue(lockManager.tryWriteLock(tx1, obj));
196: assertFalse(lockManager.tryReadLock(tx2, obj));
197: }
198:
199: /**
200: * Test 14
201: */
202: public void testMultipleWriteLock() {
203: assertTrue(lockManager.tryWriteLock(tx1, obj));
204: assertFalse(lockManager.tryWriteLock(tx2, obj));
205: }
206:
207: /**
208: * Test 15
209: */
210: public void testReleaseReadLock() {
211: assertTrue(lockManager.tryReadLock(tx1, obj));
212: assertTrue(lockManager.release(tx1, obj));
213: assertTrue(lockManager.tryWriteLock(tx2, obj));
214: }
215:
216: /**
217: * Test 16
218: */
219: public void testReleaseUpgradeLock() {
220: assertTrue(lockManager.tryUpgradeLock(tx1, obj));
221: assertTrue(lockManager.release(tx1, obj));
222: assertTrue(lockManager.tryWriteLock(tx2, obj));
223: }
224:
225: /**
226: * Test 17
227: */
228: public void testReleaseWriteLock() {
229: assertTrue(lockManager.tryWriteLock(tx1, obj));
230: assertTrue(lockManager.release(tx1, obj));
231: assertTrue(lockManager.tryWriteLock(tx2, obj));
232: }
233:
234: /**
235: * Test 18
236: */
237: public void testReadThenRead() {
238: assertTrue(lockManager.tryReadLock(tx1, obj));
239: assertTrue(lockManager.tryReadLock(tx1, obj));
240: }
241: }
|