KickJava   Java API By Example, From Geeks To Geeks.

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


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 {@link LockManager} implementation
10  * based on apache's commons-transaction locking part.
11  * It performs 17 testMethods as defined in the Locking documentation.
12  */

13 public class CommonsLockTestRepeatableReads extends OJBTestCase
14 {
15     final int testIsoLevel = LockManager.IL_REPEATABLE_READ;
16
17     public static void main(String JavaDoc[] args)
18     {
19         String JavaDoc[] arr = {CommonsLockTestRepeatableReads.class.getName()};
20         junit.textui.TestRunner.main(arr);
21     }
22
23     public CommonsLockTestRepeatableReads(String JavaDoc name)
24     {
25         super(name);
26     }
27
28     Object JavaDoc tx1;
29     Object JavaDoc tx2;
30     Article obj;
31     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(Article.class);
41         oldIsolationLevel = cld.getIsolationLevel();
42         cld.setIsolationLevel(testIsoLevel);
43
44         lockManager = LockManagerHelper.getCommonsLockManager();
45
46         // initialize the dummies
47
tx2 = new Object JavaDoc();
48         tx1 = new Object JavaDoc();
49
50         obj = 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      * Test 19
70      */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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