01: package org.jgroups.tests;
02:
03: import EDU.oswego.cs.dl.util.concurrent.ReentrantLock;
04: import junit.framework.Test;
05: import junit.framework.TestCase;
06: import junit.framework.TestSuite;
07:
08: /**
09: * Tests the ReentrantLock
10: * @author Bela Ban
11: * @version $Id: ReentrantLockTest.java,v 1.2 2006/08/31 14:08:49 belaban Exp $
12: */
13: public class ReentrantLockTest extends TestCase {
14: ReentrantLock lock;
15:
16: public ReentrantLockTest(String name) {
17: super (name);
18: }
19:
20: public void setUp() throws Exception {
21: super .setUp();
22: lock = new ReentrantLock();
23: }
24:
25: public void tearDown() throws Exception {
26: releaseAll(lock);
27: lock = null;
28: super .tearDown();
29: }
30:
31: public void testAcquireLock() {
32: try {
33: lock.acquire();
34: assertEquals(1, lock.holds());
35: lock.acquire();
36: assertEquals(2, lock.holds());
37: release(lock);
38: assertEquals(1, lock.holds());
39: release(lock);
40: assertEquals(0, lock.holds());
41: } catch (InterruptedException e) {
42: e.printStackTrace();
43: }
44: }
45:
46: public void testAcquireLock2() {
47: try {
48: lock.acquire();
49: assertEquals(1, lock.holds());
50: lock.acquire();
51: assertEquals(2, lock.holds());
52: releaseAll(lock);
53: assertEquals(0, lock.holds());
54: } catch (InterruptedException e) {
55: e.printStackTrace();
56: }
57: }
58:
59: private void release(ReentrantLock lock) {
60: if (lock != null && lock.holds() > 0)
61: lock.release();
62: }
63:
64: private void releaseAll(ReentrantLock lock) {
65: if (lock != null) {
66: long holds = lock.holds();
67: if (holds > 0)
68: lock.release(holds);
69: }
70: }
71:
72: public static Test suite() {
73: return new TestSuite(ReentrantLockTest.class);
74: }
75:
76: public static void main(String[] args) {
77: junit.textui.TestRunner.run(ReentrantLockTest.suite());
78: }
79: }
|