View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.transaction.locking;
18  
19  import java.io.PrintWriter;
20  
21  import junit.framework.TestCase;
22  
23  import org.apache.commons.transaction.util.LoggerFacade;
24  import org.apache.commons.transaction.util.PrintWriterLogger;
25  
26  /**
27   * Tests for repeatable read in locks as investigated for OJB. 
28   *
29   * @version $Id: LockTestRepeatableReads.java 493628 2007-01-07 01:42:48Z joerg $
30   */
31  public class LockTestRepeatableReads extends TestCase
32  {
33      private static final LoggerFacade logFacade = new PrintWriterLogger(new PrintWriter(System.out),
34              LockTestRepeatableReads.class.getName(), false);
35  
36      protected static final long TIMEOUT = 1000000;
37  
38     public static void main(String[] args)
39     {
40         String[] arr = {LockTestRepeatableReads.class.getName()};
41         junit.textui.TestRunner.main(arr);
42     }
43  
44     public LockTestRepeatableReads(String name)
45     {
46         super(name);
47     }
48  
49     Object tx1;
50     Object tx2;
51     Object obj;
52     ReadWriteUpgradeLockManager lockManager;
53  
54     public void setUp() throws Exception
55     {
56         super.setUp();
57  
58         lockManager = new ReadWriteUpgradeLockManager(logFacade, TIMEOUT);
59  
60         // initialize the dummies
61         tx2 = new Object();
62         tx1 = new Object();
63         obj = new Object();
64     }
65  
66     public void tearDown() throws Exception
67     {
68         try
69         {
70             lockManager.release(tx1, obj);
71             lockManager.release(tx2, obj);
72         }
73         finally
74         {
75             super.tearDown();
76         }
77     }
78  
79     /**
80      * Test 19
81      */
82     public void testWriteReleaseCheckRead()
83     {
84         assertTrue(lockManager.tryWriteLock(tx2, obj));
85         assertTrue(lockManager.checkReadLock(tx2, obj));
86         assertTrue(lockManager.release(tx2, obj));
87         assertFalse(lockManager.hasReadLock(tx2, obj));
88     }
89  
90     /**
91      * Test 20
92      */
93     public void testReadWriteReleaseCheckRead()
94     {
95         assertTrue(lockManager.tryReadLock(tx2, obj));
96         assertTrue(lockManager.tryWriteLock(tx2, obj));
97         assertTrue(lockManager.checkReadLock(tx2, obj));
98         assertTrue(lockManager.release(tx2, obj));
99         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 }