KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > lockmanager > api > NotifyTest


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.lockmanager.api;
5
6 import java.util.Collection JavaDoc;
7 import java.util.HashSet JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.LinkedList JavaDoc;
10 import java.util.Random JavaDoc;
11 import java.util.Set JavaDoc;
12
13 import junit.framework.TestCase;
14
15 public class NotifyTest extends TestCase {
16
17   public void testEqualsAndHashCode() throws Exception JavaDoc {
18     Set JavaDoc set = new HashSet JavaDoc();
19     Collection JavaDoc pairs = new LinkedList JavaDoc();
20     Random JavaDoc r = new Random JavaDoc();
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 JavaDoc 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 }
66
Popular Tags