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.Assert;
37  import org.junit.Test;
38  
39  /**
40   * Unit tests for {@link URIAuthority}.
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 }