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 class TestHost {
44
45 @Test
46 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 void testHashCode() {
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.assertNotEquals(host1.hashCode(), host2.hashCode());
74 Assertions.assertNotEquals(host1.hashCode(), host3.hashCode());
75 Assertions.assertEquals(host2.hashCode(), host4.hashCode());
76 }
77
78 @Test
79 void testEquals() {
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 void testToString() {
93 final Host host1 = new Host("somehost", 8888);
94 Assertions.assertEquals("somehost:8888", host1.toString());
95 }
96
97 @Test
98 void testSerialization() throws Exception {
99 final Host orig = new Host("somehost", 8080);
100 final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
101 try (ObjectOutputStream outStream = new ObjectOutputStream(outbuffer)) {
102 outStream.writeObject(orig);
103 }
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 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 void testCreateFromStringInvalid() {
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 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 void testIpv6HostAndPortWithoutBrackets() {
136
137 Assertions.assertThrows(URISyntaxException.class, () -> Host.create("::1:80"));
138 }
139
140 @Test
141 void testIpv6HostWithoutPort() {
142 Assertions.assertThrows(URISyntaxException.class, () -> Host.create("::1"));
143 }
144
145 @Test
146 void testIpv6HostToString() {
147 Assertions.assertEquals("[::1]:80", new Host("::1", 80).toString());
148 Assertions.assertEquals("[::1]", new Host("::1", -1).toString());
149 }
150 }