1 4 package com.tc.net.groups; 5 6 import java.io.ByteArrayInputStream ; 7 import java.io.ByteArrayOutputStream ; 8 import java.io.ObjectInput ; 9 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 NodeID("node1", b1); 25 NodeID n2 = new NodeID("node2", b2); 26 NodeID n3 = new NodeID("node3", b3); 27 NodeID n4 = new NodeID("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 49 ByteArrayOutputStream bo = new ByteArrayOutputStream (); 50 ObjectOutput oo = new ObjectOutputStream (bo); 51 oo.writeObject(n1); 52 System.err.println("Written : " + n1); 53 oo.writeObject(n2); 54 System.err.println("Written : " + n2); 55 oo.writeObject(n3); 56 System.err.println("Written : " + n3); 57 oo.writeObject(n4); 58 System.err.println("Written : " + n3); 59 oo.writeObject(NodeID.NULL_ID); 60 System.err.println("Written : " + NodeID.NULL_ID); 61 62 ByteArrayInputStream bi = new ByteArrayInputStream (bo.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(NodeID.NULL_ID, r5); 79 80 81 } 82 } 83 | Popular Tags |