1   /*
2    * $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 $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   * [Additional notices, if required by prior licensing conditions]
28   *
29   */
30  
31  package org.apache.commons.httpclient;
32  
33  import junit.framework.Test;
34  import junit.framework.TestCase;
35  import junit.framework.TestSuite;
36  
37  /***
38   * Test cases for HTTP version class
39   *
40   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
41   * 
42   * @version $Revision: 155418 $
43   */
44  public class TestHttpVersion extends TestCase {
45  
46      // ------------------------------------------------------------ Constructor
47  
48      public TestHttpVersion(String name) {
49          super(name);
50      }
51  
52      // ------------------------------------------------------- TestCase Methods
53  
54      public static Test suite() {
55          return new TestSuite(TestHttpVersion.class);
56      }
57  
58      // ------------------------------------------------------------------ Tests
59      
60      public void testHttpVersionInvalidConstructorInput() throws Exception {
61          try {
62              HttpVersion ver = new HttpVersion(-1, -1); 
63              fail("IllegalArgumentException should have been thrown");
64          } catch (IllegalArgumentException e) {
65              // expected
66          }
67          try {
68              HttpVersion ver = new HttpVersion(0, -1); 
69              fail("IllegalArgumentException should have been thrown");
70          } catch (IllegalArgumentException e) {
71              // expected
72          }
73      }
74  
75      public void testHttpVersionParsing() throws Exception {
76          String s = "HTTP/1.1";
77          HttpVersion version = HttpVersion.parse(s);
78          assertEquals("HTTP major version number", 1, version.getMajor());
79          assertEquals("HTTP minor version number", 1, version.getMinor());
80          assertEquals("HTTP version number", s, version.toString());
81  
82          s = "HTTP/123.4567";
83          version = HttpVersion.parse(s);
84          assertEquals("HTTP major version number", 123, version.getMajor());
85          assertEquals("HTTP minor version number", 4567, version.getMinor());
86          assertEquals("HTTP version number", s, version.toString());
87      }
88  
89      public void testInvalidHttpVersionParsing() throws Exception {
90          try {
91              HttpVersion.parse(null);
92              fail("IllegalArgumentException should have been thrown");
93          } catch (IllegalArgumentException e) {
94              //expected
95          }
96          try {
97              HttpVersion.parse("crap");
98              fail("ProtocolException should have been thrown");
99          } catch (ProtocolException e) {
100             //expected
101         }
102         try {
103             HttpVersion.parse("HTTP/crap");
104             fail("ProtocolException should have been thrown");
105         } catch (ProtocolException e) {
106             //expected
107         }
108         try {
109             HttpVersion.parse("HTTP/1");
110             fail("ProtocolException should have been thrown");
111         } catch (ProtocolException e) {
112             //expected
113         }
114         try {
115             HttpVersion.parse("HTTP/1234   ");
116             fail("ProtocolException should have been thrown");
117         } catch (ProtocolException e) {
118             //expected
119         }
120         try {
121             HttpVersion.parse("HTTP/1.");
122             fail("ProtocolException should have been thrown");
123         } catch (ProtocolException e) {
124             //expected
125         }
126         try {
127             HttpVersion.parse("HTTP/1.1 crap");
128             fail("ProtocolException should have been thrown");
129         } catch (ProtocolException e) {
130             //expected
131         }
132         try {
133             HttpVersion.parse("HTTP/whatever.whatever whatever");
134             fail("ProtocolException should have been thrown");
135         } catch (ProtocolException e) {
136             //expected
137         }
138         try {
139             HttpVersion.parse("HTTP/1.whatever whatever");
140             fail("ProtocolException should have been thrown");
141         } catch (ProtocolException e) {
142             //expected
143         }
144     }
145 
146     public void testHttpVersionEquality() throws Exception {
147         HttpVersion ver1 = new HttpVersion(1, 1); 
148         HttpVersion ver2 = new HttpVersion(1, 1); 
149         
150         assertEquals(ver1.hashCode(), ver2.hashCode());
151         assertTrue(ver1.equals(ver1));
152         assertTrue(ver1.equals(ver2));
153         assertTrue(ver1.equals((Object)ver1));
154         assertTrue(ver1.equals((Object)ver2));
155 
156         assertFalse(ver1.equals(new Float(1.1)));
157         
158         try {
159             ver1.equals(null);
160             fail("IllegalArgumentException should have been thrown");
161         } catch (IllegalArgumentException e) {
162         }
163 
164         assertTrue((new HttpVersion(0, 9)).equals(HttpVersion.HTTP_0_9));
165         assertTrue((new HttpVersion(1, 0)).equals(HttpVersion.HTTP_1_0));
166         assertTrue((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_1));
167         assertFalse((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_0));
168     }
169 
170     public void testHttpVersionComparison() {
171         assertTrue(HttpVersion.HTTP_0_9.lessEquals(HttpVersion.HTTP_1_1));
172         assertTrue(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_0_9));
173         assertFalse(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_1_0));
174         
175         assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_1_1) < 0);
176         assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_0_9) > 0);
177         assertTrue(HttpVersion.HTTP_1_0.compareTo((Object)HttpVersion.HTTP_1_0) == 0);
178    }
179 }
180