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.hc.core5.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  public class TestHttpVersion {
42  
43      @Test
44      public void testHttpVersionInvalidConstructorInput() throws Exception {
45          try {
46              new HttpVersion(-1, -1);
47              Assert.fail("IllegalArgumentException should have been thrown");
48          } catch (final IllegalArgumentException e) {
49              // expected
50          }
51          try {
52              new HttpVersion(0, -1);
53              Assert.fail("IllegalArgumentException should have been thrown");
54          } catch (final IllegalArgumentException e) {
55              // expected
56          }
57      }
58  
59      @Test
60      public void testHttpVersionEquality() throws Exception {
61          final HttpVersion ver1 = new HttpVersion(1, 1);
62          final HttpVersion ver2 = new HttpVersion(1, 1);
63  
64          Assert.assertEquals(ver1.hashCode(), ver2.hashCode());
65          Assert.assertTrue(ver1.equals(ver1));
66          Assert.assertTrue(ver1.equals(ver2));
67          Assert.assertTrue(ver1.equals(ver1));
68          Assert.assertTrue(ver1.equals(ver2));
69  
70          Assert.assertFalse(ver1.equals(Float.valueOf(1.1f)));
71  
72          Assert.assertTrue((new HttpVersion(0, 9)).equals(HttpVersion.HTTP_0_9));
73          Assert.assertTrue((new HttpVersion(1, 0)).equals(HttpVersion.HTTP_1_0));
74          Assert.assertTrue((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_1));
75          Assert.assertFalse((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_0));
76  
77          Assert.assertTrue
78              ((new ProtocolVersion("HTTP", 0, 9)).equals(HttpVersion.HTTP_0_9));
79          Assert.assertTrue
80              ((new ProtocolVersion("HTTP", 1, 0)).equals(HttpVersion.HTTP_1_0));
81          Assert.assertTrue
82              ((new ProtocolVersion("HTTP", 1, 1)).equals(HttpVersion.HTTP_1_1));
83          Assert.assertFalse
84              ((new ProtocolVersion("http", 1, 1)).equals(HttpVersion.HTTP_1_1));
85  
86          Assert.assertTrue
87              (HttpVersion.HTTP_0_9.equals(new ProtocolVersion("HTTP", 0, 9)));
88          Assert.assertTrue
89              (HttpVersion.HTTP_1_0.equals(new ProtocolVersion("HTTP", 1, 0)));
90          Assert.assertTrue
91              (HttpVersion.HTTP_1_1.equals(new ProtocolVersion("HTTP", 1, 1)));
92          Assert.assertFalse
93              (HttpVersion.HTTP_1_1.equals(new ProtocolVersion("http", 1, 1)));
94      }
95  
96      @Test
97      public void testHttpVersionComparison() {
98          Assert.assertTrue(HttpVersion.HTTP_0_9.lessEquals(HttpVersion.HTTP_1_1));
99          Assert.assertTrue(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_0_9));
100         Assert.assertFalse(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_1_0));
101 
102         Assert.assertTrue(HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_1_1) < 0);
103         Assert.assertTrue(HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_0_9) > 0);
104         Assert.assertTrue(HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_1_0) == 0);
105    }
106 
107     @Test
108     public void testSerialization() throws Exception {
109         final HttpVersion orig = HttpVersion.HTTP_1_1;
110         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
111         final ObjectOutputStream outstream = new ObjectOutputStream(outbuffer);
112         outstream.writeObject(orig);
113         outstream.close();
114         final byte[] raw = outbuffer.toByteArray();
115         final ByteArrayInputStream inbuffer = new ByteArrayInputStream(raw);
116         final ObjectInputStream instream = new ObjectInputStream(inbuffer);
117         final HttpVersion clone = (HttpVersion) instream.readObject();
118         Assert.assertEquals(orig, clone);
119     }
120 
121 }
122