001: // $Id: IpAddressTest.java,v 1.16 2006/05/16 11:14:28 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.stack.IpAddress;
009: import org.jgroups.util.Util;
010:
011: import java.io.*;
012: import java.net.InetAddress;
013: import java.net.UnknownHostException;
014: import java.util.HashMap;
015: import java.util.HashSet;
016:
017: public class IpAddressTest extends TestCase {
018: IpAddress a, b, c, d, e, f, g, h, i, j, k;
019:
020: public IpAddressTest(String name) {
021: super (name);
022: }
023:
024: public void setUp() throws Exception {
025: super .setUp();
026: a = new IpAddress("localhost", 5555);
027: b = new IpAddress("localhost", 5555);
028: c = b;
029: d = new IpAddress("localhost", 5556);
030: e = new IpAddress("127.0.0.1", 5555);
031: f = new IpAddress("www.ibm.com", 80);
032: g = new IpAddress("www.ibm.com", 8080);
033: h = new IpAddress("224.0.0.1", 5555);
034: }
035:
036: public void testUnknownAddress() {
037: try {
038: IpAddress tmp = new IpAddress("idontknow.com", 55);
039: fail("should throw an UnknownHostException");
040: } catch (UnknownHostException e1) {
041: }
042: }
043:
044: public void testEquality() throws Exception {
045: assertEquals(a, b);
046: assertEquals(c, b);
047: assertEquals(a, e);
048: assertEquals(c, e);
049: }
050:
051: public void testEqualityWithDnsRoundRobin()
052: throws UnknownHostException {
053: IpAddress x1, x2, x3;
054:
055: InetAddress addr = InetAddress.getByName("127.0.0.1");
056: byte[] rawAddr = addr.getAddress();
057:
058: InetAddress inet1 = InetAddress
059: .getByAddress("MyHost1", rawAddr);
060: InetAddress inet2 = InetAddress
061: .getByAddress("MyHost2", rawAddr);
062: InetAddress inet3 = InetAddress
063: .getByAddress("MyHost3", rawAddr);
064: assertEquals(inet1, inet2);
065:
066: x1 = new IpAddress(inet1, 5555);
067: x2 = new IpAddress(inet2, 5555);
068: x3 = new IpAddress(inet3, 5555);
069:
070: assertEquals(x1, x2);
071: assertEquals(x3, x1);
072:
073: HashSet s = new HashSet();
074: s.add(x1);
075: s.add(x2);
076: s.add(x3);
077: System.out.println("s=" + s);
078: assertEquals(1, s.size());
079:
080: HashMap m = new HashMap();
081: m.put(x1, "Bela");
082: m.put(x2, "Michelle");
083: m.put(x3, "Nicole");
084: assertEquals(1, m.size());
085: assertEquals("Nicole", m.get(x1));
086: }
087:
088: public void testInequality() throws Exception {
089: IpAddress tmp = null;
090: assertTrue(!a.equals(d));
091: assertTrue(!c.equals(d));
092: assertTrue(!a.equals(f));
093: assertTrue(!e.equals(f));
094: assertTrue(!f.equals(g));
095: assertFalse(a.equals(tmp));
096: }
097:
098: public void testSameHost() throws Exception {
099: assertTrue(Util.sameHost(a, b));
100: assertTrue(Util.sameHost(a, c));
101: assertTrue(Util.sameHost(a, d));
102: assertTrue(Util.sameHost(a, e));
103: assertTrue(Util.sameHost(f, g));
104: }
105:
106: public void testNotSameHost() throws Exception {
107: assertTrue(!Util.sameHost(a, f));
108: assertTrue(!Util.sameHost(e, f));
109: assertTrue(!Util.sameHost(e, null));
110: assertTrue(!Util.sameHost(null, null));
111: }
112:
113: public void testMcast() {
114: assertTrue(h.isMulticastAddress());
115: assertTrue(!a.isMulticastAddress());
116: assertTrue(!e.isMulticastAddress());
117: assertTrue(!g.isMulticastAddress());
118:
119: }
120:
121: public void testCompareTo() {
122: assertEquals(0, a.compareTo(b));
123: assertTrue(a.compareTo(d) < 0);
124: assertTrue(d.compareTo(a) > 0);
125: }
126:
127: public void testCompareTime() {
128: final int NUM = 1000000;
129: _testCompareTime(a, a, NUM);
130: _testCompareTime(a, b, NUM);
131: _testCompareTime(a, c, NUM);
132: _testCompareTime(a, d, NUM);
133: }
134:
135: private void _testCompareTime(IpAddress one, IpAddress two, int num) {
136: int rc = -99;
137: long start = System.currentTimeMillis(), stop;
138: for (int x = 0; x < num; x++) {
139: rc = one.compareTo(two);
140: }
141: stop = System.currentTimeMillis();
142: long diff = stop - start;
143: System.out.println("calling compareTo(" + one + ", " + two
144: + ") " + num + " times took " + diff + "ms, result="
145: + rc);
146: }
147:
148: public void testHashcode() {
149: int hcode_a = a.hashCode();
150: int hcode_b = b.hashCode();
151: assertEquals(hcode_a, hcode_b);
152: }
153:
154: public void testHashcodeTime() {
155: int hash = -1;
156: final int NUM = 10000000;
157:
158: long start = System.currentTimeMillis(), stop;
159: for (int x = 0; x < NUM; x++) {
160: hash = a.hashCode();
161: }
162: stop = System.currentTimeMillis();
163: long diff = stop - start;
164: System.out.println("taking the hash code of " + a + "(" + hash
165: + ") took " + diff + "ms");
166: }
167:
168: public void testIPv6WithExternalization() throws IOException,
169: ClassNotFoundException {
170: InetAddress tmp = Util.getFirstNonLoopbackIPv6Address();
171: IpAddress ip = new IpAddress(tmp, 5555);
172:
173: ByteArrayOutputStream bos = new ByteArrayOutputStream();
174: ObjectOutputStream oos = new ObjectOutputStream(bos);
175: byte[] buf = null;
176: ByteArrayInputStream bis = null;
177: ObjectInputStream ois;
178:
179: System.out.println("-- address is " + tmp);
180:
181: oos.writeObject(ip);
182: buf = bos.toByteArray();
183: bis = new ByteArrayInputStream(buf);
184: ois = new ObjectInputStream(bis);
185: IpAddress ip2 = (IpAddress) ois.readObject();
186: assertEquals(ip, ip2);
187: }
188:
189: public void testIPv6WithStreamable() throws IOException,
190: ClassNotFoundException {
191: InetAddress tmp = Util.getFirstNonLoopbackIPv6Address();
192: IpAddress ip = new IpAddress(tmp, 5555);
193:
194: ByteArrayOutputStream bos = new ByteArrayOutputStream();
195: DataOutputStream dos = new DataOutputStream(bos);
196: byte[] buf = null;
197: ByteArrayInputStream bis = null;
198: DataInputStream dis;
199:
200: System.out.println("-- address is " + tmp);
201:
202: ip.writeTo(dos);
203: buf = bos.toByteArray();
204: bis = new ByteArrayInputStream(buf);
205: dis = new DataInputStream(bis);
206: IpAddress ip2 = new IpAddress();
207: ip2.readFrom(dis);
208: assertEquals(ip, ip2);
209: }
210:
211: public void testExternalization() throws Exception {
212: ByteArrayOutputStream bos = new ByteArrayOutputStream();
213: ObjectOutputStream oos = new ObjectOutputStream(bos);
214: byte[] buf = null;
215: ByteArrayInputStream bis = null;
216: ObjectInputStream ois;
217: IpAddress a2, b2;
218:
219: a.setAdditionalData(null);
220: b.setAdditionalData("Bela Ban".getBytes());
221: oos.writeObject(a);
222: oos.writeObject(b);
223:
224: buf = bos.toByteArray();
225: bis = new ByteArrayInputStream(buf);
226: ois = new ObjectInputStream(bis);
227: a2 = (IpAddress) ois.readObject();
228: b2 = (IpAddress) ois.readObject();
229:
230: assertEquals(a, a2);
231: assertEquals(b, b2);
232:
233: assertTrue(a2.getAdditionalData() == null);
234: assertEquals("Bela Ban", new String(b2.getAdditionalData()));
235: }
236:
237: public void testExternalizationAdditionalData() throws Exception {
238: ByteArrayOutputStream bos = new ByteArrayOutputStream();
239: ObjectOutputStream oos = new ObjectOutputStream(bos);
240: byte[] buf = null;
241: ByteArrayInputStream bis = null;
242: ObjectInputStream ois;
243: IpAddress a2, b2, c2, d2, e2, f2, g2, h2;
244:
245: oos.writeObject(a);
246: oos.writeObject(b);
247: oos.writeObject(c);
248: oos.writeObject(d);
249: oos.writeObject(e);
250: oos.writeObject(f);
251: oos.writeObject(g);
252: oos.writeObject(h);
253:
254: buf = bos.toByteArray();
255: bis = new ByteArrayInputStream(buf);
256: ois = new ObjectInputStream(bis);
257: a2 = (IpAddress) ois.readObject();
258: b2 = (IpAddress) ois.readObject();
259: c2 = (IpAddress) ois.readObject();
260: d2 = (IpAddress) ois.readObject();
261: e2 = (IpAddress) ois.readObject();
262: f2 = (IpAddress) ois.readObject();
263: g2 = (IpAddress) ois.readObject();
264: h2 = (IpAddress) ois.readObject();
265:
266: assertEquals(b2, c2);
267: assertEquals(a, a2);
268: assertEquals(b, b2);
269: assertEquals(c, c2);
270: assertEquals(d, d2);
271: assertEquals(e, e2);
272: assertEquals(f, f2);
273: assertEquals(g, g2);
274: assertEquals(h, h2);
275: }
276:
277: public void testStreamable() throws Exception {
278: ByteArrayOutputStream bos = new ByteArrayOutputStream();
279: DataOutputStream oos = new DataOutputStream(bos);
280: byte[] buf = null;
281: ByteArrayInputStream bis = null;
282: DataInputStream ois;
283: IpAddress a2, b2, x, x2, y, y2;
284:
285: x = new IpAddress(5555);
286: x.setAdditionalData(new byte[] { 'b', 'e', 'l', 'a' });
287:
288: y = new IpAddress();
289: y.setAdditionalData(new byte[] { 'b', 'e', 'l', 'a' });
290:
291: a.setAdditionalData(null);
292: b.setAdditionalData("Bela Ban".getBytes());
293: a.writeTo(oos);
294: b.writeTo(oos);
295: x.writeTo(oos);
296: y.writeTo(oos);
297:
298: buf = bos.toByteArray();
299: bis = new ByteArrayInputStream(buf);
300: ois = new DataInputStream(bis);
301: a2 = new IpAddress();
302: a2.readFrom(ois);
303: b2 = new IpAddress();
304: b2.readFrom(ois);
305: x2 = new IpAddress();
306: x2.readFrom(ois);
307: y2 = new IpAddress();
308: y2.readFrom(ois);
309:
310: assertEquals(a, a2);
311: assertEquals(b, b2);
312:
313: assertNull(a2.getAdditionalData());
314: assertEquals("Bela Ban", new String(b2.getAdditionalData()));
315:
316: assertNotNull(x2.getAdditionalData());
317: assertEquals(4, x2.getAdditionalData().length);
318:
319: assertNull(y2.getIpAddress());
320: assertEquals(0, y2.getPort());
321: assertNotNull(y2.getAdditionalData());
322: assertEquals(4, y2.getAdditionalData().length);
323: }
324:
325: public void testStreamableAdditionalData() throws Exception {
326: ByteArrayOutputStream bos = new ByteArrayOutputStream();
327: DataOutputStream oos = new DataOutputStream(bos);
328: byte[] buf = null;
329: ByteArrayInputStream bis = null;
330: DataInputStream ois;
331: IpAddress a2, b2, c2, d2, e2, f2, g2, h2;
332:
333: a.writeTo(oos);
334: b.writeTo(oos);
335: c.writeTo(oos);
336: d.writeTo(oos);
337: e.writeTo(oos);
338: f.writeTo(oos);
339: g.writeTo(oos);
340: h.writeTo(oos);
341:
342: buf = bos.toByteArray();
343: bis = new ByteArrayInputStream(buf);
344: ois = new DataInputStream(bis);
345: a2 = new IpAddress();
346: a2.readFrom(ois);
347: b2 = new IpAddress();
348: b2.readFrom(ois);
349: c2 = new IpAddress();
350: c2.readFrom(ois);
351: d2 = new IpAddress();
352: d2.readFrom(ois);
353: e2 = new IpAddress();
354: e2.readFrom(ois);
355: f2 = new IpAddress();
356: f2.readFrom(ois);
357: g2 = new IpAddress();
358: g2.readFrom(ois);
359: h2 = new IpAddress();
360: h2.readFrom(ois);
361:
362: assertEquals(b2, c2);
363: assertEquals(a, a2);
364: assertEquals(b, b2);
365: assertEquals(c, c2);
366: assertEquals(d, d2);
367: assertEquals(e, e2);
368: assertEquals(f, f2);
369: assertEquals(g, g2);
370: assertEquals(h, h2);
371: }
372:
373: public static Test suite() {
374: return new TestSuite(IpAddressTest.class);
375: }
376:
377: public static void main(String[] args) {
378: junit.textui.TestRunner.run(suite());
379: }
380: }
|