001: // $Id: DigestTest.java,v 1.6 2006/08/31 13:48:13 belaban Exp $
002:
003: package org.jgroups.tests;
004:
005: import junit.framework.Test;
006: import junit.framework.TestCase;
007: import junit.framework.TestSuite;
008: import org.jgroups.protocols.pbcast.Digest;
009: import org.jgroups.stack.IpAddress;
010: import org.jgroups.util.Util;
011:
012: import java.io.*;
013:
014: public class DigestTest extends TestCase {
015: Digest d, d2;
016: IpAddress a1, a2, a3;
017:
018: public DigestTest(String name) {
019: super (name);
020: }
021:
022: public void setUp() throws Exception {
023: super .setUp();
024: d = new Digest(3);
025: a1 = new IpAddress(5555);
026: a2 = new IpAddress(6666);
027: a3 = new IpAddress(7777);
028: d.add(a1, 4, 500, 501);
029: d.add(a2, 25, 26, 26);
030: d.add(a3, 20, 25, 33);
031: }
032:
033: public void testSize() {
034: d2 = new Digest(3);
035: assertEquals(0, d2.size());
036: }
037:
038: public void testEquals() {
039: d2 = d.copy();
040: System.out.println("d: " + d + "\nd2= " + d2);
041: assertEquals(d, d);
042: assertEquals(d, d2);
043: d2.incrementHighSeqno(a1);
044: assertFalse(d.equals(d2));
045: }
046:
047: public void testAdd() {
048: assertEquals(3, d.size());
049: d.add(a1, 100, 200, 201);
050: assertEquals(3, d.size());
051: d.add(new IpAddress(14526), 1, 2, 3);
052: assertEquals(4, d.size());
053: }
054:
055: public void testAddDigest() {
056: d2 = d.copy();
057: d.add(d2);
058: assertEquals(3, d.size());
059: }
060:
061: public void testAddDigest2() {
062: d2 = new Digest(4);
063: d2.add(new IpAddress(1111), 1, 2, 3);
064: d2.add(new IpAddress(2222), 1, 2, 3);
065: d2.add(new IpAddress(5555), 1, 2, 3);
066: d2.add(new IpAddress(6666), 1, 2, 3);
067: d.add(d2);
068: assertEquals(5, d.size());
069: }
070:
071: public void testGet() {
072: Digest.Entry entry;
073: entry = d.get(a1);
074: assertEquals(entry, new Digest.Entry(4, 500, 501));
075: entry = d.get(a2);
076: assertEquals(entry, new Digest.Entry(25, 26, 26));
077: entry = d.get(a3);
078: assertEquals(entry, new Digest.Entry(20, 25, 33));
079: }
080:
081: public void testIncrementHighSeqno() {
082: d2 = new Digest(3);
083: d2.add(a1, 1, 100);
084: d2.add(a2, 3, 300);
085: d2.add(a3, 7, 700);
086:
087: long tmp = d2.highSeqnoAt(a1);
088: d2.incrementHighSeqno(a1);
089: assertEquals(d2.highSeqnoAt(a1), tmp + 1);
090:
091: tmp = d2.highSeqnoAt(a2);
092: d2.incrementHighSeqno(a2);
093: assertEquals(d2.highSeqnoAt(a2), tmp + 1);
094:
095: tmp = d2.highSeqnoAt(a3);
096: d2.incrementHighSeqno(a3);
097: assertEquals(d2.highSeqnoAt(a3), tmp + 1);
098: }
099:
100: public void testConstructor() {
101: assertEquals(3, d.size());
102: d.clear();
103: assertEquals(0, d.size());
104: d.clear();
105: assertEquals(0, d.size());
106: }
107:
108: public void testConstructor2() {
109: Digest dd = new Digest(3);
110: assertEquals(0, dd.size());
111: }
112:
113: public void testContains() {
114: assertTrue(d.contains(a1));
115: assertTrue(d.contains(a2));
116: assertTrue(d.contains(a3));
117: }
118:
119: public void testResetAt() {
120: d.resetAt(a1);
121: assertEquals(0, d.lowSeqnoAt(a1));
122: assertEquals(0, d.highSeqnoAt(a1));
123: assertEquals(d.highSeqnoSeenAt(a1), -1);
124: }
125:
126: public void testLowSeqnoAt() {
127: assertEquals(4, d.lowSeqnoAt(a1));
128: assertEquals(25, d.lowSeqnoAt(a2));
129: assertEquals(20, d.lowSeqnoAt(a3));
130: }
131:
132: public void testHighSeqnoAt() {
133: assertEquals(500, d.highSeqnoAt(a1));
134: assertEquals(26, d.highSeqnoAt(a2));
135: assertEquals(25, d.highSeqnoAt(a3));
136: }
137:
138: public void testHighSeqnoSeenAt() {
139: assertEquals(501, d.highSeqnoSeenAt(a1));
140: assertEquals(26, d.highSeqnoSeenAt(a2));
141: assertEquals(33, d.highSeqnoSeenAt(a3));
142: }
143:
144: public void testCopy() {
145: d = d.copy();
146: testLowSeqnoAt();
147: testHighSeqnoAt();
148: testHighSeqnoSeenAt();
149: testContains();
150: testResetAt();
151: }
152:
153: public void testNonConflictingMerge() {
154: Digest cons_d = new Digest(5);
155: IpAddress ip1 = new IpAddress(1111), ip2 = new IpAddress(2222);
156:
157: cons_d.add(ip1, 1, 10, 10);
158: cons_d.add(ip2, 2, 20, 20);
159: // System.out.println("\ncons_d before: " + cons_d);
160: cons_d.merge(d);
161:
162: assertEquals(5, cons_d.size());
163: //System.out.println("\ncons_d after: " + cons_d);
164: assertEquals(1, cons_d.lowSeqnoAt(ip1));
165: assertEquals(2, cons_d.lowSeqnoAt(ip2));
166: assertEquals(4, cons_d.lowSeqnoAt(a1));
167: assertEquals(25, cons_d.lowSeqnoAt(a2));
168: assertEquals(20, cons_d.lowSeqnoAt(a3));
169:
170: assertEquals(10, cons_d.highSeqnoAt(ip1));
171: assertEquals(20, cons_d.highSeqnoAt(ip2));
172: assertEquals(500, cons_d.highSeqnoAt(a1));
173: assertEquals(26, cons_d.highSeqnoAt(a2));
174: assertEquals(25, cons_d.highSeqnoAt(a3));
175:
176: assertEquals(10, cons_d.highSeqnoSeenAt(ip1));
177: assertEquals(20, cons_d.highSeqnoSeenAt(ip2));
178: assertEquals(501, cons_d.highSeqnoSeenAt(a1));
179: assertEquals(26, cons_d.highSeqnoSeenAt(a2));
180: assertEquals(33, cons_d.highSeqnoSeenAt(a3));
181: }
182:
183: public void testConflictingMerge() {
184: Digest new_d = new Digest(2);
185: new_d.add(a1, 5, 450, 501);
186: new_d.add(a3, 18, 28, 35);
187: //System.out.println("\nd before: " + d);
188: //System.out.println("new_: " + new_d);
189: d.merge(new_d);
190:
191: assertEquals(3, d.size());
192: //System.out.println("d after: " + d);
193:
194: assertEquals(4, d.lowSeqnoAt(a1)); // low_seqno should *not* have changed
195: assertEquals(500, d.highSeqnoAt(a1)); // high_seqno should *not* have changed
196: assertEquals(501, d.highSeqnoSeenAt(a1)); // high_seqno_seen should *not* have changed
197:
198: assertEquals(25, d.lowSeqnoAt(a2)); // low_seqno should *not* have changed
199: assertEquals(26, d.highSeqnoAt(a2)); // high_seqno should *not* have changed
200: assertEquals(26, d.highSeqnoSeenAt(a2)); // high_seqno_seen should *not* have changed
201:
202: assertEquals(18, d.lowSeqnoAt(a3)); // low_seqno should *not* have changed
203: assertEquals(28, d.highSeqnoAt(a3)); // high_seqno should *not* have changed
204: assertEquals(35, d.highSeqnoSeenAt(a3)); // high_seqno_seen should *not* have changed
205: }
206:
207: public void testSameSendersOtherIsNull() {
208: assertFalse(d.sameSenders(null));
209: }
210:
211: public void testSameSenders1MNullDifferentLenth() {
212: d2 = new Digest(1);
213: assertFalse(d2.sameSenders(d));
214: }
215:
216: public void testSameSenders1MNullSameLength() {
217: d2 = new Digest(3);
218: assertFalse(d2.sameSenders(d));
219: }
220:
221: public void testSameSendersIdentical() {
222: d2 = d.copy();
223: assertTrue(d.sameSenders(d2));
224: }
225:
226: public void testSameSendersNotIdentical() {
227: d2 = new Digest(3);
228: d2.add(a1, 4, 500, 501);
229: d2.add(a3, 20, 25, 33);
230: d2.add(a2, 25, 26, 26);
231: assertTrue(d.sameSenders(d2));
232: }
233:
234: public void testSameSendersNotSameLength() {
235: d2 = new Digest(3);
236: d2.add(a1, 4, 500, 501);
237: d2.add(a2, 25, 26, 26);
238: assertFalse(d.sameSenders(d2));
239: }
240:
241: public void testStreamable() throws IOException,
242: IllegalAccessException, InstantiationException {
243: ByteArrayOutputStream outstream = new ByteArrayOutputStream();
244: DataOutputStream dos = new DataOutputStream(outstream);
245: d.writeTo(dos);
246: dos.close();
247: byte[] buf = outstream.toByteArray();
248: ByteArrayInputStream instream = new ByteArrayInputStream(buf);
249: DataInputStream dis = new DataInputStream(instream);
250: Digest tmp = new Digest();
251: tmp.readFrom(dis);
252: assertEquals(d, tmp);
253: }
254:
255: public void testSerializedSize() throws Exception {
256: long len = d.serializedSize();
257: byte[] buf = Util.streamableToByteBuffer(d);
258: assertEquals(len, buf.length);
259: }
260:
261: public static Test suite() {
262: return new TestSuite(DigestTest.class);
263: }
264:
265: public static void main(String[] args) {
266: junit.textui.TestRunner.run(suite());
267: }
268: }
|