KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > DigestTest


1 // $Id: DigestTest.java,v 1.1 2007/07/04 07:29:34 belaban Exp $
2

3 package org.jgroups.tests;
4
5
6 import junit.framework.Test;
7 import junit.framework.TestCase;
8 import junit.framework.TestSuite;
9 import org.jgroups.util.Digest;
10 import org.jgroups.util.MutableDigest;
11 import org.jgroups.stack.IpAddress;
12 import org.jgroups.util.Util;
13 import org.jgroups.Address;
14
15 import java.io.*;
16 import java.util.Map JavaDoc;
17 import java.util.HashMap JavaDoc;
18
19
20 public class DigestTest extends TestCase {
21     Digest d, d2;
22     MutableDigest md;
23     IpAddress a1, a2, a3;
24
25     public DigestTest(String JavaDoc name) {
26         super(name);
27     }
28
29
30     public void setUp() throws Exception JavaDoc {
31         super.setUp();
32         Map JavaDoc<Address, Digest.Entry> map=new HashMap JavaDoc<Address, Digest.Entry>();
33         a1=new IpAddress(5555);
34         a2=new IpAddress(6666);
35         a3=new IpAddress(7777);
36         map.put(a1, new Digest.Entry(4, 500, 501));
37         map.put(a2, new Digest.Entry(25, 26, 26));
38         map.put(a3, new Digest.Entry(20, 25, 33));
39         d=new Digest(map);
40         md=new MutableDigest(map);
41     }
42
43     public void testSize() {
44         d2=new Digest(3);
45         assertEquals(0, d2.size());
46     }
47
48     public void testEquals() {
49         d2=d.copy();
50         System.out.println("d: " + d + "\nd2= " + d2);
51         assertEquals(d, d);
52         assertEquals(d, d2);
53     }
54     
55     public void testDifference(){
56         
57         Map JavaDoc<Address, Digest.Entry> map=new HashMap JavaDoc<Address, Digest.Entry>();
58         a1=new IpAddress(5555);
59         a2=new IpAddress(6666);
60         a3=new IpAddress(7777);
61         map.put(a1, new Digest.Entry(4, 500, 501));
62         map.put(a2, new Digest.Entry(25, 26, 26));
63         map.put(a3, new Digest.Entry(20, 25, 33));
64         Digest digest =new Digest(map);
65          
66         Map JavaDoc<Address, Digest.Entry> map2=new HashMap JavaDoc<Address, Digest.Entry>();
67         map2.put(a1, new Digest.Entry(4, 500, 501));
68         map2.put(a2, new Digest.Entry(25, 26, 26));
69         map2.put(a3, new Digest.Entry(20, 37, 33));
70         Digest digest2 =new Digest(map2);
71          
72         assertNotSame(digest, digest2);
73         
74         Digest diff = digest2.difference(digest);
75         System.out.println(diff);
76         assertTrue(diff.contains(a3));
77         assertEquals(1, diff.size());
78         
79         
80         Map JavaDoc<Address, Digest.Entry> map3=new HashMap JavaDoc<Address, Digest.Entry>();
81         map3.put(a1, new Digest.Entry(4, 500, 501));
82         map3.put(a2, new Digest.Entry(25, 26, 26));
83         map3.put(a3, new Digest.Entry(20, 37, 33));
84         map3.put(new IpAddress(8888), new Digest.Entry(1, 2, 3));
85         Digest digest3 =new Digest(map3);
86         
87         diff = digest3.difference(digest);
88         System.out.println(diff);
89         assertEquals(2, diff.size());
90         
91         diff = digest3.difference(digest2);
92         System.out.println(diff);
93         assertEquals(1, diff.size());
94         
95         Digest diff2 = digest2.difference(digest3);
96         System.out.println(diff2);
97         assertEquals(1, diff2.size());
98         assertEquals(diff, diff2);
99     }
100
101
102
103     public void testIsGreaterThanOrEqual() {
104         Map JavaDoc<Address, Digest.Entry> map=new HashMap JavaDoc<Address, Digest.Entry>();
105         map.put(a1, new Digest.Entry(4, 500, 501));
106         map.put(a2, new Digest.Entry(25, 26, 26));
107         map.put(a3, new Digest.Entry(20, 25, 33));
108         Digest my=new Digest(map);
109
110         System.out.println("\nd: " + d + "\nmy: " + my);
111         assertTrue(my.isGreaterThanOrEqual(d));
112
113         map.remove(a3);
114         map.put(a3, new Digest.Entry(20, 26, 33));
115         my=new Digest(map);
116         System.out.println("\nd: " + d + "\nmy: " + my);
117         assertTrue(my.isGreaterThanOrEqual(d));
118
119         map.remove(a3);
120         map.put(a3, new Digest.Entry(20, 22, 32));
121         my=new Digest(map);
122         System.out.println("\nd: " + d + "\nmy: " + my);
123         assertFalse(my.isGreaterThanOrEqual(d));
124     }
125
126     public void testEquals2() {
127         md=new MutableDigest(d);
128         System.out.println("d: " + d + "\nmd= " + md);
129         assertEquals(d, d);
130         assertEquals(d, md);
131         md.incrementHighestDeliveredSeqno(a1);
132         System.out.println("d: " + d + "\nmd= " + md);
133         assertFalse(d.equals(md));
134     }
135
136
137     public void testMutability() {
138         Digest md2=md;
139         assertEquals(md, md2);
140         md.incrementHighestDeliveredSeqno(a2);
141         assertEquals(md, md2);
142     }
143
144     public void testImmutability() {
145         MutableDigest tmp=new MutableDigest(d);
146         assertEquals(d, tmp);
147         tmp.incrementHighestDeliveredSeqno(a2);
148         assertFalse(d.equals(tmp));
149     }
150
151
152     public void testImmutability2() {
153         Digest tmp=d.copy();
154         assertEquals(d, tmp);
155     }
156
157     public void testImmutability3() {
158         Digest tmp=new Digest(d);
159         assertEquals(tmp, d);
160     }
161
162     public void testImmutability4() {
163         Digest copy=md.copy();
164         assertEquals(copy, md);
165         md.incrementHighestDeliveredSeqno(a1);
166         assertFalse(copy.equals(md));
167     }
168
169     public void testSeal() {
170         MutableDigest tmp=new MutableDigest(3);
171         tmp.add(a2, 1,2,3);
172         assertEquals(1, tmp.size());
173         tmp.seal();
174         try {
175             tmp.add(a2, 4,5,6);
176             fail("should run into an exception");
177         }
178         catch(IllegalAccessError JavaDoc e) {
179             System.out.println("received exception \"" + e.toString() + "\" - as expected");
180         }
181         assertEquals(1, tmp.size());
182     }
183
184
185     public void testSeal2() {
186         md.incrementHighestDeliveredSeqno(a1);
187         md.seal();
188         try {
189             md.incrementHighestDeliveredSeqno(a3);
190             fail("should run into an exception");
191         }
192         catch(IllegalAccessError JavaDoc e) {
193             System.out.println("received exception \"" + e.toString() + "\" - as expected");
194         }
195
196         MutableDigest tmp=new MutableDigest(md);
197         tmp.incrementHighestDeliveredSeqno(a3);
198     }
199
200     public void testAdd() {
201         assertEquals(3, md.size());
202         md.add(a1, 100, 200, 201);
203         assertEquals(3, md.size());
204         md.add(new IpAddress(14526), 1,2,3);
205         assertEquals(4, md.size());
206     }
207
208     public void testAddDigest() {
209         Digest tmp=md.copy();
210         md.add(tmp);
211         assertEquals(3, md.size());
212     }
213
214     public void testAddDigest2() {
215         MutableDigest tmp=new MutableDigest(4);
216         tmp.add(new IpAddress(1111), 1,2,3);
217         tmp.add(new IpAddress(2222), 1,2,3);
218         tmp.add(new IpAddress(5555), 1,2,3);
219         tmp.add(new IpAddress(6666), 1,2,3);
220         md.add(tmp);
221         assertEquals(5, md.size());
222     }
223
224     public void testGet() {
225         Digest.Entry entry;
226         entry=d.get(a1);
227         assertEquals(entry, new Digest.Entry(4,500,501));
228         entry=d.get(a2);
229         assertEquals(entry, new Digest.Entry(25,26,26));
230         entry=d.get(a3);
231         assertEquals(entry, new Digest.Entry(20,25,33));
232     }
233
234     public void testIncrementHighSeqno() {
235         md=new MutableDigest(3);
236         md.add(a1, 1, 100);
237         md.add(a2, 3, 300);
238         md.add(a3, 7, 700);
239
240         long tmp=md.highestDeliveredSeqnoAt(a1);
241         md.incrementHighestDeliveredSeqno(a1);
242         assertEquals(md.highestDeliveredSeqnoAt(a1), tmp+1);
243
244         tmp=md.highestDeliveredSeqnoAt(a2);
245         md.incrementHighestDeliveredSeqno(a2);
246         assertEquals(md.highestDeliveredSeqnoAt(a2), tmp+1);
247
248         tmp=md.highestDeliveredSeqnoAt(a3);
249         md.incrementHighestDeliveredSeqno(a3);
250         assertEquals(md.highestDeliveredSeqnoAt(a3), tmp+1);
251     }
252
253
254     public void testConstructor() {
255         assertEquals(3, md.size());
256         md.clear();
257         assertEquals(0, md.size());
258         md.clear();
259         assertEquals(0, md.size());
260     }
261
262
263     public void testConstructor2() {
264         Digest dd=new Digest(3);
265         assertEquals(0, dd.size());
266     }
267
268     public void testConstructor3() {
269         Digest dd=new MutableDigest(3);
270         assertEquals(0, dd.size());
271     }
272
273
274     public void testContains() {
275         assertTrue(d.contains(a1));
276         assertTrue(d.contains(a2));
277         assertTrue(d.contains(a3));
278     }
279
280
281
282     public void testResetAt() {
283         md.resetAt(a1);
284         assertEquals(0, md.lowSeqnoAt(a1));
285         assertEquals(0, md.highestDeliveredSeqnoAt(a1));
286         assertEquals(0, md.highestReceivedSeqnoAt(a1));
287     }
288
289
290     public void testLowSeqnoAt() {
291         assertEquals(4, d.lowSeqnoAt(a1));
292         assertEquals(25, d.lowSeqnoAt(a2));
293         assertEquals(20, d.lowSeqnoAt(a3));
294     }
295
296
297     public void testHighSeqnoAt() {
298         assertEquals(500, d.highestDeliveredSeqnoAt(a1));
299         assertEquals(26, d.highestDeliveredSeqnoAt(a2));
300         assertEquals(25, d.highestDeliveredSeqnoAt(a3));
301     }
302
303 // public void testSetHighSeqnoAt() {
304
// assertEquals(500, md.highSeqnoAt(a1));
305
// md.setHighSeqnoAt(a1, 555);
306
// assertEquals(555, md.highSeqnoAt(a1));
307
// }
308

309     public void testHighSeqnoSeenAt() {
310         assertEquals(501, d.highestReceivedSeqnoAt(a1));
311         assertEquals(26, d.highestReceivedSeqnoAt(a2));
312         assertEquals(33, d.highestReceivedSeqnoAt(a3));
313     }
314
315 // public void testSetHighSeenSeqnoAt() {
316
// assertEquals(26, md.highSeqnoSeenAt(a2));
317
// md.setHighSeqnoSeenAt(a2, 100);
318
// assertEquals(100, md.highSeqnoSeenAt(a2));
319
// }
320

321     public void testSetHighestDeliveredAndSeenSeqnoAt() {
322         assertEquals(4, d.lowSeqnoAt(a1));
323         assertEquals(500, d.highestDeliveredSeqnoAt(a1));
324         assertEquals(501, md.highestReceivedSeqnoAt(a1));
325         md.setHighestDeliveredAndSeenSeqnos(a1, 2, 10, 20);
326         assertEquals(2, md.lowSeqnoAt(a1));
327         assertEquals(10, md.highestDeliveredSeqnoAt(a1));
328         assertEquals(20, md.highestReceivedSeqnoAt(a1));
329     }
330
331     public void testCopy() {
332         d=d.copy();
333         testLowSeqnoAt();
334         testHighSeqnoAt();
335         testHighSeqnoSeenAt();
336         testContains();
337         testResetAt();
338     }
339
340
341     public void testCopy2() {
342         Digest tmp=d.copy();
343         assertEquals(tmp, d);
344     }
345
346
347     public void testMutableCopy() {
348         Digest copy=md.copy();
349         System.out.println("md=" + md + "\ncopy=" + copy);
350         assertEquals(md, copy);
351         md.add(a1, 4, 500, 1000);
352         System.out.println("md=" + md + "\ncopy=" + copy);
353         assertFalse(md.equals(copy));
354     }
355
356
357     public void testMerge() {
358         Map JavaDoc<Address, Digest.Entry> map=new HashMap JavaDoc<Address, Digest.Entry>();
359         map.put(a1, new Digest.Entry(3, 499, 502));
360         map.put(a2, new Digest.Entry(20, 26, 27));
361         map.put(a3, new Digest.Entry(21, 26, 35));
362         MutableDigest digest=new MutableDigest(map);
363
364         System.out.println("d: " + d);
365         System.out.println("digest: " + digest);
366         
367         digest.merge(d);
368         System.out.println("merged digest: " + digest);
369
370         assertEquals(3, d.size());
371         assertEquals(3, digest.size());
372
373         assertEquals(3, digest.lowSeqnoAt(a1));
374         assertEquals(500, digest.highestDeliveredSeqnoAt(a1));
375         assertEquals(502, digest.highestReceivedSeqnoAt(a1));
376
377         assertEquals(20, digest.lowSeqnoAt(a2));
378         assertEquals(26, digest.highestDeliveredSeqnoAt(a2));
379         assertEquals(27, digest.highestReceivedSeqnoAt(a2));
380
381         assertEquals(20, digest.lowSeqnoAt(a3));
382         assertEquals(26, digest.highestDeliveredSeqnoAt(a3));
383         assertEquals(35, digest.highestReceivedSeqnoAt(a3));
384     }
385
386     public void testNonConflictingMerge() {
387         MutableDigest cons_d=new MutableDigest(5);
388         IpAddress ip1=new IpAddress(1111), ip2=new IpAddress(2222);
389
390         cons_d.add(ip1, 1, 10, 10);
391         cons_d.add(ip2, 2, 20, 20);
392         // System.out.println("\ncons_d before: " + cons_d);
393
cons_d.merge(d);
394
395         assertEquals(5, cons_d.size());
396         //System.out.println("\ncons_d after: " + cons_d);
397
assertEquals(1, cons_d.lowSeqnoAt(ip1));
398         assertEquals(2, cons_d.lowSeqnoAt(ip2));
399         assertEquals(4, cons_d.lowSeqnoAt(a1));
400         assertEquals(25, cons_d.lowSeqnoAt(a2));
401         assertEquals(20, cons_d.lowSeqnoAt(a3));
402
403         assertEquals(10, cons_d.highestDeliveredSeqnoAt(ip1));
404         assertEquals(20, cons_d.highestDeliveredSeqnoAt(ip2));
405         assertEquals(500, cons_d.highestDeliveredSeqnoAt(a1));
406         assertEquals(26, cons_d.highestDeliveredSeqnoAt(a2));
407         assertEquals(25, cons_d.highestDeliveredSeqnoAt(a3));
408
409         assertEquals(10, cons_d.highestReceivedSeqnoAt(ip1));
410         assertEquals(20, cons_d.highestReceivedSeqnoAt(ip2));
411         assertEquals(501, cons_d.highestReceivedSeqnoAt(a1));
412         assertEquals(26, cons_d.highestReceivedSeqnoAt(a2));
413         assertEquals(33, cons_d.highestReceivedSeqnoAt(a3));
414     }
415
416
417     public void testConflictingMerge() {
418         MutableDigest new_d=new MutableDigest(2);
419         new_d.add(a1, 5, 450, 501);
420         new_d.add(a3, 18, 28, 35);
421         //System.out.println("\nd before: " + d);
422
//System.out.println("new_: " + new_d);
423
md.merge(new_d);
424
425         assertEquals(3, md.size());
426         //System.out.println("d after: " + d);
427

428         assertEquals(4, md.lowSeqnoAt(a1)); // low_seqno should *not* have changed
429
assertEquals(500, md.highestDeliveredSeqnoAt(a1)); // high_seqno should *not* have changed
430
assertEquals(501, md.highestReceivedSeqnoAt(a1)); // high_seqno_seen should *not* have changed
431

432         assertEquals(25, md.lowSeqnoAt(a2)); // low_seqno should *not* have changed
433
assertEquals(26, md.highestDeliveredSeqnoAt(a2)); // high_seqno should *not* have changed
434
assertEquals(26, md.highestReceivedSeqnoAt(a2)); // high_seqno_seen should *not* have changed
435

436         assertEquals(18, md.lowSeqnoAt(a3)); // low_seqno should *not* have changed
437
assertEquals(28, md.highestDeliveredSeqnoAt(a3)); // high_seqno should *not* have changed
438
assertEquals(35, md.highestReceivedSeqnoAt(a3)); // high_seqno_seen should *not* have changed
439
}
440
441
442     public void testSameSendersOtherIsNull() {
443         assertFalse(d.sameSenders(null));
444     }
445
446     public void testSameSenders1MNullDifferentLenth() {
447         d2=new Digest(1);
448         assertFalse(d2.sameSenders(d));
449     }
450
451     public void testSameSenders1MNullSameLength() {
452         d2=new Digest(3);
453         assertFalse(d2.sameSenders(d));
454     }
455
456     public void testSameSendersIdentical() {
457         d2=d.copy();
458         assertTrue(d.sameSenders(d2));
459     }
460
461     public void testSameSendersNotIdentical() {
462         MutableDigest tmp=new MutableDigest(3);
463         tmp.add(a1, 4, 500, 501);
464         tmp.add(a3, 20, 25, 33);
465         tmp.add(a2, 25, 26, 26);
466         assertTrue(md.sameSenders(tmp));
467         assertTrue(d.sameSenders(tmp));
468     }
469
470     public void testSameSendersNotSameLength() {
471         md=new MutableDigest(3);
472         md.add(a1, 4, 500, 501);
473         md.add(a2, 25, 26, 26);
474         assertFalse(d.sameSenders(md));
475     }
476
477
478     public void testStreamable() throws IOException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
479         ByteArrayOutputStream outstream=new ByteArrayOutputStream();
480         DataOutputStream dos=new DataOutputStream(outstream);
481         d.writeTo(dos);
482         dos.close();
483         byte[] buf=outstream.toByteArray();
484         ByteArrayInputStream instream=new ByteArrayInputStream(buf);
485         DataInputStream dis=new DataInputStream(instream);
486         Digest tmp=new Digest();
487         tmp.readFrom(dis);
488         assertEquals(d, tmp);
489     }
490
491     public void testSerializedSize() throws Exception JavaDoc {
492         long len=d.serializedSize();
493         byte[] buf=Util.streamableToByteBuffer(d);
494         assertEquals(len, buf.length);
495     }
496
497
498     public static Test suite() {
499         return new TestSuite(DigestTest.class);
500     }
501
502     public static void main(String JavaDoc[] args) {
503         junit.textui.TestRunner.run(suite());
504     }
505 }
506
Popular Tags