01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.net;
05:
06: import java.net.InetAddress;
07: import java.net.UnknownHostException;
08: import java.util.HashMap;
09: import java.util.Map;
10:
11: import junit.framework.TestCase;
12:
13: public class TCSocketAddressTest extends TestCase {
14:
15: public void testHashCode() {
16: TCSocketAddress tsa1 = new TCSocketAddress(
17: TCSocketAddress.LOOPBACK_ADDR, 9000);
18: TCSocketAddress tsa2 = new TCSocketAddress(
19: TCSocketAddress.LOOPBACK_ADDR, 9000);
20:
21: assertTrue(tsa1.hashCode() == tsa2.hashCode());
22: }
23:
24: public void testPortRanges() {
25: TCSocketAddress s1 = new TCSocketAddress(0);
26: TCSocketAddress s2 = new TCSocketAddress(65535);
27: if (s1 != null)
28: s1 = null; // silence warning
29: if (s2 != null)
30: s2 = null; // silence warning
31:
32: boolean exception = false;
33:
34: try {
35: TCSocketAddress s3 = new TCSocketAddress(-1);
36: if (s3 != null)
37: s3 = null; // silence warning
38: } catch (IllegalArgumentException e) {
39: exception = true;
40: }
41: assertTrue(exception);
42:
43: exception = false;
44: try {
45: TCSocketAddress s3 = new TCSocketAddress(65536);
46: if (s3 != null)
47: s3 = null; // silence warning
48: } catch (IllegalArgumentException e) {
49: exception = true;
50: }
51: assertTrue(exception);
52: }
53:
54: public void testStringForm() {
55: TCSocketAddress sa = new TCSocketAddress(
56: TCSocketAddress.LOOPBACK_ADDR, 12345);
57: String s = sa.getStringForm();
58: assertTrue(s.matches("^\\d+\\.\\d+\\.\\d+\\.\\d+\\:12345$"));
59: }
60:
61: public void testEquals() {
62: TCSocketAddress tsa1 = new TCSocketAddress(9000);
63: TCSocketAddress tsa2 = new TCSocketAddress(
64: TCSocketAddress.LOOPBACK_ADDR, 9000);
65: TCSocketAddress tsa3 = new TCSocketAddress(
66: TCSocketAddress.LOOPBACK_ADDR, 9000);
67:
68: assertTrue(tsa1.equals(tsa2));
69: assertTrue(tsa2.equals(tsa3));
70: }
71:
72: public void testInCollections() throws UnknownHostException {
73: TCSocketAddress tsa1 = new TCSocketAddress(9000);
74: TCSocketAddress tsa2 = new TCSocketAddress(InetAddress
75: .getByName("1.2.3.4"), 9000);
76: TCSocketAddress tsa3 = new TCSocketAddress(InetAddress
77: .getByName("1.2.3.4"), 9000);
78:
79: Map map = new HashMap();
80:
81: Object val1 = new Object();
82: Object val2 = new Object();
83: Object val3 = new Object();
84:
85: map.put(tsa1, val1);
86: map.put(tsa2, val2);
87: map.put(tsa3, val3);
88:
89: assertTrue(map.size() == 2);
90:
91: assertTrue(map.get(tsa1) == val1);
92: assertTrue(map.get(tsa2) == val3);
93: assertTrue(map.get(tsa3) == val3);
94: }
95: }
|