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.Assert;
37 import org.junit.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 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 try {
59 new Host("blah", -1);
60 Assert.fail("IllegalArgumentException should have been thrown");
61 } catch (final IllegalArgumentException expected) {
62 }
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 Assert.assertTrue(host1.hashCode() == host1.hashCode());
73 Assert.assertTrue(host1.hashCode() != host2.hashCode());
74 Assert.assertTrue(host1.hashCode() != host3.hashCode());
75 Assert.assertTrue(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 Assert.assertTrue(host1.equals(host1));
86 Assert.assertFalse(host1.equals(host2));
87 Assert.assertFalse(host1.equals(host3));
88 Assert.assertTrue(host2.equals(host4));
89 }
90
91 @Test
92 public void testToString() throws Exception {
93 final Host host1 = new Host("somehost", 8888);
94 Assert.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 Assert.assertEquals(orig, clone);
109 }
110
111 @Test
112 public void testCreateFromString() throws Exception {
113 Assert.assertEquals(new Host("somehost", 8080), Host.create("somehost:8080"));
114 Assert.assertEquals(new Host("somehost", 1234), Host.create("somehost:1234"));
115 Assert.assertEquals(new Host("somehost", 0), Host.create("somehost:0"));
116 }
117
118 @Test
119 public void testCreateFromStringInvalid() throws Exception {
120 try {
121 Host.create(" host ");
122 Assert.fail("URISyntaxException expected");
123 } catch (final URISyntaxException expected) {
124 }
125 try {
126 Host.create("host :8080");
127 Assert.fail("URISyntaxException expected");
128 } catch (final URISyntaxException expected) {
129 }
130 try {
131 Host.create("");
132 Assert.fail("IllegalArgumentException expected");
133 } catch (final IllegalArgumentException expected) {
134 }
135 }
136
137 }