1   package com.atlassian.util.concurrent;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertNotNull;
5   import static org.junit.Assert.assertSame;
6   
7   import com.atlassian.util.concurrent.WeakLockMap.LockReference;
8   
9   import org.junit.Assert;
10  import org.junit.Test;
11  
12  import java.lang.ref.ReferenceQueue;
13  import java.util.concurrent.locks.Lock;
14  
15  public class WeakLockMapTest {
16      @Test public void testGetLock() throws Exception {
17          final WeakLockMap<Integer> lockMap = new WeakLockMap<Integer>(10000);
18  
19          final Lock one = lockMap.get(1);
20          assertEquals(one, lockMap.get(1));
21      }
22  
23      @Test public void testLockReferenceNotNull() throws Exception {
24          final LockReference<String> ref = new LockReference<String>("test", new ReferenceQueue<Lock>());
25          assertNotNull(ref.getDescriptor());
26      }
27  
28      @Test public void testLockReferenceNull() throws Exception {
29          try {
30              new LockReference<String>(null, new ReferenceQueue<Lock>());
31              Assert.fail("IllegalArgumentException expected");
32          }
33          catch (final IllegalArgumentException expected) {}
34      }
35  
36      @Test public void testManyLocks() throws Exception {
37          final int size = 10000;
38          final WeakLockMap<Integer> lockMap = new WeakLockMap<Integer>(size);
39  
40          for (int i = 0; i < 10; i++) {
41              System.gc();
42              for (int j = 0; j < size; j++) {
43                  final Lock one = lockMap.get(j);
44                  assertSame(one, lockMap.get(j));
45              }
46          }
47      }
48  }