01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.net.groups;
05:
06: import java.io.ByteArrayInputStream;
07: import java.io.ByteArrayOutputStream;
08: import java.io.ObjectInput;
09: import java.io.ObjectInputStream;
10: import java.io.ObjectOutput;
11: import java.io.ObjectOutputStream;
12: import java.util.HashSet;
13:
14: import junit.framework.TestCase;
15:
16: public class NodeIDTest extends TestCase {
17:
18: public void test() throws Exception {
19: byte[] b1 = new byte[] { 34, 55, 2, (byte) 255, 0 };
20: byte[] b2 = new byte[] { 34, 55, 2, (byte) 255, 0, 4 };
21: byte[] b3 = new byte[] { 43, 5, 127, (byte) 255, -87, 9 };
22: byte[] b4 = new byte[] { 4 };
23:
24: NodeID n1 = new NodeIDImpl("node1", b1);
25: NodeID n2 = new NodeIDImpl("node2", b2);
26: NodeID n3 = new NodeIDImpl("node3", b3);
27: NodeID n4 = new NodeIDImpl("node4", b4);
28:
29: assertFalse(n1.equals(n2));
30: assertTrue(n1.equals(n1));
31: assertFalse(n1.equals(n3));
32: assertTrue(n3.equals(n3));
33: assertFalse(n1.equals(n4));
34: assertTrue(n4.equals(n4));
35: assertFalse(n3.equals(n4));
36: assertTrue(n2.equals(n2));
37:
38: HashSet set = new HashSet();
39: assertTrue(set.add(n1));
40: assertTrue(set.add(n2));
41: assertTrue(set.add(n3));
42: assertTrue(set.add(n4));
43: assertFalse(set.add(n1));
44: assertFalse(set.add(n2));
45: assertFalse(set.add(n3));
46: assertFalse(set.add(n4));
47:
48: ByteArrayOutputStream bo = new ByteArrayOutputStream();
49: ObjectOutput oo = new ObjectOutputStream(bo);
50: oo.writeObject(n1);
51: System.err.println("Written : " + n1);
52: oo.writeObject(n2);
53: System.err.println("Written : " + n2);
54: oo.writeObject(n3);
55: System.err.println("Written : " + n3);
56: oo.writeObject(n4);
57: System.err.println("Written : " + n3);
58: oo.writeObject(NodeIDImpl.NULL_ID);
59: System.err.println("Written : " + NodeIDImpl.NULL_ID);
60:
61: ByteArrayInputStream bi = new ByteArrayInputStream(bo
62: .toByteArray());
63: ObjectInput oi = new ObjectInputStream(bi);
64: NodeID r1 = (NodeID) oi.readObject();
65: System.err.println("Read : " + r1);
66: assertEquals(n1, r1);
67: NodeID r2 = (NodeID) oi.readObject();
68: System.err.println("Read : " + r2);
69: assertEquals(n2, r2);
70: NodeID r3 = (NodeID) oi.readObject();
71: System.err.println("Read : " + r3);
72: assertEquals(n3, r3);
73: NodeID r4 = (NodeID) oi.readObject();
74: System.err.println("Read : " + r4);
75: assertEquals(n4, r4);
76: NodeID r5 = (NodeID) oi.readObject();
77: System.err.println("Read : " + r5);
78: assertEquals(NodeIDImpl.NULL_ID, r5);
79:
80: }
81: }
|