01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.object.lockmanager.api;
05:
06: import java.util.Collection;
07: import java.util.HashSet;
08: import java.util.Iterator;
09: import java.util.LinkedList;
10: import java.util.Random;
11: import java.util.Set;
12:
13: import junit.framework.TestCase;
14:
15: public class NotifyTest extends TestCase {
16:
17: public void testEqualsAndHashCode() throws Exception {
18: Set set = new HashSet();
19: Collection pairs = new LinkedList();
20: Random r = new Random();
21:
22: for (int i = 0; i < 1000; i++) {
23: long startValue = r.nextLong();
24: LockID lockID1 = new LockID("" + startValue);
25: LockID lockID2 = new LockID("" + (startValue + 1));
26: ThreadID tid1 = new ThreadID(startValue);
27: ThreadID tid2 = new ThreadID(startValue + 1);
28:
29: Notify aa = new Notify(lockID1, tid1, true);
30: Notify ab = new Notify(lockID1, tid1, true);
31: pairs.add(new Notify[] { aa, ab });
32:
33: Notify ba = new Notify(lockID1, tid2, true);
34: Notify bb = new Notify(lockID1, tid2, true);
35: pairs.add(new Notify[] { ba, bb });
36:
37: Notify ca = new Notify(lockID1, tid1, false);
38: Notify cb = new Notify(lockID1, tid1, false);
39: pairs.add(new Notify[] { ca, cb });
40:
41: Notify da = new Notify(lockID2, tid1, true);
42: Notify db = new Notify(lockID2, tid1, true);
43: pairs.add(new Notify[] { da, db });
44:
45: Notify ea = new Notify(lockID2, tid2, true);
46: Notify eb = new Notify(lockID2, tid2, true);
47: pairs.add(new Notify[] { ea, eb });
48:
49: Notify fa = new Notify(lockID2, tid2, false);
50: Notify fb = new Notify(lockID2, tid2, false);
51: pairs.add(new Notify[] { fa, fb });
52: }
53:
54: for (Iterator i = pairs.iterator(); i.hasNext();) {
55: Notify[] pair = (Notify[]) i.next();
56: assertEquals(pair[0], pair[1]);
57: assertEquals(pair[0].hashCode(), pair[1].hashCode());
58: set.add(pair[0]);
59: set.add(pair[1]);
60: }
61:
62: assertEquals(pairs.size(), set.size());
63: }
64:
65: }
|