You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/06/12 23:02:14 UTC

svn commit: r413745 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/net/ test/java/tests/api/java/net/ test/resources/serialization/java/net/

Author: tellison
Date: Mon Jun 12 14:02:14 2006
New Revision: 413745

URL: http://svn.apache.org/viewvc?rev=413745&view=rev
Log:
Apply patch HARMONY-568 (Bug in java.net.InetSocketAddress serialization)

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/net/InetSocketAddress.golden.1.ser   (with props)
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/net/InetSocketAddress.golden.2.ser   (with props)
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetSocketAddress.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/InetSocketAddressTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetSocketAddress.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetSocketAddress.java?rev=413745&r1=413744&r2=413745&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetSocketAddress.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetSocketAddress.java Mon Jun 12 14:02:14 2006
@@ -1,4 +1,4 @@
-/* Copyright 2004, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 2004, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,7 +23,7 @@
 
 	private static final long serialVersionUID = 5076001401234631237L;
 
-	private String hostName;
+	private String hostname;
 
 	private InetAddress addr;
 
@@ -40,7 +40,7 @@
 			addr = InetAddress.ANY;
 		else
 			addr = address;
-		hostName = addr.getHostName();
+		hostname = addr.getHostName();
 		this.port = port;
 	}
 
@@ -55,12 +55,12 @@
 	InetSocketAddress(String host, int port, boolean needResolved){
 		if (host == null || port < 0 || port > 65535)
 			throw new IllegalArgumentException();
-		hostName = host;
+		hostname = host;
 		this.port = port;
 		if(needResolved){
 			try {
-				addr = InetAddress.getByName(hostName);
-				hostName = addr.getHostName();
+				addr = InetAddress.getByName(hostname);
+				hostname = null;
 			} catch (UnknownHostException e) {
 			}
 		}else{
@@ -90,8 +90,8 @@
 	}
 
 	public final String getHostName() {
-		return hostName;
-	}
+        return (null != addr) ? addr.hostName : hostname;
+    }
 
 	public final boolean isUnresolved() {
 		return addr == null;
@@ -99,10 +99,11 @@
 
 	public String toString() {
 		String host;
-		if (addr != null)
-			host = addr.toString();
-		else
-			host = hostName;
+		if (addr != null) {
+            host = addr.toString();
+        } else {
+            host = hostname;
+        }
 		return host + ":" + port; //$NON-NLS-1$
 	}
 
@@ -121,7 +122,7 @@
 		// we only use the hostnames in the comparison if the addrs were not
 		// resolved
 		if ((addr == null) && (iSockAddr.addr == null)) {
-			return hostName.equals(iSockAddr.hostName);
+			return hostname.equals(iSockAddr.hostname);
 		} else {
 			// addrs were resolved so use them for the comparison
 			if (addr == null) {
@@ -134,20 +135,15 @@
 	}
 
 	public final int hashCode() {
-		if (addr == null)
-			return hostName.hashCode() + port;
+		if (addr == null) {
+            return hostname.hashCode() + port;
+        }
 		return addr.hashCode() + port;
 	}
 
 	private void readObject(ObjectInputStream stream) throws IOException,
 			ClassNotFoundException {
-		stream.defaultReadObject();
-		if (addr == null) {
-			try {
-				addr = InetAddress.getByName(hostName);
-			} catch (UnknownHostException e) {
-			}
-		}
+        stream.defaultReadObject();
 	}
 
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/InetSocketAddressTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/InetSocketAddressTest.java?rev=413745&r1=413744&r2=413745&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/InetSocketAddressTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/InetSocketAddressTest.java Mon Jun 12 14:02:14 2006
@@ -16,11 +16,17 @@
 
 import java.net.InetSocketAddress;
 
+import tests.util.SerializationTester;
+
 import junit.framework.TestCase;
 
 public class InetSocketAddressTest extends TestCase {
 
-	/**
+	private static final String SERIALIZATION_FILE_NAME = "serialization/java/net/InetSocketAddress.golden.1.ser";
+    
+    private static final String SERIALIZATION_FILE_NAME_UNRESOLVED = "serialization/java/net/InetSocketAddress.golden.2.ser";
+
+    /**
 	 * @tests java.net.InetSocketAddress#createUnresolved(String, int)
 	 */
 	public void test_createUnresolvedLjava_lang_StringI() {
@@ -71,4 +77,54 @@
 			this.port = port;
 		}
 	};
+    
+    /**
+     * @tests serialization/deserialization.
+     */
+    public void test_serialization_unresolved() throws Exception {
+        InetSocketAddress ia= InetSocketAddress.createUnresolved("badhost",1000);
+        InetSocketAddress deIA = (InetSocketAddress) SerializationTester
+                .getDeserilizedObject(ia);
+        assertEquals(ia.getHostName(),deIA.getHostName());
+        assertEquals(ia.getPort(), deIA.getPort());
+        assertEquals(ia.getAddress(), deIA.getAddress());
+    }
+    
+    /**
+     * @tests serialization/deserialization.
+     */
+    public void test_serialization() throws Exception {
+        InetSocketAddress ia= InetSocketAddress.createUnresolved("badhost",1000);
+        InetSocketAddress deIA = (InetSocketAddress) SerializationTester
+                .getDeserilizedObject(ia);
+        assertEquals(ia.getHostName(),deIA.getHostName());
+        assertEquals(ia.getPort(), deIA.getPort());
+        assertEquals(ia.getAddress(), deIA.getAddress());
+    }
+
+    /**
+     * @tests serialization/deserialization.
+     */
+    public void test_serializationCompatibility_unresolved() throws Exception {
+        InetSocketAddress ia= InetSocketAddress.createUnresolved("badhost",1000);
+        InetSocketAddress deIA = (InetSocketAddress) SerializationTester
+                .readObject(ia,
+                        SERIALIZATION_FILE_NAME_UNRESOLVED);
+        assertEquals(ia.getHostName(),deIA.getHostName());
+        assertEquals(ia.getPort(), deIA.getPort());
+        assertEquals(ia.getAddress(), deIA.getAddress());
+    }
+    
+    /**
+     * @tests serialization/deserialization.
+     */
+    public void test_serializationCompatibility() throws Exception {
+        InetSocketAddress ia= new InetSocketAddress("Localhost",1000);
+        InetSocketAddress deIA = (InetSocketAddress) SerializationTester
+                .readObject(ia,
+                        SERIALIZATION_FILE_NAME);
+        assertEquals(ia.getAddress(), deIA.getAddress());
+        assertEquals(ia.getPort(), deIA.getPort());
+        assertEquals(ia.getHostName().toLowerCase(),deIA.getHostName().toLowerCase());
+    }
 }

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/net/InetSocketAddress.golden.1.ser
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/net/InetSocketAddress.golden.1.ser?rev=413745&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/net/InetSocketAddress.golden.1.ser
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/net/InetSocketAddress.golden.2.ser
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/net/InetSocketAddress.golden.2.ser?rev=413745&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/resources/serialization/java/net/InetSocketAddress.golden.2.ser
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream