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.jupiter.api.Assertions;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * Test cases for HTTP version class
40   */
41  public class TestHttpVersion {
42  
43      @Test
44      public void testEqualsMajorMinor() {
45          Assertions.assertTrue(HttpVersion.HTTP_0_9.equals(0, 9));
46          Assertions.assertTrue(HttpVersion.HTTP_1_0.equals(1, 0));
47          Assertions.assertTrue(HttpVersion.HTTP_1_1.equals(1, 1));
48          Assertions.assertTrue(HttpVersion.HTTP_2.equals(2, 0));
49          Assertions.assertTrue(HttpVersion.HTTP_2_0.equals(2, 0));
50          //
51          Assertions.assertFalse(HttpVersion.HTTP_0_9.equals(2, 0));
52      }
53  
54      @Test
55      public void testGet() {
56          Assertions.assertEquals(HttpVersion.HTTP_0_9, HttpVersion.get(0, 9));
57          Assertions.assertEquals(HttpVersion.HTTP_1_0, HttpVersion.get(1, 0));
58          Assertions.assertEquals(HttpVersion.HTTP_1_1, HttpVersion.get(1, 1));
59          Assertions.assertEquals(HttpVersion.HTTP_2_0, HttpVersion.get(2, 0));
60          Assertions.assertEquals(HttpVersion.HTTP_2, HttpVersion.get(2, 0));
61          Assertions.assertNotEquals(HttpVersion.HTTP_2_0, HttpVersion.get(2, 1));
62          //
63          Assertions.assertSame(HttpVersion.HTTP_0_9, HttpVersion.get(0, 9));
64          Assertions.assertSame(HttpVersion.HTTP_1_0, HttpVersion.get(1, 0));
65          Assertions.assertSame(HttpVersion.HTTP_1_1, HttpVersion.get(1, 1));
66          Assertions.assertSame(HttpVersion.HTTP_2_0, HttpVersion.get(2, 0));
67          Assertions.assertSame(HttpVersion.HTTP_2, HttpVersion.get(2, 0));
68          Assertions.assertNotSame(HttpVersion.HTTP_2_0, HttpVersion.get(2, 1));
69      }
70  
71      @SuppressWarnings("unused")
72      @Test
73      public void testHttpVersionInvalidConstructorInput() throws Exception {
74          Assertions.assertThrows(IllegalArgumentException.class, () -> new HttpVersion(-1, -1));
75          Assertions.assertThrows(IllegalArgumentException.class, () -> new HttpVersion(0, -1));
76      }
77  
78      @Test
79      public void testHttpVersionEquality() throws Exception {
80          final HttpVersion ver1 = new HttpVersion(1, 1);
81          final HttpVersion ver2 = new HttpVersion(1, 1);
82  
83          Assertions.assertEquals(ver1.hashCode(), ver2.hashCode());
84          Assertions.assertEquals(ver1, ver1);
85          Assertions.assertEquals(ver1, ver2);
86          Assertions.assertEquals(ver1, ver1);
87          Assertions.assertEquals(ver1, ver2);
88  
89          Assertions.assertFalse(ver1.equals(Float.valueOf(1.1f)));
90  
91          Assertions.assertEquals((new HttpVersion(0, 9)), HttpVersion.HTTP_0_9);
92          Assertions.assertEquals((new HttpVersion(1, 0)), HttpVersion.HTTP_1_0);
93          Assertions.assertEquals((new HttpVersion(1, 1)), HttpVersion.HTTP_1_1);
94          Assertions.assertNotEquals((new HttpVersion(1, 1)), HttpVersion.HTTP_1_0);
95  
96          Assertions.assertEquals((new ProtocolVersion("HTTP", 0, 9)), HttpVersion.HTTP_0_9);
97          Assertions.assertEquals((new ProtocolVersion("HTTP", 1, 0)), HttpVersion.HTTP_1_0);
98          Assertions.assertEquals((new ProtocolVersion("HTTP", 1, 1)), HttpVersion.HTTP_1_1);
99          Assertions.assertNotEquals((new ProtocolVersion("http", 1, 1)), HttpVersion.HTTP_1_1);
100 
101         Assertions.assertEquals(HttpVersion.HTTP_0_9, new ProtocolVersion("HTTP", 0, 9));
102         Assertions.assertEquals(HttpVersion.HTTP_1_0, new ProtocolVersion("HTTP", 1, 0));
103         Assertions.assertEquals(HttpVersion.HTTP_1_1, new ProtocolVersion("HTTP", 1, 1));
104         Assertions.assertNotEquals(HttpVersion.HTTP_1_1, new ProtocolVersion("http", 1, 1));
105     }
106 
107     @Test
108     public void testHttpVersionComparison() {
109         Assertions.assertTrue(HttpVersion.HTTP_0_9.lessEquals(HttpVersion.HTTP_1_1));
110         Assertions.assertTrue(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_0_9));
111         Assertions.assertFalse(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_1_0));
112 
113         Assertions.assertTrue(HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_1_1) < 0);
114         Assertions.assertTrue(HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_0_9) > 0);
115         Assertions.assertEquals(0, HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_1_0));
116    }
117 
118     @Test
119     public void testSerialization() throws Exception {
120         final HttpVersion orig = HttpVersion.HTTP_1_1;
121         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
122         try (final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer)) {
123             outStream.writeObject(orig);
124             outStream.close();
125             final byte[] raw = outbuffer.toByteArray();
126             final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
127             final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
128             final HttpVersion clone = (HttpVersion) inStream.readObject();
129             Assertions.assertEquals(orig, clone);
130         }
131     }
132 
133 }
134