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