KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > ReentrantLockTest


1 package org.jgroups.tests;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6
7 import java.util.concurrent.locks.ReentrantLock JavaDoc;
8
9
10 /**
11  * Tests the ReentrantLock
12  * @author Bela Ban
13  * @version $Id: ReentrantLockTest.java,v 1.1 2007/07/04 07:29:34 belaban Exp $
14  */

15 public class ReentrantLockTest extends TestCase {
16     ReentrantLock JavaDoc lock;
17
18     public ReentrantLockTest(String JavaDoc name) {
19         super(name);
20     }
21
22     public void setUp() throws Exception JavaDoc {
23         super.setUp();
24         lock=new ReentrantLock JavaDoc();
25     }
26
27     public void tearDown() throws Exception JavaDoc {
28         releaseAll(lock);
29         lock=null;
30         super.tearDown();
31     }
32
33
34
35     public void testAcquireLock() {
36         lock.lock();
37         assertEquals(1, lock.getHoldCount());
38         lock.lock();
39         assertEquals(2, lock.getHoldCount());
40         release(lock);
41         assertEquals(1, lock.getHoldCount());
42         release(lock);
43         assertEquals(0, lock.getHoldCount());
44     }
45
46     public void testAcquireLock2() {
47         lock.lock();
48         assertEquals(1, lock.getHoldCount());
49         lock.lock();
50         assertEquals(2, lock.getHoldCount());
51         releaseAll(lock);
52         assertEquals(0, lock.getHoldCount());
53     }
54
55     private void release(ReentrantLock JavaDoc lock) {
56         if(lock != null && lock.getHoldCount() > 0)
57             lock.unlock();
58     }
59
60     private void releaseAll(ReentrantLock JavaDoc lock) {
61         if(lock != null) {
62             long holds=lock.getHoldCount();
63             if(holds > 0) {
64                 for(int i=0; i < holds; i++)
65                     lock.unlock();
66             }
67         }
68     }
69
70
71
72     public static Test suite() {
73         return new TestSuite(ReentrantLockTest.class);
74     }
75
76     public static void main(String JavaDoc[] args) {
77         junit.textui.TestRunner.run(ReentrantLockTest.suite());
78     }
79 }
80
Popular Tags