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 ; 8 9 10 15 public class ReentrantLockTest extends TestCase { 16 ReentrantLock lock; 17 18 public ReentrantLockTest(String name) { 19 super(name); 20 } 21 22 public void setUp() throws Exception { 23 super.setUp(); 24 lock=new ReentrantLock (); 25 } 26 27 public void tearDown() throws Exception { 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 lock) { 56 if(lock != null && lock.getHoldCount() > 0) 57 lock.unlock(); 58 } 59 60 private void releaseAll(ReentrantLock 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 [] args) { 77 junit.textui.TestRunner.run(ReentrantLockTest.suite()); 78 } 79 } 80 | Popular Tags |