KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > util > lock > ObjectLockFactoryTest


1 /*
2  * CoadunationUtil: The coaduntion utility library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ObjectLockFactoryTest.java
20  */

21
22 package com.rift.coad.util.lock;
23
24 import junit.framework.*;
25 import java.util.Map JavaDoc;
26 import java.util.concurrent.ConcurrentHashMap JavaDoc;
27 import org.apache.log4j.Logger;
28 import org.apache.log4j.BasicConfigurator;
29
30 /**
31  *
32  *
33  * @author Brett Chaldecott
34  */

35 public class ObjectLockFactoryTest extends TestCase {
36     
37     private String JavaDoc lockObject = "lock object";
38     private String JavaDoc lockObject2 = "lock object2";
39     private boolean hasLock = false;
40     private long lockCount = 0;
41     
42     public ObjectLockFactoryTest(String JavaDoc testName) {
43         super(testName);
44         BasicConfigurator.configure();
45     }
46
47     protected void setUp() throws Exception JavaDoc {
48     }
49
50     protected void tearDown() throws Exception JavaDoc {
51     }
52
53     /**
54      * Test of of class com.rift.coad.util.lock.ObjectLockFactory.
55      */

56     public void testObjectLockFactory() throws Exception JavaDoc {
57         System.out.println("getInstance");
58         
59         try {
60             ObjectLockFactory.getInstance();
61             fail("Could retrieve an reference without calling init");
62         } catch (LockException ex) {
63             System.out.println(ex.getMessage());
64         }
65         
66         
67         ObjectLockFactory.init();
68         
69         ObjectLockFactory expResult = ObjectLockFactory.getInstance();
70         ObjectLockFactory result = ObjectLockFactory.getInstance();
71         assertEquals(expResult, result);
72         
73         System.out.println("Aquire write lock");
74         LockRef ref = result.acquireWriteLock(lockObject);
75         System.out.println("Test lock");
76         assertEquals(ref.getKey(),lockObject);
77         System.out.println("Test thread id");
78         assertEquals(ref.getThreadId(),Thread.currentThread().getId());
79         
80         System.out.println("Aquire lock");
81         ref = result.acquireWriteLock(lockObject,"test");
82         
83         System.out.println("Test the lock");
84         assertEquals(ref.getKey(),lockObject);
85         assertEquals(ref.getThreadId(),Thread.currentThread().getId());
86         assertEquals(ref.getLockName(),"test");
87         
88         Thread JavaDoc testThread = new Thread JavaDoc(new Runnable JavaDoc() {
89             public void run() {
90                 try {
91                     LockRef ref2 = ObjectLockFactory.getInstance().
92                             acquireWriteLock(lockObject,"test2");
93                     hasLock = true;
94                     ref2.release();
95                 } catch (Exception JavaDoc ex) {
96                     System.out.println("Failed to aquire the lock : " +
97                             ex.getMessage());
98                     ex.printStackTrace(System.out);
99                 }
100             }
101         });
102         testThread.start();
103         
104         System.out.println("Test a seperate thread");
105         Thread.sleep(1000);
106         if (hasLock == true) {
107             fail("Was able to get lock");
108         }
109         
110         System.out.println("Attempt to release");
111         ref.release();
112         ref.release();
113         
114         Thread.sleep(500);
115         if (hasLock == false) {
116             fail("Was not able to get lock");
117         }
118         
119         System.out.println("Aquire new lock");
120         ref = result.acquireWriteLock(lockObject,"test2");
121         hasLock = false;
122         System.out.println("Create second thread");
123         testThread = new Thread JavaDoc(new Runnable JavaDoc() {
124             public void run() {
125                 try {
126                     LockRef ref2 = ObjectLockFactory.getInstance().
127                             acquireWriteLock(lockObject,
128                             ObjectLockFactory.WAIT_ON_THREAD);
129                     hasLock = true;
130                     ref2.release();
131                 } catch (Exception JavaDoc ex) {
132                     System.out.println("Failed to aquire the lock : " +
133                             ex.getMessage());
134                     ex.printStackTrace(System.out);
135                 }
136             }
137         });
138         testThread.start();
139         System.out.println("Sleep");
140         Thread.sleep(1000);
141         if (hasLock == true) {
142             fail("Was able to get lock");
143         }
144         
145         System.out.println("Release");
146         ref.release();
147         
148         Thread.sleep(500);
149         if (hasLock == true) {
150             fail("Was able to get lock");
151         }
152         
153         // wait for a named lock
154
ref = result.acquireWriteLock(lockObject,"test2");
155         hasLock = false;
156         testThread = new Thread JavaDoc(new Runnable JavaDoc() {
157             public void run() {
158                 try {
159                     LockRef ref2 = ObjectLockFactory.getInstance().
160                             acquireWriteLock(lockObject,"test2",
161                             ObjectLockFactory.WAIT_ON_NAMED);
162                     hasLock = true;
163                     ref2.release();
164                 } catch (Exception JavaDoc ex) {
165                     System.out.println("Failed to aquire the lock : " +
166                             ex.getMessage());
167                     ex.printStackTrace(System.out);
168                 }
169             }
170         });
171         testThread.start();
172         
173         Thread.sleep(1000);
174         if (hasLock == false) {
175             fail("Was not able to acquire the lock");
176         }
177         
178         ref.release();
179         
180         // attempt to aquire a lock on two different objects
181
System.out.println("Aquire two different locks");
182         ref = result.acquireWriteLock(lockObject,"test2");
183         hasLock = false;
184         testThread = new Thread JavaDoc(new Runnable JavaDoc() {
185             public void run() {
186                 try {
187                     LockRef ref2 = ObjectLockFactory.getInstance().
188                             acquireWriteLock(lockObject2,"test2",
189                             ObjectLockFactory.WAIT_ON_NAMED);
190                     hasLock = true;
191                     ref2.release();
192                 } catch (Exception JavaDoc ex) {
193                     System.out.println("Failed to aquire the lock : " +
194                             ex.getMessage());
195                     ex.printStackTrace(System.out);
196                 }
197             }
198         });
199         testThread.start();
200         
201         Thread.sleep(1000);
202         if (hasLock == false) {
203             fail("Was not able to acquire the lock");
204         }
205         
206         ref.release();
207         
208         // test the read
209
Thread JavaDoc readThread1 = new Thread JavaDoc(new Runnable JavaDoc() {
210             public void run() {
211                 try {
212                     LockRef ref2 = ObjectLockFactory.getInstance().
213                             acquireReadLock(lockObject);
214                     lockCount++;
215                     Thread.sleep(1000);
216                     System.out.println("Read1: Release lock");
217                     ref2.release();
218                     System.out.println("Read1: Released lock");
219                 } catch (Exception JavaDoc ex) {
220                     System.out.println("Failed to aquire the lock : " +
221                             ex.getMessage());
222                     ex.printStackTrace(System.out);
223                 }
224             }
225         });
226         
227         Thread JavaDoc readThread2 = new Thread JavaDoc(new Runnable JavaDoc() {
228             public void run() {
229                 try {
230                     LockRef ref2 = ObjectLockFactory.getInstance().
231                             acquireReadLock(lockObject);
232                     lockCount++;
233                     Thread.sleep(1000);
234                     System.out.println("Read2: Release lock");
235                     ref2.release();
236                     System.out.println("Read2: Released lock");
237                 } catch (Exception JavaDoc ex) {
238                     System.out.println("Failed to aquire the lock : " +
239                             ex.getMessage());
240                     ex.printStackTrace(System.out);
241                 }
242             }
243         });
244         
245         readThread1.start();
246         readThread2.start();
247         
248         Thread.sleep(300);
249         if (lockCount != 2) {
250             fail("Aquire the lock");
251         }
252         
253         System.out.println("Wait for lock");
254         ref = result.acquireWriteLock(lockObject);
255         System.out.println("1: Acquired lock");
256         
257         // test the read
258
readThread1 = new Thread JavaDoc(new Runnable JavaDoc() {
259             public void run() {
260                 try {
261                     System.out.println("Read1: Acquired lock");
262                     LockRef ref2 = ObjectLockFactory.getInstance().
263                             acquireReadLock(lockObject);
264                     System.out.println("Read1: Acquired lock");
265                     lockCount++;
266                     Thread.sleep(1000);
267                     ref2.release();
268                     System.out.println("Released lock");
269                 } catch (Exception JavaDoc ex) {
270                     System.out.println("Failed to aquire the lock : " +
271                             ex.getMessage());
272                     ex.printStackTrace(System.out);
273                 }
274             }
275         });
276         
277         readThread2 = new Thread JavaDoc(new Runnable JavaDoc() {
278             public void run() {
279                 try {
280                     System.out.println("Read2: Acquire lock");
281                     LockRef ref2 = ObjectLockFactory.getInstance().
282                             acquireReadLock(lockObject);
283                     System.out.println("Read2: Acquired lock");
284                     lockCount++;
285                     Thread.sleep(1000);
286                     ref2.release();
287                     System.out.println("Released lock");
288                 } catch (Exception JavaDoc ex) {
289                     System.out.println("Failed to aquire the lock : " +
290                             ex.getMessage());
291                     ex.printStackTrace(System.out);
292                 }
293             }
294         });
295         
296         
297         lockCount = 0;
298         readThread1.start();
299         readThread2.start();
300         
301         Thread.sleep(300);
302         if (lockCount != 0) {
303             fail("The could aquire the lock");
304         }
305         
306         ref.release();
307         
308         Thread.sleep(2000);
309         if (lockCount != 2) {
310             fail("The could not acquire the lock : " + lockCount);
311         }
312         
313         // test the read
314
readThread1 = new Thread JavaDoc(new Runnable JavaDoc() {
315             public void run() {
316                 try {
317                     LockRef ref2 = ObjectLockFactory.getInstance().
318                             acquireReadLock(lockObject,"test");
319                     lockCount++;
320                     Thread.sleep(1000);
321                     ref2.release();
322                     System.out.println("Released lock");
323                 } catch (Exception JavaDoc ex) {
324                     System.out.println("Failed to aquire the lock : " +
325                             ex.getMessage());
326                     ex.printStackTrace(System.out);
327                 }
328             }
329         });
330         
331         readThread2 = new Thread JavaDoc(new Runnable JavaDoc() {
332             public void run() {
333                 try {
334                     LockRef ref2 = ObjectLockFactory.getInstance().
335                             acquireReadLock(lockObject,"test");
336                     lockCount++;
337                     Thread.sleep(1000);
338                     ref2.release();
339                     System.out.println("Released lock");
340                 } catch (Exception JavaDoc ex) {
341                     System.out.println("Failed to aquire the lock : " +
342                             ex.getMessage());
343                     ex.printStackTrace(System.out);
344                 }
345             }
346         });
347         
348         lockCount = 0;
349         readThread1.start();
350         readThread2.start();
351         
352         Thread.sleep(300);
353         if (lockCount != 2) {
354             fail("The could not aquire the lock");
355         }
356         
357         System.out.println("Wait for lock");
358         ref = result.acquireWriteLock(lockObject);
359         System.out.println("Acquired lock");
360         
361         // test the read
362
readThread1 = new Thread JavaDoc(new Runnable JavaDoc() {
363             public void run() {
364                 try {
365                     LockRef ref2 = ObjectLockFactory.getInstance().
366                             acquireReadLock(lockObject,"test");
367                     lockCount++;
368                     Thread.sleep(1000);
369                     ref2.release();
370                     System.out.println("Released lock");
371                 } catch (Exception JavaDoc ex) {
372                     System.out.println("Failed to aquire the lock : " +
373                             ex.getMessage());
374                     ex.printStackTrace(System.out);
375                 }
376             }
377         });
378         
379         readThread2 = new Thread JavaDoc(new Runnable JavaDoc() {
380             public void run() {
381                 try {
382                     LockRef ref2 = ObjectLockFactory.getInstance().
383                             acquireReadLock(lockObject,"test");
384                     lockCount++;
385                     Thread.sleep(1000);
386                     ref2.release();
387                     System.out.println("Released lock");
388                 } catch (Exception JavaDoc ex) {
389                     System.out.println("Failed to aquire the lock : " +
390                             ex.getMessage());
391                     ex.printStackTrace(System.out);
392                 }
393             }
394         });
395         
396         lockCount = 0;
397         readThread1.start();
398         readThread2.start();
399         
400         Thread.sleep(300);
401         if (lockCount != 0) {
402             fail("The could aquire the lock");
403         }
404         
405         ref.release();
406         
407         Thread.sleep(2000);
408         if (lockCount != 2) {
409             fail("The could not acquire the lock");
410         }
411         
412         
413         ObjectLockFactory.fin();
414         try {
415             ObjectLockFactory.getInstance();
416             fail("Could retrieve an reference without after calling fin");
417         } catch (LockException ex) {
418             System.out.println(ex.getMessage());
419         }
420     }
421
422     
423 }
424
Popular Tags