KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > transaction > locking > LockTestRepeatableReads


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//transaction/src/test/org/apache/commons/transaction/locking/GenericLockTest.java,v 1.12 2005/01/13 16:44:03 ozeigermann Exp $
3  * $Revision$
4  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.commons.transaction.locking;
25
26 import java.io.PrintWriter JavaDoc;
27
28 import junit.framework.TestCase;
29
30 import org.apache.commons.transaction.util.LoggerFacade;
31 import org.apache.commons.transaction.util.PrintWriterLogger;
32
33 /**
34  * Tests for repeatable read in locks as investigated for OJB.
35  *
36  * @version $Revision$
37  * @author Armin Waibel
38  */

39 public class LockTestRepeatableReads extends TestCase
40 {
41     private static final LoggerFacade logFacade = new PrintWriterLogger(new PrintWriter JavaDoc(System.out),
42             LockTestRepeatableReads.class.getName(), false);
43
44     protected static final long TIMEOUT = 1000000;
45
46    public static void main(String JavaDoc[] args)
47    {
48        String JavaDoc[] arr = {LockTestRepeatableReads.class.getName()};
49        junit.textui.TestRunner.main(arr);
50    }
51
52    public LockTestRepeatableReads(String JavaDoc name)
53    {
54        super(name);
55    }
56
57    Object JavaDoc tx1;
58    Object JavaDoc tx2;
59    Object JavaDoc obj;
60    ReadWriteUpgradeLockManager lockManager;
61
62    public void setUp() throws Exception JavaDoc
63    {
64        super.setUp();
65
66        lockManager = new ReadWriteUpgradeLockManager(logFacade, TIMEOUT);
67
68        // initialize the dummies
69
tx2 = new Object JavaDoc();
70        tx1 = new Object JavaDoc();
71        obj = new Object JavaDoc();
72    }
73
74    public void tearDown() throws Exception JavaDoc
75    {
76        try
77        {
78            lockManager.release(tx1, obj);
79            lockManager.release(tx2, obj);
80        }
81        finally
82        {
83            super.tearDown();
84        }
85    }
86
87    /**
88     * Test 19
89     */

90    public void testWriteReleaseCheckRead()
91    {
92        assertTrue(lockManager.tryWriteLock(tx2, obj));
93        assertTrue(lockManager.checkReadLock(tx2, obj));
94        assertTrue(lockManager.release(tx2, obj));
95        assertFalse(lockManager.hasReadLock(tx2, obj));
96    }
97
98    /**
99     * Test 20
100     */

101    public void testReadWriteReleaseCheckRead()
102    {
103        assertTrue(lockManager.tryReadLock(tx2, obj));
104        assertTrue(lockManager.tryWriteLock(tx2, obj));
105        assertTrue(lockManager.checkReadLock(tx2, obj));
106        assertTrue(lockManager.release(tx2, obj));
107        assertFalse(lockManager.hasReadLock(tx2, obj));
108    }
109
110    /**
111     * Test 1
112     */

113    public void testSingleReadLock()
114    {
115        assertTrue(lockManager.tryReadLock(tx1, obj));
116    }
117
118    /**
119     * Test 2
120     */

121    public void testUpgradeReadLock()
122    {
123        assertTrue(lockManager.tryReadLock(tx1, obj));
124        assertTrue(lockManager.tryUpgradeLock(tx1, obj));
125    }
126
127    /**
128     * Test3
129     */

130    public void testReadThenWrite()
131    {
132        assertTrue(lockManager.tryReadLock(tx1, obj));
133        assertTrue(lockManager.tryWriteLock(tx1, obj));
134    }
135
136    /**
137     * Test 4
138     */

139    public void testSingleWriteLock()
140    {
141        assertTrue(lockManager.tryWriteLock(tx1, obj));
142    }
143
144    /**
145     * Test 5
146     */

147    public void testWriteThenRead()
148    {
149        assertTrue(lockManager.tryWriteLock(tx1, obj));
150        assertTrue(lockManager.tryReadLock(tx1, obj));
151    }
152
153    /**
154     * Test 6
155     */

156    public void testMultipleReadLock()
157    {
158        assertTrue(lockManager.tryReadLock(tx1, obj));
159        assertTrue(lockManager.tryReadLock(tx2, obj));
160    }
161
162    /**
163     * Test 7
164     */

165    public void testUpgradeWithExistingReader()
166    {
167        assertTrue(lockManager.tryReadLock(tx1, obj));
168        assertTrue(lockManager.tryUpgradeLock(tx2, obj));
169    }
170
171    /**
172     * Test 8
173     */

174    public void testWriteWithExistingReader()
175    {
176        assertTrue(lockManager.tryReadLock(tx1, obj));
177        assertFalse(lockManager.tryWriteLock(tx2, obj));
178    }
179
180    /**
181     * Test 9
182     */

183    public void testUpgradeWithMultipleReaders()
184    {
185        assertTrue(lockManager.tryReadLock(tx1, obj));
186        assertTrue(lockManager.tryReadLock(tx2, obj));
187        assertTrue(lockManager.tryUpgradeLock(tx2, obj));
188    }
189
190    /**
191     * Test 10
192     */

193    public void testWriteWithMultipleReaders()
194    {
195        assertTrue(lockManager.tryReadLock(tx1, obj));
196        assertTrue(lockManager.tryReadLock(tx2, obj));
197        assertTrue(!lockManager.tryWriteLock(tx2, obj));
198    }
199
200    /**
201     * Test 11
202     */

203    public void testUpgradeWithMultipleReadersOn1()
204    {
205        assertTrue(lockManager.tryReadLock(tx1, obj));
206        assertTrue(lockManager.tryReadLock(tx2, obj));
207        assertTrue(lockManager.tryUpgradeLock(tx1, obj));
208    }
209
210    /**
211     * Test 12
212     */

213    public void testWriteWithMultipleReadersOn1()
214    {
215        assertTrue(lockManager.tryReadLock(tx1, obj));
216        assertTrue(lockManager.tryReadLock(tx2, obj));
217        assertFalse(lockManager.tryWriteLock(tx1, obj));
218    }
219
220    /**
221     * Test 13
222     */

223    public void testReadWithExistingWriter()
224    {
225        assertTrue(lockManager.tryWriteLock(tx1, obj));
226        assertFalse(lockManager.tryReadLock(tx2, obj));
227    }
228
229    /**
230     * Test 14
231     */

232    public void testMultipleWriteLock()
233    {
234        assertTrue(lockManager.tryWriteLock(tx1, obj));
235        assertFalse(lockManager.tryWriteLock(tx2, obj));
236    }
237
238    /**
239     * Test 15
240     */

241    public void testReleaseReadLock()
242    {
243        assertTrue(lockManager.tryReadLock(tx1, obj));
244        assertTrue(lockManager.release(tx1, obj));
245        assertTrue(lockManager.tryWriteLock(tx2, obj));
246    }
247
248    /**
249     * Test 16
250     */

251    public void testReleaseUpgradeLock()
252    {
253        assertTrue(lockManager.tryUpgradeLock(tx1, obj));
254        assertTrue(lockManager.release(tx1, obj));
255        assertTrue(lockManager.tryWriteLock(tx2, obj));
256    }
257
258    /**
259     * Test 17
260     */

261    public void testReleaseWriteLock()
262    {
263        assertTrue(lockManager.tryWriteLock(tx1, obj));
264        assertTrue(lockManager.release(tx1, obj));
265        assertTrue(lockManager.tryWriteLock(tx2, obj));
266    }
267
268    /**
269     * Test 18
270     */

271    public void testReadThenRead()
272    {
273        assertTrue(lockManager.tryReadLock(tx1, obj));
274        assertTrue(lockManager.tryReadLock(tx1, obj));
275    }
276 }
Popular Tags