1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package org.apache.harmony.luni.tests.java.net; 17 18import java.io.Serializable; 19import java.net.InetSocketAddress; 20 21import junit.framework.TestCase; 22 23import org.apache.harmony.testframework.serialization.SerializationTest; 24import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; 25 26public class InetSocketAddressTest extends TestCase { 27 28 /** 29 * @tests java.net.InetSocketAddress#InetSocketAddress(String, int) 30 */ 31 public void test_ConstructorLjava_lang_StringI() throws Exception { 32 // regression test for Harmony-1042 33 InetSocketAddress address = new InetSocketAddress("127.0.0.1", 0); 34 assertEquals("/127.0.0.1:0", address.toString()); 35 String localhostName = address.getHostName(); 36 assertNotNull(localhostName); 37 assertEquals(localhostName+"/127.0.0.1:0", address.toString()); 38 } 39 40 /** 41 * @tests java.net.InetSocketAddress#createUnresolved(String, int) 42 */ 43 public void test_createUnresolvedLjava_lang_StringI() { 44 HostPortPair[] legalHostPortPairs = { new HostPortPair("127.0.0.1", 1234), 45 new HostPortPair("192.168.0.1", 10000), new HostPortPair("127.0.0", 0), 46 new HostPortPair("127.0.0", 65535), 47 new HostPortPair("strange host", 65535) }; 48 for (int i = 0; i < legalHostPortPairs.length; i++) { 49 InetSocketAddress isa = InetSocketAddress.createUnresolved( 50 legalHostPortPairs[i].host, legalHostPortPairs[i].port); 51 assertTrue(isa.isUnresolved()); 52 assertNull(isa.getAddress()); 53 assertEquals(isa.getHostName(), legalHostPortPairs[i].host); 54 assertEquals(isa.getPort(), legalHostPortPairs[i].port); 55 } 56 } 57 58 /** 59 * @tests java.net.InetSocketAddress#createUnresolved(String, int) 60 */ 61 public void test_createUnresolvedLjava_lang_StringI_IllegalArgumentException() { 62 HostPortPair[] illegalHostPortPairs = { new HostPortPair(null, 1), 63 new HostPortPair("host", -1), new HostPortPair("host", 65536) }; 64 for (int i = 0; i < illegalHostPortPairs.length; i++) { 65 try { 66 InetSocketAddress.createUnresolved( 67 illegalHostPortPairs[i].host, 68 illegalHostPortPairs[i].port); 69 fail("should throw IllegalArgumentException, host = " 70 + illegalHostPortPairs[i].host + ",port = " 71 + illegalHostPortPairs[i].port); 72 } catch (IllegalArgumentException e) { 73 // expected 74 } 75 } 76 } 77 78 /* 79 * inner class for createUnresolved test convenience. 80 */ 81 class HostPortPair { 82 String host; 83 84 int port; 85 86 public HostPortPair(String host, int port) { 87 this.host = host; 88 this.port = port; 89 } 90 }; 91 92 // comparator for InetSocketAddress objects 93 private static final SerializableAssert COMPARATOR = new SerializableAssert() { 94 public void assertDeserialized(Serializable initial, 95 Serializable deserialized) { 96 97 InetSocketAddress init = (InetSocketAddress) initial; 98 InetSocketAddress desr = (InetSocketAddress) deserialized; 99 100 assertEquals("HostName", init.getHostName(), desr.getHostName()); 101 assertEquals("Port", init.getPort(), desr.getPort()); 102 assertEquals("Address", init.getAddress(), desr.getAddress()); 103 } 104 }; 105 106 /** 107 * @tests serialization/deserialization compatibility. 108 */ 109 public void testSerializationSelf() throws Exception { 110 111 Object[] testCases = { 112 InetSocketAddress.createUnresolved("badhost", 1000), // unresolved 113 new InetSocketAddress("Localhost", 1000) }; 114 115 SerializationTest.verifySelf(testCases, COMPARATOR); 116 } 117 118 /** 119 * @tests serialization/deserialization compatibility with RI. 120 */ 121 public void testSerializationCompatibility() throws Exception { 122 123 Object[] testCases = { 124 InetSocketAddress.createUnresolved("badhost", 1000), // unresolved 125 new InetSocketAddress("Localhost", 1000) }; 126 127 SerializationTest.verifyGolden(this, testCases, COMPARATOR); 128 } 129} 130