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 testEqualsMajorMinor() {
45          Assert.assertTrue(HttpVersion.HTTP_0_9.equals(0, 9));
46          Assert.assertTrue(HttpVersion.HTTP_1_0.equals(1, 0));
47          Assert.assertTrue(HttpVersion.HTTP_1_1.equals(1, 1));
48          Assert.assertTrue(HttpVersion.HTTP_2.equals(2, 0));
49          Assert.assertTrue(HttpVersion.HTTP_2_0.equals(2, 0));
50          //
51          Assert.assertFalse(HttpVersion.HTTP_0_9.equals(2, 0));
52      }
53  
54      @Test
55      public void testGet() {
56          Assert.assertEquals(HttpVersion.HTTP_0_9, HttpVersion.get(0, 9));
57          Assert.assertEquals(HttpVersion.HTTP_1_0, HttpVersion.get(1, 0));
58          Assert.assertEquals(HttpVersion.HTTP_1_1, HttpVersion.get(1, 1));
59          Assert.assertEquals(HttpVersion.HTTP_2_0, HttpVersion.get(2, 0));
60          Assert.assertEquals(HttpVersion.HTTP_2, HttpVersion.get(2, 0));
61          Assert.assertNotEquals(HttpVersion.HTTP_2_0, HttpVersion.get(2, 1));
62          //
63          Assert.assertSame(HttpVersion.HTTP_0_9, HttpVersion.get(0, 9));
64          Assert.assertSame(HttpVersion.HTTP_1_0, HttpVersion.get(1, 0));
65          Assert.assertSame(HttpVersion.HTTP_1_1, HttpVersion.get(1, 1));
66          Assert.assertSame(HttpVersion.HTTP_2_0, HttpVersion.get(2, 0));
67          Assert.assertSame(HttpVersion.HTTP_2, HttpVersion.get(2, 0));
68          Assert.assertNotSame(HttpVersion.HTTP_2_0, HttpVersion.get(2, 1));
69      }
70  
71      @SuppressWarnings("unused")
72      @Test
73      public void testHttpVersionInvalidConstructorInput() throws Exception {
74          try {
75              new HttpVersion(-1, -1);
76              Assert.fail("IllegalArgumentException should have been thrown");
77          } catch (final IllegalArgumentException e) {
78              // expected
79          }
80          try {
81              new HttpVersion(0, -1);
82              Assert.fail("IllegalArgumentException should have been thrown");
83          } catch (final IllegalArgumentException e) {
84              // expected
85          }
86      }
87  
88      @Test
89      public void testHttpVersionEquality() throws Exception {
90          final HttpVersion ver1 = new HttpVersion(1, 1);
91          final HttpVersion ver2 = new HttpVersion(1, 1);
92  
93          Assert.assertEquals(ver1.hashCode(), ver2.hashCode());
94          Assert.assertTrue(ver1.equals(ver1));
95          Assert.assertTrue(ver1.equals(ver2));
96          Assert.assertTrue(ver1.equals(ver1));
97          Assert.assertTrue(ver1.equals(ver2));
98  
99          Assert.assertFalse(ver1.equals(Float.valueOf(1.1f)));
100 
101         Assert.assertTrue((new HttpVersion(0, 9)).equals(HttpVersion.HTTP_0_9));
102         Assert.assertTrue((new HttpVersion(1, 0)).equals(HttpVersion.HTTP_1_0));
103         Assert.assertTrue((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_1));
104         Assert.assertFalse((new HttpVersion(1, 1)).equals(HttpVersion.HTTP_1_0));
105 
106         Assert.assertTrue
107             ((new ProtocolVersion("HTTP", 0, 9)).equals(HttpVersion.HTTP_0_9));
108         Assert.assertTrue
109             ((new ProtocolVersion("HTTP", 1, 0)).equals(HttpVersion.HTTP_1_0));
110         Assert.assertTrue
111             ((new ProtocolVersion("HTTP", 1, 1)).equals(HttpVersion.HTTP_1_1));
112         Assert.assertFalse
113             ((new ProtocolVersion("http", 1, 1)).equals(HttpVersion.HTTP_1_1));
114 
115         Assert.assertTrue
116             (HttpVersion.HTTP_0_9.equals(new ProtocolVersion("HTTP", 0, 9)));
117         Assert.assertTrue
118             (HttpVersion.HTTP_1_0.equals(new ProtocolVersion("HTTP", 1, 0)));
119         Assert.assertTrue
120             (HttpVersion.HTTP_1_1.equals(new ProtocolVersion("HTTP", 1, 1)));
121         Assert.assertFalse
122             (HttpVersion.HTTP_1_1.equals(new ProtocolVersion("http", 1, 1)));
123     }
124 
125     @Test
126     public void testHttpVersionComparison() {
127         Assert.assertTrue(HttpVersion.HTTP_0_9.lessEquals(HttpVersion.HTTP_1_1));
128         Assert.assertTrue(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_0_9));
129         Assert.assertFalse(HttpVersion.HTTP_0_9.greaterEquals(HttpVersion.HTTP_1_0));
130 
131         Assert.assertTrue(HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_1_1) < 0);
132         Assert.assertTrue(HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_0_9) > 0);
133         Assert.assertTrue(HttpVersion.HTTP_1_0.compareToVersion(HttpVersion.HTTP_1_0) == 0);
134    }
135 
136     @Test
137     public void testSerialization() throws Exception {
138         final HttpVersion orig = HttpVersion.HTTP_1_1;
139         final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
140         try (final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer)) {
141             outStream.writeObject(orig);
142             outStream.close();
143             final byte[] raw = outbuffer.toByteArray();
144             final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
145             final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
146             final HttpVersion clone = (HttpVersion) inStream.readObject();
147             Assert.assertEquals(orig, clone);
148         }
149     }
150 
151 }
152