KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > locking > LockTestRepeatableReads


1 package org.apache.ojb.broker.locking;
2
3 import org.apache.ojb.broker.Article;
4 import org.apache.ojb.broker.metadata.ClassDescriptor;
5 import org.apache.ojb.broker.metadata.MetadataManager;
6 import org.apache.ojb.junit.OJBTestCase;
7
8 /**
9  * This is the TestSuite that checks the ODMG locking strategies.
10  * It performs 17 testMethods as defined in the Locking documentation.
11  *
12  * @author Thomas Mahler
13  */

14 public class LockTestRepeatableReads extends OJBTestCase
15 {
16     final int testIsoLevel = LockManager.IL_REPEATABLE_READ;
17
18     public static void main(String JavaDoc[] args)
19     {
20         String JavaDoc[] arr = {LockTestRepeatableReads.class.getName()};
21         junit.textui.TestRunner.main(arr);
22     }
23
24     public LockTestRepeatableReads(String JavaDoc name)
25     {
26         super(name);
27     }
28
29     Object JavaDoc tx1;
30     Object JavaDoc tx2;
31     Article obj;
32     LockManager lockManager;
33     int oldIsolationLevel;
34     ClassDescriptor cld;
35
36     public void setUp() throws Exception JavaDoc
37     {
38         super.setUp();
39
40         // change isolation-level used for test
41
cld = MetadataManager.getInstance().getRepository().getDescriptorFor(Article.class);
42         oldIsolationLevel = cld.getIsolationLevel();
43         cld.setIsolationLevel(testIsoLevel);
44
45         lockManager = LockManagerHelper.getLockManagerSpecifiedByConfiguration();
46
47         // initialize the dummies
48
tx2 = new Object JavaDoc();
49         tx1 = new Object JavaDoc();
50
51         obj = Article.createInstance();
52     }
53
54     public void tearDown() throws Exception JavaDoc
55     {
56         // restore isolation level
57
cld.setIsolationLevel(oldIsolationLevel);
58         try
59         {
60             lockManager.releaseLock(tx1, obj);
61             lockManager.releaseLock(tx2, obj);
62         }
63         finally
64         {
65             super.tearDown();
66         }
67     }
68
69     /**
70      * Test 19
71      */

72     public void testWriteReleaseCheckRead()
73     {
74         assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel));
75         assertTrue(lockManager.hasRead(tx2, obj));
76         assertTrue(lockManager.releaseLock(tx2, obj));
77         assertFalse(lockManager.hasRead(tx2, obj));
78     }
79
80     /**
81      * Test 20
82      */

83     public void testReadWriteReleaseCheckRead()
84     {
85         assertTrue(lockManager.readLock(tx2, obj, testIsoLevel));
86         assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel));
87         assertTrue(lockManager.hasRead(tx2, obj));
88         assertTrue(lockManager.releaseLock(tx2, obj));
89         assertFalse(lockManager.hasRead(tx2, obj));
90     }
91
92     /**
93      * Test 1
94      */

95     public void testSingleReadLock()
96     {
97         assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
98     }
99
100     /**
101      * Test 2
102      */

103     public void testUpgradeReadLock()
104     {
105         assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
106         assertTrue(lockManager.upgradeLock(tx1, obj, testIsoLevel));
107     }
108
109     /**
110      * Test3
111      */

112     public void testReadThenWrite()
113     {
114         assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
115         assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel));
116     }
117
118     /**
119      * Test 4
120      */

121     public void testSingleWriteLock()
122     {
123         assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel));
124     }
125
126     /**
127      * Test 5
128      */

129     public void testWriteThenRead()
130     {
131         assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel));
132         assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
133     }
134
135     /**
136      * Test 6
137      */

138     public void testMultipleReadLock()
139     {
140         assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
141         assertTrue(lockManager.readLock(tx2, obj, testIsoLevel));
142     }
143
144     /**
145      * Test 7
146      */

147     public void testUpgradeWithExistingReader()
148     {
149         assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
150         assertTrue(!lockManager.upgradeLock(tx2, obj, testIsoLevel));
151     }
152
153     /**
154      * Test 8
155      */

156     public void testWriteWithExistingReader()
157     {
158         assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
159         assertTrue(!lockManager.writeLock(tx2, obj, testIsoLevel));
160     }
161
162     /**
163      * Test 9
164      */

165     public void testUpgradeWithMultipleReaders()
166     {
167         assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
168         assertTrue(lockManager.readLock(tx2, obj, testIsoLevel));
169         assertTrue(!lockManager.upgradeLock(tx2, obj, testIsoLevel));
170     }
171
172     /**
173      * Test 10
174      */

175     public void testWriteWithMultipleReaders()
176     {
177         assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
178         assertTrue(lockManager.readLock(tx2, obj, testIsoLevel));
179         assertTrue(!lockManager.writeLock(tx2, obj, testIsoLevel));
180     }
181
182     /**
183      * Test 11
184      */

185     public void testUpgradeWithMultipleReadersOn1()
186     {
187         assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
188         assertTrue(lockManager.readLock(tx2, obj, testIsoLevel));
189         assertTrue(!lockManager.upgradeLock(tx1, obj, testIsoLevel));
190     }
191
192     /**
193      * Test 12
194      */

195     public void testWriteWithMultipleReadersOn1()
196     {
197         assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
198         assertTrue(lockManager.readLock(tx2, obj, testIsoLevel));
199         assertTrue(!lockManager.writeLock(tx1, obj, testIsoLevel));
200     }
201
202     /**
203      * Test 13
204      */

205     public void testReadWithExistingWriter()
206     {
207         assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel));
208         assertTrue(!lockManager.readLock(tx2, obj, testIsoLevel));
209     }
210
211     /**
212      * Test 14
213      */

214     public void testMultipleWriteLock()
215     {
216         assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel));
217         assertTrue(!lockManager.writeLock(tx2, obj, testIsoLevel));
218     }
219
220     /**
221      * Test 15
222      */

223     public void testReleaseReadLock()
224     {
225         assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
226         assertTrue(lockManager.releaseLock(tx1, obj));
227         assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel));
228     }
229
230     /**
231      * Test 16
232      */

233     public void testReleaseUpgradeLock()
234     {
235         assertTrue(lockManager.upgradeLock(tx1, obj, testIsoLevel));
236         assertTrue(lockManager.releaseLock(tx1, obj));
237         assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel));
238     }
239
240     /**
241      * Test 17
242      */

243     public void testReleaseWriteLock()
244     {
245         assertTrue(lockManager.writeLock(tx1, obj, testIsoLevel));
246         assertTrue(lockManager.releaseLock(tx1, obj));
247         assertTrue(lockManager.writeLock(tx2, obj, testIsoLevel));
248     }
249
250     /**
251      * Test 18
252      */

253     public void testReadThenRead()
254     {
255         assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
256         assertTrue(lockManager.readLock(tx1, obj, testIsoLevel));
257     }
258 }
259
Popular Tags