1 3 package org.jgroups.tests; 4 5 import junit.framework.Test; 6 import junit.framework.TestCase; 7 import junit.framework.TestSuite; 8 import org.jgroups.stack.IpAddress; 9 import org.jgroups.util.Util; 10 11 import java.io.*; 12 import java.net.InetAddress ; 13 import java.net.UnknownHostException ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 17 18 public class IpAddressTest extends TestCase { 19 IpAddress a, b, c, d, e, f, g, h, i, j, k; 20 21 22 23 public IpAddressTest(String name) { 24 super(name); 25 } 26 27 28 public void setUp() throws Exception { 29 super.setUp(); 30 a=new IpAddress("localhost", 5555); 31 b=new IpAddress("localhost", 5555); 32 c=b; 33 d=new IpAddress("localhost", 5556); 34 e=new IpAddress("127.0.0.1", 5555); 35 f=new IpAddress("www.ibm.com", 80); 36 g=new IpAddress("www.ibm.com", 8080); 37 h=new IpAddress("224.0.0.1", 5555); 38 } 39 40 41 42 public void testUnknownAddress() { 43 try { 44 IpAddress tmp=new IpAddress("idontknow.com", 55); 45 fail("should throw an UnknownHostException"); 46 } 47 catch(UnknownHostException e1) { 48 } 49 } 50 51 public void testEquality() throws Exception { 52 assertEquals(a, b); 53 assertEquals(c, b); 54 assertEquals(a, e); 55 assertEquals(c, e); 56 } 57 58 public void testEqualityWithDnsRoundRobin() throws UnknownHostException { 59 IpAddress x1, x2, x3; 60 61 InetAddress addr=InetAddress.getByName("127.0.0.1"); 62 byte[] rawAddr=addr.getAddress(); 63 64 InetAddress inet1=InetAddress.getByAddress("MyHost1", rawAddr); 65 InetAddress inet2=InetAddress.getByAddress("MyHost2", rawAddr); 66 InetAddress inet3=InetAddress.getByAddress("MyHost3", rawAddr); 67 assertEquals(inet1, inet2); 68 69 x1=new IpAddress(inet1, 5555); 70 x2=new IpAddress(inet2, 5555); 71 x3=new IpAddress(inet3, 5555); 72 73 assertEquals(x1, x2); 74 assertEquals(x3, x1); 75 76 HashSet s=new HashSet (); 77 s.add(x1); 78 s.add(x2); 79 s.add(x3); 80 System.out.println("s=" + s); 81 assertEquals(1, s.size()); 82 83 HashMap m=new HashMap (); 84 m.put(x1, "Bela"); 85 m.put(x2, "Michelle"); 86 m.put(x3, "Nicole"); 87 assertEquals(1, m.size()); 88 assertEquals("Nicole", m.get(x1)); 89 } 90 91 92 public void testInequality() throws Exception { 93 IpAddress tmp=null; 94 assertTrue(!a.equals(d)); 95 assertTrue(!c.equals(d)); 96 assertTrue(!a.equals(f)); 97 assertTrue(!e.equals(f)); 98 assertTrue(!f.equals(g)); 99 assertFalse(a.equals(tmp)); 100 } 101 102 103 public void testSameHost() throws Exception { 104 assertTrue(Util.sameHost(a, b)); 105 assertTrue(Util.sameHost(a, c)); 106 assertTrue(Util.sameHost(a, d)); 107 assertTrue(Util.sameHost(a, e)); 108 assertTrue(Util.sameHost(f, g)); 109 } 110 111 112 public void testNotSameHost() throws Exception { 113 assertTrue(!Util.sameHost(a, f)); 114 assertTrue(!Util.sameHost(e, f)); 115 assertTrue(!Util.sameHost(e, null)); 116 assertTrue(!Util.sameHost(null, null)); 117 } 118 119 public void testMcast() { 120 assertTrue(h.isMulticastAddress()); 121 assertTrue(!a.isMulticastAddress()); 122 assertTrue(!e.isMulticastAddress()); 123 assertTrue(!g.isMulticastAddress()); 124 125 } 126 127 128 public void testCompareTo() { 129 assertEquals(0, a.compareTo(b)); 130 assertTrue(a.compareTo(d) < 0); 131 assertTrue(d.compareTo(a) > 0); 132 } 133 134 135 public void testCompareTime() { 136 final int NUM=1000000; 137 _testCompareTime(a, a, NUM); 138 _testCompareTime(a, b, NUM); 139 _testCompareTime(a, c, NUM); 140 _testCompareTime(a, d, NUM); 141 } 142 143 144 private void _testCompareTime(IpAddress one, IpAddress two, int num) { 145 int rc=-99; 146 long start=System.currentTimeMillis(), stop; 147 for(int x=0; x < num; x++) { 148 rc=one.compareTo(two); 149 } 150 stop=System.currentTimeMillis(); 151 long diff=stop-start; 152 System.out.println("calling compareTo(" + one + ", " + two + ") " + num + " times took " + 153 diff + "ms, result=" + rc); 154 } 155 156 157 public void testHashcode() { 158 int hcode_a=a.hashCode(); 159 int hcode_b=b.hashCode(); 160 assertEquals(hcode_a, hcode_b); 161 } 162 163 164 public void testHashcodeTime() { 165 int hash=-1; 166 final int NUM=10000000; 167 168 long start=System.currentTimeMillis(), stop; 169 for(int x=0; x < NUM; x++) { 170 hash=a.hashCode(); 171 } 172 stop=System.currentTimeMillis(); 173 long diff=stop-start; 174 System.out.println("taking the hash code of " + a + "(" + hash + ") took " + diff + "ms"); 175 } 176 177 178 public void testIPv6WithExternalization() throws IOException, ClassNotFoundException { 179 InetAddress tmp=Util.getFirstNonLoopbackIPv6Address(); 180 IpAddress ip=new IpAddress(tmp, 5555); 181 182 ByteArrayOutputStream bos=new ByteArrayOutputStream(); 183 ObjectOutputStream oos=new ObjectOutputStream(bos); 184 byte[] buf=null; 185 ByteArrayInputStream bis=null; 186 ObjectInputStream ois; 187 188 System.out.println("-- address is " + tmp); 189 190 oos.writeObject(ip); 191 buf=bos.toByteArray(); 192 bis=new ByteArrayInputStream(buf); 193 ois=new ObjectInputStream(bis); 194 IpAddress ip2=(IpAddress)ois.readObject(); 195 assertEquals(ip, ip2); 196 } 197 198 199 200 public void testIPv6WithStreamable() throws IOException, ClassNotFoundException { 201 InetAddress tmp=Util.getFirstNonLoopbackIPv6Address(); 202 IpAddress ip=new IpAddress(tmp, 5555); 203 204 ByteArrayOutputStream bos=new ByteArrayOutputStream(); 205 DataOutputStream dos=new DataOutputStream(bos); 206 byte[] buf=null; 207 ByteArrayInputStream bis=null; 208 DataInputStream dis; 209 210 System.out.println("-- address is " + tmp); 211 212 ip.writeTo(dos); 213 buf=bos.toByteArray(); 214 bis=new ByteArrayInputStream(buf); 215 dis=new DataInputStream(bis); 216 IpAddress ip2=new IpAddress(); 217 ip2.readFrom(dis); 218 assertEquals(ip, ip2); 219 } 220 221 public void testExternalization() throws Exception { 222 ByteArrayOutputStream bos=new ByteArrayOutputStream(); 223 ObjectOutputStream oos=new ObjectOutputStream(bos); 224 byte[] buf=null; 225 ByteArrayInputStream bis=null; 226 ObjectInputStream ois; 227 IpAddress a2, b2; 228 229 a.setAdditionalData(null); 230 b.setAdditionalData("Bela Ban".getBytes()); 231 oos.writeObject(a); 232 oos.writeObject(b); 233 234 235 buf=bos.toByteArray(); 236 bis=new ByteArrayInputStream(buf); 237 ois=new ObjectInputStream(bis); 238 a2=(IpAddress)ois.readObject(); 239 b2=(IpAddress)ois.readObject(); 240 241 assertEquals(a, a2); 242 assertEquals(b, b2); 243 244 assertTrue(a2.getAdditionalData() == null); 245 assertEquals("Bela Ban", new String (b2.getAdditionalData())); 246 } 247 248 249 250 public void testExternalizationAdditionalData() throws Exception { 251 ByteArrayOutputStream bos=new ByteArrayOutputStream(); 252 ObjectOutputStream oos=new ObjectOutputStream(bos); 253 byte[] buf=null; 254 ByteArrayInputStream bis=null; 255 ObjectInputStream ois; 256 IpAddress a2, b2, c2, d2, e2, f2, g2, h2; 257 258 oos.writeObject(a); 259 oos.writeObject(b); 260 oos.writeObject(c); 261 oos.writeObject(d); 262 oos.writeObject(e); 263 oos.writeObject(f); 264 oos.writeObject(g); 265 oos.writeObject(h); 266 267 268 buf=bos.toByteArray(); 269 bis=new ByteArrayInputStream(buf); 270 ois=new ObjectInputStream(bis); 271 a2=(IpAddress)ois.readObject(); 272 b2=(IpAddress)ois.readObject(); 273 c2=(IpAddress)ois.readObject(); 274 d2=(IpAddress)ois.readObject(); 275 e2=(IpAddress)ois.readObject(); 276 f2=(IpAddress)ois.readObject(); 277 g2=(IpAddress)ois.readObject(); 278 h2=(IpAddress)ois.readObject(); 279 280 assertEquals(b2, c2); 281 assertEquals(a, a2); 282 assertEquals(b, b2); 283 assertEquals(c, c2); 284 assertEquals(d, d2); 285 assertEquals(e, e2); 286 assertEquals(f, f2); 287 assertEquals(g, g2); 288 assertEquals(h, h2); 289 } 290 291 292 public void testStreamable() throws Exception { 293 ByteArrayOutputStream bos=new ByteArrayOutputStream(); 294 DataOutputStream oos=new DataOutputStream(bos); 295 byte[] buf=null; 296 ByteArrayInputStream bis=null; 297 DataInputStream ois; 298 IpAddress a2, b2, x, x2, y, y2; 299 300 x=new IpAddress(5555); 301 x.setAdditionalData(new byte[]{'b','e','l','a'}); 302 303 y=new IpAddress(); 304 y.setAdditionalData(new byte[]{'b','e','l','a'}); 305 306 a.setAdditionalData(null); 307 b.setAdditionalData("Bela Ban".getBytes()); 308 a.writeTo(oos); 309 b.writeTo(oos); 310 x.writeTo(oos); 311 y.writeTo(oos); 312 313 buf=bos.toByteArray(); 314 bis=new ByteArrayInputStream(buf); 315 ois=new DataInputStream(bis); 316 a2=new IpAddress(); 317 a2.readFrom(ois); 318 b2=new IpAddress(); 319 b2.readFrom(ois); 320 x2=new IpAddress(); 321 x2.readFrom(ois); 322 y2=new IpAddress(); 323 y2.readFrom(ois); 324 325 assertEquals(a, a2); 326 assertEquals(b, b2); 327 328 assertNull(a2.getAdditionalData()); 329 assertEquals("Bela Ban", new String (b2.getAdditionalData())); 330 331 assertNotNull(x2.getAdditionalData()); 332 assertEquals(4, x2.getAdditionalData().length); 333 334 assertNull(y2.getIpAddress()); 335 assertEquals(0, y2.getPort()); 336 assertNotNull(y2.getAdditionalData()); 337 assertEquals(4, y2.getAdditionalData().length); 338 } 339 340 341 342 public void testStreamableWithHighPort() throws Exception { 343 ByteArrayOutputStream bos=new ByteArrayOutputStream(); 344 DataOutputStream oos=new DataOutputStream(bos); 345 byte[] buf=null; 346 ByteArrayInputStream bis=null; 347 DataInputStream dis; 348 IpAddress x, x2; 349 350 x=new IpAddress(65535); 351 x.writeTo(oos); 352 353 buf=bos.toByteArray(); 354 bis=new ByteArrayInputStream(buf); 355 dis=new DataInputStream(bis); 356 357 x2=new IpAddress(); 358 x2.readFrom(dis); 359 System.out.println("x: " + x + ", x2: " + x2); 360 361 assertTrue(x2.getPort() > 0); 362 assertEquals(x.getPort(), x2.getPort()); 363 } 364 365 366 367 public void testStreamableAdditionalData() throws Exception { 368 ByteArrayOutputStream bos=new ByteArrayOutputStream(); 369 DataOutputStream oos=new DataOutputStream(bos); 370 byte[] buf=null; 371 ByteArrayInputStream bis=null; 372 DataInputStream ois; 373 IpAddress a2, b2, c2, d2, e2, f2, g2, h2; 374 375 a.writeTo(oos); 376 b.writeTo(oos); 377 c.writeTo(oos); 378 d.writeTo(oos); 379 e.writeTo(oos); 380 f.writeTo(oos); 381 g.writeTo(oos); 382 h.writeTo(oos); 383 384 385 buf=bos.toByteArray(); 386 bis=new ByteArrayInputStream(buf); 387 ois=new DataInputStream(bis); 388 a2=new IpAddress(); 389 a2.readFrom(ois); 390 b2=new IpAddress(); 391 b2.readFrom(ois); 392 c2=new IpAddress(); 393 c2.readFrom(ois); 394 d2=new IpAddress(); 395 d2.readFrom(ois); 396 e2=new IpAddress(); 397 e2.readFrom(ois); 398 f2=new IpAddress(); 399 f2.readFrom(ois); 400 g2=new IpAddress(); 401 g2.readFrom(ois); 402 h2=new IpAddress(); 403 h2.readFrom(ois); 404 405 assertEquals(b2, c2); 406 assertEquals(a, a2); 407 assertEquals(b, b2); 408 assertEquals(c, c2); 409 assertEquals(d, d2); 410 assertEquals(e, e2); 411 assertEquals(f, f2); 412 assertEquals(g, g2); 413 assertEquals(h, h2); 414 } 415 416 417 418 public static Test suite() { 419 return new TestSuite(IpAddressTest.class); 420 } 421 422 public static void main(String [] args) { 423 junit.textui.TestRunner.run(suite()); 424 } 425 } 426 | Popular Tags |