View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  import java.net.InetAddress;
35  
36  import org.junit.Assert;
37  import org.junit.Test;
38  
39  /**
40   * Unit tests for {@link HttpHost}.
41   *
42   */
43  public class TestHttpHost {
44  
45      @Test
46      public void testConstructor() {
47          final HttpHost host1 = new HttpHost("somehost");
48          Assert.assertEquals("somehost", host1.getHostName());
49          Assert.assertEquals(-1, host1.getPort());
50          Assert.assertEquals("http", host1.getSchemeName());
51          final HttpHost host2 = new HttpHost("somehost", 8080);
52          Assert.assertEquals("somehost", host2.getHostName());
53          Assert.assertEquals(8080, host2.getPort());
54          Assert.assertEquals("http", host2.getSchemeName());
55          final HttpHost host3 = new HttpHost("somehost", -1);
56          Assert.assertEquals("somehost", host3.getHostName());
57          Assert.assertEquals(-1, host3.getPort());
58          Assert.assertEquals("http", host3.getSchemeName());
59          final HttpHost host4 = new HttpHost("somehost", 443, "https");
60          Assert.assertEquals("somehost", host4.getHostName());
61          Assert.assertEquals(443, host4.getPort());
62          Assert.assertEquals("https", host4.getSchemeName());
63          try {
64              new HttpHost((String) null, -1, null);
65              Assert.fail("IllegalArgumentException should have been thrown");
66          } catch (final IllegalArgumentException expected) {
67          }
68          try {
69              new HttpHost("   ", -1, null);
70              Assert.fail("IllegalArgumentException should have been thrown");
71          } catch (final IllegalArgumentException expected) {
72          }
73          try {
74              new HttpHost((InetAddress) null, -1, null);
75              Assert.fail("IllegalArgumentException should have been thrown");
76          } catch (final IllegalArgumentException expected) {
77          }
78      }
79  
80      @Test
81      public void testHashCode() throws Exception {
82          final HttpHost host1 = new HttpHost("somehost", 8080, "http");
83          final HttpHost host2 = new HttpHost("somehost", 80, "http");
84          final HttpHost host3 = new HttpHost("someotherhost", 8080, "http");
85          final HttpHost host4 = new HttpHost("somehost", 80, "http");
86          final HttpHost host5 = new HttpHost("SomeHost", 80, "http");
87          final HttpHost host6 = new HttpHost("SomeHost", 80, "myhttp");
88          final HttpHost host7 = new HttpHost(
89                  InetAddress.getByAddress("127.0.0.1", new byte[] {127,0,0,1}), 80, "http");
90          final HttpHost host8 = new HttpHost("127.0.0.1", 80, "http");
91          final HttpHost host9 = new HttpHost(
92                          InetAddress.getByAddress("somehost",new byte[] {127,0,0,1}), 80, "http");
93          final HttpHost host10 = new HttpHost(
94                          InetAddress.getByAddress(new byte[] {127,0,0,1}), "somehost", 80, "http");
95          final HttpHost host11 = new HttpHost(
96                          InetAddress.getByAddress("someotherhost",new byte[] {127,0,0,1}), 80, "http");
97  
98          Assert.assertTrue(host1.hashCode() == host1.hashCode());
99          Assert.assertTrue(host1.hashCode() != host2.hashCode());
100         Assert.assertTrue(host1.hashCode() != host3.hashCode());
101         Assert.assertTrue(host2.hashCode() == host4.hashCode());
102         Assert.assertTrue(host2.hashCode() == host5.hashCode());
103         Assert.assertTrue(host5.hashCode() != host6.hashCode());
104         Assert.assertTrue(host7.hashCode() != host8.hashCode());
105         Assert.assertTrue(host8.hashCode() != host9.hashCode());
106         Assert.assertTrue(host9.hashCode() == host10.hashCode());
107         Assert.assertTrue(host10.hashCode() != host11.hashCode());
108         Assert.assertTrue(host9.hashCode() != host11.hashCode());
109     }
110 
111     @Test
112     public void testEquals() throws Exception {
113         final HttpHost host1 = new HttpHost("somehost", 8080, "http");
114         final HttpHost host2 = new HttpHost("somehost", 80, "http");
115         final HttpHost host3 = new HttpHost("someotherhost", 8080, "http");
116         final HttpHost host4 = new HttpHost("somehost", 80, "http");
117         final HttpHost host5 = new HttpHost("SomeHost", 80, "http");
118         final HttpHost host6 = new HttpHost("SomeHost", 80, "myhttp");
119         final HttpHost host7 = new HttpHost(
120                 InetAddress.getByAddress("127.0.0.1", new byte[] {127,0,0,1}), 80, "http");
121         final HttpHost host8 = new HttpHost("127.0.0.1", 80, "http");
122         final HttpHost host9 = new HttpHost(
123                         InetAddress.getByAddress("somehost", new byte[] {127,0,0,1}), 80, "http");
124         final HttpHost host10 = new HttpHost(
125                         InetAddress.getByAddress(new byte[] {127,0,0,1}), "somehost", 80, "http");
126         final HttpHost host11 = new HttpHost(
127                         InetAddress.getByAddress("someotherhost",new byte[] {127,0,0,1}), 80, "http");
128 
129         Assert.assertTrue(host1.equals(host1));
130         Assert.assertFalse(host1.equals(host2));
131         Assert.assertFalse(host1.equals(host3));
132         Assert.assertTrue(host2.equals(host4));
133         Assert.assertTrue(host2.equals(host5));
134         Assert.assertFalse(host5.equals(host6));
135         Assert.assertFalse(host7.equals(host8));
136         Assert.assertTrue(!host7.equals(host9));
137         Assert.assertFalse(host1.equals(null));
138         Assert.assertFalse(host1.equals("http://somehost"));
139         Assert.assertFalse(host9.equals("http://somehost"));
140         Assert.assertFalse(host8.equals(host9));
141         Assert.assertTrue(host9.equals(host10));
142         Assert.assertFalse(host9.equals(host11));
143     }
144 
145     @Test
146     public void testToString() throws Exception {
147         final HttpHost host1 = new HttpHost("somehost");
148         Assert.assertEquals("http://somehost", host1.toString());
149         final HttpHost host2 = new HttpHost("somehost", -1);
150         Assert.assertEquals("http://somehost", host2.toString());
151         final HttpHost host3 = new HttpHost("somehost", -1);
152         Assert.assertEquals("http://somehost", host3.toString());
153         final HttpHost host4 = new HttpHost("somehost", 8888);
154         Assert.assertEquals("http://somehost:8888", host4.toString());
155         final HttpHost host5 = new HttpHost("somehost", -1, "myhttp");
156         Assert.assertEquals("myhttp://somehost", host5.toString());
157         final HttpHost host6 = new HttpHost("somehost", 80, "myhttp");
158         Assert.assertEquals("myhttp://somehost:80", host6.toString());
159         final HttpHost host7 = new HttpHost(
160                 InetAddress.getByAddress("127.0.0.1", new byte[] {127,0,0,1}), 80, "http");
161         Assert.assertEquals("http://127.0.0.1:80", host7.toString());
162         final HttpHost host9 = new HttpHost(
163                         InetAddress.getByAddress("somehost", new byte[] {127,0,0,1}), 80, "http");
164         Assert.assertEquals("http://somehost:80", host9.toString());
165     }
166 
167     @Test
168     public void testToHostString() {
169         final HttpHost host1 = new HttpHost("somehost");
170         Assert.assertEquals("somehost", host1.toHostString());
171         final HttpHost host2 = new HttpHost("somehost");
172         Assert.assertEquals("somehost", host2.toHostString());
173         final HttpHost host3 = new HttpHost("somehost", -1);
174         Assert.assertEquals("somehost", host3.toHostString());
175         final HttpHost host4 = new HttpHost("somehost", 8888);
176         Assert.assertEquals("somehost:8888", host4.toHostString());
177     }
178 
179     @Test
180     public void testCloning() throws Exception {
181         final HttpHost orig = new HttpHost("somehost", 8080, "https");
182         final HttpHost clone = (HttpHost) orig.clone();
183         Assert.assertEquals(orig, clone);
184     }
185 
186     @Test
187     public void testSerialization() throws Exception {
188         final HttpHost orig = new HttpHost("somehost", 8080, "https");
189         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
190         final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
191         outStream.writeObject(orig);
192         outStream.close();
193         final byte[] raw = outbuffer.toByteArray();
194         final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
195         final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
196         final HttpHost clone = (HttpHost) inStream.readObject();
197         Assert.assertEquals(orig, clone);
198     }
199 
200     @Test
201     public void testCreateFromString() throws Exception {
202         Assert.assertEquals(new HttpHost("somehost", 8080, "https"), HttpHost.create("https://somehost:8080"));
203         Assert.assertEquals(new HttpHost("somehost", 8080, "https"), HttpHost.create("HttpS://SomeHost:8080"));
204         Assert.assertEquals(new HttpHost("somehost", 1234, null), HttpHost.create("somehost:1234"));
205         Assert.assertEquals(new HttpHost("somehost", -1, null), HttpHost.create("somehost"));
206     }
207 
208     @Test
209     public void testCreateFromStringInvalid() throws Exception {
210         try {
211             HttpHost.create(null);
212             Assert.fail("IllegalArgumentException expected");
213         } catch (final IllegalArgumentException expected) {
214         }
215         try {
216             HttpHost.create(" host ");
217             Assert.fail("IllegalArgumentException expected");
218         } catch (final IllegalArgumentException expected) {
219         }
220         try {
221             HttpHost.create("host :8080");
222             Assert.fail("IllegalArgumentException expected");
223         } catch (final IllegalArgumentException expected) {
224         }
225         try {
226             HttpHost.create("");
227             Assert.fail("IllegalArgumentException expected");
228         } catch (final IllegalArgumentException expected) {
229         }
230     }
231 
232 }