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.jupiter.api.Assertions;
37  import org.junit.jupiter.api.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          Assertions.assertEquals("somehost", host1.getHostName());
49          Assertions.assertEquals(8080, host1.getPort());
50          final Host host2 = new Host("somehost", 0);
51          Assertions.assertEquals("somehost", host2.getHostName());
52          Assertions.assertEquals(0, host2.getPort());
53          Assertions.assertThrows(NullPointerException.class, () -> new Host(null, 0));
54      }
55  
56      @Test
57      public void testHashCode() throws Exception {
58          final Host host1 = new Host("somehost", 8080);
59          final Host host2 = new Host("somehost", 80);
60          final Host host3 = new Host("someotherhost", 8080);
61          final Host host4 = new Host("somehost", 80);
62  
63          Assertions.assertEquals(host1.hashCode(), host1.hashCode());
64          Assertions.assertTrue(host1.hashCode() != host2.hashCode());
65          Assertions.assertTrue(host1.hashCode() != host3.hashCode());
66          Assertions.assertEquals(host2.hashCode(), host4.hashCode());
67      }
68  
69      @Test
70      public void testEquals() throws Exception {
71          final Host host1 = new Host("somehost", 8080);
72          final Host host2 = new Host("somehost", 80);
73          final Host host3 = new Host("someotherhost", 8080);
74          final Host host4 = new Host("somehost", 80);
75  
76          Assertions.assertEquals(host1, host1);
77          Assertions.assertNotEquals(host1, host2);
78          Assertions.assertNotEquals(host1, host3);
79          Assertions.assertEquals(host2, host4);
80      }
81  
82      @Test
83      public void testToString() throws Exception {
84          final Host host1 = new Host("somehost", 8888);
85          Assertions.assertEquals("somehost:8888", host1.toString());
86      }
87  
88      @Test
89      public void testSerialization() throws Exception {
90          final Host orig = new Host("somehost", 8080);
91          final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
92          final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
93          outStream.writeObject(orig);
94          outStream.close();
95          final byte[] raw = outbuffer.toByteArray();
96          final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
97          final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
98          final Host clone = (Host) inStream.readObject();
99          Assertions.assertEquals(orig, clone);
100     }
101 
102     @Test
103     public void testCreateFromString() throws Exception {
104         Assertions.assertEquals(new Host("somehost", 8080), Host.create("somehost:8080"));
105         Assertions.assertEquals(new Host("somehost", 1234), Host.create("somehost:1234"));
106         Assertions.assertEquals(new Host("somehost", 0), Host.create("somehost:0"));
107     }
108 
109     @Test
110     public void testCreateFromStringInvalid() throws Exception {
111         Assertions.assertThrows(URISyntaxException.class, () -> Host.create(" host "));
112         Assertions.assertThrows(URISyntaxException.class, () -> Host.create("host :8080"));
113         Assertions.assertThrows(IllegalArgumentException.class, () -> Host.create(""));
114     }
115 
116     @Test
117     public void testIpv6HostAndPort() throws Exception {
118         final Host host = Host.create("[::1]:80");
119         Assertions.assertEquals("::1", host.getHostName());
120         Assertions.assertEquals(80, host.getPort());
121     }
122 
123     @Test
124     public void testIpv6HostAndPortWithoutBrackets() {
125         // ambiguous
126         Assertions.assertThrows(URISyntaxException.class, () -> Host.create("::1:80"));
127     }
128 
129     @Test
130     public void testIpv6HostWithoutPort() {
131         Assertions.assertThrows(URISyntaxException.class, () -> Host.create("::1"));
132     }
133 
134     @Test
135     public void testIpv6HostToString() {
136         Assertions.assertEquals("[::1]:80", new Host("::1", 80).toString());
137         Assertions.assertEquals("[::1]", new Host("::1", -1).toString());
138     }
139 }