1 31 32 package org.apache.commons.httpclient; 33 34 import junit.framework.Test; 35 import junit.framework.TestCase; 36 import junit.framework.TestSuite; 37 38 45 public class TestHttpVersion extends TestCase { 46 47 49 public TestHttpVersion(String name) { 50 super(name); 51 } 52 53 55 public static Test suite() { 56 return new TestSuite(TestHttpVersion.class); 57 } 58 59 61 public void testHttpVersionInvalidConstructorInput() throws Exception { 62 try { 63 HttpVersion ver = new HttpVersion(-1, -1); 64 fail("IllegalArgumentException should have been thrown"); 65 } catch (IllegalArgumentException e) { 66 } 68 try { 69 HttpVersion ver = new HttpVersion(0, -1); 70 fail("IllegalArgumentException should have been thrown"); 71 } catch (IllegalArgumentException e) { 72 } 74 } 75 76 public void testHttpVersionParsing() throws Exception { 77 String s = "HTTP/1.1"; 78 HttpVersion version = HttpVersion.parse(s); 79 assertEquals("HTTP major version number", 1, version.getMajor()); 80 assertEquals("HTTP minor version number", 1, version.getMinor()); 81 assertEquals("HTTP version number", s, version.toString()); 82 83 s = "HTTP/123.4567"; 84 version = HttpVersion.parse(s); 85 assertEquals("HTTP major version number", 123, version.getMajor()); 86 assertEquals("HTTP minor version number", 4567, version.getMinor()); 87 assertEquals("HTTP version number", s, version.toString()); 88 } 89 90 public void testInvalidHttpVersionParsing() throws Exception { 91 try { 92 HttpVersion.parse(null); 93 fail("IllegalArgumentException should have been thrown"); 94 } catch (IllegalArgumentException e) { 95 } 97 try { 98 HttpVersion.parse("crap"); 99 fail("ProtocolException should have been thrown"); 100 } catch (ProtocolException e) { 101 } 103 try { 104 HttpVersion.parse("HTTP/crap"); 105 fail("ProtocolException should have been thrown"); 106 } catch (ProtocolException e) { 107 } 109 try { 110 HttpVersion.parse("HTTP/1"); 111 fail("ProtocolException should have been thrown"); 112 } catch (ProtocolException e) { 113 } 115 try { 116 HttpVersion.parse("HTTP/1234 "); 117 fail("ProtocolException should have been thrown"); 118 } catch (ProtocolException e) { 119 } 121 try { 122 HttpVersion.parse("HTTP/1."); 123 fail("ProtocolException should have been thrown"); 124 } catch (ProtocolException e) { 125 } 127 try { 128 HttpVersion.parse("HTTP/1.1 crap"); 129 fail("ProtocolException should have been thrown"); 130 } catch (ProtocolException e) { 131 } 133 try { 134 HttpVersion.parse("HTTP/whatever.whatever whatever"); 135 fail("ProtocolException should have been thrown"); 136 } catch (ProtocolException e) { 137 } 139 try { 140 HttpVersion.parse("HTTP/1.whatever whatever"); 141 fail("ProtocolException should have been thrown"); 142 } catch (ProtocolException e) { 143 } 145 } 146 147 public void testHttpVersionEquality() throws Exception { 148 HttpVersion ver1 = new HttpVersion(1, 1); 149 HttpVersion ver2 = new HttpVersion(1, 1); 150 151 assertEquals(ver1.hashCode(), ver2.hashCode()); 152 assertTrue(ver1.equals(ver1)); 153 assertTrue(ver1.equals(ver2)); 154 assertTrue(ver1.equals((Object )ver1)); 155 assertTrue(ver1.equals((Object )ver2)); 156 157 assertFalse(ver1.equals(new Float (1.1))); 158 159 try { 160 ver1.equals(null); 161 fail("IllegalArgumentException should have been thrown"); 162 } catch (IllegalArgumentException e) { 163 } 164 165 assertTrue((new HttpVersion(0, 9)).equals(HttpVersion.HTTP_0_9)); 166 assertTrue((new HttpVersion(1, 0)).equals(HttpVersion.HTTP_1_0)); 167 assertTrue((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_1)); 168 assertFalse((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_0)); 169 } 170 171 public void testHttpVersionComparison() { 172 assertTrue(HttpVersion.HTTP_0_9.lessEquals(HttpVersion.HTTP_1_1)); 173 assertTrue(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_0_9)); 174 assertFalse(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_1_0)); 175 176 assertTrue(HttpVersion.HTTP_1_0.compareTo((Object )HttpVersion.HTTP_1_1) < 0); 177 assertTrue(HttpVersion.HTTP_1_0.compareTo((Object )HttpVersion.HTTP_0_9) > 0); 178 assertTrue(HttpVersion.HTTP_1_0.compareTo((Object )HttpVersion.HTTP_1_0) == 0); 179 } 180 } 181 182 | Popular Tags |