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
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
47
48 public TestHttpVersion(String name) {
49 super(name);
50 }
51
52
53
54 public static Test suite() {
55 return new TestSuite(TestHttpVersion.class);
56 }
57
58
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
66 }
67 try {
68 HttpVersion ver = new HttpVersion(0, -1);
69 fail("IllegalArgumentException should have been thrown");
70 } catch (IllegalArgumentException e) {
71
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
95 }
96 try {
97 HttpVersion.parse("crap");
98 fail("ProtocolException should have been thrown");
99 } catch (ProtocolException e) {
100
101 }
102 try {
103 HttpVersion.parse("HTTP/crap");
104 fail("ProtocolException should have been thrown");
105 } catch (ProtocolException e) {
106
107 }
108 try {
109 HttpVersion.parse("HTTP/1");
110 fail("ProtocolException should have been thrown");
111 } catch (ProtocolException e) {
112
113 }
114 try {
115 HttpVersion.parse("HTTP/1234 ");
116 fail("ProtocolException should have been thrown");
117 } catch (ProtocolException e) {
118
119 }
120 try {
121 HttpVersion.parse("HTTP/1.");
122 fail("ProtocolException should have been thrown");
123 } catch (ProtocolException e) {
124
125 }
126 try {
127 HttpVersion.parse("HTTP/1.1 crap");
128 fail("ProtocolException should have been thrown");
129 } catch (ProtocolException e) {
130
131 }
132 try {
133 HttpVersion.parse("HTTP/whatever.whatever whatever");
134 fail("ProtocolException should have been thrown");
135 } catch (ProtocolException e) {
136
137 }
138 try {
139 HttpVersion.parse("HTTP/1.whatever whatever");
140 fail("ProtocolException should have been thrown");
141 } catch (ProtocolException e) {
142
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