001: // $Id
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.stack.LogicalAddress;
009:
010: import java.io.ByteArrayInputStream;
011: import java.io.ByteArrayOutputStream;
012: import java.io.ObjectInputStream;
013: import java.io.ObjectOutputStream;
014:
015: public class LogicalAddressTest extends TestCase {
016: LogicalAddress a, b, c;
017:
018: public LogicalAddressTest(String name) {
019: super (name);
020: }
021:
022: public void setUp() throws Exception {
023: super .setUp();
024: a = new LogicalAddress("host1", null);
025: b = new LogicalAddress("host1", null);
026: c = (LogicalAddress) a.clone();
027: }
028:
029: public void testEquality() throws Exception {
030: assertFalse(a.equals(b));
031: assertFalse(c.equals(b));
032: assertTrue(a.equals(c));
033: assertTrue(c.equals(a));
034: }
035:
036: public void testMcast() {
037: assertFalse(a.isMulticastAddress());
038: }
039:
040: public void testCompareTo() {
041: assertTrue(a.compareTo(c) == 0);
042: assertTrue(a.compareTo(b) < 0);
043: assertTrue(b.compareTo(a) > 0);
044: }
045:
046: public void testExternalization() throws Exception {
047: ByteArrayOutputStream bos = new ByteArrayOutputStream();
048: ObjectOutputStream oos = new ObjectOutputStream(bos);
049: byte[] buf = null;
050: ByteArrayInputStream bis = null;
051: ObjectInputStream ois;
052: LogicalAddress a2, b2;
053:
054: a.setAdditionalData(null);
055: b.setAdditionalData("Bela Ban".getBytes());
056: oos.writeObject(a);
057: oos.writeObject(b);
058:
059: buf = bos.toByteArray();
060: bis = new ByteArrayInputStream(buf);
061: ois = new ObjectInputStream(bis);
062: a2 = (LogicalAddress) ois.readObject();
063: b2 = (LogicalAddress) ois.readObject();
064:
065: assertTrue(a.equals(a2));
066: assertTrue(b.equals(b2));
067:
068: assertTrue(a2.getAdditionalData() == null);
069: assertTrue("Bela Ban"
070: .equals(new String(b2.getAdditionalData())));
071: }
072:
073: public void testExternalizationAdditionalData() throws Exception {
074: ByteArrayOutputStream bos = new ByteArrayOutputStream();
075: ObjectOutputStream oos = new ObjectOutputStream(bos);
076: byte[] buf = null;
077: ByteArrayInputStream bis = null;
078: ObjectInputStream ois;
079: LogicalAddress a2, b2, c2;
080:
081: oos.writeObject(a);
082: oos.writeObject(b);
083: oos.writeObject(c);
084:
085: buf = bos.toByteArray();
086: bis = new ByteArrayInputStream(buf);
087: ois = new ObjectInputStream(bis);
088: a2 = (LogicalAddress) ois.readObject();
089: b2 = (LogicalAddress) ois.readObject();
090: c2 = (LogicalAddress) ois.readObject();
091:
092: assertTrue(a.equals(a2));
093: assertTrue(b.equals(b2));
094: assertTrue(c.equals(c2));
095: assertTrue(c2.equals(a2));
096: }
097:
098: public static Test suite() {
099: TestSuite s = new TestSuite(LogicalAddressTest.class);
100: return s;
101: }
102:
103: public static void main(String[] args) {
104: junit.textui.TestRunner.run(suite());
105: }
106: }
|