001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.harmony.luni.tests.java.net;
017:
018: import java.io.Serializable;
019: import java.net.InetSocketAddress;
020:
021: import junit.framework.TestCase;
022:
023: import org.apache.harmony.testframework.serialization.SerializationTest;
024: import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
025:
026: public class InetSocketAddressTest extends TestCase {
027:
028: /**
029: * @tests java.net.InetSocketAddress#InetSocketAddress(String, int)
030: */
031: public void test_ConstructorLjava_lang_StringI() throws Exception {
032: // regression test for Harmony-1042
033: InetSocketAddress address = new InetSocketAddress("127.0.0.1",
034: 0);
035: assertNotNull(address.getHostName());
036: }
037:
038: /**
039: * @tests java.net.InetSocketAddress#createUnresolved(String, int)
040: */
041: public void test_createUnresolvedLjava_lang_StringI() {
042: HostPortPair[] legalHostPortPairs = {
043: new HostPortPair("127.0.0.1", 1234),
044: new HostPortPair("192.168.0.1", 10000),
045: new HostPortPair("127.0.0", 0),
046: new HostPortPair("127.0.0", 65535),
047: new HostPortPair("strange host", 65535) };
048: for (int i = 0; i < legalHostPortPairs.length; i++) {
049: InetSocketAddress isa = InetSocketAddress.createUnresolved(
050: legalHostPortPairs[i].host,
051: legalHostPortPairs[i].port);
052: assertTrue(isa.isUnresolved());
053: assertNull(isa.getAddress());
054: assertEquals(isa.getHostName(), legalHostPortPairs[i].host);
055: assertEquals(isa.getPort(), legalHostPortPairs[i].port);
056: }
057: }
058:
059: /**
060: * @tests java.net.InetSocketAddress#createUnresolved(String, int)
061: */
062: public void test_createUnresolvedLjava_lang_StringI_IllegalArgumentException() {
063: HostPortPair[] illegalHostPortPairs = {
064: new HostPortPair(null, 1),
065: new HostPortPair("host", -1),
066: new HostPortPair("host", 65536) };
067: for (int i = 0; i < illegalHostPortPairs.length; i++) {
068: try {
069: InetSocketAddress.createUnresolved(
070: illegalHostPortPairs[i].host,
071: illegalHostPortPairs[i].port);
072: fail("should throw IllegalArgumentException, host = "
073: + illegalHostPortPairs[i].host + ",port = "
074: + illegalHostPortPairs[i].port);
075: } catch (IllegalArgumentException e) {
076: // expected
077: }
078: }
079: }
080:
081: /*
082: * inner class for createUnresolved test convenience.
083: */
084: class HostPortPair {
085: String host;
086:
087: int port;
088:
089: public HostPortPair(String host, int port) {
090: this .host = host;
091: this .port = port;
092: }
093: };
094:
095: // comparator for InetSocketAddress objects
096: private static final SerializableAssert COMPARATOR = new SerializableAssert() {
097: public void assertDeserialized(Serializable initial,
098: Serializable deserialized) {
099:
100: InetSocketAddress init = (InetSocketAddress) initial;
101: InetSocketAddress desr = (InetSocketAddress) deserialized;
102:
103: assertEquals("HostName", init.getHostName(), desr
104: .getHostName());
105: assertEquals("Port", init.getPort(), desr.getPort());
106: assertEquals("Address", init.getAddress(), desr
107: .getAddress());
108: }
109: };
110:
111: /**
112: * @tests serialization/deserialization compatibility.
113: */
114: public void testSerializationSelf() throws Exception {
115:
116: Object[] testCases = {
117: InetSocketAddress.createUnresolved("badhost", 1000), // unresolved
118: new InetSocketAddress("Localhost", 1000) };
119:
120: SerializationTest.verifySelf(testCases, COMPARATOR);
121: }
122:
123: /**
124: * @tests serialization/deserialization compatibility with RI.
125: */
126: public void testSerializationCompatibility() throws Exception {
127:
128: Object[] testCases = {
129: InetSocketAddress.createUnresolved("badhost", 1000), // unresolved
130: new InetSocketAddress("Localhost", 1000) };
131:
132: SerializationTest.verifyGolden(this, testCases, COMPARATOR);
133: }
134: }
|