KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > tx > LockAccountingTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.tx;
5
6 import com.tc.object.lockmanager.api.LockID;
7
8 import java.util.Collection JavaDoc;
9 import java.util.HashSet JavaDoc;
10
11 import junit.framework.TestCase;
12
13 public class LockAccountingTest extends TestCase {
14
15   private LockAccounting la;
16   private LockID lockID1;
17   private LockID lockID2;
18   private LockID lockID3;
19   private LockID lockID4;
20   private TransactionID txID1;
21   private TransactionID txID2;
22   private TransactionID txID3;
23   private TransactionID txID4;
24   private Collection JavaDoc lock1Txs;
25   private Collection JavaDoc lock2Txs;
26   private Collection JavaDoc lock3Txs;
27   private Collection JavaDoc lock4Txs;
28   
29   public void setUp() {
30     la = new LockAccounting();
31   }
32   
33   public void tests() throws Exception JavaDoc {
34     lockID1 = new LockID("lock1");
35     lockID2 = new LockID("lock2");
36     lockID3 = new LockID("lock3");
37     lockID4 = new LockID("lock4");
38     txID1 = new TransactionID(1);
39     txID2 = new TransactionID(2);
40     txID3 = new TransactionID(3);
41     txID4 = new TransactionID(4);
42     lock1Txs = new HashSet JavaDoc();
43     lock2Txs = new HashSet JavaDoc();
44     lock3Txs = new HashSet JavaDoc();
45     lock4Txs = new HashSet JavaDoc();
46     Collection JavaDoc tx1locks = new HashSet JavaDoc();
47     Collection JavaDoc tx2locks = new HashSet JavaDoc();
48     Collection JavaDoc tx3locks = new HashSet JavaDoc();
49     Collection JavaDoc tx4locks = new HashSet JavaDoc();
50     
51     tx1locks.add(lockID1);
52     lock1Txs.add(txID1);
53     tx1locks.add(lockID2);
54     lock2Txs.add(txID1);
55     
56     tx2locks.add(lockID1);
57     lock1Txs.add(txID2);
58     tx2locks.add(lockID2);
59     lock2Txs.add(txID2);
60     
61     tx3locks.add(lockID3);
62     lock3Txs.add(txID3);
63     
64     tx4locks.add(lockID4);
65     lock4Txs.add(txID4);
66     
67     la.add(txID1, tx1locks);
68     la.add(txID2, tx2locks);
69     la.add(txID3, tx3locks);
70     la.add(txID4, tx4locks);
71
72     verifyGetTransactionsFor();
73     
74     la.acknowledge(txID1);
75     assertTrue(lock1Txs.remove(txID1));
76     assertTrue(lock2Txs.remove(txID1));
77     assertFalse(lock3Txs.remove(txID1));
78     assertFalse(lock4Txs.remove(txID1));
79     verifyGetTransactionsFor();
80     
81     la.acknowledge(txID2);
82     assertTrue(lock1Txs.remove(txID2));
83     assertTrue(lock2Txs.remove(txID2));
84     assertFalse(lock3Txs.remove(txID2));
85     assertFalse(lock4Txs.remove(txID2));
86     verifyGetTransactionsFor();
87     
88     la.acknowledge(txID3);
89     assertFalse(lock1Txs.remove(txID3));
90     assertFalse(lock2Txs.remove(txID3));
91     assertTrue(lock3Txs.remove(txID3));
92     assertFalse(lock4Txs.remove(txID3));
93     verifyGetTransactionsFor();
94     
95     la.acknowledge(txID4);
96     assertFalse(lock1Txs.remove(txID4));
97     assertFalse(lock2Txs.remove(txID4));
98     assertFalse(lock3Txs.remove(txID4));
99     assertTrue(lock4Txs.remove(txID4));
100     verifyGetTransactionsFor();
101     
102     assertTrue(lock1Txs.isEmpty());
103     assertTrue(lock2Txs.isEmpty());
104     assertTrue(lock3Txs.isEmpty());
105     assertTrue(lock4Txs.isEmpty());
106     assertTrue(la.isEmpty());
107   }
108
109   private void verifyGetTransactionsFor() {
110     assertEquals(lock1Txs, la.getTransactionsFor(lockID1));
111     assertEquals(lock2Txs, la.getTransactionsFor(lockID2));
112     assertEquals(lock3Txs, la.getTransactionsFor(lockID3));
113     assertEquals(lock4Txs, la.getTransactionsFor(lockID4));
114   }
115   
116 }
117
Popular Tags