KickJava   Java API By Example, From Geeks To Geeks.

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


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

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