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 TestURIAuthority {
44
45 @Test
46 public void testConstructor() {
47 final URIAuthority host1 = new URIAuthority("somehost");
48 Assert.assertEquals("somehost", host1.getHostName());
49 Assert.assertEquals(-1, host1.getPort());
50 final URIAuthority host2 = new URIAuthority("somehost", 8080);
51 Assert.assertEquals("somehost", host2.getHostName());
52 Assert.assertEquals(8080, host2.getPort());
53 final URIAuthority host3 = new URIAuthority("somehost", -1);
54 Assert.assertEquals("somehost", host3.getHostName());
55 Assert.assertEquals(-1, host3.getPort());
56 }
57
58 @Test
59 public void testHashCode() throws Exception {
60 final URIAuthority host1 = new URIAuthority("somehost", 8080);
61 final URIAuthority host2 = new URIAuthority("somehost", 80);
62 final URIAuthority host3 = new URIAuthority("someotherhost", 8080);
63 final URIAuthority host4 = new URIAuthority("somehost", 80);
64 final URIAuthority host5 = new URIAuthority("SomeHost", 80);
65 final URIAuthority host6 = new URIAuthority("user", "SomeHost", 80);
66 final URIAuthority host7 = new URIAuthority("user", "somehost", 80);
67
68 Assert.assertTrue(host1.hashCode() == host1.hashCode());
69 Assert.assertTrue(host1.hashCode() != host2.hashCode());
70 Assert.assertTrue(host1.hashCode() != host3.hashCode());
71 Assert.assertTrue(host2.hashCode() == host4.hashCode());
72 Assert.assertTrue(host2.hashCode() == host5.hashCode());
73 Assert.assertTrue(host5.hashCode() != host6.hashCode());
74 Assert.assertTrue(host6.hashCode() == host7.hashCode());
75 }
76
77 @Test
78 public void testEquals() throws Exception {
79 final URIAuthority host1 = new URIAuthority("somehost", 8080);
80 final URIAuthority host2 = new URIAuthority("somehost", 80);
81 final URIAuthority host3 = new URIAuthority("someotherhost", 8080);
82 final URIAuthority host4 = new URIAuthority("somehost", 80);
83 final URIAuthority host5 = new URIAuthority("SomeHost", 80);
84 final URIAuthority host6 = new URIAuthority("user", "SomeHost", 80);
85 final URIAuthority host7 = new URIAuthority("user", "somehost", 80);
86
87 Assert.assertTrue(host1.equals(host1));
88 Assert.assertFalse(host1.equals(host2));
89 Assert.assertFalse(host1.equals(host3));
90 Assert.assertTrue(host2.equals(host4));
91 Assert.assertTrue(host2.equals(host5));
92 Assert.assertFalse(host5.equals(host6));
93 Assert.assertTrue(host6.equals(host7));
94 }
95
96 @Test
97 public void testToString() throws Exception {
98 final URIAuthority host1 = new URIAuthority("somehost");
99 Assert.assertEquals("somehost", host1.toString());
100 final URIAuthority host2 = new URIAuthority("somehost", -1);
101 Assert.assertEquals("somehost", host2.toString());
102 final URIAuthority host3 = new URIAuthority("somehost", 8888);
103 Assert.assertEquals("somehost:8888", host3.toString());
104 }
105
106 @Test
107 public void testSerialization() throws Exception {
108 final URIAuthority orig = new URIAuthority("somehost", 8080);
109 final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
110 final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
111 outStream.writeObject(orig);
112 outStream.close();
113 final byte[] raw = outbuffer.toByteArray();
114 final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
115 final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
116 final URIAuthority clone = (URIAuthority) inStream.readObject();
117 Assert.assertEquals(orig, clone);
118 }
119
120 @Test
121 public void testCreateFromString() throws Exception {
122 Assert.assertEquals(new URIAuthority("somehost", 8080), URIAuthority.create("somehost:8080"));
123 Assert.assertEquals(new URIAuthority("somehost", 8080), URIAuthority.create("SomeHost:8080"));
124 Assert.assertEquals(new URIAuthority("somehost", 1234), URIAuthority.create("somehost:1234"));
125 Assert.assertEquals(new URIAuthority("somehost", -1), URIAuthority.create("somehost"));
126 Assert.assertEquals(new URIAuthority("user", "somehost", -1), URIAuthority.create("user@somehost"));
127 }
128
129 @Test
130 public void testCreateFromStringInvalid() throws Exception {
131 try {
132 URIAuthority.create(" host ");
133 Assert.fail("IllegalArgumentException expected");
134 } catch (final URISyntaxException expected) {
135 }
136 try {
137 URIAuthority.create("host :8080");
138 Assert.fail("URISyntaxException expected");
139 } catch (final URISyntaxException expected) {
140 }
141 try {
142 URIAuthority.create("user @ host:8080");
143 Assert.fail("URISyntaxException expected");
144 } catch (final URISyntaxException expected) {
145 }
146 }
147
148 }