KickJava   Java API By Example, From Geeks To Geeks.

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


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 CommonsLockTestSerializable extends OJBTestCase
13 {
14     final int testIsoLevel = LockManager.IL_SERIALIZABLE;
15
16     public static void main(String JavaDoc[] args)
17     {
18         String JavaDoc[] arr = {CommonsLockTestSerializable.class.getName()};
19         junit.textui.TestRunner.main(arr);
20     }
21
22     public CommonsLockTestSerializable(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      * Test 19
69      */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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