001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/TestHttpHost.java $
003: * $Revision: 604514 $
004: * $Date: 2007-12-15 21:49:40 +0100 (Sat, 15 Dec 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http;
033:
034: import junit.framework.Test;
035: import junit.framework.TestCase;
036: import junit.framework.TestSuite;
037:
038: /**
039: * Unit tests for {@link HttpHost}.
040: *
041: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
042: */
043: public class TestHttpHost extends TestCase {
044:
045: public TestHttpHost(String testName) {
046: super (testName);
047: }
048:
049: public static void main(String args[]) {
050: String[] testCaseName = { TestHttpHost.class.getName() };
051: junit.textui.TestRunner.main(testCaseName);
052: }
053:
054: public static Test suite() {
055: return new TestSuite(TestHttpHost.class);
056: }
057:
058: public void testConstructor() {
059: HttpHost host1 = new HttpHost("somehost");
060: assertEquals("somehost", host1.getHostName());
061: assertEquals(-1, host1.getPort());
062: assertEquals("http", host1.getSchemeName());
063: HttpHost host2 = new HttpHost("somehost", 8080);
064: assertEquals("somehost", host2.getHostName());
065: assertEquals(8080, host2.getPort());
066: assertEquals("http", host2.getSchemeName());
067: HttpHost host3 = new HttpHost("somehost", -1);
068: assertEquals("somehost", host3.getHostName());
069: assertEquals(-1, host3.getPort());
070: assertEquals("http", host3.getSchemeName());
071: HttpHost host4 = new HttpHost("somehost", 443, "https");
072: assertEquals("somehost", host4.getHostName());
073: assertEquals(443, host4.getPort());
074: assertEquals("https", host4.getSchemeName());
075: try {
076: new HttpHost(null, -1, null);
077: fail("IllegalArgumentException should have been thrown");
078: } catch (IllegalArgumentException ex) {
079: //expected
080: }
081: }
082:
083: public void testHashCode() {
084: HttpHost host1 = new HttpHost("somehost", 8080, "http");
085: HttpHost host2 = new HttpHost("somehost", 80, "http");
086: HttpHost host3 = new HttpHost("someotherhost", 8080, "http");
087: HttpHost host4 = new HttpHost("somehost", 80, "http");
088: HttpHost host5 = new HttpHost("SomeHost", 80, "http");
089: HttpHost host6 = new HttpHost("SomeHost", 80, "myhttp");
090:
091: assertTrue(host1.hashCode() == host1.hashCode());
092: assertTrue(host1.hashCode() != host2.hashCode());
093: assertTrue(host1.hashCode() != host3.hashCode());
094: assertTrue(host2.hashCode() == host4.hashCode());
095: assertTrue(host2.hashCode() == host5.hashCode());
096: assertTrue(host5.hashCode() != host6.hashCode());
097: }
098:
099: public void testEquals() {
100: HttpHost host1 = new HttpHost("somehost", 8080, "http");
101: HttpHost host2 = new HttpHost("somehost", 80, "http");
102: HttpHost host3 = new HttpHost("someotherhost", 8080, "http");
103: HttpHost host4 = new HttpHost("somehost", 80, "http");
104: HttpHost host5 = new HttpHost("SomeHost", 80, "http");
105: HttpHost host6 = new HttpHost("SomeHost", 80, "myhttp");
106:
107: assertTrue(host1.equals(host1));
108: assertFalse(host1.equals(host2));
109: assertFalse(host1.equals(host3));
110: assertTrue(host2.equals(host4));
111: assertTrue(host2.equals(host5));
112: assertFalse(host5.equals(host6));
113: assertFalse(host1.equals(null));
114: assertFalse(host1.equals("http://somehost"));
115: }
116:
117: public void testToString() {
118: HttpHost host1 = new HttpHost("somehost");
119: assertEquals("http://somehost", host1.toString());
120: HttpHost host2 = new HttpHost("somehost", -1);
121: assertEquals("http://somehost", host2.toString());
122: HttpHost host3 = new HttpHost("somehost", -1);
123: assertEquals("http://somehost", host3.toString());
124: HttpHost host4 = new HttpHost("somehost", 8888);
125: assertEquals("http://somehost:8888", host4.toString());
126: HttpHost host5 = new HttpHost("somehost", -1, "myhttp");
127: assertEquals("myhttp://somehost", host5.toString());
128: HttpHost host6 = new HttpHost("somehost", 80, "myhttp");
129: assertEquals("myhttp://somehost:80", host6.toString());
130: }
131:
132: public void testToHostString() {
133: HttpHost host1 = new HttpHost("somehost");
134: assertEquals("somehost", host1.toHostString());
135: HttpHost host2 = new HttpHost("somehost");
136: assertEquals("somehost", host2.toHostString());
137: HttpHost host3 = new HttpHost("somehost", -1);
138: assertEquals("somehost", host3.toHostString());
139: HttpHost host4 = new HttpHost("somehost", 8888);
140: assertEquals("somehost:8888", host4.toHostString());
141: }
142:
143: public void testCloning() throws Exception {
144: HttpHost orig = new HttpHost("somehost", 8080, "https");
145: HttpHost clone = (HttpHost) orig.clone();
146: assertEquals(orig, clone);
147: }
148:
149: }
|