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.hc.core5.net;
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.URISyntaxException;
35  
36  import org.junit.Assert;
37  import org.junit.Test;
38  
39  /**
40   * Unit tests for {@link Host}.
41   *
42   */
43  public class TestHost {
44  
45      @Test
46      public void testConstructor() {
47          final Host host1 = new Host("somehost", 8080);
48          Assert.assertEquals("somehost", host1.getHostName());
49          Assert.assertEquals(8080, host1.getPort());
50          final Host host2 = new Host("somehost", 0);
51          Assert.assertEquals("somehost", host2.getHostName());
52          Assert.assertEquals(0, host2.getPort());
53          try {
54              new Host(null, 0);
55              Assert.fail("NullPointerException should have been thrown");
56          } catch (final NullPointerException expected) {
57          }
58      }
59  
60      @Test
61      public void testHashCode() throws Exception {
62          final Host host1 = new Host("somehost", 8080);
63          final Host host2 = new Host("somehost", 80);
64          final Host host3 = new Host("someotherhost", 8080);
65          final Host host4 = new Host("somehost", 80);
66  
67          Assert.assertTrue(host1.hashCode() == host1.hashCode());
68          Assert.assertTrue(host1.hashCode() != host2.hashCode());
69          Assert.assertTrue(host1.hashCode() != host3.hashCode());
70          Assert.assertTrue(host2.hashCode() == host4.hashCode());
71      }
72  
73      @Test
74      public void testEquals() throws Exception {
75          final Host host1 = new Host("somehost", 8080);
76          final Host host2 = new Host("somehost", 80);
77          final Host host3 = new Host("someotherhost", 8080);
78          final Host host4 = new Host("somehost", 80);
79  
80          Assert.assertTrue(host1.equals(host1));
81          Assert.assertFalse(host1.equals(host2));
82          Assert.assertFalse(host1.equals(host3));
83          Assert.assertTrue(host2.equals(host4));
84      }
85  
86      @Test
87      public void testToString() throws Exception {
88          final Host host1 = new Host("somehost", 8888);
89          Assert.assertEquals("somehost:8888", host1.toString());
90      }
91  
92      @Test
93      public void testSerialization() throws Exception {
94          final Host orig = new Host("somehost", 8080);
95          final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
96          final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
97          outStream.writeObject(orig);
98          outStream.close();
99          final byte[] raw = outbuffer.toByteArray();
100         final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
101         final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
102         final Host clone = (Host) inStream.readObject();
103         Assert.assertEquals(orig, clone);
104     }
105 
106     @Test
107     public void testCreateFromString() throws Exception {
108         Assert.assertEquals(new Host("somehost", 8080), Host.create("somehost:8080"));
109         Assert.assertEquals(new Host("somehost", 1234), Host.create("somehost:1234"));
110         Assert.assertEquals(new Host("somehost", 0), Host.create("somehost:0"));
111     }
112 
113     @Test
114     public void testCreateFromStringInvalid() throws Exception {
115         try {
116             Host.create(" host ");
117             Assert.fail("URISyntaxException expected");
118         } catch (final URISyntaxException expected) {
119         }
120         try {
121             Host.create("host :8080");
122             Assert.fail("URISyntaxException expected");
123         } catch (final URISyntaxException expected) {
124         }
125         try {
126             Host.create("");
127             Assert.fail("IllegalArgumentException expected");
128         } catch (final IllegalArgumentException expected) {
129         }
130     }
131 
132     @Test
133     public void testIpv6HostAndPort() throws Exception {
134         final Host host = Host.create("[::1]:80");
135         Assert.assertEquals("::1", host.getHostName());
136         Assert.assertEquals(80, host.getPort());
137     }
138 
139     @Test
140     public void testIpv6HostAndPortWithoutBrackets() {
141         try {
142             // ambiguous
143             Host.create("::1:80");
144             Assert.fail("URISyntaxException expected");
145         } catch (final URISyntaxException expected) {
146             Assert.assertTrue(expected.getMessage().contains("Expected IPv6 address to be enclosed in brackets"));
147         }
148     }
149 
150     @Test
151     public void testIpv6HostWithoutPort() {
152         try {
153             Host.create("::1");
154             Assert.fail("URISyntaxException expected");
155         } catch (final URISyntaxException expected) {
156             Assert.assertTrue(expected.getMessage().contains("Expected IPv6 address to be enclosed in brackets"));
157         }
158     }
159 
160     @Test
161     public void testIpv6HostToString() {
162         Assert.assertEquals("[::1]:80", new Host("::1", 80).toString());
163         Assert.assertEquals("[::1]", new Host("::1", -1).toString());
164     }
165 }