View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  /**
39   * Test cases for HTTP version class
40   *
41   */
42  public class TestHttpVersion {
43  
44      @Test
45      public void testHttpVersionInvalidConstructorInput() throws Exception {
46          try {
47              new HttpVersion(-1, -1);
48              Assert.fail("IllegalArgumentException should have been thrown");
49          } catch (final IllegalArgumentException e) {
50              // expected
51          }
52          try {
53              new HttpVersion(0, -1);
54              Assert.fail("IllegalArgumentException should have been thrown");
55          } catch (final IllegalArgumentException e) {
56              // expected
57          }
58      }
59  
60      @Test
61      public void testHttpVersionEquality() throws Exception {
62          final HttpVersion ver1 = new HttpVersion(1, 1);
63          final HttpVersion ver2 = new HttpVersion(1, 1);
64  
65          Assert.assertEquals(ver1.hashCode(), ver2.hashCode());
66          Assert.assertTrue(ver1.equals(ver1));
67          Assert.assertTrue(ver1.equals(ver2));
68          Assert.assertTrue(ver1.equals(ver1));
69          Assert.assertTrue(ver1.equals(ver2));
70  
71          Assert.assertFalse(ver1.equals(new Float(1.1)));
72  
73          Assert.assertTrue((new HttpVersion(0, 9)).equals(HttpVersion.HTTP_0_9));
74          Assert.assertTrue((new HttpVersion(1, 0)).equals(HttpVersion.HTTP_1_0));
75          Assert.assertTrue((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_1));
76          Assert.assertFalse((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_0));
77  
78          Assert.assertTrue
79              ((new ProtocolVersion("HTTP", 0, 9)).equals(HttpVersion.HTTP_0_9));
80          Assert.assertTrue
81              ((new ProtocolVersion("HTTP", 1, 0)).equals(HttpVersion.HTTP_1_0));
82          Assert.assertTrue
83              ((new ProtocolVersion("HTTP", 1, 1)).equals(HttpVersion.HTTP_1_1));
84          Assert.assertFalse
85              ((new ProtocolVersion("http", 1, 1)).equals(HttpVersion.HTTP_1_1));
86  
87          Assert.assertTrue
88              (HttpVersion.HTTP_0_9.equals(new ProtocolVersion("HTTP", 0, 9)));
89          Assert.assertTrue
90              (HttpVersion.HTTP_1_0.equals(new ProtocolVersion("HTTP", 1, 0)));
91          Assert.assertTrue
92              (HttpVersion.HTTP_1_1.equals(new ProtocolVersion("HTTP", 1, 1)));
93          Assert.assertFalse
94              (HttpVersion.HTTP_1_1.equals(new ProtocolVersion("http", 1, 1)));
95      }
96  
97      @Test
98      public void testHttpVersionComparison() {
99          Assert.assertTrue(HttpVersion.HTTP_0_9.lessEquals(HttpVersion.HTTP_1_1));
100         Assert.assertTrue(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_0_9));
101         Assert.assertFalse(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_1_0));
102 
103         Assert.assertTrue(HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_1_1) < 0);
104         Assert.assertTrue(HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_0_9) > 0);
105         Assert.assertTrue(HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_1_0) == 0);
106    }
107 
108     @Test
109     public void testCloning() throws Exception {
110         final HttpVersion orig = HttpVersion.HTTP_1_1;
111         final HttpVersion clone = (HttpVersion) orig.clone();
112         Assert.assertEquals(orig, clone);
113     }
114 
115     @Test
116     public void testSerialization() throws Exception {
117         final HttpVersion orig = HttpVersion.HTTP_1_1;
118         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
119         final ObjectOutputStream outstream = new ObjectOutputStream(outbuffer);
120         outstream.writeObject(orig);
121         outstream.close();
122         final byte[] raw = outbuffer.toByteArray();
123         final ByteArrayInputStream inbuffer = new ByteArrayInputStream(raw);
124         final ObjectInputStream instream = new ObjectInputStream(inbuffer);
125         final HttpVersion clone = (HttpVersion) instream.readObject();
126         Assert.assertEquals(orig, clone);
127     }
128 
129 }
130