You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2014/08/09 14:59:41 UTC

svn commit: r1616939 - in /httpcomponents/httpcore/trunk/httpcore/src: main/java/org/apache/http/HttpHost.java test/java/org/apache/http/TestHttpHost.java

Author: olegk
Date: Sat Aug  9 12:59:41 2014
New Revision: 1616939

URL: http://svn.apache.org/r1616939
Log:
HttpHost constructor with arbitrary hostname and inet address
Contributed by  <peter at rimuhosting.com>

https://github.com/apache/httpcore/pull/1

Modified:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpHost.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/TestHttpHost.java

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpHost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpHost.java?rev=1616939&r1=1616938&r2=1616939&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpHost.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/HttpHost.java Sat Aug  9 12:59:41 2014
@@ -148,9 +148,26 @@ public final class HttpHost implements C
      * @since 4.3
      */
     public HttpHost(final InetAddress address, final int port, final String scheme) {
+        this(Args.notNull(address,"Inet address"), address.getHostName(), port, scheme);
+    }
+    /**
+     * Creates a new {@link HttpHost HttpHost}, specifying all values.
+     * Constructor for HttpHost.
+     *
+     * @param address   the inet address.
+     * @param hostname   the hostname (IP or DNS name)
+     * @param port      the port number.
+     *                  <code>-1</code> indicates the scheme default port.
+     * @param scheme    the name of the scheme.
+     *                  <code>null</code> indicates the
+     *                  {@link #DEFAULT_SCHEME_NAME default scheme}
+     *
+     * @since 4.4
+     */
+    public HttpHost(final InetAddress address, final String hostname, final int port, final String scheme) {
         super();
         this.address = Args.notNull(address, "Inet address");
-        this.hostname = address.getHostAddress();
+        this.hostname = Args.notNull(hostname, "Hostname");
         this.lcHostname = this.hostname.toLowerCase(Locale.ROOT);
         if (scheme != null) {
             this.schemeName = scheme.toLowerCase(Locale.ROOT);
@@ -291,7 +308,8 @@ public final class HttpHost implements C
             final HttpHost that = (HttpHost) obj;
             return this.lcHostname.equals(that.lcHostname)
                 && this.port == that.port
-                && this.schemeName.equals(that.schemeName);
+                && this.schemeName.equals(that.schemeName)
+                && (this.address==null ? that.address== null : this.address.equals(that.address));
         } else {
             return false;
         }
@@ -306,6 +324,9 @@ public final class HttpHost implements C
         hash = LangUtils.hashCode(hash, this.lcHostname);
         hash = LangUtils.hashCode(hash, this.port);
         hash = LangUtils.hashCode(hash, this.schemeName);
+        if (address!=null) {
+            hash = LangUtils.hashCode(hash, address);
+        }
         return hash;
     }
 

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/TestHttpHost.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/TestHttpHost.java?rev=1616939&r1=1616938&r2=1616939&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/TestHttpHost.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/TestHttpHost.java Sat Aug  9 12:59:41 2014
@@ -86,8 +86,14 @@ public class TestHttpHost {
         final HttpHost host5 = new HttpHost("SomeHost", 80, "http");
         final HttpHost host6 = new HttpHost("SomeHost", 80, "myhttp");
         final HttpHost host7 = new HttpHost(
-                InetAddress.getByAddress(new byte[] {127,0,0,1}), 80, "http");
+                InetAddress.getByAddress("127.0.0.1", new byte[] {127,0,0,1}), 80, "http");
         final HttpHost host8 = new HttpHost("127.0.0.1", 80, "http");
+        final HttpHost host9 = new HttpHost(
+                        InetAddress.getByAddress("somehost",new byte[] {127,0,0,1}), 80, "http");
+        final HttpHost host10 = new HttpHost(
+                        InetAddress.getByAddress(new byte[] {127,0,0,1}), "somehost", 80, "http");
+        final HttpHost host11 = new HttpHost(
+                        InetAddress.getByAddress("someotherhost",new byte[] {127,0,0,1}), 80, "http");
 
         Assert.assertTrue(host1.hashCode() == host1.hashCode());
         Assert.assertTrue(host1.hashCode() != host2.hashCode());
@@ -95,7 +101,11 @@ public class TestHttpHost {
         Assert.assertTrue(host2.hashCode() == host4.hashCode());
         Assert.assertTrue(host2.hashCode() == host5.hashCode());
         Assert.assertTrue(host5.hashCode() != host6.hashCode());
-        Assert.assertTrue(host7.hashCode() == host8.hashCode());
+        Assert.assertTrue(host7.hashCode() != host8.hashCode());
+        Assert.assertTrue(host8.hashCode() != host9.hashCode());
+        Assert.assertTrue(host9.hashCode() == host10.hashCode());
+        Assert.assertTrue(host10.hashCode() != host11.hashCode());
+        Assert.assertTrue(host9.hashCode() != host11.hashCode());
     }
 
     @Test
@@ -107,8 +117,14 @@ public class TestHttpHost {
         final HttpHost host5 = new HttpHost("SomeHost", 80, "http");
         final HttpHost host6 = new HttpHost("SomeHost", 80, "myhttp");
         final HttpHost host7 = new HttpHost(
-                InetAddress.getByAddress(new byte[] {127,0,0,1}), 80, "http");
+                InetAddress.getByAddress("127.0.0.1", new byte[] {127,0,0,1}), 80, "http");
         final HttpHost host8 = new HttpHost("127.0.0.1", 80, "http");
+        final HttpHost host9 = new HttpHost(
+                        InetAddress.getByAddress("somehost", new byte[] {127,0,0,1}), 80, "http");
+        final HttpHost host10 = new HttpHost(
+                        InetAddress.getByAddress(new byte[] {127,0,0,1}), "somehost", 80, "http");
+        final HttpHost host11 = new HttpHost(
+                        InetAddress.getByAddress("someotherhost",new byte[] {127,0,0,1}), 80, "http");
 
         Assert.assertTrue(host1.equals(host1));
         Assert.assertFalse(host1.equals(host2));
@@ -116,9 +132,14 @@ public class TestHttpHost {
         Assert.assertTrue(host2.equals(host4));
         Assert.assertTrue(host2.equals(host5));
         Assert.assertFalse(host5.equals(host6));
-        Assert.assertTrue(host7.equals(host8));
+        Assert.assertFalse(host7.equals(host8));
+        Assert.assertTrue(!host7.equals(host9));
         Assert.assertFalse(host1.equals(null));
         Assert.assertFalse(host1.equals("http://somehost"));
+        Assert.assertFalse(host9.equals("http://somehost"));
+        Assert.assertFalse(host8.equals(host9));
+        Assert.assertTrue(host9.equals(host10));
+        Assert.assertFalse(host9.equals(host11));
     }
 
     @Test
@@ -136,8 +157,11 @@ public class TestHttpHost {
         final HttpHost host6 = new HttpHost("somehost", 80, "myhttp");
         Assert.assertEquals("myhttp://somehost:80", host6.toString());
         final HttpHost host7 = new HttpHost(
-                InetAddress.getByAddress(new byte[] {127,0,0,1}), 80, "http");
+                InetAddress.getByAddress("127.0.0.1", new byte[] {127,0,0,1}), 80, "http");
         Assert.assertEquals("http://127.0.0.1:80", host7.toString());
+        final HttpHost host9 = new HttpHost(
+                        InetAddress.getByAddress("somehost", new byte[] {127,0,0,1}), 80, "http");
+        Assert.assertEquals("http://somehost:80", host9.toString());
     }
 
     @Test