Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 157   Methods: 13
NCLOC: 118   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
ReadWriteLockTest.java 50% 89.7% 92.3% 89%
coverage coverage
 1    package org.jboss.cache.lock;
 2   
 3    import junit.framework.Test;
 4    import junit.framework.TestCase;
 5    import junit.framework.TestSuite;
 6   
 7    import java.util.concurrent.locks.ReadWriteLock;
 8    import java.util.concurrent.locks.ReentrantReadWriteLock;
 9   
 10    /**
 11    * Tests the various ReadWriteLock implementations
 12    * @author Bela Ban
 13    * @version $Id: ReadWriteLockTest.java,v 1.4 2007/01/31 17:28:41 msurtani Exp $
 14    */
 15    public class ReadWriteLockTest extends TestCase {
 16    ReadWriteLock lock;
 17    Exception ex=null;
 18   
 19   
 20  4 protected void setUp() throws Exception {
 21  4 super.setUp();
 22  4 ex=null;
 23    }
 24   
 25  4 protected void tearDown() throws Exception {
 26  4 super.tearDown();
 27  4 lock=null;
 28  4 if(ex != null)
 29  0 throw ex;
 30    }
 31   
 32   
 33  1 public void testMoreWriteReleasesThanAcquisitions() throws InterruptedException {
 34  1 lock = new ReentrantReadWriteLock();
 35  1 lock.writeLock().lock();
 36  1 lock.writeLock().unlock();
 37  1 try
 38    {
 39  1 lock.writeLock().unlock();
 40  0 fail("Should have barfed");
 41    }
 42    catch (IllegalMonitorStateException imse)
 43    {
 44    // should barf
 45    }
 46    }
 47   
 48  1 public void testMoreReadReleasesThanAcquisitions() throws InterruptedException {
 49  1 lock = new ReentrantReadWriteLock();
 50  1 lock.readLock().lock();
 51  1 lock.readLock().unlock();
 52  1 try
 53    {
 54  1 lock.readLock().unlock();
 55  0 fail("read locks cannot be released more than acquired");
 56    }
 57    catch(IllegalMonitorStateException illegalStateEx)
 58    {
 59   
 60    }
 61    }
 62   
 63  1 public void testSimple() throws InterruptedException {
 64  1 lock = new ReentrantReadWriteLock();
 65  1 lock.readLock().lock();
 66    // upgrades must be manual; involving giving up the RL first. Sucks.
 67    // see http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html
 68  1 lock.readLock().unlock();
 69   
 70  1 lock.writeLock().lock();
 71  1 lock.writeLock().lock();
 72  1 lock.readLock().lock();
 73  1 lock.readLock().lock();
 74   
 75    // since the thread currently has a WL and we are reentrant, this works without a manual upgrade.
 76  1 lock.writeLock().lock();
 77  1 lock.writeLock().lock();
 78    }
 79   
 80   
 81  1 public void testOneWriterMultipleReaders() throws InterruptedException {
 82  1 lock = new ReentrantReadWriteLock();
 83   
 84  1 Writer writer=new Writer("writer");
 85  1 Reader reader1=new Reader("reader1");
 86  1 Reader reader2=new Reader("reader2");
 87   
 88  1 writer.start();
 89  1 reader1.start();
 90  1 reader2.start();
 91   
 92  1 writer.join();
 93  1 reader1.join();
 94  1 reader2.join();
 95    }
 96   
 97    class Writer extends Thread {
 98   
 99  1 public Writer(String name) {
 100  1 super(name);
 101    }
 102   
 103  1 public void run() {
 104  1 try {
 105  1 log("acquiring WL");
 106  1 lock.writeLock().lock();
 107  1 log("acquired WL successfully");
 108  1 sleep(1000);
 109    }
 110    catch(InterruptedException e) {
 111  0 ex=e;
 112    }
 113    finally {
 114  1 log("releasing WL");
 115  1 lock.writeLock().unlock();
 116    }
 117    }
 118    }
 119   
 120   
 121    class Reader extends Thread {
 122   
 123  2 public Reader(String name) {
 124  2 super(name);
 125    }
 126   
 127   
 128  2 public void run() {
 129  2 try {
 130  2 log("acquiring RL");
 131  2 lock.readLock().lock();
 132  2 log("acquired RL successfully");
 133  2 sleep(500);
 134    }
 135    catch(InterruptedException e) {
 136  0 ex=e;
 137    }
 138    finally {
 139  2 log("releasing RL");
 140  2 lock.readLock().unlock();
 141    }
 142    }
 143    }
 144   
 145  9 static void log(String msg) {
 146  9 System.out.println(Thread.currentThread().getName() + ": " + msg);
 147    }
 148   
 149  1 public static Test suite() {
 150  1 return new TestSuite(ReadWriteLockTest.class);
 151    }
 152   
 153  0 public static void main(String[] args) {
 154  0 junit.textui.TestRunner.run(suite());
 155    }
 156   
 157    }