001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestHttpVersion.java,v 1.3 2004/05/09 12:16:12 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: package org.apache.commons.httpclient;
033:
034: import junit.framework.Test;
035: import junit.framework.TestCase;
036: import junit.framework.TestSuite;
037:
038: /**
039: * Test cases for HTTP version class
040: *
041: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
042: *
043: * @version $Revision: 480424 $
044: */
045: public class TestHttpVersion extends TestCase {
046:
047: // ------------------------------------------------------------ Constructor
048:
049: public TestHttpVersion(String name) {
050: super (name);
051: }
052:
053: // ------------------------------------------------------- TestCase Methods
054:
055: public static Test suite() {
056: return new TestSuite(TestHttpVersion.class);
057: }
058:
059: // ------------------------------------------------------------------ Tests
060:
061: public void testHttpVersionInvalidConstructorInput()
062: throws Exception {
063: try {
064: HttpVersion ver = new HttpVersion(-1, -1);
065: fail("IllegalArgumentException should have been thrown");
066: } catch (IllegalArgumentException e) {
067: // expected
068: }
069: try {
070: HttpVersion ver = new HttpVersion(0, -1);
071: fail("IllegalArgumentException should have been thrown");
072: } catch (IllegalArgumentException e) {
073: // expected
074: }
075: }
076:
077: public void testHttpVersionParsing() throws Exception {
078: String s = "HTTP/1.1";
079: HttpVersion version = HttpVersion.parse(s);
080: assertEquals("HTTP major version number", 1, version.getMajor());
081: assertEquals("HTTP minor version number", 1, version.getMinor());
082: assertEquals("HTTP version number", s, version.toString());
083:
084: s = "HTTP/123.4567";
085: version = HttpVersion.parse(s);
086: assertEquals("HTTP major version number", 123, version
087: .getMajor());
088: assertEquals("HTTP minor version number", 4567, version
089: .getMinor());
090: assertEquals("HTTP version number", s, version.toString());
091: }
092:
093: public void testInvalidHttpVersionParsing() throws Exception {
094: try {
095: HttpVersion.parse(null);
096: fail("IllegalArgumentException should have been thrown");
097: } catch (IllegalArgumentException e) {
098: //expected
099: }
100: try {
101: HttpVersion.parse("crap");
102: fail("ProtocolException should have been thrown");
103: } catch (ProtocolException e) {
104: //expected
105: }
106: try {
107: HttpVersion.parse("HTTP/crap");
108: fail("ProtocolException should have been thrown");
109: } catch (ProtocolException e) {
110: //expected
111: }
112: try {
113: HttpVersion.parse("HTTP/1");
114: fail("ProtocolException should have been thrown");
115: } catch (ProtocolException e) {
116: //expected
117: }
118: try {
119: HttpVersion.parse("HTTP/1234 ");
120: fail("ProtocolException should have been thrown");
121: } catch (ProtocolException e) {
122: //expected
123: }
124: try {
125: HttpVersion.parse("HTTP/1.");
126: fail("ProtocolException should have been thrown");
127: } catch (ProtocolException e) {
128: //expected
129: }
130: try {
131: HttpVersion.parse("HTTP/1.1 crap");
132: fail("ProtocolException should have been thrown");
133: } catch (ProtocolException e) {
134: //expected
135: }
136: try {
137: HttpVersion.parse("HTTP/whatever.whatever whatever");
138: fail("ProtocolException should have been thrown");
139: } catch (ProtocolException e) {
140: //expected
141: }
142: try {
143: HttpVersion.parse("HTTP/1.whatever whatever");
144: fail("ProtocolException should have been thrown");
145: } catch (ProtocolException e) {
146: //expected
147: }
148: }
149:
150: public void testHttpVersionEquality() throws Exception {
151: HttpVersion ver1 = new HttpVersion(1, 1);
152: HttpVersion ver2 = new HttpVersion(1, 1);
153:
154: assertEquals(ver1.hashCode(), ver2.hashCode());
155: assertTrue(ver1.equals(ver1));
156: assertTrue(ver1.equals(ver2));
157: assertTrue(ver1.equals((Object) ver1));
158: assertTrue(ver1.equals((Object) ver2));
159:
160: assertFalse(ver1.equals(new Float(1.1)));
161:
162: try {
163: ver1.equals(null);
164: fail("IllegalArgumentException should have been thrown");
165: } catch (IllegalArgumentException e) {
166: }
167:
168: assertTrue((new HttpVersion(0, 9)).equals(HttpVersion.HTTP_0_9));
169: assertTrue((new HttpVersion(1, 0)).equals(HttpVersion.HTTP_1_0));
170: assertTrue((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_1));
171: assertFalse((new HttpVersion(1, 1))
172: .equals(HttpVersion.HTTP_1_0));
173: }
174:
175: public void testHttpVersionComparison() {
176: assertTrue(HttpVersion.HTTP_0_9
177: .lessEquals(HttpVersion.HTTP_1_1));
178: assertTrue(HttpVersion.HTTP_0_9
179: .greaterEquals(HttpVersion.HTTP_0_9));
180: assertFalse(HttpVersion.HTTP_0_9
181: .greaterEquals(HttpVersion.HTTP_1_0));
182:
183: assertTrue(HttpVersion.HTTP_1_0
184: .compareTo((Object) HttpVersion.HTTP_1_1) < 0);
185: assertTrue(HttpVersion.HTTP_1_0
186: .compareTo((Object) HttpVersion.HTTP_0_9) > 0);
187: assertTrue(HttpVersion.HTTP_1_0
188: .compareTo((Object) HttpVersion.HTTP_1_0) == 0);
189: }
190: }
|