001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.object.tx;
005:
006: import com.tc.object.lockmanager.api.LockID;
007:
008: import java.util.Collection;
009: import java.util.HashSet;
010:
011: import junit.framework.TestCase;
012:
013: public class LockAccountingTest extends TestCase {
014:
015: private LockAccounting la;
016: private LockID lockID1;
017: private LockID lockID2;
018: private LockID lockID3;
019: private LockID lockID4;
020: private TransactionID txID1;
021: private TransactionID txID2;
022: private TransactionID txID3;
023: private TransactionID txID4;
024: private Collection lock1Txs;
025: private Collection lock2Txs;
026: private Collection lock3Txs;
027: private Collection lock4Txs;
028:
029: public void setUp() {
030: la = new LockAccounting();
031: }
032:
033: public void tests() throws Exception {
034: lockID1 = new LockID("lock1");
035: lockID2 = new LockID("lock2");
036: lockID3 = new LockID("lock3");
037: lockID4 = new LockID("lock4");
038: txID1 = new TransactionID(1);
039: txID2 = new TransactionID(2);
040: txID3 = new TransactionID(3);
041: txID4 = new TransactionID(4);
042: lock1Txs = new HashSet();
043: lock2Txs = new HashSet();
044: lock3Txs = new HashSet();
045: lock4Txs = new HashSet();
046: Collection tx1locks = new HashSet();
047: Collection tx2locks = new HashSet();
048: Collection tx3locks = new HashSet();
049: Collection tx4locks = new HashSet();
050:
051: tx1locks.add(lockID1);
052: lock1Txs.add(txID1);
053: tx1locks.add(lockID2);
054: lock2Txs.add(txID1);
055:
056: tx2locks.add(lockID1);
057: lock1Txs.add(txID2);
058: tx2locks.add(lockID2);
059: lock2Txs.add(txID2);
060:
061: tx3locks.add(lockID3);
062: lock3Txs.add(txID3);
063:
064: tx4locks.add(lockID4);
065: lock4Txs.add(txID4);
066:
067: la.add(txID1, tx1locks);
068: la.add(txID2, tx2locks);
069: la.add(txID3, tx3locks);
070: la.add(txID4, tx4locks);
071:
072: verifyGetTransactionsFor();
073:
074: la.acknowledge(txID1);
075: assertTrue(lock1Txs.remove(txID1));
076: assertTrue(lock2Txs.remove(txID1));
077: assertFalse(lock3Txs.remove(txID1));
078: assertFalse(lock4Txs.remove(txID1));
079: verifyGetTransactionsFor();
080:
081: la.acknowledge(txID2);
082: assertTrue(lock1Txs.remove(txID2));
083: assertTrue(lock2Txs.remove(txID2));
084: assertFalse(lock3Txs.remove(txID2));
085: assertFalse(lock4Txs.remove(txID2));
086: verifyGetTransactionsFor();
087:
088: la.acknowledge(txID3);
089: assertFalse(lock1Txs.remove(txID3));
090: assertFalse(lock2Txs.remove(txID3));
091: assertTrue(lock3Txs.remove(txID3));
092: assertFalse(lock4Txs.remove(txID3));
093: verifyGetTransactionsFor();
094:
095: la.acknowledge(txID4);
096: assertFalse(lock1Txs.remove(txID4));
097: assertFalse(lock2Txs.remove(txID4));
098: assertFalse(lock3Txs.remove(txID4));
099: 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: }
|