KickJava   Java API By Example, From Geeks To Geeks.

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


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

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