1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
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 }